blob: 7e190be53e0766af4467692d05bab9279caa59f8 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.lib.skyframe;
15
Mark Schallerf6e32d62015-05-19 20:27:43 +000016import com.google.common.collect.ImmutableSet;
janakr5fb2a482018-03-02 17:48:57 -080017import com.google.common.collect.Interner;
Kristina Chodorowec5c07a2016-01-25 17:12:29 +000018import com.google.devtools.build.lib.cmdline.RepositoryName;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import com.google.devtools.build.lib.collect.nestedset.NestedSet;
Janak Ramakrishnanf7ff6162015-06-02 20:20:31 +000020import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
21import com.google.devtools.build.lib.collect.nestedset.Order;
janakr5fb2a482018-03-02 17:48:57 -080022import com.google.devtools.build.lib.concurrent.BlazeInterners;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
24import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
shahanbbbf3dc2018-02-23 15:41:52 -080025import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
Googler41797b62021-11-19 12:07:05 -080026import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
Mark Schallerf6e32d62015-05-19 20:27:43 +000027import com.google.devtools.build.lib.vfs.PathFragment;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028import com.google.devtools.build.lib.vfs.RootedPath;
janakr5fb2a482018-03-02 17:48:57 -080029import com.google.devtools.build.skyframe.SkyFunctionName;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030import com.google.devtools.build.skyframe.SkyValue;
Mark Schallerf6e32d62015-05-19 20:27:43 +000031
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010032/**
33 * This value represents the result of looking up all the packages under a given package path root,
34 * starting at a given directory.
35 */
36@Immutable
37@ThreadSafe
Michajlo Matijkiwd8b4be12015-09-15 23:04:17 +000038public class RecursivePkgValue implements SkyValue {
Googler41797b62021-11-19 12:07:05 -080039 @SerializationConstant
Janak Ramakrishnanf7ff6162015-06-02 20:20:31 +000040 static final RecursivePkgValue EMPTY =
adgar8c3b3fb2019-06-03 13:35:28 -070041 new RecursivePkgValue(NestedSetBuilder.<String>emptySet(Order.STABLE_ORDER), false);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010042
43 private final NestedSet<String> packages;
adgar8c3b3fb2019-06-03 13:35:28 -070044 private final boolean hasErrors;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045
adgar8c3b3fb2019-06-03 13:35:28 -070046 private RecursivePkgValue(NestedSet<String> packages, boolean hasErrors) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010047 this.packages = packages;
adgar8c3b3fb2019-06-03 13:35:28 -070048 this.hasErrors = hasErrors;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010049 }
50
adgar8c3b3fb2019-06-03 13:35:28 -070051 static RecursivePkgValue create(NestedSetBuilder<String> packages, boolean hasErrors) {
52 if (packages.isEmpty() && !hasErrors) {
Janak Ramakrishnanf7ff6162015-06-02 20:20:31 +000053 return EMPTY;
54 }
adgar8c3b3fb2019-06-03 13:35:28 -070055 return new RecursivePkgValue(packages.build(), hasErrors);
Janak Ramakrishnanf7ff6162015-06-02 20:20:31 +000056 }
57
janakr5fb2a482018-03-02 17:48:57 -080058 /** Create a transitive package lookup request. */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010059 @ThreadSafe
janakr5fb2a482018-03-02 17:48:57 -080060 public static Key key(
61 RepositoryName repositoryName,
62 RootedPath rootedPath,
Lukacs Berkid72db8d2015-09-22 07:40:24 +000063 ImmutableSet<PathFragment> excludedPaths) {
janakr5fb2a482018-03-02 17:48:57 -080064 return Key.create(repositoryName, rootedPath, excludedPaths);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065 }
66
67 public NestedSet<String> getPackages() {
68 return packages;
69 }
Mark Schallerf6e32d62015-05-19 20:27:43 +000070
adgar8c3b3fb2019-06-03 13:35:28 -070071 public boolean hasErrors() {
72 return hasErrors;
73 }
74
janakr5fb2a482018-03-02 17:48:57 -080075 @AutoCodec.VisibleForSerialization
shahanbbbf3dc2018-02-23 15:41:52 -080076 @AutoCodec
janakr5fb2a482018-03-02 17:48:57 -080077 static class Key extends RecursivePkgSkyKey {
78 private static final Interner<Key> interner = BlazeInterners.newWeakInterner();
Mark Schallerf6e32d62015-05-19 20:27:43 +000079
janakr5fb2a482018-03-02 17:48:57 -080080 private Key(
shahanbbbf3dc2018-02-23 15:41:52 -080081 RepositoryName repositoryName,
82 RootedPath rootedPath,
Lukacs Berkid72db8d2015-09-22 07:40:24 +000083 ImmutableSet<PathFragment> excludedPaths) {
janakr5fb2a482018-03-02 17:48:57 -080084 super(repositoryName, rootedPath, excludedPaths);
Mark Schallerf6e32d62015-05-19 20:27:43 +000085 }
86
janakr5fb2a482018-03-02 17:48:57 -080087 @AutoCodec.VisibleForSerialization
88 @AutoCodec.Instantiator
89 static Key create(
90 RepositoryName repositoryName,
91 RootedPath rootedPath,
92 ImmutableSet<PathFragment> excludedPaths) {
93 return interner.intern(new Key(repositoryName, rootedPath, excludedPaths));
Mark Schallerf6e32d62015-05-19 20:27:43 +000094 }
95
96 @Override
janakr5fb2a482018-03-02 17:48:57 -080097 public SkyFunctionName functionName() {
98 return SkyFunctions.RECURSIVE_PKG;
Mark Schallerf6e32d62015-05-19 20:27:43 +000099 }
100 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100101}