-
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.
This adds an intermediate class between BazelStarlarkContext and Package.Builder, to prepare the way for a separate builder type for evaluating symbolic macros. A TargetDefinitionContext is anything that can consume the side-effect of defining a target, i.e. either a BUILD file's top-level logic, or a symbolic macro. NameConflictException is migrated from Package.Builder to TargetDefinitionContext, since in principle it can happen during either BUILD file or symbolic macro evaluation. Work toward #19922. PiperOrigin-RevId: 602445896 Change-Id: I6ea14a118480418ae1da3c69973a508261c119f2
- Loading branch information
1 parent
275000c
commit 133dbe1
Showing
13 changed files
with
89 additions
and
32 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
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
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
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
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
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
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/TargetDefinitionContext.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.errorprone.annotations.CanIgnoreReturnValue; | ||
import javax.annotation.Nullable; | ||
import net.starlark.java.eval.EvalException; | ||
import net.starlark.java.eval.Starlark; | ||
import net.starlark.java.eval.StarlarkThread; | ||
|
||
/** | ||
* A context object, usually stored in a {@link StarlarkThread}, upon which rules and symbolic | ||
* macros can be instantiated. | ||
*/ | ||
// TODO(#19922): Elevate some Package.Builder methods to this class. | ||
public abstract class TargetDefinitionContext extends BazelStarlarkContext { | ||
|
||
/** | ||
* An exception used when the name of a target or symbolic macro clashes with another entity | ||
* defined in the package. | ||
* | ||
* <p>Common examples of conflicts include two targets or symbolic macros sharing the same name, | ||
* and one output file being a prefix of another. See {@link Package.Builder#checkForExistingName} | ||
* and {@link Package.Builder#checkRuleAndOutputs} for more details. | ||
*/ | ||
public static final class NameConflictException extends Exception { | ||
public NameConflictException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
protected TargetDefinitionContext(Phase phase, SymbolGenerator<?> symbolGenerator) { | ||
super(phase, symbolGenerator); | ||
} | ||
|
||
/** Retrieves this object from a Starlark thread. Returns null if not present. */ | ||
@Nullable | ||
public static TargetDefinitionContext fromOrNull(StarlarkThread thread) { | ||
BazelStarlarkContext ctx = thread.getThreadLocal(BazelStarlarkContext.class); | ||
return (ctx instanceof TargetDefinitionContext) ? (TargetDefinitionContext) ctx : null; | ||
} | ||
|
||
/** | ||
* Retrieves this object from a Starlark thread. If not present, throws {@code EvalException} with | ||
* an error message indicating that {@code what} can't be used in this Starlark environment. | ||
*/ | ||
@CanIgnoreReturnValue | ||
public static TargetDefinitionContext fromOrFail(StarlarkThread thread, String what) | ||
throws EvalException { | ||
@Nullable BazelStarlarkContext ctx = thread.getThreadLocal(BazelStarlarkContext.class); | ||
if (!(ctx instanceof TargetDefinitionContext)) { | ||
throw Starlark.errorf("%s can only be used while evaluating a BUILD file or macro", what); | ||
} | ||
return (TargetDefinitionContext) ctx; | ||
} | ||
} |
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
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
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
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
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