@@ -296,6 +296,68 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
296296 if err != nil {
297297 return "" , fmt .Errorf ("unable to determine the build output directory where Bazel produced C++ configs in the runner: %w" , err )
298298 }
299+
300+ // extract DEVELOPER_DIR for MacOS runner
301+ developerDir := ""
302+ if o .ExecOS == options .OSMacos {
303+ out , err := r .ExecCmd (bazeliskPath , "query" , "@local_config_xcode//:host_xcodes" , "--output" , "build" )
304+ if err != nil {
305+ return "" , fmt .Errorf ("`bazel query @local_config_xcode//:host_xcodes` fails in the runner: %w" , err )
306+ }
307+ versionLabel := ""
308+ for _ , line := range strings .Split (out , "\n " ) {
309+ // We're looking for a line that looks like `default = "@local_config_xcode//:<version>",` and we want to
310+ // extract @local_config_xcode//:<version>.
311+ split := strings .SplitN (line , "=" , 2 )
312+ if len (split ) != 2 {
313+ continue
314+ }
315+ key := strings .TrimSpace (split [0 ])
316+ val := strings .Trim (strings .TrimSpace (split [1 ]),"\" ," )
317+ if key == "default" {
318+ versionLabel = val
319+ }
320+ }
321+ if len (versionLabel ) == 0 {
322+ return "" , fmt .Errorf ("target @local_config_xcode//:host_xcodes has no default version" )
323+ }
324+
325+ out , err = r .ExecCmd (bazeliskPath , "query" , versionLabel , "--output" , "build" )
326+ if err != nil {
327+ return "" , fmt .Errorf ("`bazel query %s` fails in the runner: %w" , versionLabel , err )
328+ }
329+ xcodeVersion := ""
330+ for _ , line := range strings .Split (out , "\n " ) {
331+ // We're looking for a line that looks like `version = "<version>",` and we want to
332+ // extract <version>.
333+ split := strings .SplitN (line , "=" , 2 )
334+ if len (split ) != 2 {
335+ continue
336+ }
337+ key := strings .TrimSpace (split [0 ])
338+ val := strings .Trim (strings .TrimSpace (split [1 ]),"\" ," )
339+ if key == "version" {
340+ xcodeVersion = val
341+ }
342+ }
343+ if len (xcodeVersion ) == 0 {
344+ return "" , fmt .Errorf ("target %s has no version attr" , versionLabel )
345+ }
346+
347+ xcodeLocatorPath := path .Join (bazelOutputRoot , "external" , "local_config_xcode" , "xcode-locator-bin" )
348+ out , err = r .ExecCmd (xcodeLocatorPath , xcodeVersion )
349+ if err != nil {
350+ return "" , fmt .Errorf ("`%s %s` fails in the runner: %w" , xcodeLocatorPath , xcodeVersion , err )
351+ }
352+ // developerDir will be the last line
353+ outLines := strings .Split (out , "\n " )
354+ developerDir = outLines [len (outLines )- 1 ]
355+ if developerDir == "" {
356+ return "" , fmt .Errorf ("failed to find DEVELOPER_DIR" )
357+ }
358+ log .Printf ("DEVELOPER_DIR: '%s'." , developerDir )
359+ }
360+
299361 cppConfigDir := path .Join (bazelOutputRoot , "external" , o .CPPConfigRepo )
300362 log .Printf ("Extracting C++ config files generated by Bazel at %q from the runner." , cppConfigDir )
301363
@@ -339,6 +401,40 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
339401 }
340402 }
341403
404+ if o .ExecOS == options .OSMacos {
405+ // w/a for the issue described here: https://github.com/bazelbuild/bazel/pull/13529
406+ sedArgs := []string {
407+ "-I" ,
408+ "" ,
409+ "-e" ,
410+ "s|@local_config_cc_toolchains//:osx_archs.bzl|@bazel_tools//tools/osx/crosstool:osx_archs.bzl|" ,
411+ path .Join (cppConfigDir , "BUILD" ),
412+ }
413+ if _ , err := r .ExecCmd ("sed" , sedArgs ... ); err != nil {
414+ return "" , fmt .Errorf ("failed to replace osx_archs.bzl label in BUILD" )
415+ }
416+ sdkRoot := path .Join (developerDir , "Platforms" ,
417+ "%{apple_sdk_platform_value}.platform" ,
418+ "Developer" ,
419+ "SDKs" ,
420+ "%{apple_sdk_platform_value}%{apple_sdk_version_override_value}.sdk" ,
421+ )
422+ sedArgs = []string {
423+ "-I" ,
424+ "" ,
425+ "-e" ,
426+ "/cc_toolchain_config(/ a\\ \n " +
427+ " extra_env = { \\ \n " +
428+ " \" DEVELOPER_DIR\" : \" " + developerDir + "\" ,\\ \n " +
429+ " \" SDKROOT\" : \" " + sdkRoot + "\" ,\\ \n " +
430+ " }," ,
431+ path .Join (cppConfigDir , "BUILD" ),
432+ }
433+ if _ , err := r .ExecCmd ("sed" , sedArgs ... ); err != nil {
434+ return "" , fmt .Errorf ("failed to add extra_env to cc_toolchain_config in BUILD" )
435+ }
436+ }
437+
342438 outputTarball := "cpp_configs.tar"
343439 // Explicitly use absolute paths to avoid confusion on what's the working directory.
344440 outputTarballPath := path .Join (o .TempWorkDir , outputTarball )
0 commit comments