Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 16 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
Lukacs Berki | e19ee27 | 2015-12-10 11:34:29 +0000 | [diff] [blame] | 17 | import com.google.devtools.build.lib.cmdline.Label; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; |
| 19 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
Damien Martin-Guillerez | 847a418 | 2016-02-10 12:44:25 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.rules.repository.RepositoryFunction; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.util.Preconditions; |
Ulf Adams | ef7e045 | 2015-12-21 09:26:43 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.Path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 25 | import com.google.devtools.build.skyframe.SkyFunction; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 26 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import java.util.concurrent.atomic.AtomicReference; |
| 28 | |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 29 | /** Common utilities for dealing with paths outside the package roots. */ |
Nathan Harmata | 029de3d | 2015-07-27 18:08:09 +0000 | [diff] [blame] | 30 | public class ExternalFilesHelper { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | private final AtomicReference<PathPackageLocator> pkgLocator; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 32 | private final ExternalFileAction externalFileAction; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 33 | private final BlazeDirectories directories; |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 34 | |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 35 | // These variables are set to true from multiple threads, but only read in the main thread. |
Janak Ramakrishnan | 8343106 | 2015-12-08 18:42:16 +0000 | [diff] [blame] | 36 | // So volatility or an AtomicBoolean is not needed. |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 37 | private boolean anyOutputFilesSeen = false; |
| 38 | private boolean anyNonOutputExternalFilesSeen = false; |
Janak Ramakrishnan | 8343106 | 2015-12-08 18:42:16 +0000 | [diff] [blame] | 39 | |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 40 | public ExternalFilesHelper( |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 41 | AtomicReference<PathPackageLocator> pkgLocator, |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 42 | ExternalFileAction externalFileAction, |
| 43 | BlazeDirectories directories) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | this.pkgLocator = pkgLocator; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 45 | this.externalFileAction = externalFileAction; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 46 | this.directories = directories; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 49 | /** |
| 50 | * The action to take when an external path is encountered. See {@link FileType} for the |
| 51 | * definition of "external". |
| 52 | */ |
| 53 | public enum ExternalFileAction { |
| 54 | /** |
| 55 | * For paths of type {@link FileType#EXTERNAL_REPO}, introduce a Skyframe dependency on the |
| 56 | * 'external' package. |
| 57 | * |
| 58 | * <p>This is the default for Bazel, since it's required for correctness of the external |
| 59 | * repositories feature. |
| 60 | */ |
| 61 | DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, |
Nathan Harmata | 0c7a42a | 2016-10-13 18:17:48 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 64 | * For paths of type {@link FileType#EXTERNAL} or {@link FileType#OUTPUT}, assume the path does |
| 65 | * not exist and will never exist. |
| 66 | */ |
| 67 | ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS, |
| 68 | } |
| 69 | |
| 70 | /** Classification of a path encountered by Bazel. */ |
| 71 | public enum FileType { |
| 72 | /** A path inside the package roots or in an external repository. */ |
| 73 | INTERNAL, |
| 74 | |
| 75 | /** |
| 76 | * A non {@link #EXTERNAL_REPO} path outside the package roots about which we may make no other |
| 77 | * assumptions. |
| 78 | */ |
| 79 | EXTERNAL, |
| 80 | |
| 81 | /** |
| 82 | * A path in Bazel's output tree that's a proper output of an action (*not* a source file in an |
| 83 | * external repository). Such files are theoretically mutable, but certain Bazel flags may tell |
| 84 | * Bazel to assume these paths are immutable. |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 85 | * |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 86 | * <p>Note that {@link ExternalFilesHelper#maybeHandleExternalFile} is only used for {@link |
| 87 | * FileStateValue} and {@link DirectoryListingStateValue}, and also note that output files do |
| 88 | * not normally have corresponding {@link FileValue} instances (and thus also {@link |
| 89 | * FileStateValue} instances) in the Skyframe graph ({@link ArtifactFunction} only uses {@link |
| 90 | * FileValue}s for source files). But {@link FileStateValue}s for output files can still make |
| 91 | * their way into the Skyframe graph if e.g. a source file is a symlink to an output file. |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 92 | */ |
| 93 | // TODO(nharmata): Consider an alternative design where we have an OutputFileDiffAwareness. This |
| 94 | // could work but would first require that we clean up all RootedPath usage. |
| 95 | OUTPUT, |
| 96 | |
| 97 | /** |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 98 | * A path in the part of Bazel's output tree that contains (/ symlinks to) to external |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 99 | * repositories. |
| 100 | */ |
| 101 | EXTERNAL_REPO, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Thrown by {@link #maybeHandleExternalFile} when an applicable path is processed (see |
| 106 | * {@link ExternalFileAction#ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS}. |
| 107 | */ |
| 108 | static class NonexistentImmutableExternalFileException extends Exception { |
| 109 | } |
| 110 | |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 111 | static class ExternalFilesKnowledge { |
| 112 | final boolean anyOutputFilesSeen; |
| 113 | final boolean anyNonOutputExternalFilesSeen; |
| 114 | |
| 115 | private ExternalFilesKnowledge(boolean anyOutputFilesSeen, |
| 116 | boolean anyNonOutputExternalFilesSeen) { |
| 117 | this.anyOutputFilesSeen = anyOutputFilesSeen; |
| 118 | this.anyNonOutputExternalFilesSeen = anyNonOutputExternalFilesSeen; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @ThreadCompatible |
| 123 | ExternalFilesKnowledge getExternalFilesKnowledge() { |
| 124 | return new ExternalFilesKnowledge(anyOutputFilesSeen, anyNonOutputExternalFilesSeen); |
| 125 | } |
| 126 | |
| 127 | @ThreadCompatible |
| 128 | void setExternalFilesKnowledge(ExternalFilesKnowledge externalFilesKnowledge) { |
| 129 | anyOutputFilesSeen = externalFilesKnowledge.anyOutputFilesSeen; |
| 130 | anyNonOutputExternalFilesSeen = externalFilesKnowledge.anyNonOutputExternalFilesSeen; |
| 131 | } |
| 132 | |
| 133 | ExternalFilesHelper cloneWithFreshExternalFilesKnowledge() { |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 134 | return new ExternalFilesHelper(pkgLocator, externalFileAction, directories); |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | FileType getAndNoteFileType(RootedPath rootedPath) { |
| 138 | PathPackageLocator packageLocator = pkgLocator.get(); |
| 139 | if (packageLocator.getPathEntries().contains(rootedPath.getRoot())) { |
| 140 | return FileType.INTERNAL; |
| 141 | } |
| 142 | // The outputBase may be null if we're not actually running a build. |
| 143 | Path outputBase = packageLocator.getOutputBase(); |
| 144 | if (outputBase == null) { |
| 145 | anyNonOutputExternalFilesSeen = true; |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 146 | return FileType.EXTERNAL; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 147 | } |
| 148 | if (rootedPath.asPath().startsWith(outputBase)) { |
Kristina Chodorow | bd016c9 | 2016-09-09 14:10:44 +0000 | [diff] [blame] | 149 | Path externalRepoDir = outputBase.getRelative(Label.EXTERNAL_PACKAGE_NAME); |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 150 | if (rootedPath.asPath().startsWith(externalRepoDir)) { |
| 151 | anyNonOutputExternalFilesSeen = true; |
| 152 | return FileType.EXTERNAL_REPO; |
| 153 | } else { |
| 154 | anyOutputFilesSeen = true; |
| 155 | return FileType.OUTPUT; |
| 156 | } |
| 157 | } |
| 158 | anyNonOutputExternalFilesSeen = true; |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 159 | return FileType.EXTERNAL; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 162 | /** |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 163 | * If this instance is configured with |
| 164 | * {@link ExternalFileAction#DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS} and |
| 165 | * {@code rootedPath} isn't under a package root then this adds a dependency on the //external |
| 166 | * package. If the action is |
| 167 | * {@link ExternalFileAction#ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS}, it will throw |
| 168 | * a {@link NonexistentImmutableExternalFileException} instead. |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 169 | */ |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 170 | @ThreadSafe |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 171 | void maybeHandleExternalFile(RootedPath rootedPath, SkyFunction.Environment env) |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 172 | throws NonexistentImmutableExternalFileException, IOException, InterruptedException { |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 173 | FileType fileType = getAndNoteFileType(rootedPath); |
| 174 | if (fileType == FileType.INTERNAL) { |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 175 | return; |
| 176 | } |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 177 | if (fileType == FileType.OUTPUT || fileType == FileType.EXTERNAL) { |
| 178 | if (externalFileAction |
| 179 | == ExternalFileAction.ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS) { |
| 180 | throw new NonexistentImmutableExternalFileException(); |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 181 | } |
Damien Martin-Guillerez | 4aa76cc | 2016-02-16 08:28:30 +0000 | [diff] [blame] | 182 | return; |
| 183 | } |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 184 | Preconditions.checkState( |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 185 | externalFileAction == ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 186 | externalFileAction); |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 187 | RepositoryFunction.addExternalFilesDependencies(rootedPath, directories, env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 188 | } |
| 189 | } |