Skip to content

Commit

Permalink
Implement support for macOS x86_64 <-> arm64 cross compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanau committed Oct 19, 2024
1 parent 35ec39b commit 61a4bdc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
22 changes: 22 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,25 @@ build:windows --cxxopt='/Zc:__cplusplus' --host_cxxopt='/Zc:__cplusplus'
# enable clang coverage: https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
build:clang-coverage --copt="-fprofile-instr-generate" --linkopt="-fprofile-instr-generate"
build:clang-coverage --copt="-fcoverage-mapping" --linkopt="-fcoverage-mapping"

#
# Cross-Compilation
# So far, only cross-compiling on macOS between ARM and x86 is supported – using apple_support makes
# this much easier than on other platforms. While we define a configuration for Intel Mac here, the
# lack of a means to run Apple Silicon binaries there means that we can't run tests and would need
# to have mksnapshot use the host configuration again for it to work, effectively compiling much of
# V8 twice.

# basic cross-compilation options
# build:macos-cross --//:cross_compile=True
# Keep the host build in the opt configuration (this makes running torque and mksnapshot faster),
# but use -O1 to not spend too much time on building them.
# build:macos-cross --host_copt="-O1"
# Enable optimization to counteract tests being slightly slower under Rosetta.
# build:macos-cross --copt="-O1"

# Define the actual target platform.
build:macos-cross-x86_64 --cpu=darwin_x86_64 --host_cpu=darwin_arm64 --platforms //:macOS_x86
build:macos-cross-arm64 --cpu=darwin_arm64 --host_cpu=darwin_x86_64 --platforms //:macOS_arm64
# build:macos-cross-x86_64 --config=macos-cross
# build:macos-cross-arm64 --config=macos-cross
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:
# previous versions). Use remote_download_all for now.
- os: { name : windows, image : windows-2022 }
config: { suffix: '', bazel-args: "--config=windows_no_dbg --remote_download_all" }
# EXP: Cross-compile macOS Apple Silicon => x86
- os: { name : macOS, image : macos-15 }
config: { suffix: -x86-cross, bazel-args: --config=macos-cross-x86_64 }
# TODO (later): The custom Windows-debug configuration consistently runs out of disk
# space on CI, disable it for now. Once https://github.com/bazelbuild/bazel/issues/21615
# has been resolved we can likely re-enable it and possibly fold up the custom
Expand Down
27 changes: 27 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ cc_capnp_library(
visibility = ["//visibility:public"],
)

platform(
name = "macOS_x86",
constraint_values = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
)

platform(
name = "macOS_arm64",
constraint_values = [
"@platforms//os:macos",
"@platforms//cpu:arm64",
],
)

npm_link_all_packages(name = "node_modules")

npm_link_package(
Expand Down Expand Up @@ -73,6 +89,17 @@ config_setting(
flag_values = {"dead_strip": "True"},
)

# TODO(now): Unused? See if we can really cross-compile properly without this.
bool_flag(
name = "cross_compile",
build_setting_default = False,
)

config_setting(
name = "is_cross_compile",
flag_values = {"cross_compile": "True"},
)

# Workaround for bazel not supporting negated conditions (https://github.com/bazelbuild/bazel-skylib/issues/272)
selects.config_setting_group(
name = "not_dbg_build",
Expand Down
11 changes: 8 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,19 @@ git_repository(
remote = "https://chromium.googlesource.com/chromium/src/third_party/zlib.git",
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains", "rust_repository_set")

rules_rust_dependencies()

rust_register_toolchains(
edition = "2021",
# Rust registers wasm targets by default which we don't need, workerd is only built for its native platform.
extra_target_triples = [],
# Rust registers wasm targets by default which we don't need. Add support macOS cross-
# compilation, the overhead for this is limited with only a few more targets being generated
# for the extra triples.
extra_target_triples = [
"aarch64-apple-darwin",
"x86_64-apple-darwin",
],
versions = ["1.81.0"], # LLVM 18
)

Expand Down

0 comments on commit 61a4bdc

Please sign in to comment.