Skip to content

Commit 311cc83

Browse files
authored
Add Apple CC toolchain setup (#113)
This migrates the Apple CC toolchain from bazel into this repo. This allows this to evolve and be updated without a full bazel udpate. Bazel's built in unix toolchain can still be used to build macOS C++ targets, but this repo handles a wider variety of Apple specific flags and requires you have Xcode installed.
1 parent bdaa508 commit 311cc83

26 files changed

+6376
-17
lines changed

.bazelci/presubmit.yml

+27-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ x_defaults:
55
common: &common
66
platform: macos
77
build_targets:
8-
- "//..."
8+
- "//..."
99
test_targets:
10-
- "//..."
10+
- "//..."
11+
12+
toolchain_flags: &toolchain_flags
13+
build_flags:
14+
- "--apple_crosstool_top=@local_config_apple_cc//:toolchain"
15+
- "--crosstool_top=@local_config_apple_cc//:toolchain"
16+
- "--host_crosstool_top=@local_config_apple_cc//:toolchain"
17+
test_flags:
18+
- "--apple_crosstool_top=@local_config_apple_cc//:toolchain"
19+
- "--crosstool_top=@local_config_apple_cc//:toolchain"
20+
- "--host_crosstool_top=@local_config_apple_cc//:toolchain"
1121

1222
tasks:
1323
macos_latest:
@@ -24,9 +34,21 @@ tasks:
2434
name: "Latest Bazel with Head Deps"
2535
bazel: latest
2636
shell_commands:
27-
# Update the WORKSPACE to use head versions of some deps to ensure nothing
28-
# has landed on them breaking this project.
29-
- .bazelci/update_workspace_to_deps_heads.sh
37+
# Update the WORKSPACE to use head versions of some deps to ensure nothing
38+
# has landed on them breaking this project.
39+
- .bazelci/update_workspace_to_deps_heads.sh
40+
<<: *common
41+
42+
macos_latest_vendored_toolchain:
43+
name: "Latest Bazel vendored toolchain"
44+
bazel: latest
45+
<<: *common
46+
<<: *toolchain_flags
47+
48+
macos_last_green_vendored_toolchain:
49+
name: "Last Green Bazel vendored toolchain"
50+
bazel: last_green
3051
<<: *common
52+
<<: *toolchain_flags
3153

3254
buildifier: latest

BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ filegroup(
2424
testonly = 1,
2525
srcs = [
2626
"WORKSPACE",
27+
"//constraints:for_bazel_tests",
28+
"//crosstool:for_bazel_tests",
2729
"//lib:for_bazel_tests",
2830
"//rules:for_bazel_tests",
2931
"//tools:for_bazel_tests",

MODULE.bazel

+5
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ module(
1212
bazel_dep(name = "bazel_skylib", version = "1.3.0")
1313
bazel_dep(name = "platforms", version = "0.0.4")
1414
bazel_dep(name = "stardoc", version = "0.5.3", dev_dependency = True, repo_name = "io_bazel_stardoc")
15+
16+
apple_cc_configure = use_extension("//crosstool:setup.bzl", "apple_cc_configure_extension")
17+
use_repo(apple_cc_configure, "local_config_apple_cc", "local_config_apple_cc_toolchains")
18+
19+
register_toolchains("@local_config_apple_cc_toolchains//:all")

README.md

+57-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
# Apple Support for [Bazel](https://bazel.build)
22

3-
This repository contains helper methods that support building rules that
4-
target Apple platforms. See [the
5-
docs](https://github.com/bazelbuild/apple_support/tree/master/doc) for
6-
how you can use these helpers. Also see
7-
[rules_apple](https://github.com/bazelbuild/rules_apple) and
8-
[rules_swift](https://github.com/bazelbuild/rules_swift) for more Apple
9-
platform rules.
10-
11-
## Quick setup
12-
13-
Copy the `WORKSPACE` snippet from [the releases
14-
page](https://github.com/bazelbuild/apple_support/releases).
3+
This repository contains the [Apple CC toolchain](#toolchain-setup),
4+
Apple related [platforms](platforms/BUILD) and
5+
[constraints](constraints/BUILD) definitions, and small helper functions
6+
for rules authors targeting Apple platforms.
7+
8+
If you want to build iOS, tvOS, watchOS, or macOS apps, use
9+
[`rules_apple`][rules_apple].
10+
11+
If you want to build Swift use
12+
[`rules_swift`](https://github.com/bazelbuild/rules_swift).
13+
14+
See [the documentation](doc) for the helper rules provided by this
15+
repository.
16+
17+
## Installation
18+
19+
Copy the `MODULE.bazel` or `WORKSPACE` snippets from [the releases
20+
page](https://github.com/bazelbuild/apple_support/releases) into your
21+
project.
22+
23+
## Toolchain setup
24+
25+
The Apple CC toolchain in by this repository provides toolchains for
26+
building for Apple platforms besides macOS. Since Bazel 7 this toolchain
27+
is required when targeting those platforms but the toolchain also
28+
supports Bazel 6.
29+
30+
To use the Apple CC toolchain, pull this repository into your build and
31+
add this to your `.bazelrc`:
32+
33+
```bzl
34+
build --enable_platform_specific_config
35+
build:macos --apple_crosstool_top=@local_config_apple_cc//:toolchain
36+
build:macos --crosstool_top=@local_config_apple_cc//:toolchain
37+
build:macos --host_crosstool_top=@local_config_apple_cc//:toolchain
38+
```
39+
40+
This ensures that all rules provided by [`rules_apple`][rules_apple], as
41+
well as other rules like `cc_binary`, all use the toolchain provided by
42+
this repository when building on macOS.
43+
44+
NOTE: This toolchain requires a full Xcode installation, not just the
45+
Xcode Command Line Tools. If you only need to build for macOS and don't
46+
want to require a full Xcode installation in your build, use the builtin
47+
Unix toolchain provided by Bazel.
48+
49+
### Incompatible toolchain resolution
50+
51+
Bazel is currently working on migrating C++ toolchain configuration to a
52+
new discovery method that no longer uses the `--*crosstool_top` flags.
53+
If you would like to test this upcoming feature, or need to use this in
54+
your build for other reasons, you can use this toolchain with
55+
`--incompatible_enable_cc_toolchain_resolution` as long as you provide a
56+
`platform_mappings` file. Please file any issues you find as you test
57+
this work in progress configuration.
58+
59+
[rules_apple]: https://github.com/bazelbuild/rules_apple

constraints/BUILD

+8
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@ constraint_value(
2929
name = "catalyst",
3030
constraint_setting = ":target_environment",
3131
)
32+
33+
# Consumed by bazel tests.
34+
filegroup(
35+
name = "for_bazel_tests",
36+
testonly = True,
37+
srcs = glob(["**"]),
38+
visibility = ["//:__pkg__"],
39+
)

crosstool/BUILD

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
# Files which shouldn't be publicly visible and dependencies of all objc_* or ios_* rules should be excluded.
4+
exports_files(glob(
5+
["**"],
6+
))
7+
8+
cc_binary(
9+
name = "wrapped_clang",
10+
testonly = True,
11+
srcs = [
12+
"wrapped_clang.cc",
13+
],
14+
)
15+
16+
# Consumed by bazel tests.
17+
filegroup(
18+
name = "for_bazel_tests",
19+
testonly = True,
20+
srcs = glob(["**"]),
21+
visibility = ["//:__pkg__"],
22+
)

crosstool/BUILD.toolchains

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
# Target constraints for each arch.
4+
# TODO(apple-rules): Rename osx constraint to macOS.
5+
OSX_TOOLS_CONSTRAINTS = {
6+
"armeabi-v7a": ["@platforms//cpu:arm"],
7+
"darwin_arm64": [
8+
"@platforms//os:osx",
9+
"@platforms//cpu:arm64",
10+
],
11+
"darwin_arm64e": [
12+
"@platforms//os:osx",
13+
"@platforms//cpu:arm64",
14+
],
15+
"darwin_x86_64": [
16+
"@platforms//os:osx",
17+
"@platforms//cpu:x86_64",
18+
],
19+
"ios_arm64": [
20+
"@platforms//os:ios",
21+
"@platforms//cpu:arm64",
22+
"@build_bazel_apple_support//constraints:device",
23+
],
24+
"ios_arm64e": [
25+
"@platforms//os:ios",
26+
"@platforms//cpu:arm64",
27+
],
28+
"ios_armv7": [
29+
"@platforms//os:ios",
30+
"@platforms//cpu:armv7",
31+
],
32+
"ios_i386": [
33+
"@platforms//os:ios",
34+
"@platforms//cpu:i386",
35+
],
36+
"ios_x86_64": [
37+
"@platforms//os:ios",
38+
"@platforms//cpu:x86_64",
39+
],
40+
"ios_sim_arm64": [
41+
"@platforms//os:ios",
42+
"@platforms//cpu:arm64",
43+
"@build_bazel_apple_support//constraints:simulator",
44+
],
45+
"tvos_arm64": [
46+
"@platforms//os:tvos",
47+
"@platforms//cpu:arm64",
48+
"@build_bazel_apple_support//constraints:device",
49+
],
50+
"tvos_x86_64": [
51+
"@platforms//os:tvos",
52+
"@platforms//cpu:x86_64",
53+
],
54+
"tvos_sim_arm64": [
55+
"@platforms//os:tvos",
56+
"@platforms//cpu:arm64",
57+
"@build_bazel_apple_support//constraints:simulator",
58+
],
59+
"watchos_arm64": [
60+
"@platforms//os:watchos",
61+
"@platforms//cpu:arm64",
62+
],
63+
"watchos_arm64_32": [
64+
"@platforms//os:watchos",
65+
"@platforms//cpu:arm64_32",
66+
],
67+
"watchos_armv7k": [
68+
"@platforms//os:watchos",
69+
"@platforms//cpu:armv7k",
70+
],
71+
"watchos_i386": [
72+
"@platforms//os:watchos",
73+
"@platforms//cpu:i386",
74+
],
75+
"watchos_x86_64": [
76+
"@platforms//os:watchos",
77+
"@platforms//cpu:x86_64",
78+
],
79+
}
80+
81+
OSX_DEVELOPER_PLATFORM_CPUS = [
82+
"arm64",
83+
"x86_64",
84+
]
85+
86+
[
87+
toolchain(
88+
name = "cc-toolchain-" + arch + "-" + cpu,
89+
exec_compatible_with = [
90+
# These only execute on macOS.
91+
"@platforms//os:osx",
92+
"@platforms//cpu:" + cpu,
93+
],
94+
target_compatible_with = OSX_TOOLS_CONSTRAINTS[arch],
95+
toolchain = "@local_config_apple_cc//:cc-compiler-" + arch,
96+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
97+
)
98+
for arch in OSX_TOOLS_CONSTRAINTS.keys()
99+
for cpu in OSX_DEVELOPER_PLATFORM_CPUS
100+
]

0 commit comments

Comments
 (0)