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; |
| 26 | |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 27 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import java.util.concurrent.atomic.AtomicReference; |
| 29 | |
| 30 | /** Common utilities for dealing with files outside the package roots. */ |
Nathan Harmata | 029de3d | 2015-07-27 18:08:09 +0000 | [diff] [blame] | 31 | public class ExternalFilesHelper { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | private final AtomicReference<PathPackageLocator> pkgLocator; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 33 | private final ExternalFileAction externalFileAction; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 34 | private final BlazeDirectories directories; |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 35 | |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 36 | // 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] | 37 | // So volatility or an AtomicBoolean is not needed. |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 38 | private boolean anyOutputFilesSeen = false; |
| 39 | private boolean anyNonOutputExternalFilesSeen = false; |
Janak Ramakrishnan | 8343106 | 2015-12-08 18:42:16 +0000 | [diff] [blame] | 40 | |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 41 | /** |
| 42 | * @param pkgLocator an {@link AtomicReference} to a {@link PathPackageLocator} used to |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 43 | * determine what files are internal. |
| 44 | * @param errorOnExternalFiles If files outside of package paths should be allowed. |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 45 | */ |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 46 | public ExternalFilesHelper( |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 47 | AtomicReference<PathPackageLocator> pkgLocator, boolean errorOnExternalFiles, |
| 48 | BlazeDirectories directories) { |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 49 | this( |
| 50 | pkgLocator, |
| 51 | errorOnExternalFiles |
| 52 | ? ExternalFileAction.ERROR_OUT |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 53 | : ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_FILES, |
| 54 | directories); |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private ExternalFilesHelper(AtomicReference<PathPackageLocator> pkgLocator, |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 58 | ExternalFileAction externalFileAction, |
| 59 | BlazeDirectories directories) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | this.pkgLocator = pkgLocator; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 61 | this.externalFileAction = externalFileAction; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 62 | this.directories = directories; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private enum ExternalFileAction { |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 66 | /** Re-check the files when the WORKSPACE file changes. */ |
| 67 | DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_FILES, |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 68 | |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 69 | /** Throw an exception if there is an external file. */ |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 70 | ERROR_OUT, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 73 | enum FileType { |
| 74 | /** A file inside the package roots or in an external repository. */ |
| 75 | INTERNAL, |
| 76 | |
| 77 | /** A file outside the package roots about which we may make no other assumptions. */ |
| 78 | EXTERNAL_MUTABLE, |
| 79 | |
| 80 | /** |
| 81 | * A file in Bazel's output tree that's a proper output of an action (*not* a source file in an |
| 82 | * external repository). Such files are theoretically mutable, but certain Blaze flags may tell |
| 83 | * Blaze to assume these files are immutable. |
| 84 | * |
| 85 | * Note that {@link ExternalFilesHelper#maybeHandleExternalFile} is only used for |
| 86 | * {@link FileStateValue} and {@link DirectoryStateValue}, and also note that output files do |
| 87 | * not normally have corresponding {@link FileValue} instances (and thus also |
| 88 | * {@link FileStateValue} instances) in the Skyframe graph ({@link ArtifactFunction} only uses |
| 89 | * {@link FileValue}s for source files). But {@link FileStateValue}s for output files can still |
| 90 | * make their way into the Skyframe graph if e.g. a source file is a symlink to an output file. |
| 91 | */ |
| 92 | // TODO(nharmata): Consider an alternative design where we have an OutputFileDiffAwareness. This |
| 93 | // could work but would first require that we clean up all RootedPath usage. |
| 94 | OUTPUT, |
| 95 | |
| 96 | /** |
| 97 | * A file in the part of Bazel's output tree that contains (/ symlinks to) to external |
| 98 | * repositories. |
| 99 | */ |
| 100 | EXTERNAL_REPO, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 103 | static class ExternalFilesKnowledge { |
| 104 | final boolean anyOutputFilesSeen; |
| 105 | final boolean anyNonOutputExternalFilesSeen; |
| 106 | |
| 107 | private ExternalFilesKnowledge(boolean anyOutputFilesSeen, |
| 108 | boolean anyNonOutputExternalFilesSeen) { |
| 109 | this.anyOutputFilesSeen = anyOutputFilesSeen; |
| 110 | this.anyNonOutputExternalFilesSeen = anyNonOutputExternalFilesSeen; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @ThreadCompatible |
| 115 | ExternalFilesKnowledge getExternalFilesKnowledge() { |
| 116 | return new ExternalFilesKnowledge(anyOutputFilesSeen, anyNonOutputExternalFilesSeen); |
| 117 | } |
| 118 | |
| 119 | @ThreadCompatible |
| 120 | void setExternalFilesKnowledge(ExternalFilesKnowledge externalFilesKnowledge) { |
| 121 | anyOutputFilesSeen = externalFilesKnowledge.anyOutputFilesSeen; |
| 122 | anyNonOutputExternalFilesSeen = externalFilesKnowledge.anyNonOutputExternalFilesSeen; |
| 123 | } |
| 124 | |
| 125 | ExternalFilesHelper cloneWithFreshExternalFilesKnowledge() { |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 126 | return new ExternalFilesHelper(pkgLocator, externalFileAction, directories); |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | FileType getAndNoteFileType(RootedPath rootedPath) { |
| 130 | PathPackageLocator packageLocator = pkgLocator.get(); |
| 131 | if (packageLocator.getPathEntries().contains(rootedPath.getRoot())) { |
| 132 | return FileType.INTERNAL; |
| 133 | } |
| 134 | // The outputBase may be null if we're not actually running a build. |
| 135 | Path outputBase = packageLocator.getOutputBase(); |
| 136 | if (outputBase == null) { |
| 137 | anyNonOutputExternalFilesSeen = true; |
| 138 | return FileType.EXTERNAL_MUTABLE; |
| 139 | } |
| 140 | if (rootedPath.asPath().startsWith(outputBase)) { |
| 141 | Path externalRepoDir = outputBase.getRelative(Label.EXTERNAL_PATH_PREFIX); |
| 142 | if (rootedPath.asPath().startsWith(externalRepoDir)) { |
| 143 | anyNonOutputExternalFilesSeen = true; |
| 144 | return FileType.EXTERNAL_REPO; |
| 145 | } else { |
| 146 | anyOutputFilesSeen = true; |
| 147 | return FileType.OUTPUT; |
| 148 | } |
| 149 | } |
| 150 | anyNonOutputExternalFilesSeen = true; |
| 151 | return FileType.EXTERNAL_MUTABLE; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 154 | /** |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 155 | * If this instance is configured with DEPEND_ON_EXTERNAL_PKG and rootedPath is a file that isn't |
| 156 | * under a package root then this adds a dependency on the //external package. If the action is |
| 157 | * ERROR_OUT, it will throw an error instead. |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 158 | */ |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 159 | @ThreadSafe |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 160 | public void maybeHandleExternalFile(RootedPath rootedPath, SkyFunction.Environment env) |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 161 | throws IOException { |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 162 | FileType fileType = getAndNoteFileType(rootedPath); |
| 163 | if (fileType == FileType.INTERNAL) { |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 164 | return; |
| 165 | } |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 166 | if (fileType == FileType.OUTPUT || fileType == FileType.EXTERNAL_MUTABLE) { |
| 167 | if (externalFileAction == ExternalFileAction.ERROR_OUT) { |
| 168 | throw new FileOutsidePackageRootsException(rootedPath); |
| 169 | } |
Damien Martin-Guillerez | 4aa76cc | 2016-02-16 08:28:30 +0000 | [diff] [blame] | 170 | return; |
| 171 | } |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 172 | Preconditions.checkState( |
| 173 | externalFileAction == ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_FILES, |
| 174 | externalFileAction); |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 175 | RepositoryFunction.addExternalFilesDependencies(rootedPath, directories, env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 176 | } |
| 177 | } |