-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add macro() .bzl callable, for defining symbolic macros
The ability to define symbolic macros in the build is guarded by --experimental_enable_first_class_macros. (For the moment, we're using "first class macros" in user documentation and "symbolic macros" or sometimes just "macros" in internal code.) Symbolic macro types follow an analogous pattern to rules: - A MacroFunction (StarlarkRuleFunction) is the Starlark callable object, returned by `macro()`, that can be invoked during package loading to instantiate the symbolic macro. MacroFunctions accept the name of the instance (like a target name) and return `None`. - A MacroClass (RuleClass) represents the information needed to actually instantiate the macro, such as the name the macro was exported with, and its implementation function. - A MacroInstance (Rule) represents the result of calling the macro during package loading, and is the object tracked by Package.Builder. Note that MacroFunction references MacroInstance references MacroClass. Package / Package.Builder gain new fields and methods to track macro instances created during BUILD evaluation. A follow-up CL will actually evaluate macro implementation functions. A different follow-up will add name conflict checking so that macro instance names are guaranteed to not clash with targets in the package. StarlarkRuleClassFunctions: - Update javadoc on StarlarkRuleFunction; add @nullable, field comments, and other comments Work toward #19922. PiperOrigin-RevId: 597571980 Change-Id: I1789d94e02c45e23b25c80990fb6ccef82e13da9
- Loading branch information
1 parent
bbc51a0
commit 09eef85
Showing
9 changed files
with
512 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/google/devtools/build/lib/packages/MacroClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2024 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.packages; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import javax.annotation.Nullable; | ||
import net.starlark.java.eval.StarlarkFunction; | ||
|
||
/** | ||
* Represents a symbolic macro, defined in a .bzl file, that may be instantiated during Package | ||
* evaluation. | ||
* | ||
* <p>This is analogous to {@link RuleClass}. In essence, a {@code MacroClass} consists of the | ||
* macro's schema and its implementation function. | ||
*/ | ||
public final class MacroClass { | ||
|
||
private final String name; | ||
private final StarlarkFunction implementation; | ||
|
||
public MacroClass(String name, StarlarkFunction implementation) { | ||
this.name = name; | ||
this.implementation = implementation; | ||
} | ||
|
||
/** Returns the macro's exported name. */ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public StarlarkFunction getImplementation() { | ||
return implementation; | ||
} | ||
|
||
/** Builder for {@link MacroClass}. */ | ||
public static final class Builder { | ||
private final StarlarkFunction implementation; | ||
@Nullable private String name = null; | ||
|
||
public Builder(StarlarkFunction implementation) { | ||
this.implementation = implementation; | ||
} | ||
|
||
@CanIgnoreReturnValue | ||
public Builder setName(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
public MacroClass build() { | ||
Preconditions.checkNotNull(name); | ||
return new MacroClass(name, implementation); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/google/devtools/build/lib/packages/MacroInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2024 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.packages; | ||
|
||
/** | ||
* Represents a use of a symbolic macro in a package. | ||
* | ||
* <p>There is one {@code MacroInstance} for each call to a {@link | ||
* StarlarkRuleClassFunctions#MacroFunction} that is executed during a package's evaluation. Just as | ||
* a {@link MacroClass} is analogous to a {@link RuleClass}, {@code MacroInstance} is analogous to a | ||
* {@link Rule} (i.e. a rule target). | ||
*/ | ||
public final class MacroInstance { | ||
|
||
private final MacroClass macroClass; | ||
private final String name; | ||
|
||
public MacroInstance(MacroClass macroClass, String name) { | ||
this.macroClass = macroClass; | ||
this.name = name; | ||
} | ||
|
||
/** Returns the {@link MacroClass} (i.e. schema info) that this instance parameterizes. */ | ||
public MacroClass getMacroClass() { | ||
return macroClass; | ||
} | ||
|
||
/** | ||
* Returns the name of this instance, as given in the {@code name = ...} attribute in the calling | ||
* BUILD file or macro. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
} |
Oops, something went wrong.