Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conlyopts / cxxopts to objc_library #24497

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -62,6 +62,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 @@ -205,6 +207,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