diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java index 273ca4419937ad..58343bc9d16b58 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java @@ -1293,7 +1293,8 @@ private ImmutableList 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); } diff --git a/src/main/starlark/builtins_bzl/common/objc/attrs.bzl b/src/main/starlark/builtins_bzl/common/objc/attrs.bzl index bc17b786f1c626..109b382645e194 100644 --- a/src/main/starlark/builtins_bzl/common/objc/attrs.bzl +++ b/src/main/starlark/builtins_bzl/common/objc/attrs.bzl @@ -174,6 +174,26 @@ it depends, or those which depend on it.

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 "Make variable" substitution and +Bourne shell tokenization. +These flags will only apply to this target, and not those upon which +it depends, or those which depend on it. +

+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 "Make variable" substitution and +Bourne shell tokenization. +These flags will only apply to this target, and not those upon which +it depends, or those which depend on it. +

+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."""), } diff --git a/src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl b/src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl index a37dc5b7b53dca..997b697f11df5a 100644 --- a/src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl +++ b/src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl @@ -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), @@ -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, diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java index 9e311763287fb5..faf5773036dbd2 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java @@ -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(