Skip to content

Commit c5e906d

Browse files
authored
feat(toolchain): add copts and objc_copts attributes to toolchains (#1564)
When using rules_swift with a custom (rules based) cc_toolchain, it is sometimes useful to propagate certain flags to every swift invocation. And while a cc_library() could potentially be used in implicit_deps to pass the usual options (-isystem, -iquote, -D, etc...), the CcInfo provider does not allow propagating every option. Some options in particular (such as -Xclang -internal-externc-system) cannot currently be propagated that way, and they can nonetheless be necessary in certain environment. So we're making the current toolchain rules a tad more flexible in this commit, by allowing the user to specify "objc_copts" (for xcode only) and "copts" at toolchain parameters. The content of these attributes is then added to the corresponding compilation actions.
1 parent 2af3bad commit c5e906d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

swift/toolchains/swift_toolchain.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def _swift_toolchain_impl(ctx):
491491
target_triple = target_triple,
492492
sdkroot = ctx.attr.sdkroot,
493493
xctest_version = ctx.attr.xctest_version,
494-
additional_swiftc_copts = swiftcopts,
494+
additional_swiftc_copts = ctx.attr.copts + swiftcopts,
495495
)
496496

497497
if ctx.attr.os == "windows":
@@ -627,6 +627,11 @@ configuration options that are applied to targets on a per-package basis.
627627
mandatory = True,
628628
allow_single_file = True,
629629
),
630+
"copts": attr.string_list(
631+
doc = """\
632+
A list of additional Swift compiler flags that should be passed to Swift compile actions.
633+
""",
634+
),
630635
"_cc_toolchain": attr.label(
631636
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
632637
doc = """\

swift/toolchains/xcode_swift_toolchain.bzl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,12 +736,12 @@ def _xcode_swift_toolchain_impl(ctx):
736736
xcode_config = xcode_config,
737737
)
738738
all_action_configs = _all_action_configs(
739-
additional_objc_copts = command_line_objc_copts(
739+
additional_objc_copts = ctx.attr.objc_copts + command_line_objc_copts(
740740
ctx.var["COMPILATION_MODE"],
741741
ctx.fragments.cpp,
742742
ctx.fragments.objc,
743743
),
744-
additional_swiftc_copts = swiftcopts,
744+
additional_swiftc_copts = ctx.attr.copts + swiftcopts,
745745
apple_toolchain = apple_toolchain,
746746
generated_header_rewriter = generated_header_rewriter,
747747
needs_resource_directory = swift_executable or toolchain_root,
@@ -929,6 +929,17 @@ configuration options that are applied to targets on a per-package basis.
929929
doc = """\
930930
The label of the file specifying a list of protocols for extraction of conformances'
931931
const values.
932+
""",
933+
),
934+
"copts": attr.string_list(
935+
doc = """\
936+
A list of additional Swift compiler flags that should be passed to Swift compile actions.
937+
""",
938+
),
939+
"objc_copts": attr.string_list(
940+
doc = """\
941+
A list of additional Objective-C compiler flags that should be passed (preceded by `-Xcc`)
942+
to Swift compile actions *and* Swift explicit module precompile actions.
932943
""",
933944
),
934945
"_cc_toolchain": attr.label(

0 commit comments

Comments
 (0)