|
| 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 | + |
| 16 | +package com.google.devtools.build.lib.bazel.bzlmod; |
| 17 | + |
| 18 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 19 | +import static com.google.common.collect.ImmutableMap.toImmutableMap; |
| 20 | + |
| 21 | +import com.google.common.collect.BiMap; |
| 22 | +import com.google.common.collect.HashBiMap; |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import com.google.common.collect.ImmutableTable; |
| 25 | +import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleResolutionFunction.BazelModuleResolutionFunctionException; |
| 26 | +import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
| 27 | +import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 28 | +import com.google.devtools.build.lib.cmdline.RepositoryName; |
| 29 | +import com.google.devtools.build.lib.packages.LabelConverter; |
| 30 | +import com.google.devtools.build.lib.server.FailureDetails.ExternalDeps.Code; |
| 31 | +import com.google.devtools.build.lib.vfs.PathFragment; |
| 32 | +import com.google.devtools.build.skyframe.SkyFunction; |
| 33 | +import com.google.devtools.build.skyframe.SkyFunctionException; |
| 34 | +import com.google.devtools.build.skyframe.SkyFunctionException.Transience; |
| 35 | +import com.google.devtools.build.skyframe.SkyKey; |
| 36 | +import com.google.devtools.build.skyframe.SkyValue; |
| 37 | +import javax.annotation.Nullable; |
| 38 | + |
| 39 | +/** |
| 40 | + * This function runs Bazel module resolution, extracts the dependency graph from it and creates a |
| 41 | + * value containing all Bazel modules, along with a few lookup maps that help with further usage. By |
| 42 | + * this stage, module extensions are not evaluated yet. |
| 43 | + */ |
| 44 | +public class BazelDepGraphFunction implements SkyFunction { |
| 45 | + |
| 46 | + @Override |
| 47 | + @Nullable |
| 48 | + public SkyValue compute(SkyKey skyKey, Environment env) |
| 49 | + throws SkyFunctionException, InterruptedException { |
| 50 | + |
| 51 | + BazelModuleResolutionValue selectionResult = |
| 52 | + (BazelModuleResolutionValue) env.getValue(BazelModuleResolutionValue.KEY); |
| 53 | + if (env.valuesMissing()) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + ImmutableMap<ModuleKey, Module> depGraph = selectionResult.getResolvedDepGraph(); |
| 58 | + ImmutableMap<RepositoryName, ModuleKey> canonicalRepoNameLookup = |
| 59 | + depGraph.keySet().stream() |
| 60 | + .collect(toImmutableMap(ModuleKey::getCanonicalRepoName, key -> key)); |
| 61 | + |
| 62 | + // For each extension usage, we resolve (i.e. canonicalize) its bzl file label. Then we can |
| 63 | + // group all usages by the label + name (the ModuleExtensionId). |
| 64 | + ImmutableTable.Builder<ModuleExtensionId, ModuleKey, ModuleExtensionUsage> |
| 65 | + extensionUsagesTableBuilder = ImmutableTable.builder(); |
| 66 | + for (Module module : depGraph.values()) { |
| 67 | + LabelConverter labelConverter = |
| 68 | + new LabelConverter( |
| 69 | + PackageIdentifier.create(module.getCanonicalRepoName(), PathFragment.EMPTY_FRAGMENT), |
| 70 | + module.getRepoMappingWithBazelDepsOnly()); |
| 71 | + for (ModuleExtensionUsage usage : module.getExtensionUsages()) { |
| 72 | + try { |
| 73 | + ModuleExtensionId moduleExtensionId = |
| 74 | + ModuleExtensionId.create( |
| 75 | + labelConverter.convert(usage.getExtensionBzlFile()), usage.getExtensionName()); |
| 76 | + extensionUsagesTableBuilder.put(moduleExtensionId, module.getKey(), usage); |
| 77 | + } catch (LabelSyntaxException e) { |
| 78 | + throw new BazelModuleResolutionFunctionException( |
| 79 | + ExternalDepsException.withCauseAndMessage( |
| 80 | + Code.BAD_MODULE, |
| 81 | + e, |
| 82 | + "invalid label for module extension found at %s", |
| 83 | + usage.getLocation()), |
| 84 | + Transience.PERSISTENT); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + ImmutableTable<ModuleExtensionId, ModuleKey, ModuleExtensionUsage> extensionUsagesById = |
| 89 | + extensionUsagesTableBuilder.buildOrThrow(); |
| 90 | + |
| 91 | + // Calculate a unique name for each used extension id. |
| 92 | + BiMap<String, ModuleExtensionId> extensionUniqueNames = HashBiMap.create(); |
| 93 | + for (ModuleExtensionId id : extensionUsagesById.rowKeySet()) { |
| 94 | + // Ensure that the resulting extension name (and thus the repository names derived from it) do |
| 95 | + // not start with a tilde. |
| 96 | + RepositoryName repository = id.getBzlFileLabel().getRepository(); |
| 97 | + String nonEmptyRepoPart; |
| 98 | + if (repository.isMain()) { |
| 99 | + nonEmptyRepoPart = "_main"; |
| 100 | + } else { |
| 101 | + nonEmptyRepoPart = repository.getName(); |
| 102 | + } |
| 103 | + String bestName = nonEmptyRepoPart + "~" + id.getExtensionName(); |
| 104 | + if (extensionUniqueNames.putIfAbsent(bestName, id) == null) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + int suffix = 2; |
| 108 | + while (extensionUniqueNames.putIfAbsent(bestName + suffix, id) != null) { |
| 109 | + suffix++; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return BazelDepGraphValue.create( |
| 114 | + depGraph, |
| 115 | + canonicalRepoNameLookup, |
| 116 | + depGraph.values().stream().map(AbridgedModule::from).collect(toImmutableList()), |
| 117 | + extensionUsagesById, |
| 118 | + ImmutableMap.copyOf(extensionUniqueNames.inverse())); |
| 119 | + } |
| 120 | +} |
0 commit comments