Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Apple CC toolchain setup #113

Merged
merged 40 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
04a6e15
Add example toolchains setup
keith Mar 23, 2022
92d9c69
Add other var back
keith Nov 1, 2022
8e953ad
Initial mv of crosstool setup
keith Nov 1, 2022
a5bb6c3
First pass setup
keith Nov 1, 2022
6bb507e
Remove separate osx_archs.bzl
keith Nov 1, 2022
029317d
Fixup for moved files, fallback to builtin version
keith Nov 1, 2022
a0afb2d
Make crosstool tests pass
keith Nov 1, 2022
c395f9e
Fix buildifier
keith Nov 1, 2022
245e052
sync with upstream
keith Jan 6, 2023
28f9f2e
Use @local_config_apple_cc for repo
keith Jan 9, 2023
a982d7b
buildifier
keith Jan 9, 2023
5c2112d
Add some initial device vs sim constraints
keith Jan 9, 2023
7e7eb22
Add initial tests
keith Jan 9, 2023
f946429
Move more tests
keith Jan 10, 2023
978e770
Enable objc_compile for detection
keith Jan 10, 2023
b80b588
Add test filegroups
keith Jan 10, 2023
4d1691c
Move wrapped_clang test
keith Jan 10, 2023
a024f26
rm
keith Jan 10, 2023
7450436
Add objc-archive back for now
keith Jan 10, 2023
2b6373e
Remove supports_pic
keith Jan 11, 2023
12d0ad4
Add new CI jobs
keith Jan 11, 2023
46b4d51
remove test target
keith Jan 11, 2023
8eb4d6b
all options
keith Jan 11, 2023
3365b62
Move to correct location
keith Jan 11, 2023
0462476
sync upstream no dynamic_lookup
keith Jan 11, 2023
76acb02
Remove unnecessary Xcode logic
keith Jan 12, 2023
eec7cd7
Fix setup to not mirror upstream
keith Jan 12, 2023
2889c5b
no rules_cc
keith Jan 12, 2023
1d59f22
bzlmod support
keith Jan 12, 2023
48d65d5
minor todos
keith Jan 12, 2023
0224914
lint
keith Jan 12, 2023
121c3c8
format
keith Jan 12, 2023
c3588f4
names
keith Jan 12, 2023
526a361
dup
keith Jan 12, 2023
d1f735d
all
keith Jan 12, 2023
2a3bb18
update readme
keith Jan 12, 2023
badab2b
more
keith Jan 12, 2023
c54b4d7
add env var
keith Jan 12, 2023
77ad32f
Update crosstool/setup.bzl
keith Jan 13, 2023
7a01a5e
Update crosstool/setup.bzl
keith Jan 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -11,3 +11,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
thii marked this conversation as resolved.
Show resolved Hide resolved
```

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.
keith marked this conversation as resolved.
Show resolved Hide resolved
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