4141import com .google .devtools .build .lib .skyframe .serialization .ObjectCodec ;
4242import com .google .devtools .build .lib .skyframe .serialization .SerializationContext ;
4343import com .google .devtools .build .lib .skyframe .serialization .SerializationException ;
44- import com .google .devtools .build .lib .skyframe .serialization .autocodec .AutoCodec ;
4544import com .google .devtools .build .lib .skyframe .serialization .autocodec .AutoCodec .VisibleForSerialization ;
4645import com .google .devtools .build .lib .skyframe .serialization .autocodec .SerializationConstant ;
4746import com .google .devtools .build .lib .starlarkbuildapi .FileApi ;
@@ -309,7 +308,7 @@ public ArchivedTreeArtifact getArchivedTreeArtifact(SpecialArtifact treeArtifact
309308 /** A Predicate that evaluates to true if the Artifact is not a middleman artifact. */
310309 public static final Predicate <Artifact > MIDDLEMAN_FILTER = input -> !input .isMiddlemanArtifact ();
311310
312- protected final ArtifactRoot root ;
311+ private final ArtifactRoot root ;
313312
314313 private final int hashCode ;
315314 private final PathFragment execPath ;
@@ -978,7 +977,7 @@ boolean ownersEqual(Artifact other) {
978977
979978 @ Override
980979 public PathFragment getRootRelativePath () {
981- return root .isExternal () ? getExecPath ().subFragment (2 ) : getExecPath ();
980+ return getRoot () .isExternal () ? getExecPath ().subFragment (2 ) : getExecPath ();
982981 }
983982
984983 @ Override
@@ -1168,10 +1167,10 @@ public SpecialArtifact deserialize(DeserializationContext context, CodedInputStr
11681167 * TreeFileArtifact children} (and nothing else) of the tree artifact with their filesystem
11691168 * structure, relative to the {@linkplain SpecialArtifact#getExecPath() tree artifact directory}.
11701169 */
1171- @ AutoCodec
11721170 public static final class ArchivedTreeArtifact extends DerivedArtifact {
1173- private static final PathFragment ARCHIVED_ARTIFACTS_DERIVED_TREE_ROOT =
1171+ private static final PathFragment DEFAULT_DERIVED_TREE_ROOT =
11741172 PathFragment .create (":archived_tree_artifacts" );
1173+
11751174 private final SpecialArtifact treeArtifact ;
11761175
11771176 private ArchivedTreeArtifact (
@@ -1180,6 +1179,8 @@ private ArchivedTreeArtifact(
11801179 PathFragment execPath ,
11811180 Object generatingActionKey ) {
11821181 super (root , execPath , generatingActionKey );
1182+ Preconditions .checkArgument (
1183+ treeArtifact .isTreeArtifact (), "Not a tree artifact: %s" , treeArtifact );
11831184 this .treeArtifact = treeArtifact ;
11841185 }
11851186
@@ -1188,13 +1189,6 @@ public SpecialArtifact getParent() {
11881189 return treeArtifact ;
11891190 }
11901191
1191- /** Creates an archived tree artifact with a given {@code root} and {@code execPath}. */
1192- public static ArchivedTreeArtifact create (
1193- SpecialArtifact treeArtifact , ArtifactRoot root , PathFragment execPath ) {
1194- return new ArchivedTreeArtifact (
1195- treeArtifact , root , execPath , treeArtifact .getGeneratingActionKey ());
1196- }
1197-
11981192 /**
11991193 * Creates an {@link ArchivedTreeArtifact} for a given tree artifact at the path inferred from
12001194 * the provided tree.
@@ -1206,13 +1200,12 @@ public static ArchivedTreeArtifact create(
12061200 * {@linkplain ArchivedTreeArtifact artifact} of: {@code
12071201 * bazel-out/:archived_tree_artifacts/k8-fastbuild/bin/directory.zip}.
12081202 */
1209- public static ArchivedTreeArtifact createForTree (
1210- SpecialArtifact treeArtifact , PathFragment derivedPathPrefix ) {
1211- return createWithCustomDerivedTreeRoot (
1203+ public static ArchivedTreeArtifact createForTree (SpecialArtifact treeArtifact ) {
1204+ return createInternal (
12121205 treeArtifact ,
1213- derivedPathPrefix ,
1214- ARCHIVED_ARTIFACTS_DERIVED_TREE_ROOT ,
1215- treeArtifact .getRootRelativePath (). replaceName ( treeArtifact . getFilename () + ".zip" ));
1206+ DEFAULT_DERIVED_TREE_ROOT ,
1207+ treeArtifact . getRootRelativePath (). replaceName ( treeArtifact . getFilename () + ".zip" ) ,
1208+ treeArtifact .getGeneratingActionKey ( ));
12161209 }
12171210
12181211 /**
@@ -1221,31 +1214,30 @@ public static ArchivedTreeArtifact createForTree(
12211214 *
12221215 * <p>Example: for a tree artifact with root of {@code bazel-out/k8-fastbuild/bin} returns an
12231216 * {@linkplain ArchivedTreeArtifact artifact} of: {@code
1224- * bazel-out/{customDerivedTreeRoot}/k8-fastbuild/bin/{rootRelativePath}} with root of: {@code
1225- * bazel-out/{customDerivedTreeRoot}/k8-fastbuild/bin}.
1217+ * bazel-out/{derivedTreeRoot}/k8-fastbuild/bin/{rootRelativePath}} with root of: {@code
1218+ * bazel-out/{derivedTreeRoot}/k8-fastbuild/bin}.
1219+ *
1220+ * <p>Such artifacts should only be used as outputs of intermediate spawns. Action execution
1221+ * results must come from {@link #createForTree}.
12261222 */
12271223 public static ArchivedTreeArtifact createWithCustomDerivedTreeRoot (
1224+ SpecialArtifact treeArtifact , PathFragment derivedTreeRoot , PathFragment rootRelativePath ) {
1225+ return createInternal (
1226+ treeArtifact , derivedTreeRoot , rootRelativePath , treeArtifact .getGeneratingActionKey ());
1227+ }
1228+
1229+ private static ArchivedTreeArtifact createInternal (
12281230 SpecialArtifact treeArtifact ,
1229- PathFragment derivedPathPrefix ,
1230- PathFragment customDerivedTreeRoot ,
1231- PathFragment rootRelativePath ) {
1232- ArtifactRoot artifactRoot =
1233- createRootForArchivedArtifact (
1234- treeArtifact .getRoot (), derivedPathPrefix , customDerivedTreeRoot );
1235- return create (
1236- treeArtifact , artifactRoot , artifactRoot .getExecPath ().getRelative (rootRelativePath ));
1237- }
1238-
1239- private static ArtifactRoot createRootForArchivedArtifact (
1240- ArtifactRoot treeArtifactRoot ,
1241- PathFragment derivedPathPrefix ,
1242- PathFragment customDerivedTreeRoot ) {
1243- return ArtifactRoot .asDerivedRoot (
1244- getExecRoot (treeArtifactRoot ),
1245- // e.g. bazel-out/{customDerivedTreeRoot}/k8-fastbuild/bin
1246- RootType .Output ,
1247- getExecPathWithinCustomDerivedRoot (
1248- derivedPathPrefix , customDerivedTreeRoot , treeArtifactRoot .getExecPath ()));
1231+ PathFragment derivedTreeRoot ,
1232+ PathFragment rootRelativePath ,
1233+ Object generatingActionKey ) {
1234+ ArtifactRoot treeRoot = treeArtifact .getRoot ();
1235+ PathFragment archiveRoot = embedDerivedTreeRoot (treeRoot .getExecPath (), derivedTreeRoot );
1236+ return new ArchivedTreeArtifact (
1237+ treeArtifact ,
1238+ ArtifactRoot .asDerivedRoot (getExecRoot (treeRoot ), RootType .Output , archiveRoot ),
1239+ archiveRoot .getRelative (rootRelativePath ),
1240+ generatingActionKey );
12491241 }
12501242
12511243 /**
@@ -1255,23 +1247,22 @@ private static ArtifactRoot createRootForArchivedArtifact(
12551247 * <p>Example: {@code bazel-out/k8-fastbuild/bin ->
12561248 * bazel-out/{customDerivedTreeRoot}/k8-fastbuild/bin}.
12571249 */
1258- public static PathFragment getExecPathWithinArchivedArtifactsTree (
1259- PathFragment derivedPathPrefix , PathFragment execPath ) {
1260- return getExecPathWithinCustomDerivedRoot (
1261- derivedPathPrefix , ARCHIVED_ARTIFACTS_DERIVED_TREE_ROOT , execPath );
1250+ public static PathFragment getExecPathWithinArchivedArtifactsTree (PathFragment execPath ) {
1251+ return embedDerivedTreeRoot (execPath , DEFAULT_DERIVED_TREE_ROOT );
12621252 }
12631253
12641254 /**
12651255 * Translates provided output {@code execPath} to one under provided derived tree root.
12661256 *
12671257 * <p>Example: {@code bazel-out/k8-fastbuild/bin ->
1268- * bazel-out/{customDerivedTreeRoot }/k8-fastbuild/bin}.
1258+ * bazel-out/{derivedTreeRoot }/k8-fastbuild/bin}.
12691259 */
1270- private static PathFragment getExecPathWithinCustomDerivedRoot (
1271- PathFragment derivedPathPrefix , PathFragment customDerivedTreeRoot , PathFragment execPath ) {
1272- return derivedPathPrefix
1273- .getRelative (customDerivedTreeRoot )
1274- .getRelative (execPath .relativeTo (derivedPathPrefix ));
1260+ private static PathFragment embedDerivedTreeRoot (
1261+ PathFragment execPath , PathFragment derivedTreeRoot ) {
1262+ return execPath
1263+ .subFragment (0 , 1 )
1264+ .getRelative (derivedTreeRoot )
1265+ .getRelative (execPath .subFragment (1 ));
12751266 }
12761267
12771268 private static Path getExecRoot (ArtifactRoot artifactRoot ) {
@@ -1284,16 +1275,42 @@ private static Path getExecRoot(ArtifactRoot artifactRoot) {
12841275 0 , rootPathFragment .segmentCount () - artifactRoot .getExecPath ().segmentCount ());
12851276 return rootPath .getFileSystem ().getPath (execRootPath );
12861277 }
1278+ }
12871279
1288- @ VisibleForSerialization
1289- @ AutoCodec .Instantiator
1290- static ArchivedTreeArtifact createForDeserialization (
1291- SpecialArtifact treeArtifact , ArtifactRoot root , PathFragment execPath ) {
1280+ @ SuppressWarnings ("unused" ) // Codec used by reflection.
1281+ private static final class ArchivedTreeArtifactCodec
1282+ implements ObjectCodec <ArchivedTreeArtifact > {
1283+
1284+ @ Override
1285+ public Class <ArchivedTreeArtifact > getEncodedClass () {
1286+ return ArchivedTreeArtifact .class ;
1287+ }
1288+
1289+ @ Override
1290+ public void serialize (
1291+ SerializationContext context , ArchivedTreeArtifact obj , CodedOutputStream codedOut )
1292+ throws SerializationException , IOException {
1293+ PathFragment derivedTreeRoot = obj .getRoot ().getExecPath ().subFragment (1 , 2 );
1294+
1295+ context .serialize (obj .getParent (), codedOut );
1296+ context .serialize (derivedTreeRoot , codedOut );
1297+ context .serialize (obj .getRootRelativePath (), codedOut );
1298+ }
1299+
1300+ @ Override
1301+ public ArchivedTreeArtifact deserialize (
1302+ DeserializationContext context , CodedInputStream codedIn )
1303+ throws SerializationException , IOException {
1304+ SpecialArtifact treeArtifact = context .deserialize (codedIn );
1305+ PathFragment derivedTreeRoot = context .deserialize (codedIn );
1306+ PathFragment rootRelativePath = context .deserialize (codedIn );
12921307 Object generatingActionKey =
12931308 treeArtifact .hasGeneratingActionKey ()
12941309 ? treeArtifact .getGeneratingActionKey ()
12951310 : OMITTED_FOR_SERIALIZATION ;
1296- return new ArchivedTreeArtifact (treeArtifact , root , execPath , generatingActionKey );
1311+
1312+ return ArchivedTreeArtifact .createInternal (
1313+ treeArtifact , derivedTreeRoot , rootRelativePath , generatingActionKey );
12971314 }
12981315 }
12991316
0 commit comments