diff --git a/pkg/rbeconfigsgen/rbeconfigsgen.go b/pkg/rbeconfigsgen/rbeconfigsgen.go index 1918a4374..17d7a4254 100644 --- a/pkg/rbeconfigsgen/rbeconfigsgen.go +++ b/pkg/rbeconfigsgen/rbeconfigsgen.go @@ -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//:",` and we want to + // extract @local_config_xcode//:. + 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 = "",` and we want to + // extract . + 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) @@ -339,6 +401,43 @@ 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") + } + // add DEVELOPER_DIR and SDKROOT into remote executor env + // for local executions bazel takes care about it, see + // https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/exec/local/XcodeLocalEnvProvider.java + 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)