Skip to content

Commit 2cfcf0b

Browse files
authored
Prepartation for Bazel 9: add templating for CcInfo and cc_common (#8002)
Since Bazel 9 disables autoloads we can no longer use the builtin symbols and have to add loads. However, to preserver backwards compatibility this is done via templating if rules_cc is found in the module graph.
1 parent 9081d9c commit 2cfcf0b

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

aspect/cc_info.bzl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
# TEMPLATE-INCLUDE-BEGIN
2+
## #if( $isCcEnabled == "true" )
3+
##load("@rules_cc//cc:defs.bzl", "CcInfo", "cc_common")
4+
## #end
5+
##
6+
##CC_USE_GET_TOOL_FOR_ACTION = ${use_get_tool_for_action}
7+
# TEMPLATE-INCLUDE-END
8+
19
# TEMPLATE-IGNORE-BEGIN
10+
load("@rules_cc//cc:defs.bzl", "CcInfo", "cc_common")
11+
212
CC_USE_GET_TOOL_FOR_ACTION = True
313
# TEMPLATE-IGNORE-END
414

5-
# TEMPLATE-INCLUDE-BEGIN
6-
##CC_USE_GET_TOOL_FOR_ACTION = ${use_get_tool_for_action}
7-
# TEMPLATE-INCLUDE-END
15+
CcInfoCompat = CcInfo
16+
cc_common_compat = cc_common

aspect/intellij_info_impl.bzl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ load(
1313
"struct_omit_none",
1414
"to_artifact_location",
1515
)
16-
load(":cc_info.bzl", "CC_USE_GET_TOOL_FOR_ACTION")
16+
load(":cc_info.bzl", "CC_USE_GET_TOOL_FOR_ACTION", "CcInfoCompat", "cc_common_compat")
1717
load(":code_generator_info.bzl", "CODE_GENERATOR_RULE_NAMES")
1818
load(
1919
":make_variables.bzl",
@@ -151,7 +151,7 @@ def _is_language_specific_proto_library(ctx, target, semantics):
151151
"""Returns True if the target is a proto library with attached language-specific aspect."""
152152
if ctx.rule.kind != "proto_library":
153153
return False
154-
if CcInfo in target:
154+
if CcInfoCompat in target:
155155
return True
156156
return False
157157

@@ -328,16 +328,16 @@ def collect_cc_rule_context(ctx):
328328
)
329329

330330
def collect_cc_compilation_context(ctx, target):
331-
"""Collect information from the compilation context provided by the CcInfo provider."""
331+
"""Collect information from the compilation context provided by the CcInfoCompat provider."""
332332

333-
compilation_context = target[CcInfo].compilation_context
333+
compilation_context = target[CcInfoCompat].compilation_context
334334

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

339-
compilation_context = cc_common.merge_compilation_contexts(
340-
compilation_contexts = [compilation_context] + [it[CcInfo].compilation_context for it in impl_deps],
339+
compilation_context = cc_common_compat.merge_compilation_contexts(
340+
compilation_contexts = [compilation_context] + [it[CcInfoCompat].compilation_context for it in impl_deps],
341341
)
342342

343343
# external_includes available since bazel 7
@@ -355,22 +355,22 @@ def collect_cc_compilation_context(ctx, target):
355355
def collect_cpp_info(target, ctx, semantics, ide_info, ide_info_file, output_groups):
356356
"""Updates C++-specific output groups, returns false if not a C++ target."""
357357

358-
if CcInfo not in target:
358+
if CcInfoCompat not in target:
359359
return False
360360

361361
# ignore cc_proto_library, attach to proto_library with aspect attached instead
362362
if ctx.rule.kind == "cc_proto_library":
363363
return False
364364

365-
# Go targets always provide CcInfo. Usually it's empty, but even if it isn't we don't handle it
365+
# Go targets always provide CcInfoCompat. Usually it's empty, but even if it isn't we don't handle it
366366
if ctx.rule.kind.startswith("go_"):
367367
return False
368368

369369
ide_info["c_ide_info"] = struct(
370370
rule_context = collect_cc_rule_context(ctx),
371371
compilation_context = collect_cc_compilation_context(ctx, target),
372372
)
373-
resolve_files = target[CcInfo].compilation_context.headers
373+
resolve_files = target[CcInfoCompat].compilation_context.headers
374374

375375
# TODO(brendandouglas): target to cpp files only
376376
compile_files = target[OutputGroupInfo].compilation_outputs if hasattr(target[OutputGroupInfo], "compilation_outputs") else depset([])
@@ -391,11 +391,11 @@ def collect_c_toolchain_info(target, ctx, semantics, ide_info, ide_info_file, ou
391391
# to generic platforms and toolchains.
392392
if ctx.rule.kind != "cc_toolchain" and ctx.rule.kind != "cc_toolchain_suite" and ctx.rule.kind != "cc_toolchain_alias":
393393
return False
394-
if cc_common.CcToolchainInfo not in target:
394+
if cc_common_compat.CcToolchainInfo not in target:
395395
return False
396396

397397
# cc toolchain to access compiler flags
398-
cpp_toolchain = target[cc_common.CcToolchainInfo]
398+
cpp_toolchain = target[cc_common_compat.CcToolchainInfo]
399399

400400
# cpp fragment to access bazel options
401401
cpp_fragment = ctx.fragments.cpp
@@ -404,39 +404,39 @@ def collect_c_toolchain_info(target, ctx, semantics, ide_info, ide_info_file, ou
404404
cxxopts = cpp_fragment.cxxopts
405405
conlyopts = cpp_fragment.conlyopts
406406

407-
feature_configuration = cc_common.configure_features(
407+
feature_configuration = cc_common_compat.configure_features(
408408
ctx = ctx,
409409
cc_toolchain = cpp_toolchain,
410410
requested_features = ctx.features,
411411
unsupported_features = ctx.disabled_features + UNSUPPORTED_FEATURES,
412412
)
413-
c_variables = cc_common.create_compile_variables(
413+
c_variables = cc_common_compat.create_compile_variables(
414414
feature_configuration = feature_configuration,
415415
cc_toolchain = cpp_toolchain,
416416
user_compile_flags = copts + conlyopts,
417417
)
418-
cpp_variables = cc_common.create_compile_variables(
418+
cpp_variables = cc_common_compat.create_compile_variables(
419419
feature_configuration = feature_configuration,
420420
cc_toolchain = cpp_toolchain,
421421
user_compile_flags = copts + cxxopts,
422422
)
423-
c_options = cc_common.get_memory_inefficient_command_line(
423+
c_options = cc_common_compat.get_memory_inefficient_command_line(
424424
feature_configuration = feature_configuration,
425425
action_name = ACTION_NAMES.c_compile,
426426
variables = c_variables,
427427
)
428-
cpp_options = cc_common.get_memory_inefficient_command_line(
428+
cpp_options = cc_common_compat.get_memory_inefficient_command_line(
429429
feature_configuration = feature_configuration,
430430
action_name = ACTION_NAMES.cpp_compile,
431431
variables = cpp_variables,
432432
)
433433

434434
if CC_USE_GET_TOOL_FOR_ACTION:
435-
c_compiler = cc_common.get_tool_for_action(
435+
c_compiler = cc_common_compat.get_tool_for_action(
436436
feature_configuration = feature_configuration,
437437
action_name = ACTION_NAMES.c_compile,
438438
)
439-
cpp_compiler = cc_common.get_tool_for_action(
439+
cpp_compiler = cc_common_compat.get_tool_for_action(
440440
feature_configuration = feature_configuration,
441441
action_name = ACTION_NAMES.cpp_compile,
442442
)
@@ -649,7 +649,7 @@ def make_intellij_info_aspect(aspect_impl, semantics, **kwargs):
649649
attr_aspects = attr_aspects,
650650
attrs = attrs,
651651
fragments = ["cpp"],
652-
required_aspect_providers = [[CcInfo]] + semantics.extra_required_aspect_providers,
652+
required_aspect_providers = [[CcInfoCompat]] + semantics.extra_required_aspect_providers,
653653
implementation = aspect_impl,
654654
**kwargs
655655
)

base/src/com/google/idea/blaze/base/sync/aspects/storage/CcAspectTemplateWriter.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.idea.blaze.base.sync.aspects.storage
1717

18+
import com.google.idea.blaze.base.sync.SyncProjectState
1819
import com.google.idea.blaze.base.sync.aspects.storage.AspectRepositoryProvider.ASPECT_TEMPLATE_DIRECTORY
1920
import com.google.idea.blaze.base.util.TemplateWriter
2021
import com.intellij.openapi.project.Project
@@ -28,9 +29,10 @@ class CcAspectTemplateWriter : AspectWriter {
2829

2930
override fun name(): String = "CC Aspect Templates"
3031

31-
override fun writeDumb(dst: Path, project: Project) {
32+
override fun write(dst: Path, project: Project, state: SyncProjectState) {
3233
val options = mapOf(
33-
getRegistryOption("bazel.cc.aspect.use_get_tool_for_action", true)
34+
getRegistryOption("bazel.cc.aspect.use_get_tool_for_action", true),
35+
getCcEnabledOption(state),
3436
)
3537

3638
TemplateWriter.evaluate(
@@ -42,6 +44,11 @@ class CcAspectTemplateWriter : AspectWriter {
4244
)
4345
}
4446

47+
private fun getCcEnabledOption(state: SyncProjectState): Pair<String, String> {
48+
val hasRulesCc = state.externalWorkspaceData?.getByRepoName("rules_cc") != null
49+
return Pair("isCcEnabled", if (hasRulesCc) "true" else "false")
50+
}
51+
4552
private fun getRegistryOption(key: String, default: Boolean): Pair<String, String> {
4653
val value = Registry.`is`(key, default)
4754
val name = key.split('.').last()

0 commit comments

Comments
 (0)