Skip to content

Commit

Permalink
Supply code-gen rule names via settings (#6889)
Browse files Browse the repository at this point in the history
* Supply code-gen rule names via settings

A prior PR has allowed that, for some languages, certain
Rules may be specified as being "code generators". This
mechanism was via Bazel tags on the target. This commit
changes the mechanism so that the rule names are specified
by settings instead.

* Supply code-gen rule names via settings (fix android test)

* Supply code-gen rule names via settings (fix android test 2)

* switch the detection of code-gen rule names to action env

* switch the detection of code-gen rule names to action env - comments corrections

* Revert "switch the detection of code-gen rule names to action env - comments corrections"

This reverts commit bffe170.

* Revert "switch the detection of code-gen rule names to action env"

This reverts commit e1e9bd0.

* switch to use .bzl templates to convey the languages' rules' names to the aspect

* switch to use .bzl templates to convey the languages' rules' names to the aspect - fix android test

(cherry picked from commit 22a4474)
  • Loading branch information
andponlin-canva authored and mai93 committed Oct 30, 2024
1 parent 77b7a71 commit 682be4f
Show file tree
Hide file tree
Showing 33 changed files with 437 additions and 443 deletions.
32 changes: 26 additions & 6 deletions aspect/intellij_info_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ load(":flag_hack.bzl", "FlagHackInfo")

load("@intellij_aspect_template//:java_info.bzl", "get_java_info", "java_info_in_target", "java_info_reference")

load("@intellij_aspect_template//:code_generator_info.bzl", "CODE_GENERATOR_RULE_NAMES")

load(
":make_variables.bzl",
"expand_make_variables",
Expand Down Expand Up @@ -81,8 +83,6 @@ PY2 = 1

PY3 = 2

TARGET_TAG_PY_CODE_GENERATOR = "intellij-py-code-generator"

# PythonCompatVersion enum; must match PyIdeInfo.PythonSrcsVersion
SRC_PY2 = 1

Expand All @@ -96,6 +96,22 @@ SRC_PY3ONLY = 5

##### Helpers

def get_code_generator_rule_names(ctx, language_name):
"""Supplies a list of Rule names for code generation for the language specified
For some languages, it is possible to specify Rules' names that are interpreted as
code-generators for the language. These Rules' names are specified as attrs and are provided to
the aspect using the `AspectStrategy#AspectParameter` in the plugin logic.
"""

if not language_name:
fail("the `language_name` must be provided")

if hasattr(CODE_GENERATOR_RULE_NAMES, language_name):
return getattr(CODE_GENERATOR_RULE_NAMES, language_name)

return []

def get_registry_flag(ctx, name):
"""Registry flags are passed to aspects using defines. See CppAspectArgsProvider."""

Expand Down Expand Up @@ -341,12 +357,13 @@ def collect_py_info(target, ctx, semantics, ide_info, ide_info_file, output_grou
data_deps = getattr(ctx.rule.attr, "data", [])
args = _do_starlark_string_expansion(ctx, "args", args, data_deps)
imports = getattr(ctx.rule.attr, "imports", [])
is_code_generator = False

# If there are apparently no sources found from `srcs` and the target has the tag
# for code-generation then use the run-files as the sources and take the imports
# from the PyInfo.
# If there are apparently no sources found from `srcs` and the target has a rule name which is
# one of the ones pre-specified to the aspect as being a code-generator for Python then
# interpret the outputs of the target specified in the PyInfo as being sources.

if 0 == len(sources) and TARGET_TAG_PY_CODE_GENERATOR in getattr(ctx.rule.attr, "tags", []):
if 0 == len(sources) and ctx.rule.kind in get_code_generator_rule_names(ctx, "python"):
def provider_import_to_attr_import(provider_import):
"""\
Remaps the imports from PyInfo
Expand Down Expand Up @@ -396,13 +413,16 @@ def collect_py_info(target, ctx, semantics, ide_info, ide_info_file, output_grou
if runfiles and runfiles.files:
sources.extend([artifact_location(f) for f in runfiles.files.to_list()])

is_code_generator = True

ide_info["py_ide_info"] = struct_omit_none(
launcher = py_launcher,
python_version = _get_python_version(ctx),
sources = sources,
srcs_version = _get_python_srcs_version(ctx),
args = args,
imports = imports,
is_code_generator = is_code_generator,
)

update_sync_output_groups(output_groups, "intellij-info-py", depset([ide_info_file]))
Expand Down
22 changes: 17 additions & 5 deletions aspect_template/BUILD
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
filegroup(
name = "aspect_files",
srcs = [
"WORKSPACE",
"BUILD.bazel",
"WORKSPACE",
"code_generator_info.bzl",
"code_generator_info.template.bzl",
"java_info.bzl",
"java_info.template.bzl",
],
visibility = ["//visibility:public"],
)

STRIP_TEMPLATE_SCRIPT = """cat $(SRCS) >$@ &&
sed -i -e '/TEMPLATE-IGNORE-BEGIN/,/TEMPLATE-IGNORE-END/d' $@ &&
sed -i -e '/TEMPLATE-INCLUDE-BEGIN/,/TEMPLATE-INCLUDE-END/{s/^[#][#]//;}' $@
"""

genrule(
name = "generate_java_info_template",
outs = ["java_info.template.bzl"],
srcs = ["java_info.bzl"],
cmd = "cat $(SRCS) >$@ && " +
"sed -i -e '/TEMPLATE-IGNORE-BEGIN/,/TEMPLATE-IGNORE-END/d' $@ && " +
"sed -i -e '/TEMPLATE-INCLUDE-BEGIN/,/TEMPLATE-INCLUDE-END/{s/^[#][#]//;}' $@",
outs = ["java_info.template.bzl"],
cmd = STRIP_TEMPLATE_SCRIPT,
)

genrule(
name = "generate_code_generator_info_template",
srcs = ["code_generator_info.bzl"],
outs = ["code_generator_info.template.bzl"],
cmd = STRIP_TEMPLATE_SCRIPT,
)

genrule(
Expand Down
18 changes: 18 additions & 0 deletions aspect_template/code_generator_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Data required for the code-generator system"""

# The following is a list of the languages to the set of Rule names
# which can be considered code-generators for that language. Look
# for the `get_code_generator_rule_names` function in the aspect
# logic that integrates with this constant.

CODE_GENERATOR_RULE_NAMES = struct(
# TEMPLATE-INCLUDE-BEGIN
###foreach( $aLanguageClassRuleNames in $languageClassRuleNames )
## $aLanguageClassRuleNames.languageClass.name = [
###foreach ( $aRuleName in $aLanguageClassRuleNames.ruleNames )
## "$aRuleName",
###end
## ],
###end
# TEMPLATE-INCLUDE-END
)
2 changes: 0 additions & 2 deletions base/src/META-INF/blaze-base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@
<extensionPoint qualifiedName="com.google.idea.blaze.BuildSystemVersionChecker" interface="com.google.idea.blaze.base.plugin.BuildSystemVersionChecker"/>
<extensionPoint qualifiedName="com.google.idea.blaze.BlazeIssueParserProvider" interface="com.google.idea.blaze.base.issueparser.BlazeIssueParserProvider"/>
<extensionPoint qualifiedName="com.google.idea.blaze.DirectoryToTargetProvider" interface="com.google.idea.blaze.base.dependencies.DirectoryToTargetProvider"/>
<extensionPoint qualifiedName="com.google.idea.blaze.TargetTagFilter" interface="com.google.idea.blaze.base.dependencies.TargetTagFilter"/>
<extensionPoint qualifiedName="com.google.idea.blaze.SourceToTargetProvider" interface="com.google.idea.blaze.base.dependencies.SourceToTargetProvider"/>
<extensionPoint qualifiedName="com.google.idea.blaze.SourceToTargetFilteringStrategy" interface="com.google.idea.blaze.base.dependencies.SourceToTargetFilteringStrategy"/>
<extensionPoint qualifiedName="com.google.idea.blaze.MacroTargetProvider"
Expand Down Expand Up @@ -641,7 +640,6 @@
<TargetFinder implementation="com.google.idea.blaze.base.run.targetfinder.ProjectTargetFinder"/>
<SourceToTargetFinder implementation="com.google.idea.blaze.base.run.testmap.ProjectSourceToTargetFinder"/>
<DirectoryToTargetProvider implementation="com.google.idea.blaze.base.dependencies.BlazeQueryDirectoryToTargetProvider" order="last"/>
<TargetTagFilter implementation="com.google.idea.blaze.base.dependencies.BlazeQueryTargetTagFilter" order="last"/>
<SourceToTargetProvider implementation="com.google.idea.blaze.base.dependencies.BlazeQuerySourceToTargetProvider" order="last"/>
<SourceToTargetFilteringStrategy implementation="com.google.idea.blaze.base.dependencies.SourceToTargetFilteringStrategy$IgnoredRules"/>
<SourceToTargetFilteringStrategy implementation="com.google.idea.blaze.base.dependencies.SourceToTargetFilteringStrategy$SupportedLanguages"/>
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 682be4f

Please sign in to comment.