Skip to content

Commit

Permalink
Move summarization action to Aspects.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 554521299
  • Loading branch information
j2objc-copybara authored and copybara-github committed Aug 7, 2023
1 parent 2867504 commit a3aa08e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.io.Resources;
Expand Down Expand Up @@ -67,6 +68,7 @@ class Options {
private File outputFile = new File("tree-shaker-report.txt");
private LibraryInfo summary;
private String summaryOutputFile;
private List<LibraryInfo> summaries = Lists.newArrayList();

// The default source version number if not passed with -source is determined from the system
// properties of the running java version after parsing the argument list.
Expand Down Expand Up @@ -135,11 +137,27 @@ public LibraryInfo getSummary() {
public void setSummary(LibraryInfo summary) {
this.summary = summary;
}


public List<LibraryInfo> getSummaries() {
return summaries;
}

public void setSummaries(List<LibraryInfo> summaries) {
this.summaries = summaries;
}

public void addSummary(LibraryInfo summary) {
this.summaries.add(summary);
}

public String getSummaryOutputFile() {
return summaryOutputFile;
}

public void setSummaryOutputFile(String summaryOutputFile) {
this.summaryOutputFile = summaryOutputFile;
}

private void addManifest(String manifestFile) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(new File(manifestFile)));
try {
Expand Down Expand Up @@ -192,6 +210,16 @@ public static Options parse(String[] args) throws IOException {
return options;
}

private static List<LibraryInfo> readSummaries(List<String> summaries) throws IOException {
List<LibraryInfo> result = Lists.newArrayList();
for (String summary : summaries) {
result.add(
LibraryInfo.parseFrom(
Files.toByteArray(new File(summary)), ExtensionRegistry.getGeneratedRegistry()));
}
return result;
}

private static void processArgsFile(String filename, Options options) throws IOException {
if (filename.isEmpty()) {
usage("no @ file specified");
Expand Down Expand Up @@ -220,6 +248,11 @@ private static void processArgs(String[] args, Options options) throws IOExcepti
usage("-classpath requires an argument");
}
options.classpath = args[nArg];
} else if (arg.equals("-summaries")) {
if (++nArg == args.length) {
usage("--summaries requires an argument");
}
options.setSummaries(readSummaries(ImmutableList.copyOf(args[nArg].split(":"))));
} else if (arg.equals("-summary")) {
if (++nArg == args.length) {
usage("-summary requires an argument");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

package com.google.devtools.treeshaker;

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.flogger.GoogleLogger;
import com.google.common.io.Files;
import com.google.devtools.j2objc.ast.CompilationUnit;
Expand Down Expand Up @@ -170,6 +173,12 @@ private TypeGraphBuilder createTypeGraphBuilder() throws IOException {
LibraryInfo info = options.getSummary();
LibraryInfo markedInfo = UsedCodeMarker.mark(info, options.getTreeShakerRoots());
return new TypeGraphBuilder(markedInfo);
} else if (!options.getSummaries().isEmpty()) {
ImmutableList<LibraryInfo> markedInfo =
options.getSummaries().stream()
.map(summary -> UsedCodeMarker.mark(summary, options.getTreeShakerRoots()))
.collect(toImmutableList());
return new TypeGraphBuilder(markedInfo);
}
return new TypeGraphBuilder(createLibraryInfo());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

TypeGraphBuilder(List<LibraryInfo> libraryInfos) {
Map<String, Type> typesByName = new LinkedHashMap<>();
externalTypeReferences = new HashSet<>();
unknownMethodReferences = new HashSet<>();
for (LibraryInfo libraryInfo : libraryInfos) {
for (TypeInfo typeInfo : libraryInfo.getTypeList()) {
Type type = Type.buildFrom(typeInfo, libraryInfo.getTypeMap(typeInfo.getTypeId()));
typesByName.put(type.getName(), type);
}
}

// Build cross-references between types and members
for (LibraryInfo libraryInfo : libraryInfos) {
buildCrossReferences(libraryInfo, typesByName);
}

types = typesByName.values();
}

Collection<Type> getTypes() {
return types;
}
Expand Down

0 comments on commit a3aa08e

Please sign in to comment.