Skip to content

Commit

Permalink
Add Apple CC toolchain setup (#113)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
keith authored Jan 17, 2023
1 parent bdaa508 commit 311cc83
Show file tree
Hide file tree
Showing 26 changed files with 6,376 additions and 17 deletions.
32 changes: 27 additions & 5 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ x_defaults:
common: &common
platform: macos
build_targets:
- "//..."
- "//..."
test_targets:
- "//..."
- "//..."

toolchain_flags: &toolchain_flags
build_flags:
- "--apple_crosstool_top=@local_config_apple_cc//:toolchain"
- "--crosstool_top=@local_config_apple_cc//:toolchain"
- "--host_crosstool_top=@local_config_apple_cc//:toolchain"
test_flags:
- "--apple_crosstool_top=@local_config_apple_cc//:toolchain"
- "--crosstool_top=@local_config_apple_cc//:toolchain"
- "--host_crosstool_top=@local_config_apple_cc//:toolchain"

tasks:
macos_latest:
Expand All @@ -24,9 +34,21 @@ tasks:
name: "Latest Bazel with Head Deps"
bazel: latest
shell_commands:
# Update the WORKSPACE to use head versions of some deps to ensure nothing
# has landed on them breaking this project.
- .bazelci/update_workspace_to_deps_heads.sh
# Update the WORKSPACE to use head versions of some deps to ensure nothing
# has landed on them breaking this project.
- .bazelci/update_workspace_to_deps_heads.sh
<<: *common

macos_latest_vendored_toolchain:
name: "Latest Bazel vendored toolchain"
bazel: latest
<<: *common
<<: *toolchain_flags

macos_last_green_vendored_toolchain:
name: "Last Green Bazel vendored toolchain"
bazel: last_green
<<: *common
<<: *toolchain_flags

buildifier: latest
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ filegroup(
testonly = 1,
srcs = [
"WORKSPACE",
"//constraints:for_bazel_tests",
"//crosstool:for_bazel_tests",
"//lib:for_bazel_tests",
"//rules:for_bazel_tests",
"//tools:for_bazel_tests",
Expand Down
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ module(
bazel_dep(name = "bazel_skylib", version = "1.3.0")
bazel_dep(name = "platforms", version = "0.0.4")
bazel_dep(name = "stardoc", version = "0.5.3", dev_dependency = True, repo_name = "io_bazel_stardoc")

apple_cc_configure = use_extension("//crosstool:setup.bzl", "apple_cc_configure_extension")
use_repo(apple_cc_configure, "local_config_apple_cc", "local_config_apple_cc_toolchains")

register_toolchains("@local_config_apple_cc_toolchains//:all")
69 changes: 57 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
# Apple Support for [Bazel](https://bazel.build)

This repository contains helper methods that support building rules that
target Apple platforms. See [the
docs](https://github.com/bazelbuild/apple_support/tree/master/doc) for
how you can use these helpers. Also see
[rules_apple](https://github.com/bazelbuild/rules_apple) and
[rules_swift](https://github.com/bazelbuild/rules_swift) for more Apple
platform rules.

## Quick setup

Copy the `WORKSPACE` snippet from [the releases
page](https://github.com/bazelbuild/apple_support/releases).
This repository contains the [Apple CC toolchain](#toolchain-setup),
Apple related [platforms](platforms/BUILD) and
[constraints](constraints/BUILD) definitions, and small helper functions
for rules authors targeting Apple platforms.

If you want to build iOS, tvOS, watchOS, or macOS apps, use
[`rules_apple`][rules_apple].

If you want to build Swift use
[`rules_swift`](https://github.com/bazelbuild/rules_swift).

See [the documentation](doc) for the helper rules provided by this
repository.

## Installation

Copy the `MODULE.bazel` or `WORKSPACE` snippets from [the releases
page](https://github.com/bazelbuild/apple_support/releases) into your
project.

## Toolchain setup

The Apple CC toolchain in by this repository provides toolchains for
building for Apple platforms besides macOS. Since Bazel 7 this toolchain
is required when targeting those platforms but the toolchain also
supports Bazel 6.

To use the Apple CC toolchain, pull this repository into your build and
add this to your `.bazelrc`:

```bzl
build --enable_platform_specific_config
build:macos --apple_crosstool_top=@local_config_apple_cc//:toolchain
build:macos --crosstool_top=@local_config_apple_cc//:toolchain
build:macos --host_crosstool_top=@local_config_apple_cc//:toolchain
```

This ensures that all rules provided by [`rules_apple`][rules_apple], as
well as other rules like `cc_binary`, all use the toolchain provided by
this repository when building on macOS.

NOTE: This toolchain requires a full Xcode installation, not just the
Xcode Command Line Tools. If you only need to build for macOS and don't
want to require a full Xcode installation in your build, use the builtin
Unix toolchain provided by Bazel.

### Incompatible toolchain resolution

Bazel is currently working on migrating C++ toolchain configuration to a
new discovery method that no longer uses the `--*crosstool_top` flags.
If you would like to test this upcoming feature, or need to use this in
your build for other reasons, you can use this toolchain with
`--incompatible_enable_cc_toolchain_resolution` as long as you provide a
`platform_mappings` file. Please file any issues you find as you test
this work in progress configuration.

[rules_apple]: https://github.com/bazelbuild/rules_apple
8 changes: 8 additions & 0 deletions constraints/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ constraint_value(
name = "catalyst",
constraint_setting = ":target_environment",
)

# Consumed by bazel tests.
filegroup(
name = "for_bazel_tests",
testonly = True,
srcs = glob(["**"]),
visibility = ["//:__pkg__"],
)
22 changes: 22 additions & 0 deletions crosstool/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package(default_visibility = ["//visibility:public"])

# Files which shouldn't be publicly visible and dependencies of all objc_* or ios_* rules should be excluded.
exports_files(glob(
["**"],
))

cc_binary(
name = "wrapped_clang",
testonly = True,
srcs = [
"wrapped_clang.cc",
],
)

# Consumed by bazel tests.
filegroup(
name = "for_bazel_tests",
testonly = True,
srcs = glob(["**"]),
visibility = ["//:__pkg__"],
)
100 changes: 100 additions & 0 deletions crosstool/BUILD.toolchains
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package(default_visibility = ["//visibility:public"])

# Target constraints for each arch.
# TODO(apple-rules): Rename osx constraint to macOS.
OSX_TOOLS_CONSTRAINTS = {
"armeabi-v7a": ["@platforms//cpu:arm"],
"darwin_arm64": [
"@platforms//os:osx",
"@platforms//cpu:arm64",
],
"darwin_arm64e": [
"@platforms//os:osx",
"@platforms//cpu:arm64",
],
"darwin_x86_64": [
"@platforms//os:osx",
"@platforms//cpu:x86_64",
],
"ios_arm64": [
"@platforms//os:ios",
"@platforms//cpu:arm64",
"@build_bazel_apple_support//constraints:device",
],
"ios_arm64e": [
"@platforms//os:ios",
"@platforms//cpu:arm64",
],
"ios_armv7": [
"@platforms//os:ios",
"@platforms//cpu:armv7",
],
"ios_i386": [
"@platforms//os:ios",
"@platforms//cpu:i386",
],
"ios_x86_64": [
"@platforms//os:ios",
"@platforms//cpu:x86_64",
],
"ios_sim_arm64": [
"@platforms//os:ios",
"@platforms//cpu:arm64",
"@build_bazel_apple_support//constraints:simulator",
],
"tvos_arm64": [
"@platforms//os:tvos",
"@platforms//cpu:arm64",
"@build_bazel_apple_support//constraints:device",
],
"tvos_x86_64": [
"@platforms//os:tvos",
"@platforms//cpu:x86_64",
],
"tvos_sim_arm64": [
"@platforms//os:tvos",
"@platforms//cpu:arm64",
"@build_bazel_apple_support//constraints:simulator",
],
"watchos_arm64": [
"@platforms//os:watchos",
"@platforms//cpu:arm64",
],
"watchos_arm64_32": [
"@platforms//os:watchos",
"@platforms//cpu:arm64_32",
],
"watchos_armv7k": [
"@platforms//os:watchos",
"@platforms//cpu:armv7k",
],
"watchos_i386": [
"@platforms//os:watchos",
"@platforms//cpu:i386",
],
"watchos_x86_64": [
"@platforms//os:watchos",
"@platforms//cpu:x86_64",
],
}

OSX_DEVELOPER_PLATFORM_CPUS = [
"arm64",
"x86_64",
]

[
toolchain(
name = "cc-toolchain-" + arch + "-" + cpu,
exec_compatible_with = [
# These only execute on macOS.
"@platforms//os:osx",
"@platforms//cpu:" + cpu,
],
target_compatible_with = OSX_TOOLS_CONSTRAINTS[arch],
toolchain = "@local_config_apple_cc//:cc-compiler-" + arch,
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
for arch in OSX_TOOLS_CONSTRAINTS.keys()
for cpu in OSX_DEVELOPER_PLATFORM_CPUS
]
Loading

0 comments on commit 311cc83

Please sign in to comment.