diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/swift/SwiftCommandLineOptions.java b/src/main/java/com/google/devtools/build/lib/rules/apple/swift/SwiftCommandLineOptions.java index 8c4e28db494ccc..bf81f91bddf0a9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/apple/swift/SwiftCommandLineOptions.java +++ b/src/main/java/com/google/devtools/build/lib/rules/apple/swift/SwiftCommandLineOptions.java @@ -31,4 +31,20 @@ public class SwiftCommandLineOptions extends FragmentOptions { help = "Additional options to pass to Swift compilation." ) public List copts; + + @Option( + name = "host_swiftcopt", + allowMultiple = true, + defaultValue = "", + documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS, + effectTags = {OptionEffectTag.ACTION_COMMAND_LINES, OptionEffectTag.AFFECTS_OUTPUTS}, + help = "Additional options to pass to swiftc for host tools.") + public List hostSwiftcoptList; + + @Override + public FragmentOptions getHost() { + SwiftCommandLineOptions host = (SwiftCommandLineOptions) super.getHost(); + host.copts = this.hostSwiftcoptList; + return host; + } } diff --git a/src/test/java/com/google/devtools/build/lib/rules/apple/SwiftConfigurationTest.java b/src/test/java/com/google/devtools/build/lib/rules/apple/SwiftConfigurationTest.java index 0a5f80768743b2..c0d31e03ca8316 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/apple/SwiftConfigurationTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/apple/SwiftConfigurationTest.java @@ -58,4 +58,37 @@ public void testSkylarkApi() throws Exception { assertThat(copts).containsAllOf("foo", "bar"); } + + @Test + public void testHostSwiftcopt() throws Exception { + scratch.file("examples/rule/BUILD"); + scratch.file( + "examples/rule/apple_rules.bzl", + "def swift_binary_impl(ctx):", + " copts = ctx.fragments.swift.copts()", + " return struct(", + " copts=copts,", + " )", + "swift_binary = rule(", + " implementation = swift_binary_impl,", + " fragments = ['swift']", + ")"); + + scratch.file( + "examples/swift_skylark/BUILD", + "load('//examples/rule:apple_rules.bzl', 'swift_binary')", + "swift_binary(", + " name='my_target',", + ")"); + + useConfiguration("--swiftcopt=foo", "--host_swiftcopt=bar", "--host_swiftcopt=baz"); + ConfiguredTarget target = + getConfiguredTarget("//examples/swift_skylark:my_target", getHostConfiguration()); + + @SuppressWarnings("unchecked") + List copts = (List) target.get("copts"); + + assertThat(copts).doesNotContain("foo"); + assertThat(copts).containsAllOf("bar", "baz"); + } }