blob: 935f137397f75f457612f0414b3fd7c049d85d1b [file] [log] [blame]
John Caterf0cadba2022-02-17 15:02:08 -08001// 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
15package com.google.devtools.build.lib.packages;
16
wyv4858cbf2022-05-04 13:56:45 -070017import com.google.devtools.build.lib.cmdline.BazelModuleContext;
John Caterf0cadba2022-02-17 15:02:08 -080018import com.google.devtools.build.lib.cmdline.Label;
19import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
Googler67f22fa2022-06-13 11:26:45 -070020import com.google.devtools.build.lib.cmdline.PackageIdentifier;
John Caterf0cadba2022-02-17 15:02:08 -080021import com.google.devtools.build.lib.cmdline.RepositoryMapping;
22import java.util.HashMap;
23import java.util.Map;
John Caterf0cadba2022-02-17 15:02:08 -080024import net.starlark.java.eval.StarlarkThread;
25
Googler67f22fa2022-06-13 11:26:45 -070026/**
27 * Converts a label literal string into a {@link Label} object, using the appropriate base package
28 * and repo mapping.
29 */
John Caterf0cadba2022-02-17 15:02:08 -080030public class LabelConverter {
31
Googler67f22fa2022-06-13 11:26:45 -070032 /**
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) {
Googler6c3e9d52023-08-09 12:12:32 -070038 BazelModuleContext moduleContext = BazelModuleContext.ofInnermostBzlOrThrow(thread);
Googler98853a72022-07-01 04:23:57 -070039 return new LabelConverter(moduleContext.packageContext());
John Caterf0cadba2022-02-17 15:02:08 -080040 }
41
Googler98853a72022-07-01 04:23:57 -070042 private final Label.PackageContext packageContext;
Googler67f22fa2022-06-13 11:26:45 -070043 private final Map<String, Label> labelCache = new HashMap<>();
John Caterf0cadba2022-02-17 15:02:08 -080044
Googler98853a72022-07-01 04:23:57 -070045 public LabelConverter(Label.PackageContext packageContext) {
46 this.packageContext = packageContext;
47 }
48
Googler67f22fa2022-06-13 11:26:45 -070049 /** Creates a label converter using the given base package and repo mapping. */
50 public LabelConverter(PackageIdentifier base, RepositoryMapping repositoryMapping) {
Googler98853a72022-07-01 04:23:57 -070051 this(Label.PackageContext.of(base, repositoryMapping));
John Caterf0cadba2022-02-17 15:02:08 -080052 }
53
Googler67f22fa2022-06-13 11:26:45 -070054 /** Returns the base package identifier that relative labels will be resolved against. */
55 PackageIdentifier getBasePackage() {
Googler98853a72022-07-01 04:23:57 -070056 return packageContext.packageIdentifier();
John Caterf0cadba2022-02-17 15:02:08 -080057 }
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) {
Googler98853a72022-07-01 04:23:57 -070067 converted = Label.parseWithPackageContext(input, packageContext);
John Caterf0cadba2022-02-17 15:02:08 -080068 labelCache.put(input, converted);
69 }
70 return converted;
71 }
72
John Caterf0cadba2022-02-17 15:02:08 -080073 @Override
74 public String toString() {
Googler98853a72022-07-01 04:23:57 -070075 return getBasePackage().toString();
John Caterf0cadba2022-02-17 15:02:08 -080076 }
77}