Skip to content

Use Bazel-provided DEVELOPER_DIR and SDKROOT#938

Open
Yannic wants to merge 1 commit intobazel-contrib:mainfrom
EngFlow:yannic-macos-env
Open

Use Bazel-provided DEVELOPER_DIR and SDKROOT#938
Yannic wants to merge 1 commit intobazel-contrib:mainfrom
EngFlow:yannic-macos-env

Conversation

@Yannic
Copy link
Copy Markdown

@Yannic Yannic commented Jun 30, 2022

With this change, rules_foreign_cc respects the value from
--xcode_version and doesn't rely on the user's default Xcode
being the one that's used.

@Yannic Yannic force-pushed the yannic-macos-env branch 5 times, most recently from f1b9a8a to effdb22 Compare June 30, 2022 17:46
@Yannic Yannic marked this pull request as draft June 30, 2022 19:21
@Yannic
Copy link
Copy Markdown
Author

Yannic commented Jun 30, 2022

@lfpino FYI

@Yannic Yannic force-pushed the yannic-macos-env branch 2 times, most recently from 843f2c3 to e222a7e Compare July 1, 2022 10:00
With this change, `rules_foreign_cc` respects the value from
`--xcode_version` and doesn't rely on the user's default Xcode
being the one that's used.
@Yannic Yannic force-pushed the yannic-macos-env branch from e222a7e to a473d42 Compare July 1, 2022 10:05
@Yannic Yannic marked this pull request as ready for review July 1, 2022 10:47
@keith
Copy link
Copy Markdown
Member

keith commented Jul 1, 2022

I'm pretty sure we don't get values for these variables which is why I had to do this in the first place 🤔 #767 did something change in bazel for this?

@Yannic
Copy link
Copy Markdown
Author

Yannic commented Jul 1, 2022

There are a lot of subtleties unfortunately. We get the vars iff we set use_default_shell_env = False and env = <something not None>. I haven't tracked down the code in Bazel yet, but I confirmed that we get the env vars from Bazel by printing the environment from within the action.

@keith
Copy link
Copy Markdown
Member

keith commented Jul 1, 2022

Aren't there other downsides of setting that to False? I can't recall the behavior matrix but I remember hitting issues with this before bazelbuild/rules_swift#502

@Yannic
Copy link
Copy Markdown
Author

Yannic commented Jul 1, 2022

We are adding ctx.configuration.default_shell_env to env, so we should be good?

Yannic added a commit to Yannic/envoy-mobile that referenced this pull request Jul 4, 2022
This upgrades `rules_foreign_cc` to a version including
bazel-contrib/rules_foreign_cc#938, which fixes
a build failure when the requested Apple SDK from `--xcode_version` does
not match the system's default Xcode's SDKs.

Example output:
```
rules_foreign_cc: Build failed!
rules_foreign_cc: Keeping temp build directory and dependencies directory for debug.
rules_foreign_cc: Please note that the directories inside a sandbox are still cleaned unless you specify --sandbox_debug Bazel command line flag.
rules_foreign_cc: Printing build logs:
_____ BEGIN BUILD LOGS _____
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: unable to lookup item 'Path' in SDK 'macosx12.1'
+ XCODE_VERSION_OVERRIDE=13.2.1.13C100
+ APPLE_SDK_VERSION_OVERRIDE=12.1
+ APPLE_SDK_PLATFORM=MacOSX
```

This fixes a hermeticity problem in the build and is a prerequisite for
upgradting the macOS RE cluster to macOS 12, which in turn is a
requirement for upgrading to Xcode 13.4.

Progress on envoyproxy#2100
Yannic added a commit to Yannic/envoy-mobile that referenced this pull request Jul 4, 2022
This upgrades `rules_foreign_cc` to a version including
bazel-contrib/rules_foreign_cc#938, which fixes
a build failure when the requested Apple SDK from `--xcode_version` does
not match the system's default Xcode's SDKs.

Example output:
```
rules_foreign_cc: Build failed!
rules_foreign_cc: Keeping temp build directory and dependencies directory for debug.
rules_foreign_cc: Please note that the directories inside a sandbox are still cleaned unless you specify --sandbox_debug Bazel command line flag.
rules_foreign_cc: Printing build logs:
_____ BEGIN BUILD LOGS _____
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: unable to lookup item 'Path' in SDK 'macosx12.1'
+ XCODE_VERSION_OVERRIDE=13.2.1.13C100
+ APPLE_SDK_VERSION_OVERRIDE=12.1
+ APPLE_SDK_PLATFORM=MacOSX
```

This fixes a hermeticity problem in the build and is a prerequisite for
upgradting the macOS RE cluster to macOS 12, which in turn is a
requirement for upgrading to Xcode 13.4.

Progress on envoyproxy#2100

Signed-off-by: Yannic Bonenberger <yannic@engflow.com>
@Yannic
Copy link
Copy Markdown
Author

Yannic commented Jul 4, 2022

This is what I get on my local machine without this patch when explicitly passing --xcode_version:

yannic@Yannics-iMac rules_foreign_cc % xcode-select --print-path
/Library/Developer/CommandLineTools
yannic@Yannics-iMac rules_foreign_cc % bazel build --xcode_version=13 //toolchains/...
DEBUG: /private/var/tmp/_bazel_yannic/336b1286a10bf713ca7071372228a986/external/bazel_toolchains/rules/rbe_repo/version_check.bzl:68:14: 
Current running Bazel is ahead of bazel-toolchains repo. Please update your pin to bazel-toolchains repo in your WORKSPACE file.
DEBUG: /private/var/tmp/_bazel_yannic/336b1286a10bf713ca7071372228a986/external/bazel_toolchains/rules/rbe_repo/checked_in.bzl:125:14: buildkite_config not using checked in configs; Bazel version 5.2.0 was picked/selected but no checked in config was found in map {"0.20.0": ["8.0.0"], "0.21.0": ["8.0.0"], "0.22.0": ["8.0.0", "9.0.0"], "0.23.0": ["8.0.0", "9.0.0"], "0.23.1": ["8.0.0", "9.0.0"], "0.23.2": ["9.0.0"], "0.24.0": ["9.0.0"], "0.24.1": ["9.0.0"], "0.25.0": ["9.0.0"], "0.25.1": ["9.0.0"], "0.25.2": ["9.0.0"], "0.26.0": ["9.0.0"], "0.26.1": ["9.0.0"], "0.27.0": ["9.0.0"], "0.27.1": ["9.0.0"], "0.28.0": ["9.0.0"], "0.28.1": ["9.0.0"], "0.29.0": ["9.0.0"], "0.29.1": ["9.0.0", "10.0.0"], "1.0.0": ["9.0.0", "10.0.0"], "1.0.1": ["10.0.0"], "1.1.0": ["10.0.0"], "1.2.0": ["10.0.0"], "1.2.1": ["10.0.0"], "2.0.0": ["10.0.0"], "2.1.0": ["10.0.0"], "2.1.1": ["10.0.0", "11.0.0"], "2.2.0": ["11.0.0"], "3.0.0": ["11.0.0"], "3.1.0": ["11.0.0"], "3.2.0": ["11.0.0"], "3.3.0": ["11.0.0"], "3.3.1": ["11.0.0"], "3.4.1": ["11.0.0"], "3.5.0": ["11.0.0"], "3.5.1": ["11.0.0"], "3.6.0": ["11.0.0"], "3.7.0": ["11.0.0"], "3.7.1": ["11.0.0"], "3.7.2": ["11.0.0"], "4.0.0": ["11.0.0"]}
INFO: Analyzed 27 targets (0 packages loaded, 0 targets configured).
INFO: Found 27 targets...
ERROR: /Users/yannic/Developer/rules_foreign_cc/toolchains/BUILD.bazel:130:10: BootstrapGNUMake toolchains/make failed: (Exit 77): bash failed: error executing command /bin/bash -c bazel-out/darwin-opt-exec-2B5CBBC6/bin/toolchains/make_tool_foreign_cc/wrapper_build_script.sh

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
rules_foreign_cc: Build failed!
rules_foreign_cc: Keeping temp build directory and dependencies directory for debug.
rules_foreign_cc: Please note that the directories inside a sandbox are still cleaned unless you specify --sandbox_debug Bazel command line flag.
rules_foreign_cc: Printing build logs:
_____ BEGIN BUILD LOGS _____
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: unable to lookup item 'Path' in SDK 'macosx12.1'
+ XCODE_VERSION_OVERRIDE=13.2.1.13C100
+ APPLE_SDK_VERSION_OVERRIDE=12.1
+ APPLE_SDK_PLATFORM=MacOSX
+ ZERO_AR_DATE=1
+ AR=/private/var/tmp/_bazel_yannic/336b1286a10bf713ca7071372228a986/sandbox/darwin-sandbox/6/execroot/rules_foreign_cc/external/local_config_cc/libtool
+ ARFLAGS=-o
+ CC=/private/var/tmp/_bazel_yannic/336b1286a10bf713ca7071372228a986/sandbox/darwin-sandbox/6/execroot/rules_foreign_cc/external/local_config_cc/wrapped_clang
+ CFLAGS='-D_FORTIFY_SOURCE=1 -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -g0 -O2 -DNDEBUG -DNS_BLOCK_ASSERTIONS=1 DEBUG_PREFIX_MAP_PWD=. -isysroot __BAZEL_XCODE_SDKROOT__ -F__BAZEL_XCODE_SDKROOT__/System/Library/Frameworks -F__BAZEL_XCODE_DEVELOPER_DIR__/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-canonical-prefixes -pthread -no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__=redacted -D__TIMESTAMP__=redacted -D__TIME__=redacted -target x86_64-apple-macosx12.1'
+ LD=/private/var/tmp/_bazel_yannic/336b1286a10bf713ca7071372228a986/sandbox/darwin-sandbox/6/execroot/rules_foreign_cc/external/local_config_cc/cc_wrapper.sh
+ LDFLAGS='-lc++ -fobjc-link-runtime -headerpad_max_install_names -no-canonical-prefixes -target x86_64-apple-macosx12.1 -lc++ -target x86_64-apple-macosx12.1 -undefined error'
+ ./configure --without-guile --with-guile=no --disable-dependency-tracking --prefix=/private/var/tmp/_bazel_yannic/336b1286a10bf713ca7071372228a986/sandbox/darwin-sandbox/6/execroot/rules_foreign_cc/bazel-out/darwin-opt-exec-2B5CBBC6/bin/toolchains/make
checking for a BSD-compatible install... /usr/local/bin/ginstall -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/local/bin/gmkdir -p
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports the include directive... yes (GNU style)
checking for gcc... /private/var/tmp/_bazel_yannic/336b1286a10bf713ca7071372228a986/sandbox/darwin-sandbox/6/execroot/rules_foreign_cc/external/local_config_cc/wrapped_clang
checking whether the C compiler works... no
configure: error: in `/private/var/tmp/_bazel_yannic/336b1286a10bf713ca7071372228a986/sandbox/darwin-sandbox/6/execroot/rules_foreign_cc/bazel-out/darwin-opt-exec-2B5CBBC6/bin/toolchains/make.build_tmpdir':
configure: error: C compiler cannot create executables
See `config.log' for more details
_____ END BUILD LOGS _____
rules_foreign_cc: Build wrapper script location: bazel-out/darwin-opt-exec-2B5CBBC6/bin/toolchains/make_tool_foreign_cc/wrapper_build_script.sh
rules_foreign_cc: Build script location: bazel-out/darwin-opt-exec-2B5CBBC6/bin/toolchains/make_tool_foreign_cc/build_script.sh
rules_foreign_cc: Build log location: bazel-out/darwin-opt-exec-2B5CBBC6/bin/toolchains/make_tool_foreign_cc/BootstrapGNUMake.log

INFO: Elapsed time: 6.101s, Critical Path: 5.74s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

jpsim pushed a commit to envoyproxy/envoy-mobile that referenced this pull request Jul 5, 2022
This upgrades `rules_foreign_cc` to a version including
bazel-contrib/rules_foreign_cc#938, which fixes
a build failure when the requested Apple SDK from `--xcode_version` does
not match the system's default Xcode's SDKs.

Example output:
```
rules_foreign_cc: Build failed!
rules_foreign_cc: Keeping temp build directory and dependencies directory for debug.
rules_foreign_cc: Please note that the directories inside a sandbox are still cleaned unless you specify --sandbox_debug Bazel command line flag.
rules_foreign_cc: Printing build logs:
_____ BEGIN BUILD LOGS _____
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: unable to lookup item 'Path' in SDK 'macosx12.1'
+ XCODE_VERSION_OVERRIDE=13.2.1.13C100
+ APPLE_SDK_VERSION_OVERRIDE=12.1
+ APPLE_SDK_PLATFORM=MacOSX
```

This fixes a hermeticity problem in the build and is a prerequisite for
upgradting the macOS RE cluster to macOS 12, which in turn is a
requirement for upgrading to Xcode 13.4.

Progress on #2100

Signed-off-by: Yannic Bonenberger <yannic@engflow.com>
@jpsim
Copy link
Copy Markdown

jpsim commented Jul 5, 2022

Would a more focused solution be to exclude or override DEVELOPER_DIR and SDKROOT if --xcode_version is specified?

@Yannic
Copy link
Copy Markdown
Author

Yannic commented Jul 5, 2022

Explicitly setting --xcode_version only overrides the default Bazel picks (I think Bazel always picks latest available). Bazel always sets DEVELOPER_DIR and SDKROOT for native C++ actions and we should do the same for "foreign" C++ actions.

@keith
Copy link
Copy Markdown
Member

keith commented Jul 5, 2022

We are adding ctx.configuration.default_shell_env to env, so we should be good?

IIRC the issue had something to do with the --action_env=FOO form of passing a var through, I think those don't show up in the default_shell_env if they aren't set to an explicit value?

jpsim pushed a commit to envoyproxy/envoy that referenced this pull request Nov 29, 2022
This upgrades `rules_foreign_cc` to a version including
bazel-contrib/rules_foreign_cc#938, which fixes
a build failure when the requested Apple SDK from `--xcode_version` does
not match the system's default Xcode's SDKs.

Example output:
```
rules_foreign_cc: Build failed!
rules_foreign_cc: Keeping temp build directory and dependencies directory for debug.
rules_foreign_cc: Please note that the directories inside a sandbox are still cleaned unless you specify --sandbox_debug Bazel command line flag.
rules_foreign_cc: Printing build logs:
_____ BEGIN BUILD LOGS _____
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: SDK "macosx12.1" cannot be located
xcrun: error: unable to lookup item 'Path' in SDK 'macosx12.1'
+ XCODE_VERSION_OVERRIDE=13.2.1.13C100
+ APPLE_SDK_VERSION_OVERRIDE=12.1
+ APPLE_SDK_PLATFORM=MacOSX
```

This fixes a hermeticity problem in the build and is a prerequisite for
upgradting the macOS RE cluster to macOS 12, which in turn is a
requirement for upgrading to Xcode 13.4.

Progress on envoyproxy/envoy-mobile#2100

Signed-off-by: Yannic Bonenberger <yannic@engflow.com>
Signed-off-by: JP Simard <jp@jpsim.com>
phlax added a commit to phlax/envoy that referenced this pull request Nov 18, 2025
Not clear if this is needed the related PR (bazel-contrib/rules_foreign_cc#938)
never landed, but it seems the pinned version is not compatible with hermetic toolchains

Signed-off-by: Ryan Northey <ryan@synca.io>
phlax added a commit to envoyproxy/envoy that referenced this pull request Nov 18, 2025
Not clear if this is needed - the related PR
(bazel-contrib/rules_foreign_cc#938) never landed,
but it seems the pinned version is not compatible with hermetic
toolchains

Signed-off-by: Ryan Northey <ryan@synca.io>
grnmeira pushed a commit to grnmeira/envoy that referenced this pull request Mar 20, 2026
Not clear if this is needed - the related PR
(bazel-contrib/rules_foreign_cc#938) never landed,
but it seems the pinned version is not compatible with hermetic
toolchains

Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Gustavo <grnmeira@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants