diff --git a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java index 8869f747316755..31e2755cab50fa 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java @@ -373,7 +373,7 @@ public StarlarkCallable macro( } MacroClass.Builder builder = new MacroClass.Builder(implementation); - builder.addAttribute(RuleClass.NAME_ATTRIBUTE); + // "name" and "visibility" attributes are added automatically by the builder. for (Map.Entry descriptorEntry : Dict.cast(attrs, String.class, Descriptor.class, "attrs").entrySet()) { String attrName = descriptorEntry.getKey(); diff --git a/src/main/java/com/google/devtools/build/lib/packages/MacroClass.java b/src/main/java/com/google/devtools/build/lib/packages/MacroClass.java index 1e1eeea51058b6..9a3751f138bc8a 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/MacroClass.java +++ b/src/main/java/com/google/devtools/build/lib/packages/MacroClass.java @@ -15,6 +15,7 @@ package com.google.devtools.build.lib.packages; import static com.google.common.collect.ImmutableList.toImmutableList; +import static com.google.devtools.build.lib.packages.BuildType.NODEP_LABEL_LIST; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -59,6 +60,17 @@ public final class MacroClass { public static final ImmutableSet RESERVED_MACRO_ATTR_NAMES = ImmutableSet.of("name", "visibility"); + /** + * "visibility" attribute present on all symbolic macros. + * + *

This is similar to the visibility attribute for rules, but lacks the exec transitions. + */ + public static final Attribute VISIBILITY_ATTRIBUTE = + Attribute.attr("visibility", NODEP_LABEL_LIST) + .orderIndependent() + .nonconfigurable("special attribute integrated more deeply into Bazel's core logic") + .build(); + private final String name; private final Label definingBzlLabel; private final StarlarkFunction implementation; @@ -66,7 +78,7 @@ public final class MacroClass { private final ImmutableMap attributes; private final boolean isFinalizer; - public MacroClass( + private MacroClass( String name, Label definingBzlLabel, StarlarkFunction implementation, @@ -116,6 +128,9 @@ public static final class Builder { public Builder(StarlarkFunction implementation) { this.implementation = implementation; + + addAttribute(RuleClass.NAME_ATTRIBUTE); + addAttribute(VISIBILITY_ATTRIBUTE); } @CanIgnoreReturnValue diff --git a/src/test/java/com/google/devtools/build/lib/analysis/MacroVisibilityTest.java b/src/test/java/com/google/devtools/build/lib/analysis/MacroVisibilityTest.java index 13b9a5a667bcf5..63be8d2320faf6 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/MacroVisibilityTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/MacroVisibilityTest.java @@ -27,6 +27,9 @@ /** * Tests for the how the visibility system works with respect to symbolic macros, i.e. the * Macro-Aware Visibility design. + * + *

This does *not* include tests of how the {@code visibility} attribute's value gets determined + * and threaded through macros. */ @RunWith(TestParameterInjector.class) public final class MacroVisibilityTest extends BuildViewTestCase { @@ -180,7 +183,7 @@ public void buildFileAccessToMacroTargetsIsControlled() throws Exception { """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name): + def _impl(name, visibility): simple_rule( name = name + "_exported", visibility = ["//pkg:__pkg__"], @@ -226,7 +229,7 @@ public void macroAccessToBuildFileTargetsIsControlled() throws Exception { """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name): + def _impl(name, visibility): simple_rule(name = name + "_consumes_exported_ruletarget", dep = "//pkg:exported") simple_rule(name = name + "_consumes_exported_output", dep = "//pkg:exported.bin") simple_rule(name = name + "_consumes_exported_input", dep = "//pkg:exported_input") @@ -272,7 +275,7 @@ public void macroAccessToOtherMacrosInSameBuildFileIsControlled() throws Excepti """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name): + def _impl(name, visibility): simple_rule( name = name + "_internal", ) @@ -293,7 +296,7 @@ def _impl(name): """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name): + def _impl(name, visibility): simple_rule( name = name + "_consumes_internal", dep = "//pkg:foo_internal", @@ -334,7 +337,7 @@ public void siblingsInSameMacroCanSeeEachOther() throws Exception { """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name): + def _impl(name, visibility): simple_rule( name = name + "_ruletarget", ) @@ -382,7 +385,7 @@ public void buildFileTargetIsVisibleToTargetsDefinedByMacroFromSamePackage() thr """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name): + def _impl(name, visibility): simple_rule(name = name + "_macro_target", dep = "//pkg:build_target") my_macro = macro(implementation=_impl) @@ -436,7 +439,7 @@ def helper(name): "B/impl.bzl", """ load("//A:helper.bzl", "helper") - def impl(name): + def impl(name, visibility): helper(name) """); scratch.file("C/BUILD"); @@ -504,7 +507,7 @@ public void locationOfTargetDeclarationIsInnermostMacro() throws Exception { """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name): + def _impl(name, visibility): simple_rule( name = name + "_wants_vis_to_inner", dep = "//common:vis_to_inner", @@ -523,7 +526,7 @@ def _impl(name): load("//rules:simple_rule.bzl", "simple_rule") load("//inner:macro.bzl", "inner_macro") - def _impl(name): + def _impl(name, visibility): inner_macro(name = name + "_inner") simple_rule( name = name + "_wants_vis_to_inner", @@ -566,7 +569,7 @@ public void buildFileCanDelegateVisibilityPrivilegesToMacro(boolean depIsConfigu """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name, dep): + def _impl(name, visibility, dep): simple_rule( name = name, dep = dep, @@ -622,7 +625,7 @@ private void defineWrappingMacro(String name, String wraps, String attrExpr) thr """ load("%2$s", "%3$s") - def _impl(name, dep): + def _impl(name, visibility, dep): %3$s( name = name, %4$s, @@ -772,7 +775,7 @@ public void noDelegationIfCallerDoesNotPassInTarget() throws Exception { """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name, _v2P_implicitdep, _v2M_implicitdep): + def _impl(name, visibility, _v2P_implicitdep, _v2M_implicitdep): simple_rule( name = name + "_consumes_v2P_hardcoded", dep = "//common:v2P_hardcoded", diff --git a/src/test/java/com/google/devtools/build/lib/analysis/SymbolicMacroTest.java b/src/test/java/com/google/devtools/build/lib/analysis/SymbolicMacroTest.java index 66a9e66fed4dd6..bd01ce9426ac2f 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/SymbolicMacroTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/SymbolicMacroTest.java @@ -104,7 +104,7 @@ public void implementationIsInvokedWithNameParam() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): print("my_macro called with name = %s" % name) my_macro = macro(implementation=_impl) """); @@ -136,7 +136,8 @@ def _impl(): my_macro(name="abc") """); - assertGetPackageFailsWithEvent("pkg", "_impl() got unexpected keyword argument: name"); + assertGetPackageFailsWithEvent( + "pkg", "_impl() got unexpected keyword arguments: name, visibility"); } /** @@ -149,7 +150,7 @@ private void setupForMacroWithSingleTarget(String pkgName, String macroName, Str String.format("%s/foo.bzl", pkgName), String.format( """ - def _impl(name): + def _impl(name, visibility): native.cc_library(name="%s") my_macro = macro(implementation=_impl) """, @@ -228,7 +229,7 @@ public void illegallyNamedExportsFilesDoNotBreakPackageLoadingButCannotBeConfigu scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): # valid names native.exports_files(srcs=["abc_txt"]) native.exports_files(srcs=["abc-txt"]) @@ -273,7 +274,7 @@ def _my_rule_impl(ctx): "out4": "lib%{name}.so", }, ) - def _my_macro_impl(name): + def _my_macro_impl(name, visibility): my_rule(name=name) my_macro = macro(implementation=_my_macro_impl) """); @@ -316,7 +317,7 @@ public void targetOutsideMacroMayInvadeMacroNamespace() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library(name = name + "_inside_macro") my_macro = macro(implementation=_impl) """); @@ -339,7 +340,7 @@ public void targetOutsideMacroMayNotClashWithTargetInsideMacro() throws Exceptio scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library(name = name + "_target") my_macro = macro(implementation=_impl) """); @@ -360,7 +361,7 @@ public void macroCanReferToInputFile() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library( name = name, srcs = ["explicit_input.cc", "implicit_input.cc"], @@ -388,7 +389,7 @@ public void macroCannotForceCreationOfImplicitInputFileOnItsOwn() throws Excepti scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library( name = name, srcs = ["implicit_input.cc"], @@ -415,10 +416,10 @@ public void macroCanDeclareSubmacros() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _inner_impl(name): + def _inner_impl(name, visibility): native.cc_library(name = name + "_lib") inner_macro = macro(implementation=_inner_impl) - def _impl(name): + def _impl(name, visibility): inner_macro(name = name + "_inner") my_macro = macro(implementation=_impl) """); @@ -440,10 +441,10 @@ public void submacroNameMustFollowPrefixNamingConvention() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _inner_impl(name): + def _inner_impl(name, visibility): pass inner_macro = macro(implementation=_inner_impl) - def _impl(name, sep): + def _impl(name, visibility, sep): inner_macro(name = name + sep + "inner") my_macro = macro(implementation=_impl, attrs={"sep": attr.string(configurable=False)}) """); @@ -472,15 +473,15 @@ public void submacroMayHaveSameNameAsAncestorMacros() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _inner_impl(name): + def _inner_impl(name, visibility): native.cc_library(name = name) inner_macro = macro(implementation=_inner_impl) - def _middle_impl(name): + def _middle_impl(name, visibility): inner_macro(name = name) middle_macro = macro(implementation=_middle_impl) - def _outer_impl(name): + def _outer_impl(name, visibility): middle_macro(name = name) outer_macro = macro(implementation = _outer_impl) """); @@ -504,7 +505,7 @@ public void cannotHaveTwoMainTargets() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library(name = name) native.cc_library(name = name) my_macro = macro(implementation=_impl) @@ -525,11 +526,11 @@ public void cannotHaveTwoMainSubmacros() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _inner_impl(name): + def _inner_impl(name, visibility): pass inner_macro = macro(implementation=_inner_impl) - def _impl(name): + def _impl(name, visibility): inner_macro(name = name) inner_macro(name = name) my_macro = macro(implementation=_impl) @@ -550,13 +551,13 @@ public void cannotHaveBothMainTargetAndMainSubmacro_submacroDeclaredFirst() thro scratch.file( "pkg/foo.bzl", """ - def _inner_impl(name): + def _inner_impl(name, visibility): # Don't define a main target; we don't want to trigger a name conflict between this and # the outer target. pass inner_macro = macro(implementation=_inner_impl) - def _impl(name): + def _impl(name, visibility): inner_macro(name = name) native.cc_library(name = name) my_macro = macro(implementation=_impl) @@ -577,13 +578,13 @@ public void cannotHaveBothMainTargetAndMainSubmacro_targetDeclaredFirst() throws scratch.file( "pkg/foo.bzl", """ - def _inner_impl(name): + def _inner_impl(name, visibility): # Don't define a main target; we don't want to trigger a name conflict between this and # the outer target. pass inner_macro = macro(implementation=_inner_impl) - def _impl(name): + def _impl(name, visibility): native.cc_library(name = name) inner_macro(name = name) my_macro = macro(implementation=_impl) @@ -607,7 +608,7 @@ private void doCannotCallApiTest(String apiName, String usageLine) throws Except "pkg/foo.bzl", String.format( """ - def _impl(name): + def _impl(name, visibility): %s my_macro = macro(implementation=_impl) """, @@ -671,7 +672,7 @@ public void existingRules_canSeeTargetsCreatedByOrdinaryMacros() throws Exceptio scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_binary(name = name + "_lib") my_macro = macro(implementation=_impl) def query(): @@ -696,7 +697,7 @@ public void existingRules_cannotSeeTargetsCreatedByFinalizers() throws Exception scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_binary(name = name + "_lib") my_macro = macro(implementation=_impl, finalizer=True) def query(): @@ -723,7 +724,7 @@ public void hardcodedDefaultAttrValue_isUsedWhenNotOverriddenAndAttrHasNoUserSpe scratch.file( "pkg/foo.bzl", """ - def _impl(name, dep): + def _impl(name, visibility, dep): print("dep is %s" % dep) my_macro = macro( implementation=_impl, @@ -750,7 +751,7 @@ public void defaultAttrValue_isUsedWhenNotOverridden() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): + def _impl(name, visibility, xyz): print("xyz is %s" % xyz) my_macro = macro( implementation=_impl, @@ -776,7 +777,7 @@ public void defaultAttrValue_canBeOverridden() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): + def _impl(name, visibility, xyz): print("xyz is %s" % xyz) my_macro = macro( implementation=_impl, @@ -805,7 +806,7 @@ public void defaultAttrValue_isUsed_whenAttrIsImplicit() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, _xyz): + def _impl(name, visibility, _xyz): print("xyz is %s" % _xyz) my_macro = macro( implementation=_impl, @@ -831,7 +832,7 @@ public void noneAttrValue_doesNotOverrideDefault() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): + def _impl(name, visibility, xyz): print("xyz is %s" % xyz) my_macro = macro( implementation=_impl, @@ -860,7 +861,7 @@ public void noneAttrValue_doesNotSatisfyMandatoryRequirement() throws Exception scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro( implementation = _impl, @@ -888,7 +889,7 @@ public void noneAttrValue_disallowedWhenAttrDoesNotExist() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro( implementation = _impl, @@ -917,7 +918,7 @@ public void stringAttrsAreConvertedToLabelsAndInRightContext() throws Exception scratch.file( "lib/foo.bzl", """ - def _impl(name, xyz, _xyz): + def _impl(name, visibility, xyz, _xyz): print("xyz is %s" % xyz) print("_xyz is %s" % _xyz) my_macro = macro( @@ -949,7 +950,7 @@ public void cannotMutateAttrValues() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): + def _impl(name, visibility, xyz): xyz.append(4) my_macro = macro( implementation=_impl, @@ -977,7 +978,7 @@ public void attrsAllowSelectsByDefault() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): + def _impl(name, visibility, xyz): print("xyz is %s" % xyz) my_macro = macro( implementation=_impl, @@ -1017,21 +1018,21 @@ public void noneAttrValue_canAppearInSelects() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, attr_using_schema_default, attr_using_hardcoded_nonnull_default, - attr_using_hardcoded_null_default): - print("attr_using_schema_default is %s" % attr_using_schema_default) - print("attr_using_hardcoded_nonnull_default is %s" - % attr_using_hardcoded_nonnull_default) - print("attr_using_hardcoded_null_default is %s" % attr_using_hardcoded_null_default) - my_macro = macro( - implementation=_impl, - attrs = { - "attr_using_schema_default": attr.string(default="some_default"), - "attr_using_hardcoded_nonnull_default": attr.string(), - "attr_using_hardcoded_null_default": attr.label(), - }, - ) - """); +def _impl(name, visibility, attr_using_schema_default, attr_using_hardcoded_nonnull_default, + attr_using_hardcoded_null_default): + print("attr_using_schema_default is %s" % attr_using_schema_default) + print("attr_using_hardcoded_nonnull_default is %s" + % attr_using_hardcoded_nonnull_default) + print("attr_using_hardcoded_null_default is %s" % attr_using_hardcoded_null_default) +my_macro = macro( + implementation=_impl, + attrs = { + "attr_using_schema_default": attr.string(default="some_default"), + "attr_using_hardcoded_nonnull_default": attr.string(), + "attr_using_hardcoded_null_default": attr.label(), + }, +) +"""); scratch.file( "pkg/BUILD", """ @@ -1077,7 +1078,7 @@ public void configurableAttrValuesArePromotedToSelects() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, configurable_xyz, nonconfigurable_xyz): + def _impl(name, visibility, configurable_xyz, nonconfigurable_xyz): print("configurable_xyz is '%s' (type %s)" % (str(configurable_xyz), type(configurable_xyz))) print("nonconfigurable_xyz is '%s' (type %s)" % @@ -1115,7 +1116,7 @@ public void nonconfigurableAttrValuesProhibitSelects() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): + def _impl(name, visibility, xyz): print("xyz is %s" % xyz) my_macro = macro( implementation=_impl, @@ -1144,7 +1145,7 @@ public void selectableAttrCanBeEvaluatedAsBool() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): + def _impl(name, visibility, xyz): # Allowed for now when xyz is a select(). # In the future, we'll ban implicit conversion and only allow # if there's an explicit bool(xyz). @@ -1181,7 +1182,7 @@ public void labelVisitation() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, **kwargs): + def _impl(name, visibility, **kwargs): pass my_macro = macro( implementation = _impl, diff --git a/src/test/java/com/google/devtools/build/lib/analysis/VisibilityProviderTest.java b/src/test/java/com/google/devtools/build/lib/analysis/VisibilityProviderTest.java index 6e5ff1419859fa..0dfbe9a81635d2 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/VisibilityProviderTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/VisibilityProviderTest.java @@ -142,7 +142,7 @@ public void providerValueForTargetsInMacro() throws Exception { """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name): + def _impl(name, visibility): simple_rule( name = name + "_rule_target", # No implicit input file, because they can only be created outside a symbolic @@ -208,7 +208,7 @@ public void providerValueForAlias() throws Exception { """ load("//rules:simple_rule.bzl", "simple_rule") - def _impl(name): + def _impl(name, visibility): simple_rule( name = name + "_actual", visibility = ["//actual_client:__pkg__"]) diff --git a/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java b/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java index e95fdb097dbc2e..2e428e39d4cc83 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java +++ b/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java @@ -1185,7 +1185,7 @@ private void defineEmptyMacroBzl() throws Exception { scratch.file( "pkg/my_macro.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro(implementation = _impl) """); @@ -1376,7 +1376,7 @@ public void testSymbolicMacro_implicitCreationOfInputFilesIsNotTriggeredByMacros scratch.file( "pkg/my_macro.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library( name = name, srcs = ["//pkg:src_A.txt", "//pkg:src_B.txt"], @@ -1405,15 +1405,15 @@ public void testSymbolicMacro_deferredEvaluationExpandsTransitively() throws Exc scratch.file( "pkg/my_macro.bzl", """ - def _inner_impl(name): + def _inner_impl(name, visibility): native.cc_library(name = name) inner_macro = macro(implementation=_inner_impl, finalizer = True) - def _middle_impl(name): + def _middle_impl(name, visibility): inner_macro(name = name) middle_macro = macro(implementation=_middle_impl, finalizer = True) - def _outer_impl(name): + def _outer_impl(name, visibility): middle_macro(name = name) outer_macro = macro(implementation=_outer_impl, finalizer = True) """); @@ -1434,7 +1434,7 @@ private void defineRecursiveMacro(boolean deferredEvaluation) throws Exception { "pkg/recursive_macro.bzl", String.format( """ - def _impl(name, height): + def _impl(name, visibility, height): if height == 0: native.cc_library(name = name) else: @@ -1501,7 +1501,7 @@ public void testSymbolicMacro_indirectRecursionAlsoProhibited() throws Exception scratch.file( "pkg/recursive_macro.bzl", """ - def _A_impl(name, stop): + def _A_impl(name, visibility, stop): if stop: native.cc_library(name = name) else: @@ -1514,7 +1514,7 @@ def _A_impl(name, stop): }, ) - def _B_impl(name): + def _B_impl(name, visibility): macro_A( name = name + "_A", stop = True, @@ -1522,7 +1522,7 @@ def _B_impl(name): macro_B = macro(implementation = _B_impl) - def _main_impl(name): + def _main_impl(name, visibility): macro_A(name = name) main_macro = macro(implementation = _main_impl) @@ -1573,7 +1573,7 @@ public void testDeclarationVisibilityUnioning_onlyOccursWithinMacros() throws Ex scratch.file( "lib/macro.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library( name = name, visibility = ["//other_pkg:__pkg__"], @@ -1604,7 +1604,7 @@ public void testDeclarationVisibilityUnioning_usesInnermostMacroLocation() throw scratch.file( "inner/macro.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library( name = name, visibility = ["//other_pkg:__pkg__"], @@ -1616,7 +1616,7 @@ def _impl(name): "outer/macro.bzl", """ load("//inner:macro.bzl", "inner_macro") - def _impl(name): + def _impl(name, visibility): inner_macro(name = name) outer_macro = macro(implementation = _impl) """); @@ -1640,7 +1640,7 @@ public void testDeclarationVisibilityUnioning_doesNotApplyPackageDefaultVisibili scratch.file( "lib/macro.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library(name = name) my_macro = macro(implementation = _impl) """); @@ -1667,7 +1667,7 @@ public void testDeclarationVisibilityUnioning_worksWithPublicPrivateAndDuplicate scratch.file( "lib/macro.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library( name = name + "_public", visibility = ["//visibility:public"], @@ -1705,7 +1705,7 @@ public void testDeclarationVisibilityUnioning_appliesToExportsFiles() throws Exc scratch.file( "lib/macro.bzl", """ - def _impl(name): + def _impl(name, visibility): native.exports_files([name + "_exported"]) native.exports_files([name + "_internal"], visibility = ["//visibility:private"]) my_macro = macro(implementation = _impl) @@ -1730,7 +1730,7 @@ public void testDeclarationVisibilityUnioning_hasNoEffectOnPackageGroups() throw scratch.file( "lib/macro.bzl", """ - def _impl(name): + def _impl(name, visibility): native.package_group(name = name) my_macro = macro(implementation = _impl) """); @@ -1754,7 +1754,7 @@ public void testDeclarationVisibilityUnioning_failsGracefullyOnInvalidVisibility scratch.file( "lib/macro.bzl", """ - def _impl(name): + def _impl(name, visibility): native.cc_library( name = name, visibility = ["//visibility:not_a_valid_specifier"], diff --git a/src/test/java/com/google/devtools/build/lib/packages/RuleFinalizerTest.java b/src/test/java/com/google/devtools/build/lib/packages/RuleFinalizerTest.java index 855771fcaf353d..39b8aea1760b59 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/RuleFinalizerTest.java +++ b/src/test/java/com/google/devtools/build/lib/packages/RuleFinalizerTest.java @@ -62,7 +62,7 @@ public void basicFunctionality() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, targets_of_interest): + def _impl(name, visibility, targets_of_interest): for r in native.existing_rules().values(): if r["name"] in [t.name for t in targets_of_interest]: genrule_name = name + "_" + r["name"] + "_finalize" @@ -98,7 +98,7 @@ public void finalizer_canCallFinalizer() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl_inner(name): + def _impl_inner(name, visibility): for r in native.existing_rules().values(): if r["name"] == "foo": genrule_name = name + "_" + r["name"] + "_finalize" @@ -111,7 +111,7 @@ def _impl_inner(name): my_finalizer_inner = macro(implementation = _impl_inner, finalizer = True) - def _impl_outer(name): + def _impl_outer(name, visibility): my_finalizer_inner(name = name + "_inner") my_finalizer_outer = macro(implementation = _impl_outer, finalizer = True) @@ -134,7 +134,7 @@ public void finalizer_canCallNonFinalizerMacro() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl_macro(name, deps): + def _impl_macro(name, visibility, deps): native.genrule( name = name, srcs = deps, @@ -144,7 +144,7 @@ def _impl_macro(name, deps): my_macro = macro(implementation = _impl_macro, attrs = {"deps": attr.label_list()}) - def _impl_finalizer(name): + def _impl_finalizer(name, visibility): for r in native.existing_rules().values(): if r["name"] == "foo": my_macro(name=name + "_" + r["name"] + "_finalize", deps = [r["name"]]) @@ -170,7 +170,7 @@ public void nonFinalizerMacro_cannotCallFinalizer() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl_finalizer(name): + def _impl_finalizer(name, visibility): for r in native.existing_rules().values(): if r["name"] == "foo": genrule_name = name + "_" + r["name"] + "_finalize" @@ -183,7 +183,7 @@ def _impl_finalizer(name): my_finalizer = macro(implementation = _impl_finalizer, finalizer = True) - def _impl_macro(name): + def _impl_macro(name, visibility): my_finalizer(name = name + "_inner") my_macro = macro(implementation = _impl_macro) @@ -232,18 +232,18 @@ def check_existing_rules(): fail("native.existing_rule(" + t + ") != None") print("native.existing_rules and native.existing_rule are as expected") - def _impl_macro(name): + def _impl_macro(name, visibility): native.cc_library(name = name + "_inner_lib") my_macro = macro(implementation = _impl_macro) - def _impl_inner_finalizer(name): + def _impl_inner_finalizer(name, visibility): native.cc_library(name = name + "_inner_lib") check_existing_rules() inner_finalizer = macro(implementation = _impl_inner_finalizer, finalizer = True) - def _impl_finalizer(name): + def _impl_finalizer(name, visibility): native.cc_library(name = name + "_inner_lib") my_macro(name = name + "_inner_macro") inner_finalizer(name = name + "_inner_finalizer") diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java index 3bde346e2e6314..ba2db9594bb35d 100644 --- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java +++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java @@ -1718,7 +1718,7 @@ public void testPrintProviderCollection() throws Exception { def _top_level_rule_impl(ctx): print('My Dep Providers:', ctx.attr.my_dep) - def _dep_rule_impl(name): + def _dep_rule_impl(ctx): providers = [ FooInfo(), BarInfo(), diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java index 023ed99c2cd765..4b0a86a3c2b686 100644 --- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java +++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java @@ -59,6 +59,7 @@ import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.packages.ExecGroup; import com.google.devtools.build.lib.packages.ImplicitOutputsFunction; +import com.google.devtools.build.lib.packages.MacroClass; import com.google.devtools.build.lib.packages.NoSuchPackageException; import com.google.devtools.build.lib.packages.Package; import com.google.devtools.build.lib.packages.PredicateWithMessage; @@ -272,7 +273,7 @@ public void testSymbolicMacro_failsWithoutFlag() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro(implementation=_impl) """); @@ -295,7 +296,7 @@ public void testSymbolicMacro_instantiationRegistersOnPackage() throws Exception scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro(implementation=_impl) """); @@ -320,7 +321,7 @@ public void testSymbolicMacro_instantiationRequiresExport() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass s = struct(m = macro(implementation=_impl)) """); @@ -345,7 +346,7 @@ public void testSymbolicMacro_cannotInstantiateInBzlThread() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro(implementation=_impl) @@ -375,7 +376,7 @@ public void testSymbolicMacro_requiresNameAttribute() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro(implementation=_impl) """); @@ -400,7 +401,7 @@ public void testSymbolicMacro_prohibitsPositionalArgs() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro(implementation=_impl) """); @@ -425,7 +426,7 @@ public void testSymbolicMacroCanAcceptAttributes() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, target_suffix): + def _impl(name, visibility, target_suffix): native.cc_library(name = name + "_" + target_suffix) my_macro = macro( implementation=_impl, @@ -456,7 +457,7 @@ public void testSymbolicMacro_rejectsUnknownAttribute() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro( implementation = _impl, @@ -490,7 +491,7 @@ public void testSymbolicMacro_rejectsReservedAttributeName() throws Exception { evalAndExport( ev, """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro( implementation = _impl, @@ -510,7 +511,7 @@ public void testSymbolicMacro_requiresMandatoryAttribute() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name): + def _impl(name, visibility): pass my_macro = macro( implementation = _impl, @@ -540,7 +541,7 @@ public void testSymbolicMacro_cannotOverrideImplicitAttribute() throws Exception scratch.file( "pkg/foo.bzl", """ - def _impl(name, _xyz): + def _impl(name, visibility, _xyz): print("_xyz is %s" % _xyz) my_macro = macro( implementation=_impl, @@ -572,7 +573,7 @@ public void testSymbolicMacro_doesNotSupportComputedDefaults() throws Exception ev.checkEvalErrorContains( "In macro attribute 'xyz': Macros do not support computed defaults or late-bound defaults", """ - def _impl(name, xyz): pass + def _impl(name, visibility, xyz): pass def _computed_default(): return "DEFAULT" my_macro = macro( implementation=_impl, @@ -599,7 +600,7 @@ public void testSymbolicMacro_doesNotSupportLateBoundDefaults() throws Exception ev.checkEvalErrorContains( "In macro attribute 'xyz': Macros do not support computed defaults or late-bound defaults", """ - def _impl(name, xyz): pass + def _impl(name, visibility, xyz): pass _latebound_default = configuration_field(fragment = "cpp", name = "cc_toolchain") my_macro = macro( implementation=_impl, @@ -617,7 +618,7 @@ public void testSymbolicMacro_macroFunctionApi() throws Exception { evalAndExport( ev, """ - def _impl(name): + def _impl(name, visibility): pass exported = macro( implementation=_impl, @@ -662,6 +663,8 @@ def _impl(name): .containsExactly( "name", RuleClass.NAME_ATTRIBUTE, + "visibility", + MacroClass.VISIBILITY_ATTRIBUTE, "abc", Attribute.attr("abc", Type.INTEGER).starlarkDefined().build(), "xyz", @@ -851,8 +854,8 @@ public void testRuleCannotSetConfigurableOnAttr() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): - print("xyz is %s" % xyz) + def _impl(ctx): + print("xyz is %s" % ctx.attr.xyz) my_rule = rule( implementation=_impl, attrs = { @@ -884,8 +887,8 @@ public void testAspectCannotSetConfigurableOnAttr() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): - print("xyz is %s" % xyz) + def _impl(ctx): + print("xyz is %s" % ctx.attr.xyz) my_aspect = aspect( implementation=_impl, attrs = { @@ -6514,8 +6517,8 @@ public void starlarkRuleFunctionCodec() throws Exception { scratch.file( "pkg/foo.bzl", """ - def _impl(name, xyz): - print("xyz is %s" % xyz) + def _impl(ctx): + print("xyz is %s" % ctx.attr.xyz) my_rule = rule( implementation=_impl, attrs = { diff --git a/src/test/java/com/google/devtools/build/lib/starlarkdocextract/ModuleInfoExtractorTest.java b/src/test/java/com/google/devtools/build/lib/starlarkdocextract/ModuleInfoExtractorTest.java index 00b90667989b66..d0d4415852f599 100644 --- a/src/test/java/com/google/devtools/build/lib/starlarkdocextract/ModuleInfoExtractorTest.java +++ b/src/test/java/com/google/devtools/build/lib/starlarkdocextract/ModuleInfoExtractorTest.java @@ -888,7 +888,7 @@ public void unexportedRule_notDocumented() throws Exception { Module module = exec( """ - def _my_impl(name): + def _my_impl(ctx): pass s = struct( @@ -908,7 +908,7 @@ public void macroDocstring() throws Exception { execWithOptions( ImmutableList.of("--experimental_enable_first_class_macros"), """ - def _my_impl(name): + def _my_impl(name, visibility): pass documented_macro = macro(