@@ -136,20 +136,21 @@ def cc_toolchain_config(
136
136
"--target=" + target_system_name ,
137
137
"-lm" ,
138
138
"-no-canonical-prefixes" ,
139
+ "-fuse-ld=lld" ,
139
140
]
140
141
142
+ if exec_os == "darwin" :
143
+ # These will get expanded by osx_cc_wrapper's `sanitize_option`
144
+ link_flags .append ("--ld-path=ld.lld" if is_xcompile else "--ld-path=ld64.lld" )
145
+
141
146
# Similar to link_flags, but placed later in the command line such that
142
147
# unused symbols are not stripped.
143
148
link_libs = []
144
149
145
- # Flags for ar.
146
- archive_flags = []
150
+ is_darwin_exec_and_target = exec_os == "darwin" and not is_xcompile
147
151
148
- # Linker flags:
149
- if exec_os == "darwin" and not is_xcompile :
150
- # lld is experimental for Mach-O, so we use the native ld64 linker.
151
- # TODO: How do we cross-compile from Linux to Darwin?
152
- use_lld = False
152
+ # Linker and archive flags
153
+ if is_darwin_exec_and_target :
153
154
link_flags .extend ([
154
155
"-headerpad_max_install_names" ,
155
156
"-fobjc-link-runtime" ,
@@ -161,21 +162,15 @@ def cc_toolchain_config(
161
162
# Pre-installed libtool on macOS has -static as default, but llvm-libtool-darwin needs it
162
163
# explicitly. cc_common.create_link_variables does not automatically add this either if
163
164
# output_file arg to it is None.
164
- archive_flags .extend ([
165
- "-static" ,
166
- ])
165
+ archive_flags = ["-static" ]
167
166
else :
168
- # Note that for xcompiling from darwin to linux, the native ld64 is
169
- # not an option because it is not a cross-linker, so lld is the
170
- # only option.
171
- use_lld = True
172
167
link_flags .extend ([
173
- "-fuse-ld=lld" ,
174
168
"-Wl,--build-id=md5" ,
175
169
"-Wl,--hash-style=gnu" ,
176
170
"-Wl,-z,relro,-z,now" ,
177
171
])
178
172
use_libtool = False
173
+ archive_flags = []
179
174
180
175
# Flags related to C++ standard.
181
176
# The linker has no way of knowing if there are C++ objects; so we
@@ -199,20 +194,7 @@ def cc_toolchain_config(
199
194
# https://github.com/llvm/llvm-project/commit/0556138624edf48621dd49a463dbe12e7101f17d
200
195
cxx_flags .append ("-Xclang" )
201
196
cxx_flags .append ("-fno-cxx-modules" )
202
- if use_lld :
203
- # For single-platform builds, we can statically link the bundled
204
- # libraries.
205
- link_flags .extend ([
206
- "-l:libc++.a" ,
207
- "-l:libc++abi.a" ,
208
- "-l:libunwind.a" ,
209
- # Compiler runtime features.
210
- "-rtlib=compiler-rt" ,
211
- # To support libunwind.
212
- "-lpthread" ,
213
- "-ldl" ,
214
- ])
215
- else :
197
+ if is_darwin_exec_and_target :
216
198
# Several system libraries on macOS dynamically link libc++ and
217
199
# libc++abi, so static linking them becomes a problem. We need to
218
200
# ensure that they are dynamic linked from the system sysroot and
@@ -228,7 +210,19 @@ def cc_toolchain_config(
228
210
"-Bdynamic" ,
229
211
"-L{}lib" .format (toolchain_path_prefix ),
230
212
])
231
-
213
+ else :
214
+ # For single-platform builds, we can statically link the bundled
215
+ # libraries.
216
+ link_flags .extend ([
217
+ "-l:libc++.a" ,
218
+ "-l:libc++abi.a" ,
219
+ "-l:libunwind.a" ,
220
+ # Compiler runtime features.
221
+ "-rtlib=compiler-rt" ,
222
+ # To support libunwind.
223
+ "-lpthread" ,
224
+ "-ldl" ,
225
+ ])
232
226
elif stdlib == "libc++" :
233
227
cxx_flags = [
234
228
"-std=" + cxx_standard ,
@@ -282,7 +276,7 @@ def cc_toolchain_config(
282
276
"dwp" : tools_path_prefix + "llvm-dwp" ,
283
277
"gcc" : wrapper_bin_prefix + "cc_wrapper.sh" ,
284
278
"gcov" : tools_path_prefix + "llvm-profdata" ,
285
- "ld" : tools_path_prefix + "ld.lld" if use_lld else "/usr/bin/ld" ,
279
+ "ld" : tools_path_prefix + "ld.lld" ,
286
280
"llvm-cov" : tools_path_prefix + "llvm-cov" ,
287
281
"llvm-profdata" : tools_path_prefix + "llvm-profdata" ,
288
282
"nm" : tools_path_prefix + "llvm-nm" ,
@@ -295,9 +289,8 @@ def cc_toolchain_config(
295
289
# This was added to `lld` in this patch: http://reviews.llvm.org/D18814
296
290
#
297
291
# The oldest version of LLVM that we support is 6.0.0 which was released
298
- # after the above patch was merged, so we just set this to `True` when
299
- # `lld` is being used as the linker.
300
- supports_start_end_lib = use_lld
292
+ # after the above patch was merged, so we just set this to `True`.
293
+ supports_start_end_lib = True
301
294
302
295
# Replace flags with any user-provided overrides.
303
296
if compiler_configuration ["compile_flags" ] != None :
0 commit comments