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