41
41
import com .google .devtools .build .lib .skyframe .serialization .ObjectCodec ;
42
42
import com .google .devtools .build .lib .skyframe .serialization .SerializationContext ;
43
43
import com .google .devtools .build .lib .skyframe .serialization .SerializationException ;
44
- import com .google .devtools .build .lib .skyframe .serialization .autocodec .AutoCodec ;
45
44
import com .google .devtools .build .lib .skyframe .serialization .autocodec .AutoCodec .VisibleForSerialization ;
46
45
import com .google .devtools .build .lib .skyframe .serialization .autocodec .SerializationConstant ;
47
46
import com .google .devtools .build .lib .starlarkbuildapi .FileApi ;
@@ -309,7 +308,7 @@ public ArchivedTreeArtifact getArchivedTreeArtifact(SpecialArtifact treeArtifact
309
308
/** A Predicate that evaluates to true if the Artifact is not a middleman artifact. */
310
309
public static final Predicate <Artifact > MIDDLEMAN_FILTER = input -> !input .isMiddlemanArtifact ();
311
310
312
- protected final ArtifactRoot root ;
311
+ private final ArtifactRoot root ;
313
312
314
313
private final int hashCode ;
315
314
private final PathFragment execPath ;
@@ -978,7 +977,7 @@ boolean ownersEqual(Artifact other) {
978
977
979
978
@ Override
980
979
public PathFragment getRootRelativePath () {
981
- return root .isExternal () ? getExecPath ().subFragment (2 ) : getExecPath ();
980
+ return getRoot () .isExternal () ? getExecPath ().subFragment (2 ) : getExecPath ();
982
981
}
983
982
984
983
@ Override
@@ -1168,10 +1167,10 @@ public SpecialArtifact deserialize(DeserializationContext context, CodedInputStr
1168
1167
* TreeFileArtifact children} (and nothing else) of the tree artifact with their filesystem
1169
1168
* structure, relative to the {@linkplain SpecialArtifact#getExecPath() tree artifact directory}.
1170
1169
*/
1171
- @ AutoCodec
1172
1170
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 =
1174
1172
PathFragment .create (":archived_tree_artifacts" );
1173
+
1175
1174
private final SpecialArtifact treeArtifact ;
1176
1175
1177
1176
private ArchivedTreeArtifact (
@@ -1180,6 +1179,8 @@ private ArchivedTreeArtifact(
1180
1179
PathFragment execPath ,
1181
1180
Object generatingActionKey ) {
1182
1181
super (root , execPath , generatingActionKey );
1182
+ Preconditions .checkArgument (
1183
+ treeArtifact .isTreeArtifact (), "Not a tree artifact: %s" , treeArtifact );
1183
1184
this .treeArtifact = treeArtifact ;
1184
1185
}
1185
1186
@@ -1188,13 +1189,6 @@ public SpecialArtifact getParent() {
1188
1189
return treeArtifact ;
1189
1190
}
1190
1191
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
-
1198
1192
/**
1199
1193
* Creates an {@link ArchivedTreeArtifact} for a given tree artifact at the path inferred from
1200
1194
* the provided tree.
@@ -1206,13 +1200,12 @@ public static ArchivedTreeArtifact create(
1206
1200
* {@linkplain ArchivedTreeArtifact artifact} of: {@code
1207
1201
* bazel-out/:archived_tree_artifacts/k8-fastbuild/bin/directory.zip}.
1208
1202
*/
1209
- public static ArchivedTreeArtifact createForTree (
1210
- SpecialArtifact treeArtifact , PathFragment derivedPathPrefix ) {
1211
- return createWithCustomDerivedTreeRoot (
1203
+ public static ArchivedTreeArtifact createForTree (SpecialArtifact treeArtifact ) {
1204
+ return createInternal (
1212
1205
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 ( ));
1216
1209
}
1217
1210
1218
1211
/**
@@ -1221,31 +1214,30 @@ public static ArchivedTreeArtifact createForTree(
1221
1214
*
1222
1215
* <p>Example: for a tree artifact with root of {@code bazel-out/k8-fastbuild/bin} returns an
1223
1216
* {@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}.
1226
1222
*/
1227
1223
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 (
1228
1230
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 );
1249
1241
}
1250
1242
1251
1243
/**
@@ -1255,23 +1247,22 @@ private static ArtifactRoot createRootForArchivedArtifact(
1255
1247
* <p>Example: {@code bazel-out/k8-fastbuild/bin ->
1256
1248
* bazel-out/{customDerivedTreeRoot}/k8-fastbuild/bin}.
1257
1249
*/
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 );
1262
1252
}
1263
1253
1264
1254
/**
1265
1255
* Translates provided output {@code execPath} to one under provided derived tree root.
1266
1256
*
1267
1257
* <p>Example: {@code bazel-out/k8-fastbuild/bin ->
1268
- * bazel-out/{customDerivedTreeRoot }/k8-fastbuild/bin}.
1258
+ * bazel-out/{derivedTreeRoot }/k8-fastbuild/bin}.
1269
1259
*/
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 ));
1275
1266
}
1276
1267
1277
1268
private static Path getExecRoot (ArtifactRoot artifactRoot ) {
@@ -1284,16 +1275,42 @@ private static Path getExecRoot(ArtifactRoot artifactRoot) {
1284
1275
0 , rootPathFragment .segmentCount () - artifactRoot .getExecPath ().segmentCount ());
1285
1276
return rootPath .getFileSystem ().getPath (execRootPath );
1286
1277
}
1278
+ }
1287
1279
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 );
1292
1307
Object generatingActionKey =
1293
1308
treeArtifact .hasGeneratingActionKey ()
1294
1309
? treeArtifact .getGeneratingActionKey ()
1295
1310
: OMITTED_FOR_SERIALIZATION ;
1296
- return new ArchivedTreeArtifact (treeArtifact , root , execPath , generatingActionKey );
1311
+
1312
+ return ArchivedTreeArtifact .createInternal (
1313
+ treeArtifact , derivedTreeRoot , rootRelativePath , generatingActionKey );
1297
1314
}
1298
1315
}
1299
1316
0 commit comments