rules_foreign_cc icon indicating copy to clipboard operation
rules_foreign_cc copied to clipboard

Use Bazel-provided `DEVELOPER_DIR` and `SDKROOT`

Open Yannic opened this issue 3 years ago • 9 comments

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 avatar Jun 30 '22 16:06 Yannic

@lfpino FYI

Yannic avatar Jun 30 '22 19:06 Yannic

I'm pretty sure we don't get values for these variables which is why I had to do this in the first place 🤔 https://github.com/bazelbuild/rules_foreign_cc/pull/767 did something change in bazel for this?

keith avatar Jul 01 '22 11:07 keith

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.

Yannic avatar Jul 01 '22 11:07 Yannic

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 https://github.com/bazelbuild/rules_swift/pull/502

keith avatar Jul 01 '22 11:07 keith

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

Yannic avatar Jul 01 '22 12:07 Yannic

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

Yannic avatar Jul 04 '22 08:07 Yannic

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

jpsim avatar Jul 05 '22 15:07 jpsim

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.

Yannic avatar Jul 05 '22 15:07 Yannic

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?

keith avatar Jul 05 '22 16:07 keith