Skip to content

Commit 4919d4a

Browse files
UebelAndrekeithShreeM01
authored
Add --host_per_file_copt (bazelbuild#16695)
Fixes bazelbuild#12406 Closes bazelbuild#16618. PiperOrigin-RevId: 486616830 Change-Id: Icd9774b2d05d09288a34375e229b13e2a74cf3d7 Co-authored-by: Keith Smiley <[email protected]> Co-authored-by: kshyanashree <[email protected]>
1 parent e9a931f commit 4919d4a

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java

+24
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,28 @@ public Label getPropellerOptimizeLabel() {
636636
help = "Additional option to pass to gcc when compiling C source files for host tools.")
637637
public List<String> hostConlyoptList;
638638

639+
@Option(
640+
name = "host_per_file_copt",
641+
allowMultiple = true,
642+
converter = PerLabelOptions.PerLabelOptionsConverter.class,
643+
defaultValue = "null",
644+
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
645+
effectTags = {OptionEffectTag.ACTION_COMMAND_LINES, OptionEffectTag.AFFECTS_OUTPUTS},
646+
help =
647+
"Additional options to selectively pass to the C/C++ compiler when "
648+
+ "compiling certain files in the host or exec configurations. "
649+
+ "This option can be passed multiple times. "
650+
+ "Syntax: regex_filter@option_1,option_2,...,option_n. Where regex_filter stands "
651+
+ "for a list of include and exclude regular expression patterns (Also see "
652+
+ "--instrumentation_filter). option_1 to option_n stand for "
653+
+ "arbitrary command line options. If an option contains a comma it has to be "
654+
+ "quoted with a backslash. Options can contain @. Only the first @ is used to "
655+
+ "split the string. Example: "
656+
+ "--host_per_file_copt=//foo/.*\\.cc,-//foo/bar\\.cc@-O0 adds the -O0 "
657+
+ "command line option to the gcc command line of all cc files in //foo/ "
658+
+ "except bar.cc.")
659+
public List<PerLabelOptions> hostPerFileCoptsList;
660+
639661
@Option(
640662
name = "host_linkopt",
641663
defaultValue = "null",
@@ -1218,6 +1240,7 @@ public FragmentOptions getHost() {
12181240
host.coptList = coptListBuilder.addAll(hostCoptList).build();
12191241
host.cxxoptList = cxxoptListBuilder.addAll(hostCxxoptList).build();
12201242
host.conlyoptList = ImmutableList.copyOf(hostConlyoptList);
1243+
host.perFileCopts = ImmutableList.copyOf(hostPerFileCoptsList);
12211244
host.linkoptList = ImmutableList.copyOf(hostLinkoptList);
12221245

12231246
host.useStartEndLib = useStartEndLib;
@@ -1252,6 +1275,7 @@ public FragmentOptions getHost() {
12521275
host.hostCppCompiler = hostCppCompiler;
12531276
host.hostCrosstoolTop = hostCrosstoolTop;
12541277
host.hostCxxoptList = hostCxxoptList;
1278+
host.hostPerFileCoptsList = hostPerFileCoptsList;
12551279
host.hostLibcTopLabel = hostLibcTopLabel;
12561280
host.hostLinkoptList = hostLinkoptList;
12571281

src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ java_test(
377377
srcs = ["CompileBuildVariablesTest.java"],
378378
deps = [
379379
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
380+
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
380381
"//src/main/java/com/google/devtools/build/lib/rules/cpp",
381382
"//src/main/java/com/google/devtools/build/lib/vfs",
382383
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",

src/test/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariablesTest.java

+32-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.Iterables;
21+
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
2122
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
2223
import com.google.devtools.build.lib.analysis.util.AnalysisMock;
2324
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
@@ -34,21 +35,21 @@
3435
@RunWith(JUnit4.class)
3536
public class CompileBuildVariablesTest extends BuildViewTestCase {
3637

37-
private CppCompileAction getCppCompileAction(final String label, final String name) throws
38-
Exception {
38+
private CppCompileAction getCppCompileAction(ConfiguredTarget target, final String name)
39+
throws Exception {
3940
return (CppCompileAction)
4041
getGeneratingAction(
4142
Iterables.find(
42-
getGeneratingAction(getFilesToBuild(getConfiguredTarget(label)).getSingleton())
43-
.getInputs()
44-
.toList(),
43+
getGeneratingAction(getFilesToBuild(target).getSingleton()).getInputs().toList(),
4544
(artifact) -> artifact.getExecPath().getBaseName().startsWith(name)));
4645
}
4746

4847
/** Returns active build variables for a compile action of given type for given target. */
4948
protected CcToolchainVariables getCompileBuildVariables(String label, String name)
5049
throws Exception {
51-
return getCppCompileAction(label, name).getCompileCommandLine().getVariables();
50+
return getCppCompileAction(getConfiguredTarget(label), name)
51+
.getCompileCommandLine()
52+
.getVariables();
5253
}
5354

5455
@Test
@@ -100,7 +101,10 @@ public void testPresenceOfUserCompileFlags() throws Exception {
100101
public void testPerFileCoptsAreInUserCompileFlags() throws Exception {
101102
scratch.file("x/BUILD", "cc_binary(name = 'bin', srcs = ['bin.cc'])");
102103
scratch.file("x/bin.cc");
103-
useConfiguration("--per_file_copt=//x:bin@-foo", "--per_file_copt=//x:bar\\.cc@-bar");
104+
useConfiguration(
105+
"--per_file_copt=//x:bin@-foo",
106+
"--per_file_copt=//x:bar\\.cc@-bar",
107+
"--host_per_file_copt=//x:bin@-baz");
104108

105109
CcToolchainVariables variables = getCompileBuildVariables("//x:bin", "bin");
106110

@@ -110,6 +114,27 @@ public void testPerFileCoptsAreInUserCompileFlags() throws Exception {
110114
assertThat(copts).containsExactly("-foo").inOrder();
111115
}
112116

117+
@Test
118+
public void testHostPerFileCoptsAreInUserCompileFlags() throws Exception {
119+
scratch.file("x/BUILD", "cc_binary(name = 'bin', srcs = ['bin.cc'])");
120+
scratch.file("x/bin.cc");
121+
useConfiguration(
122+
"--host_per_file_copt=//x:bin@-foo",
123+
"--host_per_file_copt=//x:bar\\.cc@-bar",
124+
"--per_file_copt=//x:bin@-baz");
125+
126+
ConfiguredTarget target = getConfiguredTarget("//x:bin", getHostConfiguration());
127+
CcToolchainVariables variables =
128+
getCppCompileAction(target, "bin").getCompileCommandLine().getVariables();
129+
130+
ImmutableList<String> copts =
131+
CcToolchainVariables.toStringList(
132+
variables, CompileBuildVariables.USER_COMPILE_FLAGS.getVariableName());
133+
assertThat(copts).contains("-foo");
134+
assertThat(copts).doesNotContain("-bar");
135+
assertThat(copts).doesNotContain("-baz");
136+
}
137+
113138
@Test
114139
public void testPresenceOfSysrootBuildVariable() throws Exception {
115140
AnalysisMock.get()

0 commit comments

Comments
 (0)