Skip to content

Commit

Permalink
Add --host_per_file_copt
Browse files Browse the repository at this point in the history
Fixes #12406

Closes #16618.

PiperOrigin-RevId: 486616830
Change-Id: Icd9774b2d05d09288a34375e229b13e2a74cf3d7
  • Loading branch information
keith authored and copybara-github committed Nov 7, 2022
1 parent 8f28513 commit 88e5c91
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,28 @@ public Label getPropellerOptimizeLabel() {
+ " in the host or exec configurations.")
public List<String> 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<PerLabelOptions> hostPerFileCoptsList;

@Option(
name = "host_linkopt",
defaultValue = "null",
Expand Down Expand Up @@ -1226,6 +1248,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;
Expand Down Expand Up @@ -1260,6 +1283,7 @@ public FragmentOptions getHost() {
host.hostCppCompiler = hostCppCompiler;
host.hostCrosstoolTop = hostCrosstoolTop;
host.hostCxxoptList = hostCxxoptList;
host.hostPerFileCoptsList = hostPerFileCoptsList;
host.hostLibcTopLabel = hostLibcTopLabel;
host.hostLinkoptList = hostLinkoptList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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");

Expand All @@ -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<String> 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()
Expand Down

0 comments on commit 88e5c91

Please sign in to comment.