Skip to content

Commit

Permalink
Add additional_compiler_inputs support to local_defines/defines
Browse files Browse the repository at this point in the history
Fixes #23885

Closes #23102.

PiperOrigin-RevId: 684433748
Change-Id: I12cdb9a04f930a52e57bbd8551c8f2559a15932a
  • Loading branch information
keith authored and bazel-io committed Oct 10, 2024
1 parent c481551 commit d580e9f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1142,11 +1142,11 @@ def _linkopts(ctx, additional_make_variable_substitutions, cc_toolchain):
fail("in linkopts attribute of cc_library rule {}: Apple builds do not support statically linked binaries".format(ctx.label))
return tokens

def _defines_attribute(ctx, additional_make_variable_substitutions, attr_name):
def _defines_attribute(ctx, additional_make_variable_substitutions, attr_name, additional_targets):
defines = getattr(ctx.attr, attr_name, [])
if len(defines) == 0:
return []
targets = []
targets = list(additional_targets)
for dep in ctx.attr.deps:
targets.append(dep)
result = []
Expand All @@ -1164,10 +1164,10 @@ def _defines_attribute(ctx, additional_make_variable_substitutions, attr_name):
return result

def _defines(ctx, additional_make_variable_substitutions):
return _defines_attribute(ctx, additional_make_variable_substitutions, "defines")
return _defines_attribute(ctx, additional_make_variable_substitutions, "defines", [])

def _local_defines(ctx, additional_make_variable_substitutions):
return _defines_attribute(ctx, additional_make_variable_substitutions, "local_defines")
return _defines_attribute(ctx, additional_make_variable_substitutions, "local_defines", getattr(ctx.attr, "additional_compiler_inputs", []))

def _linker_scripts(ctx):
result = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2502,4 +2502,26 @@ public void testAdditionalCompilerInputsArePassedToCompile() throws Exception {
assertThat(action.getInputs().toList()).contains(getSourceArtifact("foo/compiler_input.txt"));
assertThat(action.getArguments()).contains("foo/compiler_input.txt");
}

@Test
public void testAdditionalCompilerInputsArePassedToCompileFromLocalDefines() throws Exception {
AnalysisMock.get().ccSupport().setupCcToolchainConfig(mockToolsConfig);
scratch.file(
"foo/BUILD",
"""
cc_library(
name = 'foo',
srcs = ['hello.cc'],
local_defines = ['FOO=$(location compiler_input.txt)'],
additional_compiler_inputs = ['compiler_input.txt'],
)
""");
scratch.file("foo/compiler_input.txt", "hello world!");

ConfiguredTarget lib = getConfiguredTarget("//foo:foo");
Artifact artifact = getBinArtifact("_objs/foo/hello.o", lib);
CppCompileAction action = (CppCompileAction) getGeneratingAction(artifact);
assertThat(action.getInputs().toList()).contains(getSourceArtifact("foo/compiler_input.txt"));
assertThat(action.getArguments()).contains("-DFOO=foo/compiler_input.txt");
}
}

0 comments on commit d580e9f

Please sign in to comment.