diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java index c2633de89cd16b..dc34959384ff64 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java @@ -636,6 +636,28 @@ public Label getPropellerOptimizeLabel() { help = "Additional option to pass to gcc when compiling C source files for host tools.") public List hostConlyoptList; + @Option( + name = "host_per_file_copt", + allowMultiple = true, + converter = PerLabelOptions.PerLabelOptionsConverter.class, + defaultValue = "null", + documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS, + effectTags = {OptionEffectTag.ACTION_COMMAND_LINES, OptionEffectTag.AFFECTS_OUTPUTS}, + help = + "Additional options to selectively pass to the C/C++ compiler when " + + "compiling certain files in the host or exec configurations. " + + "This option can be passed multiple times. " + + "Syntax: regex_filter@option_1,option_2,...,option_n. Where regex_filter stands " + + "for a list of include and exclude regular expression patterns (Also see " + + "--instrumentation_filter). option_1 to option_n stand for " + + "arbitrary command line options. If an option contains a comma it has to be " + + "quoted with a backslash. Options can contain @. Only the first @ is used to " + + "split the string. Example: " + + "--host_per_file_copt=//foo/.*\\.cc,-//foo/bar\\.cc@-O0 adds the -O0 " + + "command line option to the gcc command line of all cc files in //foo/ " + + "except bar.cc.") + public List hostPerFileCoptsList; + @Option( name = "host_linkopt", defaultValue = "null", @@ -1218,6 +1240,7 @@ public FragmentOptions getHost() { host.coptList = coptListBuilder.addAll(hostCoptList).build(); host.cxxoptList = cxxoptListBuilder.addAll(hostCxxoptList).build(); host.conlyoptList = ImmutableList.copyOf(hostConlyoptList); + host.perFileCopts = ImmutableList.copyOf(hostPerFileCoptsList); host.linkoptList = ImmutableList.copyOf(hostLinkoptList); host.useStartEndLib = useStartEndLib; @@ -1252,6 +1275,7 @@ public FragmentOptions getHost() { host.hostCppCompiler = hostCppCompiler; host.hostCrosstoolTop = hostCrosstoolTop; host.hostCxxoptList = hostCxxoptList; + host.hostPerFileCoptsList = hostPerFileCoptsList; host.hostLibcTopLabel = hostLibcTopLabel; host.hostLinkoptList = hostLinkoptList; diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD b/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD index cee71cc8df3771..c5cfa476849af1 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD @@ -377,6 +377,7 @@ java_test( srcs = ["CompileBuildVariablesTest.java"], deps = [ "//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster", + "//src/main/java/com/google/devtools/build/lib/analysis:configured_target", "//src/main/java/com/google/devtools/build/lib/rules/cpp", "//src/main/java/com/google/devtools/build/lib/vfs", "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment", diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariablesTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariablesTest.java index 2a1b24607ee4cf..5ccafcbb0b3631 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariablesTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariablesTest.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; +import com.google.devtools.build.lib.analysis.ConfiguredTarget; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget; import com.google.devtools.build.lib.analysis.util.AnalysisMock; import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; @@ -34,21 +35,21 @@ @RunWith(JUnit4.class) public class CompileBuildVariablesTest extends BuildViewTestCase { - private CppCompileAction getCppCompileAction(final String label, final String name) throws - Exception { + private CppCompileAction getCppCompileAction(ConfiguredTarget target, final String name) + throws Exception { return (CppCompileAction) getGeneratingAction( Iterables.find( - getGeneratingAction(getFilesToBuild(getConfiguredTarget(label)).getSingleton()) - .getInputs() - .toList(), + getGeneratingAction(getFilesToBuild(target).getSingleton()).getInputs().toList(), (artifact) -> artifact.getExecPath().getBaseName().startsWith(name))); } /** Returns active build variables for a compile action of given type for given target. */ protected CcToolchainVariables getCompileBuildVariables(String label, String name) throws Exception { - return getCppCompileAction(label, name).getCompileCommandLine().getVariables(); + return getCppCompileAction(getConfiguredTarget(label), name) + .getCompileCommandLine() + .getVariables(); } @Test @@ -100,7 +101,10 @@ public void testPresenceOfUserCompileFlags() throws Exception { public void testPerFileCoptsAreInUserCompileFlags() throws Exception { scratch.file("x/BUILD", "cc_binary(name = 'bin', srcs = ['bin.cc'])"); scratch.file("x/bin.cc"); - useConfiguration("--per_file_copt=//x:bin@-foo", "--per_file_copt=//x:bar\\.cc@-bar"); + useConfiguration( + "--per_file_copt=//x:bin@-foo", + "--per_file_copt=//x:bar\\.cc@-bar", + "--host_per_file_copt=//x:bin@-baz"); CcToolchainVariables variables = getCompileBuildVariables("//x:bin", "bin"); @@ -110,6 +114,27 @@ public void testPerFileCoptsAreInUserCompileFlags() throws Exception { assertThat(copts).containsExactly("-foo").inOrder(); } + @Test + public void testHostPerFileCoptsAreInUserCompileFlags() throws Exception { + scratch.file("x/BUILD", "cc_binary(name = 'bin', srcs = ['bin.cc'])"); + scratch.file("x/bin.cc"); + useConfiguration( + "--host_per_file_copt=//x:bin@-foo", + "--host_per_file_copt=//x:bar\\.cc@-bar", + "--per_file_copt=//x:bin@-baz"); + + ConfiguredTarget target = getConfiguredTarget("//x:bin", getHostConfiguration()); + CcToolchainVariables variables = + getCppCompileAction(target, "bin").getCompileCommandLine().getVariables(); + + ImmutableList copts = + CcToolchainVariables.toStringList( + variables, CompileBuildVariables.USER_COMPILE_FLAGS.getVariableName()); + assertThat(copts).contains("-foo"); + assertThat(copts).doesNotContain("-bar"); + assertThat(copts).doesNotContain("-baz"); + } + @Test public void testPresenceOfSysrootBuildVariable() throws Exception { AnalysisMock.get()