Skip to content
Merged
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
3 changes: 2 additions & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 45 additions & 44 deletions aspect/intellij_info_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,47 @@ def collect_py_info(target, ctx, semantics, ide_info, ide_info_file, output_grou
update_sync_output_groups(output_groups, "intellij-resolve-py", to_build)
return True

def collect_cc_rule_context(ctx):
"""Collect additional information from the rule attributes of cc_xxx rules."""

if not ctx.rule.kind.startswith("cc_"):
return struct()

return struct(
sources = artifacts_from_target_list_attr(ctx, "srcs"),
headers = artifacts_from_target_list_attr(ctx, "hdrs"),
textual_headers = artifacts_from_target_list_attr(ctx, "textual_hdrs"),
copts = expand_make_variables(ctx, True, getattr(ctx.rule.attr, "copts", [])),
args = expand_make_variables(ctx, True, getattr(ctx.rule.attr, "args", [])),
include_prefix = getattr(ctx.rule.attr, "include_prefix", ""),
strip_include_prefix = getattr(ctx.rule.attr, "strip_include_prefix", ""),
)

def collect_cc_compilation_context(ctx, target):
"""Collect information from the compilation context provided by the CcInfo provider."""

compilation_context = target[CcInfo].compilation_context

# merge current compilation context with context of implementation dependencies
if ctx.rule.kind.startswith("cc_") and hasattr(ctx.rule.attr, "implementation_deps"):
impl_deps = ctx.rule.attr.implementation_deps

compilation_context = cc_common.merge_compilation_contexts(
compilation_contexts = [compilation_context] + [it[CcInfo].compilation_context for it in impl_deps],
)

# external_includes available since bazel 7
external_includes = getattr(compilation_context, "external_includes", depset()).to_list()

return struct(
direct_headers = [artifact_location(it) for it in compilation_context.direct_headers],
defines = compilation_context.defines.to_list(),
includes = compilation_context.includes.to_list(),
quote_includes = compilation_context.quote_includes.to_list(),
# both system and external includes are added using `-isystem`
system_includes = compilation_context.system_includes.to_list() + external_includes,
)

def collect_cpp_info(target, ctx, semantics, ide_info, ide_info_file, output_groups):
"""Updates C++-specific output groups, returns false if not a C++ target."""

Expand All @@ -325,51 +366,11 @@ def collect_cpp_info(target, ctx, semantics, ide_info, ide_info_file, output_gro
if ctx.rule.kind.startswith("go_"):
return False

sources = artifacts_from_target_list_attr(ctx, "srcs")
headers = artifacts_from_target_list_attr(ctx, "hdrs")
textual_headers = artifacts_from_target_list_attr(ctx, "textual_hdrs")

target_copts = []
if hasattr(ctx.rule.attr, "copts"):
target_copts += ctx.rule.attr.copts
extra_targets = []
if hasattr(ctx.rule.attr, "additional_compiler_inputs"):
extra_targets += ctx.rule.attr.additional_compiler_inputs
if hasattr(semantics, "cc") and hasattr(semantics.cc, "get_default_copts"):
target_copts += semantics.cc.get_default_copts(ctx)

target_copts = expand_make_variables(ctx, True, target_copts)
args = expand_make_variables(ctx, True, getattr(ctx.rule.attr, "args", []))

compilation_context = target[CcInfo].compilation_context

# Merge current compilation context with context of implementation dependencies.
if hasattr(ctx.rule.attr, "implementation_deps"):
implementation_deps = ctx.rule.attr.implementation_deps
compilation_context = cc_common.merge_compilation_contexts(
compilation_contexts =
[compilation_context] + [impl[CcInfo].compilation_context for impl in implementation_deps],
)

# external_includes available since bazel 7
external_includes = getattr(compilation_context, "external_includes", depset()).to_list()

c_info = struct_omit_none(
header = headers,
source = sources,
target_copt = target_copts,
textual_header = textual_headers,
transitive_define = compilation_context.defines.to_list(),
transitive_include_directory = compilation_context.includes.to_list(),
transitive_quote_include_directory = compilation_context.quote_includes.to_list(),
# both system and external includes are add using `-isystem`
transitive_system_include_directory = compilation_context.system_includes.to_list() + external_includes,
include_prefix = getattr(ctx.rule.attr, "include_prefix", None),
strip_include_prefix = getattr(ctx.rule.attr, "strip_include_prefix", None),
args = args,
ide_info["c_ide_info"] = struct(
rule_context = collect_cc_rule_context(ctx),
compilation_context = collect_cc_compilation_context(ctx, target),
)
ide_info["c_ide_info"] = c_info
resolve_files = compilation_context.headers
resolve_files = target[CcInfo].compilation_context.headers

# TODO(brendandouglas): target to cpp files only
compile_files = target[OutputGroupInfo].compilation_outputs if hasattr(target[OutputGroupInfo], "compilation_outputs") else depset([])
Expand Down
18 changes: 18 additions & 0 deletions aspect/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,21 @@ java_library(
"//aspect/testing/rules:IntellijAspectTest",
],
)

# All aspect tests.
test_suite(
name = "aspect_tests",
tests = [
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/ccbinary:CcBinaryTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/cclibrary:CcLibraryTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/cctest:CcTestTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/cctoolchain:CcToolchainTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/coptsmakevars:CoptsMakeVarsTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/general/alias:AliasTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/general/analysistest:AnalysisTestTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/general/artifacts:ArtifactTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/general/build:BuildFileTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/general/noide:NoIdeTest",
"//aspect/testing/tests/src/com/google/idea/blaze/aspect/general/tags:TagTest",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ load(
cc_binary(
name = "simple",
srcs = ["simple/simple.cc"],
args = ["simpleArg"],
)

cc_binary(
Expand All @@ -28,8 +29,8 @@ genrule(
intellij_aspect_test_fixture(
name = "aspect_fixture",
deps = [
":simple",
":expand_datadeps",
":simple",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ public void testCcBinary() throws Exception {
final var target = findTarget(testFixture, ":simple");
assertThat(target.getKindString()).isEqualTo("cc_binary");

assertThat(target.hasCIdeInfo()).isTrue();
assertThat(target.hasJavaIdeInfo()).isFalse();
assertThat(target.hasAndroidIdeInfo()).isFalse();
final var cTargetIdeInfo = target.getCIdeInfo();
final var ideInfo = target.getCIdeInfo();
assertThat(ideInfo.hasCompilationContext()).isTrue();
assertThat(ideInfo.hasRuleContext()).isTrue();

assertThat(cTargetIdeInfo.getTargetCoptList()).isEmpty();
// rule context
final var ruleCtx = ideInfo.getRuleContext();
assertThat(ruleCtx.getCoptsList()).isEmpty();
assertThat(ruleCtx.getArgs(0)).isEqualTo("simpleArg");
assertThat(ruleCtx.getSources(0).getRelativePath()).endsWith("simple/simple.cc");

// output groups
assertThat(getOutputGroupFiles(testFixture, "intellij-resolve-cpp")).isEmpty();
assertThat(getOutputGroupFiles(testFixture, "intellij-info-generic")).isEmpty();

assertThat(getOutputGroupFiles(testFixture, "intellij-info-cpp")).contains(
testRelative(intellijInfoFileName("simple")));
assertThat(getOutputGroupFiles(testFixture, "intellij-info-cpp")).contains(testRelative(intellijInfoFileName("simple")));
}

@Test
Expand All @@ -51,9 +53,8 @@ public void testExpandDataDeps() throws Exception {
final var target = findTarget(testFixture, ":expand_datadeps");
assertThat(target.getKindString()).isEqualTo("cc_binary");

final var args = target.getCIdeInfo().getArgsList();
final var args = target.getCIdeInfo().getRuleContext().getArgsList();
assertThat(args).hasSize(1);
assertThat(args.get(0)).endsWith(
"/aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/ccbinary/datadepfile.txt");
assertThat(args.get(0)).endsWith("datadepfile.txt");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,35 @@
/** Tests cc_library */
@RunWith(JUnit4.class)
public class CcLibraryTest extends BazelIntellijAspectTest {

@Test
public void testCcLibrary() throws Exception {
IntellijAspectTestFixture testFixture = loadTestFixture(":simple_fixture");
TargetIdeInfo target = findTarget(testFixture, ":simple");

final var testFixture = loadTestFixture(":simple_fixture");
final var target = findTarget(testFixture, ":simple");
assertThat(target.getKindString()).isEqualTo("cc_library");
assertThat(target.hasCIdeInfo()).isTrue();
assertThat(target.hasJavaIdeInfo()).isFalse();
assertThat(target.hasAndroidIdeInfo()).isFalse();
assertThat(target.hasPyIdeInfo()).isFalse();

assertThat(relativePathsForArtifacts(target.getCIdeInfo().getSourceList()))
final var ideInfo = target.getCIdeInfo();
assertThat(ideInfo.hasCompilationContext()).isTrue();
assertThat(ideInfo.hasRuleContext()).isTrue();

// rule context
final var ruleCtx = target.getCIdeInfo().getRuleContext();
assertThat(relativePathsForArtifacts(ruleCtx.getSourcesList()))
.containsExactly(testRelative("simple/simple.cc"));
assertThat(relativePathsForArtifacts(target.getCIdeInfo().getHeaderList()))
assertThat(relativePathsForArtifacts(ruleCtx.getHeadersList()))
.containsExactly(testRelative("simple/simple.h"));
assertThat(relativePathsForArtifacts(target.getCIdeInfo().getTextualHeaderList()))
assertThat(relativePathsForArtifacts(ruleCtx.getTextualHeadersList()))
.containsExactly(testRelative("simple/simple_textual.h"));

CIdeInfo cTargetIdeInfo = target.getCIdeInfo();
assertThat(cTargetIdeInfo.getTargetCoptList())
assertThat(ruleCtx.getCoptsList())
.containsExactly("-DGOPT", "-Ifoo/baz/", "-I", "other/headers");

// Make sure our understanding of where this attributes show up in other providers is correct.
assertThat(cTargetIdeInfo.getTransitiveSystemIncludeDirectoryList())
.contains(testRelative("foo/bar"));
assertThat(cTargetIdeInfo.getTransitiveDefineList()).contains("VERSION2");

List<String> transQuoteIncludeDirList = cTargetIdeInfo.getTransitiveQuoteIncludeDirectoryList();
assertThat(transQuoteIncludeDirList).contains(".");
// compilation context
final var compilationCtx = target.getCIdeInfo().getCompilationContext();
assertThat(compilationCtx.getDefinesList()).containsExactly("VERSION2");
assertThat(compilationCtx.getSystemIncludesList()).contains(testRelative("foo/bar"));
assertThat(compilationCtx.getQuoteIncludesList()).contains(".");
assertThat(relativePathsForArtifacts(compilationCtx.getDirectHeadersList()))
.containsExactly(testRelative("simple/simple.h"));

// Can't test for this because the cc code stuffs source artifacts into
// the output group
Expand All @@ -68,21 +68,14 @@ public void testCcLibrary() throws Exception {

@Test
public void testCcLibraryHasToolchain() throws Exception {
IntellijAspectTestFixture testFixture = loadTestFixture(":simple_fixture");
List<TargetIdeInfo> toolchains =
testFixture.getTargetsList().stream()
.filter(x -> x.hasCToolchainIdeInfo() && x.getKindString().equals("cc_toolchain_alias"))
.collect(Collectors.toList());
// TODO(b/200011173): Remove once Blaze/Bazel has been released with Starlark cc_library.
if (toolchains.isEmpty()) {
toolchains =
testFixture.getTargetsList().stream()
.filter(TargetIdeInfo::hasCToolchainIdeInfo)
.collect(toImmutableList());
}
final var testFixture = loadTestFixture(":simple_fixture");
final var toolchains = testFixture.getTargetsList().stream()
.filter(x -> x.hasCToolchainIdeInfo() && x.getKindString().equals("cc_toolchain_alias"))
.collect(Collectors.toList());

assertThat(toolchains).hasSize(1);

TargetIdeInfo target = findTarget(testFixture, ":simple");
final var target = findTarget(testFixture, ":simple");
assertThat(dependenciesForTarget(target)).contains(dep(toolchains.get(0)));
}

Expand All @@ -94,16 +87,15 @@ public void testCcDependencies() throws Exception {

assertThat(lib1.hasCIdeInfo()).isTrue();
assertThat(lib2.hasCIdeInfo()).isTrue();
CIdeInfo cIdeInfo1 = lib1.getCIdeInfo();
final var ruleContext = lib1.getCIdeInfo().getRuleContext();
final var compilationContext = lib1.getCIdeInfo().getCompilationContext();

assertThat(cIdeInfo1.getTransitiveSystemIncludeDirectoryList())
.contains(testRelative("foo/bar"));
assertThat(cIdeInfo1.getTransitiveSystemIncludeDirectoryList())
.contains(testRelative("baz/lib"));
assertThat(compilationContext.getSystemIncludesList()).contains(testRelative("foo/bar"));
assertThat(compilationContext.getSystemIncludesList()).contains(testRelative("baz/lib"));

assertThat(cIdeInfo1.getTargetCoptList()).containsExactly("-DGOPT", "-Ifoo/baz/");
assertThat(ruleContext.getCoptsList()).containsExactly("-DGOPT", "-Ifoo/baz/");

assertThat(cIdeInfo1.getTransitiveDefineList()).contains("VERSION2");
assertThat(cIdeInfo1.getTransitiveDefineList()).contains("COMPLEX_IMPL");
assertThat(compilationContext.getDefinesList()).contains("VERSION2");
assertThat(compilationContext.getDefinesList()).contains("COMPLEX_IMPL");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testCcTest() throws Exception {
assertThat(target.hasAndroidIdeInfo()).isFalse();
CIdeInfo cTargetIdeInfo = target.getCIdeInfo();

assertThat(cTargetIdeInfo.getTargetCoptList()).isEmpty();
assertThat(cTargetIdeInfo.getRuleContext().getCoptsList()).isEmpty();

// Can't test for this because the cc code stuffs source
// artifacts into the output group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,32 @@ private CIdeInfo getCIdeInfo(String targetName) throws IOException {
@Test
public void testCoptsPrefinedMakeVars() throws IOException {
final var ideInfo = getCIdeInfo(":simple_prefined");
assertThat(ideInfo.getTargetCoptList()).hasSize(2);
assertThat(ideInfo.getRuleContext().getCoptsList()).hasSize(2);

// These predefined variables' values are dependent on build system and configuration.
assertThat(ideInfo.getTargetCoptList().get(0)).containsMatch("^-DPREFINED_BINDIR=bazel-out/[0-9a-z_-]+/bin$");
assertThat(ideInfo.getTargetCoptList().get(1)).isEqualTo("-DPREFINED_BINDIR2=$(BINDIR)");
assertThat(ideInfo.getRuleContext().getCoptsList().get(0)).containsMatch("^-DPREFINED_BINDIR=bazel-out/[0-9a-z_-]+/bin$");
assertThat(ideInfo.getRuleContext().getCoptsList().get(1)).isEqualTo("-DPREFINED_BINDIR2=$(BINDIR)");
}

@Test
public void testCoptsEmptyVariable() throws IOException {
final var ideInfo = getCIdeInfo(":empty_variable");
assertThat(ideInfo.getTargetCoptList()).hasSize(1);
assertThat(ideInfo.getTargetCoptList()).contains("-Wall");
assertThat(ideInfo.getRuleContext().getCoptsList()).hasSize(1);
assertThat(ideInfo.getRuleContext().getCoptsList()).contains("-Wall");
}

@Test
public void testCoptsMakeVars() throws IOException {
final var ideInfo = getCIdeInfo(":simple_make_var");
assertThat(ideInfo.getTargetCoptList()).hasSize(4);
assertThat(ideInfo.getRuleContext().getCoptsList()).hasSize(4);

assertThat(ideInfo.getTargetCopt(0)).isEqualTo(
assertThat(ideInfo.getRuleContext().getCopts(0)).isEqualTo(
"-DEXECPATH=\"aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/coptsmakevars/simple/simple.cc\"");
assertThat(ideInfo.getTargetCopt(1)).isEqualTo(
assertThat(ideInfo.getRuleContext().getCopts(1)).isEqualTo(
"-DROOTPATH=\"aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/coptsmakevars/simple/simple.cc\"");
assertThat(ideInfo.getTargetCopt(2)).isEqualTo(
assertThat(ideInfo.getRuleContext().getCopts(2)).isEqualTo(
"-DRLOCATIONPATH=\"_main/aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/coptsmakevars/simple/simple.cc\"");
assertThat(ideInfo.getTargetCopt(3)).isEqualTo(
assertThat(ideInfo.getRuleContext().getCopts(3)).isEqualTo(
"-DLOCATION=\"aspect/testing/tests/src/com/google/idea/blaze/aspect/cpp/coptsmakevars/simple/simple.cc\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testSourceFilesAreCorrectlyMarkedAsSourceOrGenerated() throws Except
IntellijAspectTestFixture testFixture = loadTestFixture(":gen_sources_fixture");
TargetIdeInfo source = findTarget(testFixture, ":source");
TargetIdeInfo gen = findTarget(testFixture, ":gen");
assertThat(getOnlyElement(source.getCIdeInfo().getSourceList()).getIsSource()).isTrue();
assertThat(getOnlyElement(gen.getCIdeInfo().getSourceList()).getIsSource()).isFalse();
assertThat(getOnlyElement(source.getCIdeInfo().getRuleContext().getSourcesList()).getIsSource()).isTrue();
assertThat(getOnlyElement(gen.getCIdeInfo().getRuleContext().getSourcesList()).getIsSource()).isFalse();
}
}
Loading