bazelbuild / bazelbuild/bazel

Apple toolchain env values don't get mixed into actions unless --cpu=darwin

Open
#16,784 0 comments 0 reactions 0 assignees View on GitHub
help wanted P3 platform: apple team-Rules-CPP type: bug
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the bug:

When building a `cc_binary` (or other CcToolchain-depending target), Bazel sets up some environment variables for the actions, based mostly on flags passed to bazel, to drive the clang wrapper in particular ways, e.g. the `APPLE_SDK_PLATFORM` and `APPLE_SDK_VERSION_OVERRIDE` environment variables based on e.g. the `--macos_sdk_version` flag.

Ideally, this would all be configured based on platforms - the `--platforms` flag, or transitions within the build graph.

Without this, it's harder to perform cross-compiling builds (e.g. triggering remote macOS builds from a Linux host), or to do multi-platform builds (e.g. building some things for Linux and others for Mac).

### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Write a `cc_binary`, and try to compile it remotely on macOS from a Linux host, without setting `--cpu=darwin`.

(In particular, when building with `--incompatible_enable_cc_toolchain_resolution`, which 🤞 we can actually flip the default of soon).

### Which operating system are you running Bazel on?

Ubuntu 20.04

### What is the output of `bazel info release`?

release 5.3.1

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.

_No response_

### What's the output of `git remote get-url origin; git rev-parse master; git rev-parse HEAD` ?

_No response_

### Have you found anything relevant by searching the web?

_No response_

### Any other information, logs, or outputs that you want to share?

Some relevant code traces:

[This code](https://github.com/bazelbuild/bazel/blob/6c1e4b72ae63417e5e0f2c7aef42fdcb4c172fe6/src/main/java/com/google/devtools/build/lib/rules/apple/cpp/AppleCcToolchain.java#L124-L135) only sets up an `ApplePlatform` and fetches config values from it if the `--cpu` flag has a darwin-like value.

[This code](https://github.com/bazelbuild/bazel/blob/a67c823938dcc426952d25b3a68b825cbe5f6586/src/main/java/com/google/devtools/build/lib/rules/apple/ApplePlatform.java#L135-L156) does the `ApplePlatform` loading itself.

[This code](https://github.com/bazelbuild/bazel/blob/a67c823938dcc426952d25b3a68b825cbe5f6586/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfigInfo.java#L206-L233) conditionally reads from the `--macos_sdk_version` flag based on that platform.

[This code](https://github.com/bazelbuild/bazel/blob/6c1e4b72ae63417e5e0f2c7aef42fdcb4c172fe6/tools/osx/crosstool/cc_toolchain_config.bzl#L1543-L1559) actually sets up the env vars based on the variables which don't end up getting set if `--cpu` isn't darwin-like (or rather, they get set to empty values, because the variables they depend on aren't set).

Contributor guide

Open the contributing guide

Research direction

Start with src/main/java/com/google/devtools/build/lib/rules/apple/cpp/AppleCcToolchain.java and trace how ApplePlatform.java and XcodeConfigInfo.java obtain configuration when --cpu is not darwin-like. Then inspect tools/osx/crosstool/cc_toolchain_config.bzl, especially the environment setup around lines 1543-1559. Done means Apple SDK-related action environment values are available for platform-selected remote or multi-platform builds without requiring --cpu=darwin.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, java
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.