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. |
| 29 | */ |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 30 | public class LabelConverter { |
| 31 | |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Returns a label converter for the given thread, which MUST be evaluating a .bzl file. It uses |
| 34 | * the package containing the .bzl file as the base package, and the repo mapping of the repo |
| 35 | * containing the .bzl file. |
| 36 | */ |
| 37 | public static LabelConverter forBzlEvaluatingThread(StarlarkThread thread) { |
Googler | 6c3e9d5 | 2023-08-09 12:12:32 -0700 | [diff] [blame^] | 38 | BazelModuleContext moduleContext = BazelModuleContext.ofInnermostBzlOrThrow(thread); |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 39 | return new LabelConverter(moduleContext.packageContext()); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 42 | private final Label.PackageContext packageContext; |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 43 | private final Map<String, Label> labelCache = new HashMap<>(); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 44 | |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 45 | public LabelConverter(Label.PackageContext packageContext) { |
| 46 | this.packageContext = packageContext; |
| 47 | } |
| 48 | |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 49 | /** Creates a label converter using the given base package and repo mapping. */ |
| 50 | public LabelConverter(PackageIdentifier base, RepositoryMapping repositoryMapping) { |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 51 | this(Label.PackageContext.of(base, repositoryMapping)); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Googler | 67f22fa | 2022-06-13 11:26:45 -0700 | [diff] [blame] | 54 | /** Returns the base package identifier that relative labels will be resolved against. */ |
| 55 | PackageIdentifier getBasePackage() { |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 56 | return packageContext.packageIdentifier(); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** Returns the Label corresponding to the input, using the current conversion context. */ |
| 60 | public Label convert(String input) throws LabelSyntaxException { |
| 61 | // Optimization: First check the package-local map, avoiding Label validation, Label |
| 62 | // construction, and global Interner lookup. This approach tends to be very profitable |
| 63 | // overall, since it's common for the targets in a single package to have duplicate |
| 64 | // label-strings across all their attribute values. |
| 65 | Label converted = labelCache.get(input); |
| 66 | if (converted == null) { |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 67 | converted = Label.parseWithPackageContext(input, packageContext); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 68 | labelCache.put(input, converted); |
| 69 | } |
| 70 | return converted; |
| 71 | } |
| 72 | |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 73 | @Override |
| 74 | public String toString() { |
Googler | 98853a7 | 2022-07-01 04:23:57 -0700 | [diff] [blame] | 75 | return getBasePackage().toString(); |
John Cater | f0cadba | 2022-02-17 15:02:08 -0800 | [diff] [blame] | 76 | } |
| 77 | } |