Skip to content

Commit 41a2655

Browse files
Googlersellophane
authored andcommitted
Support storing multiple kinds of artifacts in RuntimeArtifactCache
Bug: 401298873 Test: Updated Change-Id: I33ffdafb02a0cf95091be6253f770e4449a7d9b2 AOSP: 84e8dd8997e32815e24ae4c4a3e845c8203c8b41
1 parent 694e6b9 commit 41a2655

File tree

12 files changed

+392
-324
lines changed

12 files changed

+392
-324
lines changed

base/src/com/google/idea/blaze/base/command/buildresult/LocalFileArtifact.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.ImmutableList;
2121
import com.google.idea.blaze.base.io.FileOperationProvider;
2222
import com.google.idea.blaze.base.run.RuntimeArtifactCache;
23+
import com.google.idea.blaze.base.run.RuntimeArtifactKind;
2324
import com.google.idea.blaze.base.scope.BlazeContext;
2425
import com.google.idea.blaze.common.Label;
2526
import com.google.idea.blaze.common.artifact.BlazeArtifact;
@@ -29,7 +30,9 @@
2930
import java.nio.file.Path;
3031
import java.util.Collection;
3132

32-
/** A file artifact available on the local file system. */
33+
/**
34+
* A file artifact available on the local file system.
35+
*/
3336
public interface LocalFileArtifact extends BlazeArtifact {
3437

3538
/**
@@ -39,9 +42,11 @@ public interface LocalFileArtifact extends BlazeArtifact {
3942
* runfiles directories).
4043
*/
4144
static ImmutableList<File> getLocalFiles(Label target, Collection<? extends OutputArtifact> artifacts, BlazeContext context,
42-
Project project) {
45+
Project project, RuntimeArtifactKind artifactKind) {
4346
var runtimeArtifactCache = RuntimeArtifactCache.getInstance(project);
44-
return runtimeArtifactCache.fetchArtifacts(target, artifacts.stream().toList(), context).stream().map(Path::toFile).collect(toImmutableList());
47+
return runtimeArtifactCache.fetchArtifacts(target, artifacts.stream().toList(), context, artifactKind).stream()
48+
.map(Path::toFile)
49+
.collect(toImmutableList());
4550
}
4651

4752
/**
@@ -53,7 +58,7 @@ static ImmutableList<File> getLocalFiles(Label target, Collection<? extends Outp
5358
static ImmutableList<File> getLocalFilesForLegacySync(Collection<? extends BlazeArtifact> artifacts) {
5459
return artifacts.stream()
5560
.filter(a -> a instanceof LocalFileArtifact)
56-
.map(a -> ((LocalFileArtifact) a).getFile())
61+
.map(a -> ((LocalFileArtifact)a).getFile())
5762
.collect(toImmutableList());
5863
}
5964

base/src/com/google/idea/blaze/base/run/RuntimeArtifactCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ static RuntimeArtifactCache getInstance(Project project) {
3535
* build.
3636
*/
3737
ImmutableList<Path> fetchArtifacts(
38-
Label target, List<? extends OutputArtifact> artifacts, BlazeContext context);
38+
Label target, List<? extends OutputArtifact> artifacts, BlazeContext context, RuntimeArtifactKind artifactKind);
3939
}

base/src/com/google/idea/blaze/base/run/RuntimeArtifactCacheImpl.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.idea.blaze.qsync.project.ProjectProto.ProjectArtifact;
3333
import com.intellij.openapi.diagnostic.Logger;
3434
import com.intellij.openapi.project.Project;
35+
import com.intellij.openapi.util.Pair;
3536
import java.io.IOException;
3637
import java.nio.file.Path;
3738
import java.nio.file.Paths;
@@ -43,7 +44,7 @@
4344

4445
public final class RuntimeArtifactCacheImpl implements RuntimeArtifactCache {
4546
private static final Logger logger = Logger.getInstance(RuntimeArtifactCacheImpl.class);
46-
private final Map<Label, Map<Path, ProjectProto.ProjectArtifact>> artifactCacheMap = new HashMap<>();
47+
private final Map<Pair<Label, RuntimeArtifactKind>, Map<Path, ProjectArtifact>> artifactCacheMap = new HashMap<>();
4748
private final Path runfilesDirectory;
4849
private static final String SEPARATOR_DIR_NAME = "_";
4950
private final BuildArtifactCache buildArtifactCache;
@@ -67,20 +68,21 @@ private RuntimeArtifactCacheImpl(Project project) throws IOException {
6768

6869
@Override
6970
public ImmutableList<Path> fetchArtifacts(
70-
Label target, List<? extends OutputArtifact> artifacts, BlazeContext context) {
71+
Label target, List<? extends OutputArtifact> artifacts, BlazeContext context, RuntimeArtifactKind artifactKind) {
72+
var targetKind = Pair.create(target, artifactKind);
7173
final var artifactsCachedFuture =
7274
buildArtifactCache.addAll(artifacts.stream().collect(toImmutableList()), context);
73-
artifactCacheMap.put(target, buildArtifactLayout(target, artifacts));
75+
artifactCacheMap.put(targetKind, buildArtifactLayout(target, artifacts));
7476
final var artifactDirectoryContents = buildArtifactDirectoryContents(artifactCacheMap);
7577
waitForArtifacts(artifactsCachedFuture);
7678
updateArtifactDirectory(artifactDirectoryContents);
7779

78-
return resolveArtifactLayoutPaths(target, artifactCacheMap.get(target).keySet());
80+
return resolveArtifactLayoutPaths(target, artifactKind, artifactCacheMap.get(targetKind).keySet());
7981
}
8082

81-
private ImmutableList<Path> resolveArtifactLayoutPaths(Label target, Set<Path> artifactPaths) {
83+
private ImmutableList<Path> resolveArtifactLayoutPaths(Label target, RuntimeArtifactKind artifactKind, Set<Path> artifactPaths) {
8284
return artifactPaths.stream()
83-
.map(artifactPath -> runfilesDirectory.resolve(getArtifactLocalPath(target, artifactPath)))
85+
.map(artifactPath -> runfilesDirectory.resolve(getArtifactLocalPath(target, artifactKind, artifactPath)))
8486
.collect(toImmutableList());
8587
}
8688

@@ -131,15 +133,15 @@ private void updateArtifactDirectory(
131133
* path -> project artifact.
132134
*/
133135
private static ProjectProto.ArtifactDirectoryContents buildArtifactDirectoryContents(
134-
Map<Label, Map<Path, ProjectProto.ProjectArtifact>> artifacts) {
136+
Map<Pair<Label, RuntimeArtifactKind>, Map<Path, ProjectProto.ProjectArtifact>> artifacts) {
135137
final var artifactDirectoryContents = ProjectProto.ArtifactDirectoryContents.newBuilder();
136138
for (final var entry : artifacts.entrySet()) {
137-
final var target = entry.getKey();
139+
final var key = entry.getKey();
138140
for (final var artifactPathAndDigest : entry.getValue().entrySet()) {
139141
final var artifactPath = artifactPathAndDigest.getKey();
140142
final var artifact = artifactPathAndDigest.getValue();
141143
artifactDirectoryContents.putContents(
142-
getArtifactLocalPath(target, artifactPath).toString(), artifact);
144+
getArtifactLocalPath(key.first, key.second, artifactPath).toString(), artifact);
143145
}
144146
}
145147
return artifactDirectoryContents.build();
@@ -149,7 +151,7 @@ private static ProjectProto.ArtifactDirectoryContents buildArtifactDirectoryCont
149151
* Generates the local artifact path from the target and artifact path. Local Artifact path ->
150152
* Target + SEPARATOR_DIR_NAME + artifactPath.
151153
*/
152-
public static Path getArtifactLocalPath(Label target, Path artifactPath) {
153-
return target.toFilePath().resolve(Path.of(SEPARATOR_DIR_NAME)).resolve(artifactPath);
154+
public static Path getArtifactLocalPath(Label target, RuntimeArtifactKind artifactKind, Path artifactPath) {
155+
return target.toFilePath().resolve(Path.of(artifactKind.name())).resolve(Path.of(SEPARATOR_DIR_NAME)).resolve(artifactPath);
154156
}
155157
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2025 The Bazel Authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.idea.blaze.base.run;
17+
18+
public enum RuntimeArtifactKind {
19+
APK(".apk"),
20+
SYMBOL_FILE(".so"),
21+
JAR(".jar");
22+
23+
public final String extension;
24+
25+
private RuntimeArtifactKind(String extension) {
26+
this.extension = extension;
27+
}
28+
}

base/tests/integrationtests/com/google/idea/blaze/base/run/RuntimeArtifactCacheImplTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ public void fetchArtifacts() throws Exception {
8282
Label target = Label.of("//some/label:target");
8383
List<Path> paths =
8484
runtimeArtifactCache.fetchArtifacts(
85-
target, ImmutableList.of(artifact1, artifact2), BlazeContext.create());
85+
target, ImmutableList.of(artifact1, artifact2), BlazeContext.create(), RuntimeArtifactKind.JAR);
8686
assertThat(paths)
8787
.containsExactly(
8888
runfilesDirectory.resolve(
8989
RuntimeArtifactCacheImpl.getArtifactLocalPath(
90-
target, Path.of("out/test1.jar"))),
90+
target, RuntimeArtifactKind.JAR, Path.of("out/test1.jar"))),
9191
runfilesDirectory.resolve(
9292
RuntimeArtifactCacheImpl.getArtifactLocalPath(
93-
target, Path.of("out/test2.jar"))));
93+
target, RuntimeArtifactKind.JAR, Path.of("out/test2.jar"))));
9494
assertThat(Files.readAllBytes(paths.get(0))).isEqualTo("abc".getBytes());
9595
assertThat(Files.readAllBytes(paths.get(1))).isEqualTo("def".getBytes());
9696
assertThat(testArtifactFetcher.getCopiedArtifacts()).isEqualTo(List.of(artifact1, artifact2));
@@ -112,7 +112,7 @@ public void fetchArtifacts_failed() throws Exception {
112112
.build();
113113
Label target = Label.of("//some/label:target");
114114
assertThrows(IllegalStateException.class, () -> runtimeArtifactCache.fetchArtifacts(
115-
target, ImmutableList.of(artifact1, artifact2), BlazeContext.create()));
115+
target, ImmutableList.of(artifact1, artifact2), BlazeContext.create(), RuntimeArtifactKind.JAR));
116116
}
117117

118118
private BuildArtifactCacheDirectory createBuildArtifactCache(RuntimeArtifactCacheImplTest.TestArtifactFetcher artifactFetcher) throws

0 commit comments

Comments
 (0)