Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 16 | import com.google.common.base.Suppliers; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
| 18 | import com.google.common.collect.ImmutableMap; |
Rumou Duan | 7387620 | 2016-06-06 18:52:08 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame^] | 20 | import com.google.devtools.build.lib.actions.ActionKeyContext; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ActionLookupValue.ActionLookupKey; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 23 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction; |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy; |
Luis Fernando Pino Duque | be10218 | 2016-05-23 14:03:55 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.testutil.TestConstants; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.testutil.TestRuleClassProvider; |
| 29 | import com.google.devtools.build.lib.testutil.TestUtils; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
| 31 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 32 | import com.google.devtools.build.lib.vfs.Path; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 34 | import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator; |
janakr | bfdad90 | 2017-05-03 21:38:28 +0200 | [diff] [blame] | 35 | import com.google.devtools.build.skyframe.LegacySkyKey; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.skyframe.MemoizingEvaluator; |
| 37 | import com.google.devtools.build.skyframe.RecordingDifferencer; |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 38 | import com.google.devtools.build.skyframe.SequencedRecordingDifferencer; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.skyframe.SequentialBuildDriver; |
| 40 | import com.google.devtools.build.skyframe.SkyFunction; |
| 41 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 42 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 43 | import com.google.devtools.build.skyframe.SkyKey; |
| 44 | import com.google.devtools.build.skyframe.SkyValue; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 45 | import java.io.IOException; |
| 46 | import java.util.HashSet; |
| 47 | import java.util.Set; |
| 48 | import java.util.UUID; |
| 49 | import java.util.concurrent.atomic.AtomicReference; |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 50 | import org.junit.Before; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 51 | |
| 52 | abstract class ArtifactFunctionTestCase { |
Janak Ramakrishnan | 5cb6554 | 2017-03-20 20:37:09 +0000 | [diff] [blame] | 53 | static final ActionLookupKey ALL_OWNER = new SingletonActionLookupKey(); |
janakr | bfdad90 | 2017-05-03 21:38:28 +0200 | [diff] [blame] | 54 | static final SkyKey OWNER_KEY = LegacySkyKey.create(SkyFunctions.ACTION_LOOKUP, ALL_OWNER); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 55 | |
Rumou Duan | 7387620 | 2016-06-06 18:52:08 +0000 | [diff] [blame] | 56 | protected Set<ActionAnalysisMetadata> actions; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 57 | protected boolean fastDigest = false; |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 58 | protected RecordingDifferencer differencer = new SequencedRecordingDifferencer(); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 59 | protected SequentialBuildDriver driver; |
| 60 | protected MemoizingEvaluator evaluator; |
| 61 | protected Path root; |
Janak Ramakrishnan | a5578af | 2017-03-21 17:28:39 +0000 | [diff] [blame] | 62 | protected Path middlemanPath; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame^] | 63 | protected final ActionKeyContext actionKeyContext = new ActionKeyContext(); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * The test action execution function. The Skyframe evaluator's action execution function |
| 67 | * delegates to this one. |
| 68 | */ |
| 69 | protected SkyFunction delegateActionExecutionFunction; |
| 70 | |
| 71 | @Before |
| 72 | public void baseSetUp() throws Exception { |
| 73 | setupRoot(new CustomInMemoryFs()); |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 74 | AtomicReference<PathPackageLocator> pkgLocator = |
| 75 | new AtomicReference<>( |
| 76 | new PathPackageLocator( |
| 77 | root.getFileSystem().getPath("/outputbase"), |
| 78 | ImmutableList.of(root), |
| 79 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY)); |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 80 | BlazeDirectories directories = |
| 81 | new BlazeDirectories(new ServerDirectories(root, root), root, TestConstants.PRODUCT_NAME); |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 82 | ExternalFilesHelper externalFilesHelper = new ExternalFilesHelper( |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 83 | pkgLocator, |
| 84 | ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, |
| 85 | directories); |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 86 | differencer = new SequencedRecordingDifferencer(); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 87 | evaluator = |
| 88 | new InMemoryMemoizingEvaluator( |
| 89 | ImmutableMap.<SkyFunctionName, SkyFunction>builder() |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 90 | .put( |
| 91 | SkyFunctions.FILE_STATE, |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 92 | new FileStateFunction( |
| 93 | new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper)) |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 94 | .put(SkyFunctions.FILE, new FileFunction(pkgLocator)) |
Googler | 17810ad | 2017-11-29 10:54:23 -0800 | [diff] [blame] | 95 | .put(SkyFunctions.ARTIFACT, new ArtifactFunction()) |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 96 | .put(SkyFunctions.ACTION_EXECUTION, new SimpleActionExecutionFunction()) |
| 97 | .put( |
| 98 | SkyFunctions.PACKAGE, |
| 99 | new PackageFunction(null, null, null, null, null, null, null)) |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 100 | .put( |
| 101 | SkyFunctions.PACKAGE_LOOKUP, |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 102 | new PackageLookupFunction( |
| 103 | null, |
| 104 | CrossRepositoryLabelViolationStrategy.ERROR, |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 105 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY)) |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 106 | .put( |
| 107 | SkyFunctions.WORKSPACE_AST, |
| 108 | new WorkspaceASTFunction(TestRuleClassProvider.getRuleClassProvider())) |
| 109 | .put( |
| 110 | SkyFunctions.WORKSPACE_FILE, |
| 111 | new WorkspaceFileFunction( |
| 112 | TestRuleClassProvider.getRuleClassProvider(), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 113 | TestConstants.PACKAGE_FACTORY_BUILDER_FACTORY_FOR_TESTING |
| 114 | .builder() |
| 115 | .build( |
| 116 | TestRuleClassProvider.getRuleClassProvider(), root.getFileSystem()), |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 117 | directories)) |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 118 | .put(SkyFunctions.EXTERNAL_PACKAGE, new ExternalPackageFunction()) |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 119 | .put( |
| 120 | SkyFunctions.ACTION_TEMPLATE_EXPANSION, |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame^] | 121 | new ActionTemplateExpansionFunction( |
| 122 | actionKeyContext, Suppliers.ofInstance(false))) |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 123 | .build(), |
| 124 | differencer); |
| 125 | driver = new SequentialBuildDriver(evaluator); |
| 126 | PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID()); |
| 127 | PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get()); |
| 128 | actions = new HashSet<>(); |
| 129 | } |
| 130 | |
| 131 | protected void setupRoot(CustomInMemoryFs fs) throws IOException { |
Janak Ramakrishnan | a5578af | 2017-03-21 17:28:39 +0000 | [diff] [blame] | 132 | Path tmpDir = fs.getPath(TestUtils.tmpDir()); |
| 133 | root = tmpDir.getChild("root"); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 134 | FileSystemUtils.createDirectoryAndParents(root); |
| 135 | FileSystemUtils.createEmptyFile(root.getRelative("WORKSPACE")); |
Janak Ramakrishnan | a5578af | 2017-03-21 17:28:39 +0000 | [diff] [blame] | 136 | middlemanPath = tmpDir.getChild("middlemanRoot"); |
| 137 | FileSystemUtils.createDirectoryAndParents(middlemanPath); |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | protected static void writeFile(Path path, String contents) throws IOException { |
| 141 | FileSystemUtils.createDirectoryAndParents(path.getParentDirectory()); |
| 142 | FileSystemUtils.writeContentAsLatin1(path, contents); |
| 143 | } |
| 144 | |
| 145 | /** ActionExecutionFunction that delegates to our delegate. */ |
| 146 | private class SimpleActionExecutionFunction implements SkyFunction { |
| 147 | @Override |
| 148 | public SkyValue compute(SkyKey skyKey, Environment env) |
| 149 | throws SkyFunctionException, InterruptedException { |
| 150 | return delegateActionExecutionFunction.compute(skyKey, env); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public String extractTag(SkyKey skyKey) { |
| 155 | return delegateActionExecutionFunction.extractTag(skyKey); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private static class SingletonActionLookupKey extends ActionLookupKey { |
| 160 | @Override |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 161 | protected SkyKey getSkyKeyInternal() { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 162 | return OWNER_KEY; |
| 163 | } |
| 164 | |
| 165 | @Override |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 166 | protected SkyFunctionName getType() { |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 167 | throw new UnsupportedOperationException(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** InMemoryFileSystem that can pretend to do a fast digest. */ |
| 172 | protected class CustomInMemoryFs extends InMemoryFileSystem { |
| 173 | @Override |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 174 | protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException { |
| 175 | return fastDigest ? getDigest(path, hashFunction) : null; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |