Skip to content

Commit 80c56ff

Browse files
keithcopybara-github
authored andcommitted
Compile Apple tools as fat binaries if possible
The Apple toolchain has 2 native binaries that are inputs to every single action. Because of this if you want to share caches between Apple Silicon machines and Intel machines, you either need to force them to be x86_64 binaries and suffer the performance loss on Apple Silicon machiens, or use fat binaries so the sha's match on both architectures, which is what this change does. These binaries are so small that the size impact of this doesn't matter. Since Apple Silicon support requires Xcode 12 this falls back to compiling the single architecture binary if it fails, under the assumption that means you're on Xcode 11 or lower. We don't have a better indication at this point of what Xcode version you're using, so this seems like a fine workaround until Xcode 12 is the minimum supported version. Closes #13452. PiperOrigin-RevId: 405842940
1 parent 3c09f34 commit 80c56ff

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

tools/cpp/osx_cc_configure.bzl

+52-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains)
5353

5454
return include_dirs
5555

56-
def _compile_cc_file(repository_ctx, src_name, out_name):
56+
# TODO: Remove once Xcode 12 is the minimum supported version
57+
def _compile_cc_file_single_arch(repository_ctx, src_name, out_name):
5758
env = repository_ctx.os.environ
5859
xcrun_result = repository_ctx.execute([
5960
"env",
@@ -83,6 +84,56 @@ def _compile_cc_file(repository_ctx, src_name, out_name):
8384
"https://github.com/bazelbuild/bazel/issues with the following:\n" +
8485
error_msg)
8586

87+
def _compile_cc_file(repository_ctx, src_name, out_name):
88+
env = repository_ctx.os.environ
89+
xcrun_result = repository_ctx.execute([
90+
"env",
91+
"-i",
92+
"DEVELOPER_DIR={}".format(env.get("DEVELOPER_DIR", default = "")),
93+
"xcrun",
94+
"--sdk",
95+
"macosx",
96+
"clang",
97+
"-mmacosx-version-min=10.9",
98+
"-std=c++11",
99+
"-lc++",
100+
"-arch",
101+
"arm64",
102+
"-arch",
103+
"x86_64",
104+
"-Wl,-no_adhoc_codesign",
105+
"-O3",
106+
"-o",
107+
out_name,
108+
src_name,
109+
], 30)
110+
111+
if xcrun_result.return_code == 0:
112+
xcrun_result = repository_ctx.execute([
113+
"env",
114+
"-i",
115+
"codesign",
116+
"--identifier", # Required to be reproducible across archs
117+
out_name,
118+
"--force",
119+
"--sign",
120+
"-",
121+
out_name,
122+
], 30)
123+
if xcrun_result.return_code != 0:
124+
error_msg = (
125+
"codesign return code {code}, stderr: {err}, stdout: {out}"
126+
).format(
127+
code = xcrun_result.return_code,
128+
err = xcrun_result.stderr,
129+
out = xcrun_result.stdout,
130+
)
131+
fail(out_name + " failed to generate. Please file an issue at " +
132+
"https://github.com/bazelbuild/bazel/issues with the following:\n" +
133+
error_msg)
134+
else:
135+
_compile_cc_file_single_arch(repository_ctx, src_name, out_name)
136+
86137
def configure_osx_toolchain(repository_ctx, cpu_value, overriden_tools):
87138
"""Configure C++ toolchain on macOS.
88139

0 commit comments

Comments
 (0)