Skip to content

Commit

Permalink
add DEVELOPER_DIR and SDKROOT
Browse files Browse the repository at this point in the history
  • Loading branch information
amkartashov committed Jun 4, 2021
1 parent bb64087 commit 7debf5e
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions pkg/rbeconfigsgen/rbeconfigsgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,68 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
if err != nil {
return "", fmt.Errorf("unable to determine the build output directory where Bazel produced C++ configs in the runner: %w", err)
}

// extract DEVELOPER_DIR for MacOS runner
developerDir := ""
if o.ExecOS == options.OSMacos {
out, err := r.ExecCmd(bazeliskPath, "query", "@local_config_xcode//:host_xcodes", "--output", "build")
if err != nil {
return "", fmt.Errorf("`bazel query @local_config_xcode//:host_xcodes` fails in the runner: %w", err)
}
versionLabel := ""
for _, line := range strings.Split(out, "\n") {
// We're looking for a line that looks like `default = "@local_config_xcode//:<version>",` and we want to
// extract @local_config_xcode//:<version>.
split := strings.SplitN(line, "=", 2)
if len(split) != 2 {
continue
}
key := strings.TrimSpace(split[0])
val := strings.Trim(strings.TrimSpace(split[1]),"\",")
if key == "default" {
versionLabel = val
}
}
if len(versionLabel) == 0 {
return "", fmt.Errorf("target @local_config_xcode//:host_xcodes has no default version")
}

out, err = r.ExecCmd(bazeliskPath, "query", versionLabel, "--output", "build")
if err != nil {
return "", fmt.Errorf("`bazel query %s` fails in the runner: %w", versionLabel, err)
}
xcodeVersion := ""
for _, line := range strings.Split(out, "\n") {
// We're looking for a line that looks like `version = "<version>",` and we want to
// extract <version>.
split := strings.SplitN(line, "=", 2)
if len(split) != 2 {
continue
}
key := strings.TrimSpace(split[0])
val := strings.Trim(strings.TrimSpace(split[1]),"\",")
if key == "version" {
xcodeVersion = val
}
}
if len(xcodeVersion) == 0 {
return "", fmt.Errorf("target %s has no version attr", versionLabel)
}

xcodeLocatorPath := path.Join(bazelOutputRoot, "external", "local_config_xcode", "xcode-locator-bin")
out, err = r.ExecCmd(xcodeLocatorPath, xcodeVersion)
if err != nil {
return "", fmt.Errorf("`%s %s` fails in the runner: %w", xcodeLocatorPath, xcodeVersion, err)
}
// developerDir will be the last line
outLines := strings.Split(out, "\n")
developerDir = outLines[len(outLines)-1]
if developerDir == "" {
return "", fmt.Errorf("failed to find DEVELOPER_DIR")
}
log.Printf("DEVELOPER_DIR: '%s'.", developerDir)
}

cppConfigDir := path.Join(bazelOutputRoot, "external", o.CPPConfigRepo)
log.Printf("Extracting C++ config files generated by Bazel at %q from the runner.", cppConfigDir)

Expand Down Expand Up @@ -339,6 +401,40 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
}
}

if o.ExecOS == options.OSMacos {
// w/a for the issue described here: https://github.com/bazelbuild/bazel/pull/13529
sedArgs := []string{
"-I",
"",
"-e",
"s|@local_config_cc_toolchains//:osx_archs.bzl|@bazel_tools//tools/osx/crosstool:osx_archs.bzl|",
path.Join(cppConfigDir, "BUILD"),
}
if _, err := r.ExecCmd("sed", sedArgs...); err != nil {
return "", fmt.Errorf("failed to replace osx_archs.bzl label in BUILD")
}
sdkRoot := path.Join(developerDir, "Platforms",
"%{apple_sdk_platform_value}.platform",
"Developer",
"SDKs",
"%{apple_sdk_platform_value}%{apple_sdk_version_override_value}.sdk",
)
sedArgs = []string{
"-I",
"",
"-e",
"/cc_toolchain_config(/ a\\\n" +
" extra_env = { \\\n" +
" \"DEVELOPER_DIR\": \"" + developerDir + "\",\\\n" +
" \"SDKROOT\": \"" + sdkRoot + "\",\\\n" +
" },",
path.Join(cppConfigDir, "BUILD"),
}
if _, err := r.ExecCmd("sed", sedArgs...); err != nil {
return "", fmt.Errorf("failed to add extra_env to cc_toolchain_config in BUILD")
}
}

outputTarball := "cpp_configs.tar"
// Explicitly use absolute paths to avoid confusion on what's the working directory.
outputTarballPath := path.Join(o.TempWorkDir, outputTarball)
Expand Down

0 comments on commit 7debf5e

Please sign in to comment.