|
| 1 | +// Copyright 2017 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.rules.repository; |
| 16 | + |
| 17 | +import com.google.auto.value.AutoValue; |
| 18 | +import com.google.devtools.build.lib.cmdline.Label; |
| 19 | +import com.google.devtools.build.lib.cmdline.RepositoryName; |
| 20 | +import com.google.devtools.build.lib.packages.Rule; |
| 21 | +import com.google.devtools.build.lib.skyframe.PackageLookupValue; |
| 22 | +import com.google.devtools.build.lib.skyframe.SkyFunctions; |
| 23 | +import com.google.devtools.build.lib.skyframe.WorkspaceFileValue; |
| 24 | +import com.google.devtools.build.lib.util.Preconditions; |
| 25 | +import com.google.devtools.build.lib.vfs.RootedPath; |
| 26 | +import com.google.devtools.build.skyframe.SkyFunction; |
| 27 | +import com.google.devtools.build.skyframe.SkyKey; |
| 28 | +import com.google.devtools.build.skyframe.SkyValue; |
| 29 | +import javax.annotation.Nullable; |
| 30 | + |
| 31 | +/** |
| 32 | + * Used for repositories that are declared in other repositories' WORKSPACE files. |
| 33 | + */ |
| 34 | +public class RepositoryVisibilityFunction implements SkyFunction { |
| 35 | + public static SkyKey key(RepositoryName parent, RepositoryName child) { |
| 36 | + return SkyKey.create(SkyFunctions.REPOSITORY_DEPENDENCY, Key.create(parent, child)); |
| 37 | + } |
| 38 | + |
| 39 | + @Nullable |
| 40 | + @Override |
| 41 | + public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException { |
| 42 | + Key key = (Key) skyKey.argument(); |
| 43 | + RepositoryDirectoryValue parentDir = (RepositoryDirectoryValue) env.getValue( |
| 44 | + RepositoryDirectoryValue.key(key.parent())); |
| 45 | + if (env.valuesMissing()) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + SkyKey parentWorkspaceKey; |
| 50 | + if (parentDir.repositoryExists()) { |
| 51 | + parentWorkspaceKey = WorkspaceFileValue.key( |
| 52 | + RootedPath.toRootedPath(parentDir.getPath(), Label.EXTERNAL_PACKAGE_FILE_NAME)); |
| 53 | + } else { |
| 54 | + // This is kind of hacky: the main repository won't exist under output_base/external, so RDF |
| 55 | + // won't find it above. However, we know that the parent repository has to exist, so we'll |
| 56 | + // just assume it's the main repository if RDF couldn't find it. |
| 57 | + PackageLookupValue packageLookupValue = (PackageLookupValue) env.getValue( |
| 58 | + PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER)); |
| 59 | + if (env.valuesMissing()) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + Preconditions.checkState(packageLookupValue.packageExists()); |
| 63 | + parentWorkspaceKey = WorkspaceFileValue.key( |
| 64 | + RootedPath.toRootedPath(packageLookupValue.getRoot(), Label.EXTERNAL_PACKAGE_FILE_NAME)); |
| 65 | + } |
| 66 | + |
| 67 | + // This just looks for the child repo name. It doesn't care if the name is used for a different |
| 68 | + // repository (either a different type or a different path/url/commit/whichever) than is |
| 69 | + // actually being used. For example, if someone has a copy of Boost on their system that |
| 70 | + // they're using but a library they're depending downloads Boost, we just want "everyone" to |
| 71 | + // use the local copy of Boost, not error out. This is the check that the library actually |
| 72 | + // declares a repository "dependency" on @boost. |
| 73 | + while (true) { |
| 74 | + WorkspaceFileValue parentWorkspace = (WorkspaceFileValue) env.getValue(parentWorkspaceKey); |
| 75 | + if (env.valuesMissing()) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + Rule child = parentWorkspace.getPackage().getRule(key.child().strippedName()); |
| 80 | + if (child == null) { |
| 81 | + if (parentWorkspace.hasNext()) { |
| 82 | + parentWorkspaceKey = parentWorkspace.next(); |
| 83 | + } else { |
| 84 | + break; |
| 85 | + } |
| 86 | + } else { |
| 87 | + return RepositoryVisibilityValue.OK; |
| 88 | + } |
| 89 | + } |
| 90 | + return RepositoryVisibilityValue.NOT_FOUND; |
| 91 | + } |
| 92 | + |
| 93 | + @Nullable |
| 94 | + @Override |
| 95 | + public String extractTag(SkyKey skyKey) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Represents a parent repository and a child repository that we expect to be defined in the |
| 101 | + * parent's WORKSPACE file. |
| 102 | + */ |
| 103 | + @AutoValue |
| 104 | + public abstract static class Key { |
| 105 | + public static Key create(RepositoryName parent, RepositoryName child) { |
| 106 | + return new AutoValue_RepositoryVisibilityFunction_Key(parent, child); |
| 107 | + } |
| 108 | + |
| 109 | + public abstract RepositoryName parent(); |
| 110 | + public abstract RepositoryName child(); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns if the repository definition was found or not. |
| 115 | + */ |
| 116 | + public static class RepositoryVisibilityValue implements SkyValue { |
| 117 | + private static RepositoryVisibilityValue OK = new RepositoryVisibilityValue(); |
| 118 | + private static RepositoryVisibilityValue NOT_FOUND = new RepositoryVisibilityValue(); |
| 119 | + |
| 120 | + private RepositoryVisibilityValue() { |
| 121 | + } |
| 122 | + |
| 123 | + public boolean ok() { |
| 124 | + return this == OK; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments