John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 1 | // Copyright 2022 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.packages; |
| 16 | |
wyv | 4858cbf | 2022-05-04 13:56:45 -0700 | [diff] [blame] | 17 | import com.google.devtools.build.lib.cmdline.BazelModuleContext; |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 18 | import com.google.devtools.build.lib.cmdline.Label; |
| 19 | import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.cmdline.RepositoryMapping; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 24 | import net.starlark.java.eval.StarlarkThread; |
| 25 | |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Converts a label literal string into a {@link Label} object, using the appropriate base package |
| 28 | * and repo mapping. |
Googler | bc1c1de | 2023-12-19 12:50:44 -0800 | [diff] [blame] | 29 | * |
| 30 | * <p>Instances are not thread-safe, due to an internal cache. |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 31 | */ |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 32 | public class LabelConverter { |
| 33 | |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 34 | /** |
Googler | 835512a | 2023-08-11 09:32:06 -0700 | [diff] [blame] | 35 | * Returns a label converter for the given thread, which MUST be currently evaluating Starlark |
| 36 | * code in a .bzl file (top-level, macro, rule implementation function, etc.). It uses the package |
| 37 | * containing the .bzl file as the base package, and the repo mapping of the repo containing the |
| 38 | * .bzl file. |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 39 | */ |
| 40 | public static LabelConverter forBzlEvaluatingThread(StarlarkThread thread) { |
Googler | 6c3e9d5 | 2023-08-09 12:12:32 -0700 | [diff] [blame] | 41 | BazelModuleContext moduleContext = BazelModuleContext.ofInnermostBzlOrThrow(thread); |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 42 | return new LabelConverter(moduleContext.packageContext()); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 45 | private final Label.PackageContext packageContext; |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 46 | private final Map<String, Label> labelCache = new HashMap<>(); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 47 | |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 48 | public LabelConverter(Label.PackageContext packageContext) { |
| 49 | this.packageContext = packageContext; |
| 50 | } |
| 51 | |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 52 | /** Creates a label converter using the given base package and repo mapping. */ |
| 53 | public LabelConverter(PackageIdentifier base, RepositoryMapping repositoryMapping) { |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 54 | this(Label.PackageContext.of(base, repositoryMapping)); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 57 | /** Returns the base package identifier that relative labels will be resolved against. */ |
| 58 | PackageIdentifier getBasePackage() { |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 59 | return packageContext.packageIdentifier(); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** Returns the Label corresponding to the input, using the current conversion context. */ |
| 63 | public Label convert(String input) throws LabelSyntaxException { |
| 64 | // Optimization: First check the package-local map, avoiding Label validation, Label |
| 65 | // construction, and global Interner lookup. This approach tends to be very profitable |
| 66 | // overall, since it's common for the targets in a single package to have duplicate |
| 67 | // label-strings across all their attribute values. |
| 68 | Label converted = labelCache.get(input); |
| 69 | if (converted == null) { |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 70 | converted = Label.parseWithPackageContext(input, packageContext); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 71 | labelCache.put(input, converted); |
| 72 | } |
| 73 | return converted; |
| 74 | } |
| 75 | |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 76 | @Override |
| 77 | public String toString() { |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 78 | return getBasePackage().toString(); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 79 | } |
| 80 | } |