Skip to content

Commit 05d1dfd

Browse files
committed
Add a separate linker wrapper
This CL adds a new device and host linker wrapper. This allows us to separate out arguments that are specific to different stages of the build process and be more precise in our configuration of the rustc build process. Test: ./toolchain/android_rust/build.py Change-Id: Iea0aff461f667226deaca1e19fec35d8576feabe
1 parent cc0ef3b commit 05d1dfd

12 files changed

+119
-51
lines changed

build_platform.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ def system() -> str:
1414
return 'darwin'
1515
raise RuntimeError("Unknown System: " + sys)
1616

17+
def is_linux() -> bool:
18+
return platform.system() == 'Linux'
19+
20+
def is_darwin() -> bool:
21+
return platform.system() == 'Darwin'
22+
1723
def prebuilt() -> str:
1824
"""Returns the prebuilt subdirectory for prebuilts which do not use
1925
subarch specialization."""

config.py

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,16 @@
2929

3030
ANDROID_TARGET_VERSION: str = '31'
3131

32-
CONFIG_TOML_TEMPLATE: Path = TEMPLATES_PATH / 'config.toml.template'
33-
DEVICE_CC_WRAPPER_TEMPLATE: Path = TEMPLATES_PATH / 'device_cc_wrapper.template'
34-
DEVICE_TARGET_TEMPLATE: Path = TEMPLATES_PATH / 'device_target.template'
35-
HOST_CC_WRAPPER_TEMPLATE: Path = TEMPLATES_PATH / 'host_cc_wrapper.template'
36-
HOST_CXX_WRAPPER_TEMPLATE: Path = TEMPLATES_PATH / 'host_cxx_wrapper.template'
37-
HOST_TARGET_TEMPLATE: Path = TEMPLATES_PATH / 'host_target.template'
38-
39-
# Add the path at which libc++ can be found in Android checkouts
40-
CXX_LINKER_FLAGS: str = ' -Wl,-rpath,'
41-
if build_platform.system() == 'darwin':
42-
CXX_LINKER_FLAGS += '@loader_path/../lib64'
43-
CXX_LINKER_FLAGS += ' -mmacosx-version-min=10.14'
44-
else:
45-
CXX_LINKER_FLAGS += '\\$ORIGIN/../lib64'
46-
# Add the path at which libc++ can be found during the build
47-
CXX_LINKER_FLAGS += (' -L' + LLVM_CXX_RUNTIME_PATH.as_posix() +
48-
' -Wl,-rpath,' + LLVM_CXX_RUNTIME_PATH.as_posix())
49-
50-
LD_OPTIONS: str = None
51-
if build_platform.system() == 'linux':
52-
LD_OPTIONS = '-fuse-ld=lld -Wno-unused-command-line-argument'
53-
else:
54-
LD_OPTIONS = ''
32+
CONFIG_TOML_TEMPLATE: Path = TEMPLATES_PATH / 'config.toml.template'
33+
DEVICE_CC_WRAPPER_TEMPLATE: Path = TEMPLATES_PATH / 'device_cc_wrapper.template'
34+
DEVICE_LINKER_WRAPPER_TEMPLATE: Path = TEMPLATES_PATH / 'device_linker_wrapper.template'
35+
DEVICE_TARGET_TEMPLATE: Path = TEMPLATES_PATH / 'device_target.template'
36+
HOST_CC_WRAPPER_TEMPLATE: Path = TEMPLATES_PATH / 'host_cc_wrapper.template'
37+
HOST_CXX_WRAPPER_TEMPLATE: Path = TEMPLATES_PATH / 'host_cxx_wrapper.template'
38+
HOST_LINKER_WRAPPER_TEMPLATE: Path = TEMPLATES_PATH / 'host_linker_wrapper.template'
39+
HOST_TARGET_TEMPLATE: Path = TEMPLATES_PATH / 'host_target.template'
40+
41+
MACOSX_VERSION_FLAG: str = '-mmacosx-version-min=10.14'
5542

5643

5744
def instantiate_template_exec(template_path: Path, output_path: Path, **kwargs):
@@ -66,39 +53,47 @@ def instantiate_template_file(template_path: Path, output_path: Path, make_exec:
6653
output_path.chmod(output_path.stat().st_mode | stat.S_IEXEC)
6754

6855

69-
def host_config(target: str, toolchain_flags: str) -> str:
70-
cc_wrapper_name = OUT_PATH_WRAPPERS / ('clang-%s' % target)
71-
cxx_wrapper_name = OUT_PATH_WRAPPERS / ('clang++-%s' % target)
56+
def host_config(target: str, macosx_flags: str, linker_flags: str) -> str:
57+
cc_wrapper_name = OUT_PATH_WRAPPERS / ('clang-%s' % target)
58+
cxx_wrapper_name = OUT_PATH_WRAPPERS / ('clang++-%s' % target)
59+
linker_wrapper_name = OUT_PATH_WRAPPERS / ('linker-%s' % target)
7260

7361
instantiate_template_exec(
7462
HOST_CC_WRAPPER_TEMPLATE,
7563
cc_wrapper_name,
7664
real_cc=CC_PATH,
77-
ld_option=LD_OPTIONS,
7865
target=target,
79-
toolchain_flags=toolchain_flags)
66+
macosx_flags=macosx_flags)
8067

8168
instantiate_template_exec(
8269
HOST_CXX_WRAPPER_TEMPLATE,
8370
cxx_wrapper_name,
8471
real_cxx=CXX_PATH,
85-
ld_option=LD_OPTIONS,
8672
target=target,
87-
toolchain_flags=toolchain_flags,
88-
cxxstd=CXXSTD_PATH,
89-
cxx_linker_flags=CXX_LINKER_FLAGS)
73+
macosx_flags=macosx_flags,
74+
cxxstd=CXXSTD_PATH)
75+
76+
instantiate_template_exec(
77+
HOST_LINKER_WRAPPER_TEMPLATE,
78+
linker_wrapper_name,
79+
real_cxx=CXX_PATH,
80+
target=target,
81+
macosx_flags=macosx_flags,
82+
linker_flags=linker_flags)
9083

9184
with open(HOST_TARGET_TEMPLATE, 'r') as template_file:
9285
return Template(template_file.read()).substitute(
9386
target=target,
9487
cc=cc_wrapper_name,
9588
cxx=cxx_wrapper_name,
89+
linker=linker_wrapper_name,
9690
ar=AR_PATH,
9791
ranlib=RANLIB_PATH)
9892

9993

10094
def device_config(target: str) -> str:
101-
cc_wrapper_name = OUT_PATH_WRAPPERS / ('clang-%s' % target)
95+
cc_wrapper_name = OUT_PATH_WRAPPERS / ('clang-%s' % target)
96+
linker_wrapper_name = OUT_PATH_WRAPPERS / ('linker-%s' % target)
10297

10398
clang_target = target + ANDROID_TARGET_VERSION
10499

@@ -109,28 +104,51 @@ def device_config(target: str) -> str:
109104
target=clang_target,
110105
sysroot=NDK_SYSROOT_PATH)
111106

107+
instantiate_template_exec(
108+
DEVICE_LINKER_WRAPPER_TEMPLATE,
109+
linker_wrapper_name,
110+
real_cc=CC_PATH,
111+
target=clang_target,
112+
sysroot=NDK_SYSROOT_PATH)
113+
112114
with open(DEVICE_TARGET_TEMPLATE, 'r') as template_file:
113115
return Template(template_file.read()).substitute(
114-
target=target, cc=cc_wrapper_name, ar=AR_PATH)
116+
target=target,
117+
cc=cc_wrapper_name,
118+
linker=linker_wrapper_name,
119+
ar=AR_PATH)
115120

116121

117122
def configure():
118-
"""Generates config.toml for the rustc build."""
119-
host_toolchain_flags = None
123+
"""Generates config.toml and compiler wrapers for the rustc build."""
124+
125+
macosx_flags: str = ''
126+
host_ld_selector: str = '-fuse-ld=lld' if build_platform.is_linux() else ''
127+
host_bin_search: str = ('-B' + GCC_TOOLCHAIN_PATH.as_posix()) if build_platform.is_linux() else ''
128+
host_llvm_libpath: str = '-L' + LLVM_CXX_RUNTIME_PATH.as_posix()
129+
host_rpath_buildtime: str = '-Wl,-rpath,' + LLVM_CXX_RUNTIME_PATH.as_posix()
130+
host_rpath_runtime: str = '-Wl,-rpath,' + (
131+
'$ORIGIN/../lib64' if build_platform.is_linux() else '@loader_path/../lib64')
120132

121-
if build_platform.system() == 'darwin':
133+
if build_platform.is_darwin():
122134
# Apple removed the normal sysroot at / on Mojave+, so we need
123135
# to go hunt for it on OSX
124136
# On pre-Mojave, this command will output the empty string.
125137
output = subprocess.check_output(
126138
['xcrun', '--sdk', 'macosx', '--show-sdk-path'])
127-
host_toolchain_flags = "--sysroot " + output.rstrip().decode('utf-8')
128-
else:
129-
# On Linux build hosts we need to set the path to the gcc toolchain.
130-
host_toolchain_flags = "-B " + GCC_TOOLCHAIN_PATH.as_posix()
139+
macosx_flags = (
140+
MACOSX_VERSION_FLAG +
141+
" --sysroot " + output.rstrip().decode('utf-8'))
142+
143+
host_linker_flags = ' '.join([
144+
host_ld_selector,
145+
host_bin_search,
146+
host_llvm_libpath,
147+
host_rpath_buildtime,
148+
host_rpath_runtime])
131149

132150
host_configs = '\n'.join(
133-
[host_config(target, host_toolchain_flags) for target in HOST_TARGETS])
151+
[host_config(target, macosx_flags, host_linker_flags) for target in HOST_TARGETS])
134152
device_configs = '\n'.join(
135153
[device_config(target) for target in DEVICE_TARGETS])
136154

@@ -140,6 +158,7 @@ def configure():
140158
instantiate_template_file(
141159
CONFIG_TOML_TEMPLATE,
142160
OUT_PATH_RUST_SOURCE / 'config.toml',
161+
llvm_ldflags=host_linker_flags,
143162
all_targets=all_targets,
144163
cargo=CARGO_PATH,
145164
rustc=RUSTC_PATH,

do_build.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def main():
9898
]] + [env['PATH']])
9999

100100
# Only adjust the library path on Linux - on OSX, use the devtools curl
101-
if build_platform.system() == 'linux':
101+
if build_platform.is_linux():
102102
if 'LIBRARY_PATH' in env:
103103
old_library_path = ':{0}'.format(env['LIBRARY_PATH'])
104104
else:
@@ -168,7 +168,7 @@ def main():
168168
sys.exit(ec)
169169

170170
# Install sources
171-
if build_platform.system() == 'linux':
171+
if build_platform.is_linux():
172172
shutil.rmtree(OUT_PATH_STDLIB_SRCS, ignore_errors=True)
173173
for stdlib in STDLIB_SOURCES:
174174
shutil.copytree(OUT_PATH_RUST_SOURCE / stdlib, OUT_PATH_STDLIB_SRCS / stdlib)
@@ -187,7 +187,7 @@ def main():
187187
OUT_PATH_PACKAGE / 'bin' / 'rustdoc'])
188188

189189
# Install the libc++ library to out/package/lib64/
190-
if build_platform.system() == 'darwin':
190+
if build_platform.is_darwin():
191191
libcxx_name = 'libc++.dylib'
192192
else:
193193
libcxx_name = 'libc++.so.1'
@@ -199,7 +199,7 @@ def main():
199199

200200
# Some stdlib crates might include Android.mk or Android.bp files.
201201
# If they do, filter them out.
202-
if build_platform.system() == 'linux':
202+
if build_platform.is_linux():
203203
for f in OUT_PATH_STDLIB_SRCS.glob('**/Android.{mk,bp}'):
204204
f.unlink()
205205

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
From 7c73299dbc68fb7a59637d98fc56fdbcaf7ab67d Mon Sep 17 00:00:00 2001
2+
From: Chris Wailes <[email protected]>
3+
Date: Mon, 13 Sep 2021 23:58:25 -0700
4+
Subject: [PATCH] Modify Rust build system to correctly handle use-libcxx
5+
6+
This CL modifies the Rust build system so that it will pass
7+
LLVM_ENABLE_LIBCXX when the use-libcxx option is enabled in config.toml.
8+
9+
Change-Id: Ibc1285f49843238aa912606d87295dd19f24ae6e
10+
---
11+
src/bootstrap/native.rs | 4 ++++
12+
1 file changed, 4 insertions(+)
13+
14+
diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs
15+
index 1be414b29a..849c0f0cc1 100644
16+
--- a/src/bootstrap/native.rs
17+
+++ b/src/bootstrap/native.rs
18+
@@ -182,6 +182,10 @@ impl Step for Llvm {
19+
.define("LLVM_TARGET_ARCH", target_native.split('-').next().unwrap())
20+
.define("LLVM_DEFAULT_TARGET_TRIPLE", target_native);
21+
22+
+ if builder.config.llvm_use_libcxx {
23+
+ cfg.define("LLVM_ENABLE_LIBCXX", "ON");
24+
+ }
25+
+
26+
if target != "aarch64-apple-darwin" && !target.contains("windows") {
27+
cfg.define("LLVM_ENABLE_ZLIB", "ON");
28+
} else {
29+
--
30+
2.33.0.309.g3052b89438-goog
31+

templates/config.toml.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
changelog-seen = 2
2+
23
[llvm]
34
ninja = true
45
targets = "AArch64;ARM;X86"
56
experimental-targets = ""
7+
ldflags = "$llvm_ldflags"
68
use-libcxx = true
9+
710
[build]
811
target = $all_targets
912
cargo = "$cargo"
@@ -19,12 +22,16 @@ full-bootstrap = true
1922
extended = true
2023
tools = ["cargo", "clippy", "rustfmt", "rust-analyzer"]
2124
cargo-native-static = true
25+
2226
[install]
2327
prefix = "/"
2428
sysconfdir = "etc"
29+
2530
[rust]
2631
channel = "dev"
2732
remap-debuginfo = true
2833
deny-warnings = false
34+
2935
$host_configs
36+
3037
$device_configs

templates/device_cc_wrapper.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
$real_cc $${*/"-lgcc"} -fuse-ld=lld -Wno-unused-command-line-argument --target=$target --sysroot=$sysroot
2+
$real_cc $$* --target=$target --sysroot=$sysroot
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
$real_cc $${*/"-lgcc"} -fuse-ld=lld --target=$target --sysroot=$sysroot

templates/device_target.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[target.$target]
22
cc = "$cc"
3+
linker = "$linker"
34
ar = "$ar"

templates/host_cc_wrapper.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
$real_cc $$* $ld_option --target=$target $toolchain_flags
2+
$real_cc $$* --target=$target $macosx_flags

templates/host_cxx_wrapper.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
$real_cxx -I$cxxstd $$* $ld_option --target=$target $toolchain_flags $cxx_linker_flags -stdlib=libc++
2+
$real_cxx $$* --target=$target -stdlib=libc++ $macosx_flags -I$cxxstd
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
$real_cxx $$* --target=$target -stdlib=libc++ $macosx_flags $linker_flags

templates/host_target.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[target.$target]
22
cc = "$cc"
33
cxx = "$cxx"
4+
linker = "$linker"
45
ar = "$ar"
5-
ranlib = "$ranlib"
6-
linker = "$cxx"
6+
ranlib = "$ranlib"

0 commit comments

Comments
 (0)