Skip to content

Commit 49f7ca8

Browse files
danakjAravind Vasudevan
authored andcommitted
Use bootstrap clang and set --target when cross-compiling Rust for ARM
The TARGET env var seems to be set by x.py or Cargo, so setting it in our build_rust.py script isn't helping. Use --target to specify that Rustc should be able to generate ARM binaries. The clang executable in the cross-compiling environment is only able to run on ARM as well. Instead of using it, use the bootstrap compiler, which was used to build clang, to build the C/C++ parts of Rustc as well. Ensure the linker has the -isysroot argument as well as the compiler. Bug: 1386212 Change-Id: Id91ce75e302055259eeb78f0b9abb974e0b12cce Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4163447 Reviewed-by: Hans Wennborg <[email protected]> Commit-Queue: danakj <[email protected]> Cr-Commit-Position: refs/heads/main@{#1096319} NOKEYCHECK=True GitOrigin-RevId: 50585d202871e5ce170010dd82e3acb64aa2ae4e
1 parent ee5eed4 commit 49f7ca8

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

build_rust.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def VerifyStage0JsonHash():
123123
sys.exit(1)
124124

125125

126-
def Configure(llvm_libs_root):
126+
def Configure(llvm_bins_path, llvm_libs_root):
127127
# Read the config.toml template file...
128128
with open(RUST_CONFIG_TEMPLATE_PATH, 'r') as input:
129129
template = string.Template(input.read())
@@ -134,15 +134,15 @@ def quote_string(s: str):
134134
subs = {}
135135
subs['INSTALL_DIR'] = quote_string(str(RUST_TOOLCHAIN_OUT_DIR))
136136
subs['LLVM_LIB_ROOT'] = quote_string(str(llvm_libs_root))
137-
subs['LLVM_BIN'] = quote_string(str(os.path.join(LLVM_BUILD_DIR, 'bin')))
137+
subs['LLVM_BIN'] = quote_string(str(llvm_bins_path))
138138
subs['PACKAGE_VERSION'] = GetPackageVersionForBuild()
139139

140140
# ...and apply substitutions, writing to config.toml in Rust tree.
141141
with open(os.path.join(RUST_SRC_DIR, 'config.toml'), 'w') as output:
142142
output.write(template.substitute(subs))
143143

144144

145-
def RunXPy(sub, args, zlib_path, libxml2_dirs, build_mac_arm,
145+
def RunXPy(sub, args, llvm_bins_path, zlib_path, libxml2_dirs, build_mac_arm,
146146
gcc_toolchain_path, verbose):
147147
''' Run x.py, Rust's build script'''
148148
# We append to these flags, make sure they exist.
@@ -158,45 +158,32 @@ def RunXPy(sub, args, zlib_path, libxml2_dirs, build_mac_arm,
158158
for f in ENV_FLAGS:
159159
RUSTENV.setdefault(f, '')
160160

161-
# The TARGET is consumed by the cc crate for building C/C++ sources, the
162-
# CARGO_BUILD_TARGET is used for building Rust sources.
163-
if sys.platform == 'win32':
164-
RUSTENV['TARGET'] = 'x86_64-pc-windows-msvc'
165-
RUSTENV['CARGO_BUILD_TARGET'] = 'x86_64-pc-windows-msvc'
166-
elif sys.platform == 'darwin':
167-
if build_mac_arm or platform.machine() == 'arm64':
168-
RUSTENV['TARGET'] = 'arm64-apple-darwin'
169-
RUSTENV['CARGO_BUILD_TARGET'] = 'arm64-apple-darwin'
170-
else:
171-
RUSTENV['TARGET'] = 'amd64-apple-darwin'
172-
RUSTENV['CARGO_BUILD_TARGET'] = 'amd64-apple-darwin'
173-
else:
174-
RUSTENV['TARGET'] = 'x86_64-unknown-linux-gnu'
175-
RUSTENV['CARGO_BUILD_TARGET'] = 'x86_64-unknown-linux-gnu'
176-
177161
# The AR, CC, CXX flags control the C/C++ compiler used through the `cc`
178162
# crate. There are also C/C++ targets that are part of the Rust toolchain
179163
# build for which the tool is controlled from `config.toml`, so these must
180164
# be duplicated there.
181165

182-
clang_bin = os.path.join(LLVM_BUILD_DIR, 'bin')
183166
if sys.platform == 'win32':
184-
RUSTENV['AR'] = os.path.join(clang_bin, 'llvm-lib.exe')
185-
RUSTENV['CC'] = os.path.join(clang_bin, 'clang-cl.exe')
186-
RUSTENV['CXX'] = os.path.join(clang_bin, 'clang-cl.exe')
167+
RUSTENV['AR'] = os.path.join(llvm_bins_path, 'llvm-lib.exe')
168+
RUSTENV['CC'] = os.path.join(llvm_bins_path, 'clang-cl.exe')
169+
RUSTENV['CXX'] = os.path.join(llvm_bins_path, 'clang-cl.exe')
187170
else:
188-
RUSTENV['AR'] = os.path.join(clang_bin, 'llvm-ar')
189-
RUSTENV['CC'] = os.path.join(clang_bin, 'clang')
190-
RUSTENV['CXX'] = os.path.join(clang_bin, 'clang++')
171+
RUSTENV['AR'] = os.path.join(llvm_bins_path, 'llvm-ar')
172+
RUSTENV['CC'] = os.path.join(llvm_bins_path, 'clang')
173+
RUSTENV['CXX'] = os.path.join(llvm_bins_path, 'clang++')
191174

192-
if RUSTENV['CARGO_BUILD_TARGET'].endswith('apple-darwin'):
193-
# The system/xcode compiler would find system headers correctly, but
175+
if sys.platform == 'darwin':
176+
# The system/xcode compiler would find system SDK correctly, but
194177
# the Clang we've built does not. See
195178
# https://github.com/llvm/llvm-project/issues/45225
196179
sdk_path = subprocess.check_output(['xcrun', '--show-sdk-path'],
197180
text=True).rstrip()
198181
RUSTENV['CFLAGS'] += f' -isysroot {sdk_path}'
199182
RUSTENV['CXXFLAGS'] += f' -isysroot {sdk_path}'
183+
RUSTENV['LDFLAGS'] += f' -isysroot {sdk_path}'
184+
RUSTENV['RUSTFLAGS_BOOTSTRAP'] += (f' -Clink-arg=-isysroot {sdk_path}')
185+
RUSTENV['RUSTFLAGS_NOT_BOOTSTRAP'] += (
186+
f' -Clink-arg=-isysroot {sdk_path}')
200187

201188
if zlib_path:
202189
RUSTENV['CFLAGS'] += f' -I{zlib_path}'
@@ -236,6 +223,10 @@ def RunXPy(sub, args, zlib_path, libxml2_dirs, build_mac_arm,
236223
# Cargo normally stores files in $HOME. Override this.
237224
RUSTENV['CARGO_HOME'] = CARGO_HOME_DIR
238225

226+
# This enables the compiler to produce Mac ARM binaries.
227+
if build_mac_arm:
228+
args.extend(['--target', 'aarch64-apple-darwin'])
229+
239230
os.chdir(RUST_SRC_DIR)
240231
cmd = [sys.executable, 'x.py', sub]
241232
if verbose and verbose > 0:
@@ -324,6 +315,15 @@ def main():
324315
else:
325316
llvm_libs_root = build.LLVM_BOOTSTRAP_DIR
326317

318+
# If we're building for Mac ARM on an x86_64 Mac, we can't use the final
319+
# clang binaries as they don't have x86_64 support. Building them with that
320+
# support would blow up their size a lot. So, we use the bootstrap binaries
321+
# until such time as the Mac ARM builder becomes an actual Mac ARM machine.
322+
if not args.build_mac_arm:
323+
llvm_bins_path = os.path.join(LLVM_BUILD_DIR, 'bin')
324+
else:
325+
llvm_bins_path = os.path.join(build.LLVM_BOOTSTRAP_DIR, 'bin')
326+
327327
VerifyStage0JsonHash()
328328
if args.verify_stage0_hash:
329329
# The above function exits and prints the actual hash if verification
@@ -342,7 +342,7 @@ def main():
342342
build.MaybeDownloadHostGcc(args)
343343

344344
# Set up config.toml in Rust source tree to configure build.
345-
Configure(llvm_libs_root)
345+
Configure(llvm_bins_path, llvm_libs_root)
346346

347347
AddCMakeToPath()
348348

@@ -366,21 +366,21 @@ def main():
366366
if args.run_xpy:
367367
if rest[0] == '--':
368368
rest = rest[1:]
369-
RunXPy(rest[0], rest[1:], zlib_path, libxml2_dirs, args.build_mac_arm,
370-
args.gcc_toolchain, args.verbose)
369+
RunXPy(rest[0], rest[1:], llvm_bins_path, zlib_path, libxml2_dirs,
370+
args.build_mac_arm, args.gcc_toolchain, args.verbose)
371371
return 0
372372
else:
373373
assert not rest
374374

375375
if not args.skip_clean:
376376
print('Cleaning build artifacts...')
377-
RunXPy('clean', [], zlib_path, libxml2_dirs, args.build_mac_arm,
378-
args.gcc_toolchain, args.verbose)
377+
RunXPy('clean', [], llvm_bins_path, zlib_path, libxml2_dirs,
378+
args.build_mac_arm, args.gcc_toolchain, args.verbose)
379379

380380
if not args.skip_test:
381381
print('Running stage 2 tests...')
382382
# Run a subset of tests. Tell x.py to keep the rustc we already built.
383-
RunXPy('test', GetTestArgs(), zlib_path, libxml2_dirs,
383+
RunXPy('test', GetTestArgs(), llvm_bins_path, zlib_path, libxml2_dirs,
384384
args.build_mac_arm, args.gcc_toolchain, args.verbose)
385385

386386
targets = [
@@ -391,8 +391,8 @@ def main():
391391
# Build stage 2 compiler, tools, and libraries. This should reuse earlier
392392
# stages from the test command (if run).
393393
print('Building stage 2 artifacts...')
394-
RunXPy('build', ['--stage', '2'] + targets, zlib_path, libxml2_dirs,
395-
args.build_mac_arm, args.gcc_toolchain, args.verbose)
394+
RunXPy('build', ['--stage', '2'] + targets, llvm_bins_path, zlib_path,
395+
libxml2_dirs, args.build_mac_arm, args.gcc_toolchain, args.verbose)
396396

397397
if args.skip_install:
398398
# Rust is fully built. We can quit.
@@ -403,8 +403,8 @@ def main():
403403
if os.path.exists(RUST_TOOLCHAIN_OUT_DIR):
404404
shutil.rmtree(RUST_TOOLCHAIN_OUT_DIR)
405405

406-
RunXPy('install', DISTRIBUTION_ARTIFACTS, zlib_path, libxml2_dirs,
407-
args.gcc_toolchain, args.verbose)
406+
RunXPy('install', DISTRIBUTION_ARTIFACTS, llvm_bins_path, zlib_path,
407+
libxml2_dirs, args.gcc_toolchain, args.verbose)
408408

409409
with open(VERSION_STAMP_PATH, 'w') as stamp:
410410
stamp.write(GetVersionStamp())

package_rust.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
PACKAGE_VERSION = GetPackageVersionForBuild()
2424
BUILDLOG_NAME = f'rust-buildlog-{PACKAGE_VERSION}.txt'
2525
RUST_TOOLCHAIN_PACKAGE_NAME = f'rust-toolchain-{PACKAGE_VERSION}.tgz'
26-
BUILD_MAC_ARM = False
2726

2827

2928
def BuildCrubit(build_mac_arm):

0 commit comments

Comments
 (0)