Skip to content

Commit 011b550

Browse files
googlewaltcopybara-github
authored andcommitted
Handle --objccopt in C++ Build API
This requires moving the required information from ObjcConfiguration to CppConfiguration. CppConfiuration.objccopts now has the same information as ObjcConfiguration.copts, and we can eventually delete the latter. There are a couple slight changes in behavior: - C/C++ sources in objc_library will no longer get the options from --objccopt. This matches the behavior of other copts variants which are all done based on file extensions. - Objective-C parse header actions will no longer get the options from --objccopt. This is not likely to matter, as they are just validation actions. PiperOrigin-RevId: 455112668 Change-Id: I9dc8857fb6f2ae58dd7e33d450c1101e9e938c52
1 parent 3916df6 commit 011b550

9 files changed

Lines changed: 82 additions & 11 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,11 @@ public static ImmutableList<String> getCoptsFromOptions(
16171617
flagsBuilder.addAll(config.getCxxopts());
16181618
}
16191619

1620+
if (CppFileTypes.OBJC_SOURCE.matches(sourceFilename)
1621+
|| CppFileTypes.OBJCPP_SOURCE.matches(sourceFilename)) {
1622+
flagsBuilder.addAll(config.getObjcopts());
1623+
}
1624+
16201625
return flagsBuilder.build();
16211626
}
16221627

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public String toString() {
172172

173173
private final ImmutableList<String> copts;
174174
private final ImmutableList<String> cxxopts;
175+
private final ImmutableList<String> objcopts;
175176

176177
private final ImmutableList<String> linkopts;
177178
private final ImmutableList<String> ltoindexOptions;
@@ -278,6 +279,7 @@ public CppConfiguration(BuildOptions options) throws InvalidConfigurationExcepti
278279
this.conlyopts = ImmutableList.copyOf(cppOptions.conlyoptList);
279280
this.copts = ImmutableList.copyOf(cppOptions.coptList);
280281
this.cxxopts = ImmutableList.copyOf(cppOptions.cxxoptList);
282+
this.objcopts = ImmutableList.copyOf(cppOptions.objcoptList);
281283
this.linkopts = linkoptsBuilder.build();
282284
this.ltoindexOptions = ImmutableList.copyOf(cppOptions.ltoindexoptList);
283285
this.ltobackendOptions = ImmutableList.copyOf(cppOptions.ltobackendoptList);
@@ -540,6 +542,12 @@ public ImmutableList<String> getConlyopts() {
540542
return conlyopts;
541543
}
542544

545+
/** Returns flags passed to Bazel by --objccopt option. */
546+
@Override
547+
public ImmutableList<String> getObjcopts() {
548+
return objcopts;
549+
}
550+
543551
/** Returns flags passed to Bazel by --linkopt option. */
544552
@Override
545553
public ImmutableList<String> getLinkopts() {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,15 @@ public String getTypeDescription() {
293293
help = "Additional option to pass to gcc when compiling C source files.")
294294
public List<String> conlyoptList;
295295

296+
@Option(
297+
name = "objccopt",
298+
allowMultiple = true,
299+
defaultValue = "null",
300+
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
301+
effectTags = {OptionEffectTag.ACTION_COMMAND_LINES},
302+
help = "Additional options to pass to gcc when compiling Objective C/C++ source files.")
303+
public List<String> objcoptList;
304+
296305
@Option(
297306
name = "linkopt",
298307
defaultValue = "null",

src/main/java/com/google/devtools/build/lib/rules/objc/ObjcCommandLineOptions.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,6 @@ public class ObjcCommandLineOptions extends FragmentOptions {
9797
)
9898
public boolean generateLinkmap;
9999

100-
@Option(
101-
name = "objccopt",
102-
allowMultiple = true,
103-
defaultValue = "null",
104-
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
105-
effectTags = {OptionEffectTag.ACTION_COMMAND_LINES},
106-
help = "Additional options to pass to Objective C compilation.")
107-
public List<String> copts;
108-
109100
@Option(
110101
name = "ios_memleaks",
111102
defaultValue = "false",

src/main/java/com/google/devtools/build/lib/rules/objc/ObjcConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class ObjcConfiguration extends Fragment implements ObjcConfigurationApi<
6767
public ObjcConfiguration(BuildOptions buildOptions) {
6868
CoreOptions options = buildOptions.get(CoreOptions.class);
6969
ObjcCommandLineOptions objcOptions = buildOptions.get(ObjcCommandLineOptions.class);
70+
CppOptions cppOptions = buildOptions.get(CppOptions.class);
7071

7172
this.iosSimulatorDevice = objcOptions.iosSimulatorDevice;
7273
this.iosSimulatorVersion = DottedVersion.maybeUnwrap(objcOptions.iosSimulatorVersion);
@@ -76,7 +77,7 @@ public ObjcConfiguration(BuildOptions buildOptions) {
7677
this.tvosSimulatorVersion = DottedVersion.maybeUnwrap(objcOptions.tvosSimulatorVersion);
7778
this.generateLinkmap = objcOptions.generateLinkmap;
7879
this.runMemleaks = objcOptions.runMemleaks;
79-
this.copts = ImmutableList.copyOf(objcOptions.copts);
80+
this.copts = ImmutableList.copyOf(cppOptions.objcoptList);
8081
this.compilationMode = Preconditions.checkNotNull(options.compilationMode, "compilationMode");
8182
this.fastbuildOptions = ImmutableList.copyOf(objcOptions.fastbuildOptions);
8283
this.enableBinaryStripping = objcOptions.enableBinaryStripping;

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CppConfigurationApi.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ public interface CppConfigurationApi<InvalidConfigurationExceptionT extends Exce
8282
+ "<code>--conlyopt</code></a> option.")
8383
ImmutableList<String> getConlyopts() throws EvalException;
8484

85+
@StarlarkMethod(
86+
name = "objccopts",
87+
structField = true,
88+
doc =
89+
"The flags passed to Bazel by <a href=\"${link user-manual#flag--objccopt}\">"
90+
+ "<code>--objccopt</code></a> option.")
91+
ImmutableList<String> getObjcopts() throws EvalException;
92+
8593
@StarlarkMethod(
8694
name = "linkopts",
8795
structField = true,

src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ def _validate_attributes(common_variables):
242242
def _get_compile_rule_copts(common_variables):
243243
attributes = common_variables.compilation_attributes
244244
copts = []
245-
copts.extend(common_variables.objc_config.copts)
246245
copts.extend(attributes.copts)
247246

248247
if attributes.enable_modules and common_variables.ctx.attr.module_map == None:

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public void testConlyopts() throws Exception {
7575
assertThat(result).containsExactly("-wololoo");
7676
}
7777

78+
@Test
79+
public void testObjcopts() throws Exception {
80+
writeRuleReturning("ctx.fragments.cpp.objccopts");
81+
useConfiguration("--objccopt=-wololoo");
82+
83+
@SuppressWarnings("unchecked")
84+
Sequence<String> result = (Sequence<String>) getConfiguredTarget("//foo:bar").get("result");
85+
assertThat(result).containsExactly("-wololoo");
86+
}
87+
7888
@Test
7989
public void testLinkopts() throws Exception {
8090
writeRuleReturning("ctx.fragments.cpp.linkopts");

src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,46 @@ public void testObjcPlusPlusCompileDarwin() throws Exception {
232232
.containsAtLeast("-stdlib=libc++", "-std=gnu++11", "-mmacosx-version-min=9.10.11");
233233
}
234234

235+
@Test
236+
public void testObjcSourceContainsObjccopt() throws Exception {
237+
useConfiguration("--objccopt=--xyzzy");
238+
scratch.file("objc/a.m");
239+
scratch.file("objc/BUILD", RULE_TYPE.target(scratch, "objc", "lib", "srcs", "['a.m']"));
240+
241+
CommandAction compileActionA = compileAction("//objc:lib", "a.o");
242+
assertThat(compileActionA.getArguments()).contains("--xyzzy");
243+
}
244+
245+
@Test
246+
public void testObjcppSourceContainsObjccopt() throws Exception {
247+
useConfiguration("--objccopt=--xyzzy");
248+
scratch.file("objc/a.mm");
249+
scratch.file("objc/BUILD", RULE_TYPE.target(scratch, "objc", "lib", "srcs", "['a.mm']"));
250+
251+
CommandAction compileActionA = compileAction("//objc:lib", "a.o");
252+
assertThat(compileActionA.getArguments()).contains("--xyzzy");
253+
}
254+
255+
@Test
256+
public void testCSourceDoesNotContainObjccopt() throws Exception {
257+
useConfiguration("--objccopt=--xyzzy");
258+
scratch.file("objc/a.c");
259+
scratch.file("objc/BUILD", RULE_TYPE.target(scratch, "objc", "lib", "srcs", "['a.c']"));
260+
261+
CommandAction compileActionA = compileAction("//objc:lib", "a.o");
262+
assertThat(compileActionA.getArguments()).doesNotContain("--xyzzy");
263+
}
264+
265+
@Test
266+
public void testCppSourceDoesNotContainObjccopt() throws Exception {
267+
useConfiguration("--objccopt=--xyzzy");
268+
scratch.file("objc/a.cc");
269+
scratch.file("objc/BUILD", RULE_TYPE.target(scratch, "objc", "lib", "srcs", "['a.cc']"));
270+
271+
CommandAction compileActionA = compileAction("//objc:lib", "a.o");
272+
assertThat(compileActionA.getArguments()).doesNotContain("--xyzzy");
273+
}
274+
235275
@Test
236276
public void testCompilationModeDbg() throws Exception {
237277
useConfiguration(

0 commit comments

Comments
 (0)