@@ -193,8 +193,13 @@ def cc_toolchain_config(
193
193
link_flags = [
194
194
"--target=" + target_system_name ,
195
195
"-no-canonical-prefixes" ,
196
+ "-fuse-ld=lld" ,
196
197
]
197
198
199
+ if exec_os == "darwin" :
200
+ # These will get expanded by osx_cc_wrapper's `sanitize_option`
201
+ link_flags .append ("--ld-path=ld.lld" if is_xcompile else "--ld-path=ld64.lld" )
202
+
198
203
stdlib = compiler_configuration ["stdlib" ]
199
204
if stdlib != "none" :
200
205
link_flags .extend ([
@@ -207,45 +212,33 @@ def cc_toolchain_config(
207
212
libunwind_link_flags = []
208
213
compiler_rt_link_flags = []
209
214
210
- # Flags for ar.
211
- archive_flags = []
215
+ is_darwin_exec_and_target = exec_os == "darwin" and not is_xcompile
212
216
213
217
# Linker flags:
214
- if exec_os == "darwin" and not is_xcompile :
215
- # lld is experimental for Mach-O, so we use the native ld64 linker.
216
- # TODO: How do we cross-compile from Linux to Darwin?
217
- use_lld = False
218
+ if is_darwin_exec_and_target :
218
219
link_flags .extend ([
219
220
"-headerpad_max_install_names" ,
220
221
"-fobjc-link-runtime" ,
221
222
])
222
223
223
224
# Use the bundled libtool (llvm-libtool-darwin).
224
225
use_libtool = True
225
-
226
- # Pre-installed libtool on macOS has -static as default, but llvm-libtool-darwin needs it
227
- # explicitly. cc_common.create_link_variables does not automatically add this either if
228
- # output_file arg to it is None.
229
- archive_flags .extend ([
230
- "-static" ,
231
- ])
232
226
elif target_arch in ["wasm32" , "wasm64" ]:
233
227
# lld is invoked as wasm-ld for WebAssembly targets.
234
- use_lld = True
235
228
use_libtool = False
236
229
else :
237
- # Note that for xcompiling from darwin to linux, the native ld64 is
238
- # not an option because it is not a cross-linker, so lld is the
239
- # only option.
240
- use_lld = True
241
230
link_flags .extend ([
242
- "-fuse-ld=lld" ,
243
231
"-Wl,--build-id=md5" ,
244
232
"-Wl,--hash-style=gnu" ,
245
233
"-Wl,-z,relro,-z,now" ,
246
234
])
247
235
use_libtool = False
248
236
237
+ # Pre-installed libtool on macOS has -static as default, but llvm-libtool-darwin needs it
238
+ # explicitly. cc_common.create_link_variables does not automatically add this either if
239
+ # output_file arg to it is None.
240
+ archive_flags = ["-static" ] if is_darwin_exec_and_target else []
241
+
249
242
# Flags related to C++ standard.
250
243
# The linker has no way of knowing if there are C++ objects; so we
251
244
# always link C++ libraries.
@@ -259,21 +252,7 @@ def cc_toolchain_config(
259
252
"-std=" + cxx_standard ,
260
253
"-stdlib=libc++" ,
261
254
]
262
- if use_lld :
263
- # For single-platform builds, we can statically link the bundled
264
- # libraries.
265
- link_flags .extend ([
266
- "-l:libc++.a" ,
267
- "-l:libc++abi.a" ,
268
- ])
269
- compiler_rt_link_flags = ["-rtlib=compiler-rt" ]
270
- libunwind_link_flags = [
271
- "-l:libunwind.a" ,
272
- # To support libunwind.
273
- "-lpthread" ,
274
- "-ldl" ,
275
- ]
276
- else :
255
+ if is_darwin_exec_and_target :
277
256
# Several system libraries on macOS dynamically link libc++ and
278
257
# libc++abi, so static linking them becomes a problem. We need to
279
258
# ensure that they are dynamic linked from the system sysroot and
@@ -291,6 +270,20 @@ def cc_toolchain_config(
291
270
"-Bstatic" ,
292
271
"-lunwind" ,
293
272
]
273
+ else :
274
+ # For single-platform builds, we can statically link the bundled
275
+ # libraries.
276
+ link_flags .extend ([
277
+ "-l:libc++.a" ,
278
+ "-l:libc++abi.a" ,
279
+ ])
280
+ compiler_rt_link_flags = ["-rtlib=compiler-rt" ]
281
+ libunwind_link_flags = [
282
+ "-l:libunwind.a" ,
283
+ # To support libunwind.
284
+ "-lpthread" ,
285
+ "-ldl" ,
286
+ ]
294
287
295
288
elif stdlib == "libc++" :
296
289
cxx_flags = [
@@ -368,7 +361,7 @@ def cc_toolchain_config(
368
361
"dwp" : tools_path_prefix + "llvm-dwp" ,
369
362
"gcc" : wrapper_bin_prefix + "cc_wrapper.sh" ,
370
363
"gcov" : tools_path_prefix + "llvm-profdata" ,
371
- "ld" : tools_path_prefix + "ld.lld" if use_lld else "/usr/bin/ld" ,
364
+ "ld" : tools_path_prefix + "ld.lld" ,
372
365
"llvm-cov" : tools_path_prefix + "llvm-cov" ,
373
366
"llvm-profdata" : tools_path_prefix + "llvm-profdata" ,
374
367
"nm" : tools_path_prefix + "llvm-nm" ,
@@ -382,9 +375,8 @@ def cc_toolchain_config(
382
375
# This was added to `lld` in this patch: http://reviews.llvm.org/D18814
383
376
#
384
377
# The oldest version of LLVM that we support is 6.0.0 which was released
385
- # after the above patch was merged, so we just set this to `True` when
386
- # `lld` is being used as the linker.
387
- supports_start_end_lib = use_lld
378
+ # after the above patch was merged, so we just set this to `True`.
379
+ supports_start_end_lib = True
388
380
389
381
# Replace flags with any user-provided overrides.
390
382
if compiler_configuration ["compile_flags" ] != None :
0 commit comments