Skip to content

Commit a3aa08e

Browse files
j2objc-copybaracopybara-github
authored andcommitted
Move summarization action to Aspects.
PiperOrigin-RevId: 554521299
1 parent 2867504 commit a3aa08e

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

tree_shaker/src/main/java/com/google/devtools/treeshaker/Options.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.common.annotations.VisibleForTesting;
1818
import com.google.common.base.Preconditions;
1919
import com.google.common.base.Strings;
20+
import com.google.common.collect.ImmutableList;
2021
import com.google.common.collect.Lists;
2122
import com.google.common.io.Files;
2223
import com.google.common.io.Resources;
@@ -67,6 +68,7 @@ class Options {
6768
private File outputFile = new File("tree-shaker-report.txt");
6869
private LibraryInfo summary;
6970
private String summaryOutputFile;
71+
private List<LibraryInfo> summaries = Lists.newArrayList();
7072

7173
// The default source version number if not passed with -source is determined from the system
7274
// properties of the running java version after parsing the argument list.
@@ -135,11 +137,27 @@ public LibraryInfo getSummary() {
135137
public void setSummary(LibraryInfo summary) {
136138
this.summary = summary;
137139
}
138-
140+
141+
public List<LibraryInfo> getSummaries() {
142+
return summaries;
143+
}
144+
145+
public void setSummaries(List<LibraryInfo> summaries) {
146+
this.summaries = summaries;
147+
}
148+
149+
public void addSummary(LibraryInfo summary) {
150+
this.summaries.add(summary);
151+
}
152+
139153
public String getSummaryOutputFile() {
140154
return summaryOutputFile;
141155
}
142156

157+
public void setSummaryOutputFile(String summaryOutputFile) {
158+
this.summaryOutputFile = summaryOutputFile;
159+
}
160+
143161
private void addManifest(String manifestFile) throws IOException {
144162
BufferedReader in = new BufferedReader(new FileReader(new File(manifestFile)));
145163
try {
@@ -192,6 +210,16 @@ public static Options parse(String[] args) throws IOException {
192210
return options;
193211
}
194212

213+
private static List<LibraryInfo> readSummaries(List<String> summaries) throws IOException {
214+
List<LibraryInfo> result = Lists.newArrayList();
215+
for (String summary : summaries) {
216+
result.add(
217+
LibraryInfo.parseFrom(
218+
Files.toByteArray(new File(summary)), ExtensionRegistry.getGeneratedRegistry()));
219+
}
220+
return result;
221+
}
222+
195223
private static void processArgsFile(String filename, Options options) throws IOException {
196224
if (filename.isEmpty()) {
197225
usage("no @ file specified");
@@ -220,6 +248,11 @@ private static void processArgs(String[] args, Options options) throws IOExcepti
220248
usage("-classpath requires an argument");
221249
}
222250
options.classpath = args[nArg];
251+
} else if (arg.equals("-summaries")) {
252+
if (++nArg == args.length) {
253+
usage("--summaries requires an argument");
254+
}
255+
options.setSummaries(readSummaries(ImmutableList.copyOf(args[nArg].split(":"))));
223256
} else if (arg.equals("-summary")) {
224257
if (++nArg == args.length) {
225258
usage("-summary requires an argument");

tree_shaker/src/main/java/com/google/devtools/treeshaker/TreeShaker.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414

1515
package com.google.devtools.treeshaker;
1616

17+
import static com.google.common.collect.ImmutableList.toImmutableList;
18+
1719
import com.google.common.annotations.VisibleForTesting;
1820
import com.google.common.base.Strings;
21+
import com.google.common.collect.ImmutableList;
1922
import com.google.common.flogger.GoogleLogger;
2023
import com.google.common.io.Files;
2124
import com.google.devtools.j2objc.ast.CompilationUnit;
@@ -170,6 +173,12 @@ private TypeGraphBuilder createTypeGraphBuilder() throws IOException {
170173
LibraryInfo info = options.getSummary();
171174
LibraryInfo markedInfo = UsedCodeMarker.mark(info, options.getTreeShakerRoots());
172175
return new TypeGraphBuilder(markedInfo);
176+
} else if (!options.getSummaries().isEmpty()) {
177+
ImmutableList<LibraryInfo> markedInfo =
178+
options.getSummaries().stream()
179+
.map(summary -> UsedCodeMarker.mark(summary, options.getTreeShakerRoots()))
180+
.collect(toImmutableList());
181+
return new TypeGraphBuilder(markedInfo);
173182
}
174183
return new TypeGraphBuilder(createLibraryInfo());
175184
}

tree_shaker/src/main/java/com/google/devtools/treeshaker/TypeGraphBuilder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Collection;
1717
import java.util.HashSet;
1818
import java.util.LinkedHashMap;
19+
import java.util.List;
1920
import java.util.Map;
2021
import java.util.Set;
2122

@@ -38,6 +39,25 @@ class TypeGraphBuilder {
3839
types = typesByName.values();
3940
}
4041

42+
TypeGraphBuilder(List<LibraryInfo> libraryInfos) {
43+
Map<String, Type> typesByName = new LinkedHashMap<>();
44+
externalTypeReferences = new HashSet<>();
45+
unknownMethodReferences = new HashSet<>();
46+
for (LibraryInfo libraryInfo : libraryInfos) {
47+
for (TypeInfo typeInfo : libraryInfo.getTypeList()) {
48+
Type type = Type.buildFrom(typeInfo, libraryInfo.getTypeMap(typeInfo.getTypeId()));
49+
typesByName.put(type.getName(), type);
50+
}
51+
}
52+
53+
// Build cross-references between types and members
54+
for (LibraryInfo libraryInfo : libraryInfos) {
55+
buildCrossReferences(libraryInfo, typesByName);
56+
}
57+
58+
types = typesByName.values();
59+
}
60+
4161
Collection<Type> getTypes() {
4262
return types;
4363
}

0 commit comments

Comments
 (0)