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 | |
Lukacs Berki | e19ee27 | 2015-12-10 11:34:29 +0000 | [diff] [blame] | 16 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.util.Preconditions; |
Ulf Adams | ef7e045 | 2015-12-21 09:26:43 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.vfs.Path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 21 | import com.google.devtools.build.skyframe.SkyFunction; |
| 22 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import java.util.concurrent.atomic.AtomicReference; |
| 24 | |
| 25 | /** Common utilities for dealing with files outside the package roots. */ |
Nathan Harmata | 029de3d | 2015-07-27 18:08:09 +0000 | [diff] [blame] | 26 | public class ExternalFilesHelper { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | private final AtomicReference<PathPackageLocator> pkgLocator; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 28 | private final ExternalFileAction externalFileAction; |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 29 | |
Janak Ramakrishnan | 8343106 | 2015-12-08 18:42:16 +0000 | [diff] [blame] | 30 | // This variable is set to true from multiple threads, but only read once, in the main thread. |
| 31 | // So volatility or an AtomicBoolean is not needed. |
| 32 | private boolean externalFileSeen = false; |
| 33 | |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 34 | /** |
| 35 | * @param pkgLocator an {@link AtomicReference} to a {@link PathPackageLocator} used to |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 36 | * determine what files are internal. |
| 37 | * @param errorOnExternalFiles If files outside of package paths should be allowed. |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 38 | */ |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 39 | public ExternalFilesHelper( |
| 40 | AtomicReference<PathPackageLocator> pkgLocator, boolean errorOnExternalFiles) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | this.pkgLocator = pkgLocator; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 42 | if (errorOnExternalFiles) { |
| 43 | this.externalFileAction = ExternalFileAction.ERROR_OUT; |
| 44 | } else { |
| 45 | this.externalFileAction = ExternalFileAction.DEPEND_ON_EXTERNAL_PKG; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private enum ExternalFileAction { |
| 50 | // Re-check the files when the WORKSPACE file changes. |
| 51 | DEPEND_ON_EXTERNAL_PKG, |
| 52 | |
| 53 | // Throw an exception if there is an external file. |
| 54 | ERROR_OUT, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Janak Ramakrishnan | 8343106 | 2015-12-08 18:42:16 +0000 | [diff] [blame] | 57 | boolean isExternalFileSeen() { |
| 58 | return externalFileSeen; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Janak Ramakrishnan | 8343106 | 2015-12-08 18:42:16 +0000 | [diff] [blame] | 61 | static boolean isInternal(RootedPath rootedPath, PathPackageLocator packageLocator) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | // TODO(bazel-team): This is inefficient when there are a lot of package roots or there are a |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 63 | // lot of external directories. Consider either explicitly preventing this case or using a more |
| 64 | // efficient approach here (e.g. use a trie for determining if a file is under an external |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | // directory). |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 66 | return packageLocator.getPathEntries().contains(rootedPath.getRoot()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 69 | /** |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 70 | * If this instance is configured with DEPEND_ON_EXTERNAL_PKG and rootedPath is a file that isn't |
| 71 | * under a package root then this adds a dependency on the //external package. If the action is |
| 72 | * ERROR_OUT, it will throw an error instead. |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 73 | */ |
| 74 | public void maybeHandleExternalFile(RootedPath rootedPath, SkyFunction.Environment env) |
| 75 | throws FileOutsidePackageRootsException { |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 76 | if (isInternal(rootedPath, pkgLocator.get())) { |
| 77 | return; |
| 78 | } |
| 79 | |
Janak Ramakrishnan | 8343106 | 2015-12-08 18:42:16 +0000 | [diff] [blame] | 80 | externalFileSeen = true; |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 81 | if (externalFileAction == ExternalFileAction.ERROR_OUT) { |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 82 | throw new FileOutsidePackageRootsException(rootedPath); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | } |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 84 | |
Ulf Adams | ef7e045 | 2015-12-21 09:26:43 +0000 | [diff] [blame] | 85 | // The outputBase may be null if we're not actually running a build. |
| 86 | Path outputBase = pkgLocator.get().getOutputBase(); |
| 87 | if (outputBase != null && !rootedPath.asPath().startsWith( |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 88 | pkgLocator.get().getOutputBase().getRelative(Label.EXTERNAL_PATH_PREFIX))) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // For files that are under $OUTPUT_BASE/external, add a dependency on the //external package |
| 93 | // so that if the WORKSPACE file changes, the File/DirectoryStateValue will be re-evaluated. |
| 94 | // |
| 95 | // Note that: |
| 96 | // - We don't add a dependency on the parent directory at the package root boundary, so |
| 97 | // the only transitive dependencies from files inside the package roots to external files |
| 98 | // are through symlinks. So the upwards transitive closure of external files is small. |
| 99 | // - The only way other than external repositories for external source files to get into the |
| 100 | // skyframe graph in the first place is through symlinks outside the package roots, which we |
| 101 | // neither want to encourage nor optimize for since it is not common. So the set of external |
| 102 | // files is small. |
| 103 | PackageValue pkgValue = (PackageValue) env.getValue(PackageValue.key( |
| 104 | Label.EXTERNAL_PACKAGE_IDENTIFIER)); |
| 105 | if (pkgValue == null) { |
| 106 | return; |
| 107 | } |
| 108 | Preconditions.checkState(!pkgValue.getPackage().containsErrors()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 109 | } |
| 110 | } |