18
18
19
19
import com .google .common .collect .ImmutableList ;
20
20
import com .google .common .collect .Iterables ;
21
+ import com .google .devtools .build .lib .analysis .ConfiguredTarget ;
21
22
import com .google .devtools .build .lib .analysis .configuredtargets .RuleConfiguredTarget ;
22
23
import com .google .devtools .build .lib .analysis .util .AnalysisMock ;
23
24
import com .google .devtools .build .lib .analysis .util .BuildViewTestCase ;
34
35
@ RunWith (JUnit4 .class )
35
36
public class CompileBuildVariablesTest extends BuildViewTestCase {
36
37
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 {
39
40
return (CppCompileAction )
40
41
getGeneratingAction (
41
42
Iterables .find (
42
- getGeneratingAction (getFilesToBuild (getConfiguredTarget (label )).getSingleton ())
43
- .getInputs ()
44
- .toList (),
43
+ getGeneratingAction (getFilesToBuild (target ).getSingleton ()).getInputs ().toList (),
45
44
(artifact ) -> artifact .getExecPath ().getBaseName ().startsWith (name )));
46
45
}
47
46
48
47
/** Returns active build variables for a compile action of given type for given target. */
49
48
protected CcToolchainVariables getCompileBuildVariables (String label , String name )
50
49
throws Exception {
51
- return getCppCompileAction (label , name ).getCompileCommandLine ().getVariables ();
50
+ return getCppCompileAction (getConfiguredTarget (label ), name )
51
+ .getCompileCommandLine ()
52
+ .getVariables ();
52
53
}
53
54
54
55
@ Test
@@ -100,7 +101,10 @@ public void testPresenceOfUserCompileFlags() throws Exception {
100
101
public void testPerFileCoptsAreInUserCompileFlags () throws Exception {
101
102
scratch .file ("x/BUILD" , "cc_binary(name = 'bin', srcs = ['bin.cc'])" );
102
103
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" );
104
108
105
109
CcToolchainVariables variables = getCompileBuildVariables ("//x:bin" , "bin" );
106
110
@@ -110,6 +114,27 @@ public void testPerFileCoptsAreInUserCompileFlags() throws Exception {
110
114
assertThat (copts ).containsExactly ("-foo" ).inOrder ();
111
115
}
112
116
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
+
113
138
@ Test
114
139
public void testPresenceOfSysrootBuildVariable () throws Exception {
115
140
AnalysisMock .get ()
0 commit comments