@@ -24,6 +24,12 @@ should be loaded here. Do not load anything else, even common libraries like
24
24
Skylib.
25
25
"""
26
26
27
+ load ("@bazel_tools//tools/cpp:unix_cc_configure.bzl" , "configure_unix_toolchain" )
28
+ load ("@bazel_tools//tools/cpp:windows_cc_configure.bzl" , "configure_windows_toolchain" )
29
+ load (
30
+ "@bazel_tools//tools/cpp:lib_cc_configure.bzl" ,
31
+ "get_cpu_value" ,
32
+ )
27
33
load (
28
34
"@build_bazel_rules_swift//swift/internal:feature_names.bzl" ,
29
35
"SWIFT_FEATURE_CODEVIEW_DEBUG_INFO" ,
@@ -256,6 +262,63 @@ def _normalized_linux_cpu(cpu):
256
262
return "x86_64"
257
263
return cpu
258
264
265
+ def _toolchain_root (repository_ctx ):
266
+ path_to_swiftc = repository_ctx .which ("swiftc" )
267
+ if not path_to_swiftc :
268
+ fail ("No 'swiftc' executable found in $PATH" )
269
+ return path_to_swiftc .dirname
270
+
271
+ def _create_xcode_cc_toolchain (repository_ctx ):
272
+ """Creates BUILD alias for the C++ toolchain provided by apple_support
273
+
274
+ Args:
275
+ repository_ctx: The repository rule context.
276
+ """
277
+
278
+ repository_ctx .file ("BUILD" , """
279
+ alias(
280
+ name = "toolchain",
281
+ actual = "@local_config_apple_cc//:toolchain",
282
+ visibility = ["//visibility:public"]
283
+ )
284
+ """ )
285
+
286
+ def _toolchain_overriden_tools (toolchain_root , extension = "" ):
287
+ tools = {
288
+ "ld" : toolchain_root .get_child ("lld" + extension ),
289
+ "llvm-cov" : toolchain_root .get_child ("llvm-cov" + extension ),
290
+ "llvm-profdata" : toolchain_root .get_child ("llvm-profdata" + extension ),
291
+ "cpp" : toolchain_root .get_child ("clang-cpp" + extension ),
292
+ "gcc" : toolchain_root .get_child ("clang" + extension ),
293
+ }
294
+
295
+ # llvm-ar is not shipped before Swift 5.8
296
+ ar = toolchain_root .get_child ("llvm-ar" + extension )
297
+ if ar .exists :
298
+ tools ["ar" ] = ar
299
+ return tools
300
+
301
+ def _create_linux_cc_toolchain (repository_ctx ):
302
+ """Creates BUILD targets for the Swift-provided C++ toolchain on Linux.
303
+
304
+ Args:
305
+ repository_ctx: The repository rule context.
306
+ """
307
+
308
+ toolchain_root = _toolchain_root (repository_ctx )
309
+ cpu = get_cpu_value (repository_ctx )
310
+ configure_unix_toolchain (repository_ctx , cpu , overriden_tools = _toolchain_overriden_tools (toolchain_root ))
311
+
312
+ def _create_windows_cc_toolchain (repository_ctx ):
313
+ """Creates BUILD targets for the Swift-provided C++ toolchain on Windows.
314
+
315
+ Args:
316
+ repository_ctx: The repository rule context.
317
+ """
318
+
319
+ toolchain_root = _toolchain_root (repository_ctx )
320
+ configure_windows_toolchain (repository_ctx , overriden_tools = _toolchain_overriden_tools (toolchain_root , ".exe" ))
321
+
259
322
def _create_linux_toolchain (repository_ctx ):
260
323
"""Creates BUILD targets for the Swift toolchain on Linux.
261
324
@@ -266,6 +329,7 @@ def _create_linux_toolchain(repository_ctx):
266
329
if not path_to_swiftc :
267
330
fail ("No 'swiftc' executable found in $PATH" )
268
331
332
+ toolchain_root = _toolchain_root (repository_ctx )
269
333
root = path_to_swiftc .dirname .dirname
270
334
feature_values = _compute_feature_values (repository_ctx , path_to_swiftc )
271
335
version_file = _write_swift_version (repository_ctx , path_to_swiftc )
@@ -306,6 +370,7 @@ swift_toolchain(
306
370
for feature in feature_values
307
371
]),
308
372
root = root ,
373
+ toolchain_root = toolchain_root ,
309
374
version_file = version_file ,
310
375
),
311
376
)
@@ -421,10 +486,16 @@ swift_toolchain(
421
486
),
422
487
)
423
488
489
+ def _swift_cc_autoconfiguration_impl (repository_ctx ):
490
+ os_name = repository_ctx .os .name .lower ()
491
+ if os_name .startswith ("mac os" ):
492
+ _create_xcode_cc_toolchain (repository_ctx )
493
+ elif os_name .startswith ("windows" ):
494
+ _create_windows_cc_toolchain (repository_ctx )
495
+ else :
496
+ _create_linux_cc_toolchain (repository_ctx )
497
+
424
498
def _swift_autoconfiguration_impl (repository_ctx ):
425
- # TODO(allevato): This is expedient and fragile. Use the
426
- # platforms/toolchains APIs instead to define proper toolchains, and make it
427
- # possible to support non-Xcode toolchains on macOS as well.
428
499
os_name = repository_ctx .os .name .lower ()
429
500
if os_name .startswith ("mac os" ):
430
501
_create_xcode_toolchain (repository_ctx )
@@ -433,6 +504,12 @@ def _swift_autoconfiguration_impl(repository_ctx):
433
504
else :
434
505
_create_linux_toolchain (repository_ctx )
435
506
507
+ swift_cc_autoconfiguration = repository_rule (
508
+ environ = ["PATH" ],
509
+ implementation = _swift_cc_autoconfiguration_impl ,
510
+ local = True ,
511
+ )
512
+
436
513
swift_autoconfiguration = repository_rule (
437
514
environ = ["CC" , "PATH" , "ProgramData" , "Path" ],
438
515
implementation = _swift_autoconfiguration_impl ,
0 commit comments