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 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 17 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.syntax.BuildFileAST; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.syntax.Mutability; |
| 21 | import com.google.devtools.build.lib.syntax.Runtime; |
| 22 | import com.google.devtools.build.lib.syntax.ValidationEnvironment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.Path; |
| 24 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 25 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 26 | import com.google.devtools.build.skyframe.SkyFunction; |
| 27 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 28 | import com.google.devtools.build.skyframe.SkyFunctionException.Transience; |
| 29 | import com.google.devtools.build.skyframe.SkyKey; |
| 30 | import com.google.devtools.build.skyframe.SkyValue; |
| 31 | |
| 32 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | |
| 34 | import javax.annotation.Nullable; |
| 35 | |
| 36 | /** |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 37 | * A SkyFunction for {@link ASTFileLookupValue}s. |
| 38 | * |
| 39 | * <p> Given a {@link Label} referencing a Skylark file, loads it as a syntax tree |
| 40 | * ({@link BuildFileAST}). The Label must be absolute, and must not reference the special |
| 41 | * {@code external} package. If the file (or the package containing it) doesn't exist, the |
| 42 | * function doesn't fail, but instead returns a specific {@code NO_FILE} {@link ASTFileLookupValue}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | */ |
| 44 | public class ASTFileLookupFunction implements SkyFunction { |
| 45 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | private final RuleClassProvider ruleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 48 | public ASTFileLookupFunction(RuleClassProvider ruleClassProvider) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | this.ruleClassProvider = ruleClassProvider; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, |
| 54 | InterruptedException { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 55 | Label fileLabel = (Label) skyKey.argument(); |
| 56 | PathFragment filePathFragment = fileLabel.toPathFragment(); |
| 57 | |
| 58 | // |
| 59 | // Determine whether the package designated by fileLabel exists. |
| 60 | // |
| 61 | SkyKey pkgSkyKey = PackageLookupValue.key(fileLabel.getPackageIdentifier()); |
| 62 | PackageLookupValue pkgLookupValue = null; |
| 63 | pkgLookupValue = (PackageLookupValue) env.getValue(pkgSkyKey); |
| 64 | if (pkgLookupValue == null) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | return null; |
| 66 | } |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 67 | if (!pkgLookupValue.packageExists()) { |
| 68 | return ASTFileLookupValue.forBadPackage(fileLabel, pkgLookupValue.getErrorMsg()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 69 | } |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 70 | |
| 71 | // |
| 72 | // Determine whether the file designated by fileLabel exists. |
| 73 | // |
| 74 | Path packageRoot = pkgLookupValue.getRoot(); |
| 75 | RootedPath rootedPath = RootedPath.toRootedPath(packageRoot, filePathFragment); |
| 76 | SkyKey fileSkyKey = FileValue.key(rootedPath); |
| 77 | FileValue fileValue = null; |
John Field | 925dc5c | 2015-09-21 18:59:19 +0000 | [diff] [blame] | 78 | try { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 79 | fileValue = (FileValue) env.getValueOrThrow(fileSkyKey, IOException.class, |
| 80 | FileSymlinkException.class, InconsistentFilesystemException.class); |
| 81 | } catch (IOException | FileSymlinkException e) { |
| 82 | throw new ASTLookupFunctionException(new ErrorReadingSkylarkExtensionException(e), |
| 83 | Transience.PERSISTENT); |
| 84 | } catch (InconsistentFilesystemException e) { |
| 85 | throw new ASTLookupFunctionException(e, Transience.PERSISTENT); |
| 86 | } |
| 87 | if (fileValue == null) { |
| 88 | return null; |
| 89 | } |
| 90 | if (!fileValue.isFile()) { |
| 91 | return ASTFileLookupValue.forBadFile(fileLabel); |
| 92 | } |
| 93 | |
| 94 | // |
| 95 | // Both the package and the file exist; load the file and parse it as an AST. |
| 96 | // |
| 97 | BuildFileAST ast = null; |
| 98 | Path path = rootedPath.asPath(); |
| 99 | // Skylark files end with bzl |
| 100 | boolean parseAsSkylark = filePathFragment.getPathString().endsWith(".bzl"); |
| 101 | try { |
| 102 | long astFileSize = fileValue.getSize(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 103 | if (parseAsSkylark) { |
| 104 | try (Mutability mutability = Mutability.create("validate")) { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 105 | ast = BuildFileAST.parseSkylarkFile(path, astFileSize, env.getListener(), |
Michajlo Matijkiw | 2a7c802 | 2015-09-22 02:22:12 +0000 | [diff] [blame] | 106 | new ValidationEnvironment( |
| 107 | ruleClassProvider.createSkylarkRuleClassEnvironment( |
| 108 | mutability, |
| 109 | env.getListener(), |
| 110 | // the two below don't matter for extracting the ValidationEnvironment: |
| 111 | /*astFileContentHashCode=*/null, |
| 112 | /*importMap=*/null) |
Lukacs Berki | 5d9b8a0 | 2015-10-21 12:36:56 +0000 | [diff] [blame] | 113 | .setupDynamic(Runtime.PKG_NAME, Runtime.NONE) |
| 114 | .setupDynamic(Runtime.REPOSITORY_NAME, Runtime.NONE))); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 115 | } |
| 116 | } else { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 117 | ast = BuildFileAST.parseBuildFile(path, astFileSize, env.getListener(), false); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 118 | } |
| 119 | } catch (IOException e) { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 120 | throw new ASTLookupFunctionException(new ErrorReadingSkylarkExtensionException(e), |
| 121 | Transience.TRANSIENT); |
Michajlo Matijkiw | 2a7c802 | 2015-09-22 02:22:12 +0000 | [diff] [blame] | 122 | } |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 123 | |
Michajlo Matijkiw | 2a7c802 | 2015-09-22 02:22:12 +0000 | [diff] [blame] | 124 | return ASTFileLookupValue.withFile(ast); |
| 125 | } |
| 126 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 127 | @Nullable |
| 128 | @Override |
| 129 | public String extractTag(SkyKey skyKey) { |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | private static final class ASTLookupFunctionException extends SkyFunctionException { |
| 134 | private ASTLookupFunctionException(ErrorReadingSkylarkExtensionException e, |
| 135 | Transience transience) { |
| 136 | super(e, transience); |
| 137 | } |
| 138 | |
| 139 | private ASTLookupFunctionException(InconsistentFilesystemException e, Transience transience) { |
| 140 | super(e, transience); |
| 141 | } |
| 142 | } |
| 143 | } |