Skip to content

Commit

Permalink
Add conlyopts / cxxopts to objc_library
Browse files Browse the repository at this point in the history
This mirrors the behavior from cc_library

Closes bazelbuild#24497.

PiperOrigin-RevId: 711864090
Change-Id: I284c466c67d476836f2c0b561d06e919bd7860e3
  • Loading branch information
keith authored and copybara-github committed Jan 3, 2025
1 parent e405851 commit d38b16e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,8 @@ private ImmutableList<String> getCopts(Artifact sourceFile, Label sourceLabel) {
if (CppFileTypes.CPP_SOURCE.matches(sourceFilename)
|| CppFileTypes.CPP_HEADER.matches(sourceFilename)
|| CppFileTypes.CPP_MODULE_MAP.matches(sourceFilename)
|| CppFileTypes.CLIF_INPUT_PROTO.matches(sourceFilename)) {
|| CppFileTypes.CLIF_INPUT_PROTO.matches(sourceFilename)
|| CppFileTypes.OBJCPP_SOURCE.matches(sourceFilename)) {
coptsList.addAll(cxxopts);
}

Expand Down
20 changes: 20 additions & 0 deletions src/main/starlark/builtins_bzl/common/objc/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ it depends, or those which depend on it.
<p>
Note that for the generated Xcode project, directory paths specified using "-I" flags in
copts are parsed out, prepended with "$(WORKSPACE_ROOT)/" if they are relative paths, and
added to the header search paths for the associated Xcode target."""),
"conlyopts": attr.string_list(doc = """
Extra flags to pass to the compiler for C files.
Subject to <a href="${link make-variables}">"Make variable"</a> substitution and
<a href="${link common-definitions#sh-tokenization}">Bourne shell tokenization</a>.
These flags will only apply to this target, and not those upon which
it depends, or those which depend on it.
<p>
Note that for the generated Xcode project, directory paths specified using "-I" flags in
copts are parsed out, prepended with "$(WORKSPACE_ROOT)/" if they are relative paths, and
added to the header search paths for the associated Xcode target."""),
"cxxopts": attr.string_list(doc = """
Extra flags to pass to the compiler for Objective-C++ and C++ files.
Subject to <a href="${link make-variables}">"Make variable"</a> substitution and
<a href="${link common-definitions#sh-tokenization}">Bourne shell tokenization</a>.
These flags will only apply to this target, and not those upon which
it depends, or those which depend on it.
<p>
Note that for the generated Xcode project, directory paths specified using "-I" flags in
copts are parsed out, prepended with "$(WORKSPACE_ROOT)/" if they are relative paths, and
added to the header search paths for the associated Xcode target."""),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def _create_compilation_attributes(ctx):
sdk_dylibs = depset(getattr(ctx.attr, "sdk_dylibs", [])),
linkopts = objc_internal.expand_and_tokenize(ctx = ctx, attr = "linkopts", flags = getattr(ctx.attr, "linkopts", [])),
copts = objc_internal.expand_and_tokenize(ctx = ctx, attr = "copts", flags = getattr(ctx.attr, "copts", [])),
conlyopts = objc_internal.expand_and_tokenize(ctx = ctx, attr = "conlyopts", flags = getattr(ctx.attr, "conlyopts", [])),
cxxopts = objc_internal.expand_and_tokenize(ctx = ctx, attr = "cxxopts", flags = getattr(ctx.attr, "cxxopts", [])),
additional_linker_inputs = getattr(ctx.files, "additional_linker_inputs", []),
defines = objc_internal.expand_and_tokenize(ctx = ctx, attr = "defines", flags = getattr(ctx.attr, "defines", [])),
enable_modules = getattr(ctx.attr, "enable_modules", False),
Expand Down Expand Up @@ -206,6 +208,8 @@ def _compile(
compilation_contexts = compilation_contexts,
implementation_compilation_contexts = objc_compilation_context.implementation_cc_compilation_contexts,
user_compile_flags = runtimes_copts + user_compile_flags,
cxx_flags = common_variables.compilation_attributes.cxxopts,
conly_flags = common_variables.compilation_attributes.conlyopts,
module_map = module_map,
propagate_module_map_to_compile_action = True,
variables_extension = extension,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,77 @@ public void testObjcCxxopts_argumentOrdering() throws Exception {
assertThat(bArgs).doesNotContain("-cxxfoo");
}

@Test
public void testMultipleLanguagesCopts() throws Exception {
useConfiguration("--apple_platform_type=ios", "--platforms=" + MockObjcSupport.IOS_ARM64);

scratch.file(
"objc/defs.bzl",
"""
def _var_providing_rule_impl(ctx):
return [
platform_common.TemplateVariableInfo({
"FOO": "$(BAR)",
"BAR": ctx.attr.var_value,
"BAZ": "$(FOO)",
}),
]
var_providing_rule = rule(
implementation = _var_providing_rule_impl,
attrs = {"var_value": attr.string()},
)
""");

scratch.file(
"objc/BUILD",
"""
load("//objc:defs.bzl", "var_providing_rule")
var_providing_rule(
name = "set_foo_to_bar",
var_value = "bar",
)
objc_library(
name = "lib",
srcs = [
"c.c",
"cpp.cpp",
"objc.m",
"objcpp.mm",
],
copts = ["-DFROM_SHARED=$(FOO),$(BAR),$(BAZ)"],
conlyopts = ["-DFROM_CONLYOPTS=$(FOO),$(BAR),$(BAZ)"],
cxxopts = ["-DFROM_CXXOPTS=$(FOO),$(BAR),$(BAZ)"],
toolchains = [":set_foo_to_bar"],
)
""");

CommandAction cCompileAction = compileAction("//objc:lib", "c.o");
assertThat(cCompileAction.getArguments())
.containsAtLeast("-DFROM_SHARED=bar,bar,bar", "-DFROM_CONLYOPTS=bar,bar,bar")
.inOrder();
assertThat(cCompileAction.getArguments()).doesNotContain("-DFROM_CXXOPTS=bar,bar,bar");

CommandAction objcCompileAction = compileAction("//objc:lib", "objc.o");
assertThat(objcCompileAction.getArguments()).contains("-DFROM_SHARED=bar,bar,bar");
assertThat(objcCompileAction.getArguments()).doesNotContain("-DFROM_CONLYOPTS=bar,bar,bar");
assertThat(objcCompileAction.getArguments()).doesNotContain("-DFROM_CXXOPTS=bar,bar,bar");

CommandAction objcppCompileAction = compileAction("//objc:lib", "objcpp.o");
assertThat(objcppCompileAction.getArguments())
.containsAtLeast("-DFROM_SHARED=bar,bar,bar", "-DFROM_CXXOPTS=bar,bar,bar")
.inOrder();
assertThat(objcppCompileAction.getArguments()).doesNotContain("-DFROM_CONLYOPTS=bar,bar,bar");

CommandAction cppCompileAction = compileAction("//objc:lib", "cpp.o");
assertThat(cppCompileAction.getArguments())
.containsAtLeast("-DFROM_SHARED=bar,bar,bar", "-DFROM_CXXOPTS=bar,bar,bar")
.inOrder();
assertThat(cppCompileAction.getArguments()).doesNotContain("-DFROM_CONLYOPTS=bar,bar,bar");
}

@Test
public void testBothModuleNameAndModuleMapGivesError() throws Exception {
checkError(
Expand Down

0 comments on commit d38b16e

Please sign in to comment.