From 38ec368ae7efb502176f068ed9f39b5965bc14db Mon Sep 17 00:00:00 2001 From: J2ObjC Team Date: Mon, 7 Aug 2023 10:34:52 -0700 Subject: [PATCH] Move summarization action to Aspects. PiperOrigin-RevId: 554521299 --- .../google/devtools/treeshaker/Options.java | 35 ++++++++++++++++++- .../devtools/treeshaker/TreeShaker.java | 9 +++++ .../devtools/treeshaker/TypeGraphBuilder.java | 20 +++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/tree_shaker/src/main/java/com/google/devtools/treeshaker/Options.java b/tree_shaker/src/main/java/com/google/devtools/treeshaker/Options.java index 7bab4d668b..8eb4caa6a2 100644 --- a/tree_shaker/src/main/java/com/google/devtools/treeshaker/Options.java +++ b/tree_shaker/src/main/java/com/google/devtools/treeshaker/Options.java @@ -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; @@ -67,6 +68,7 @@ class Options { private File outputFile = new File("tree-shaker-report.txt"); private LibraryInfo summary; private String summaryOutputFile; + private List 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. @@ -135,11 +137,27 @@ public LibraryInfo getSummary() { public void setSummary(LibraryInfo summary) { this.summary = summary; } - + + public List getSummaries() { + return summaries; + } + + public void setSummaries(List 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 { @@ -192,6 +210,16 @@ public static Options parse(String[] args) throws IOException { return options; } + private static List readSummaries(List summaries) throws IOException { + List 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"); @@ -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"); diff --git a/tree_shaker/src/main/java/com/google/devtools/treeshaker/TreeShaker.java b/tree_shaker/src/main/java/com/google/devtools/treeshaker/TreeShaker.java index 17fa925a43..7f940239dc 100644 --- a/tree_shaker/src/main/java/com/google/devtools/treeshaker/TreeShaker.java +++ b/tree_shaker/src/main/java/com/google/devtools/treeshaker/TreeShaker.java @@ -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; @@ -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 markedInfo = + options.getSummaries().stream() + .map(summary -> UsedCodeMarker.mark(summary, options.getTreeShakerRoots())) + .collect(toImmutableList()); + return new TypeGraphBuilder(markedInfo); } return new TypeGraphBuilder(createLibraryInfo()); } diff --git a/tree_shaker/src/main/java/com/google/devtools/treeshaker/TypeGraphBuilder.java b/tree_shaker/src/main/java/com/google/devtools/treeshaker/TypeGraphBuilder.java index eb771c7719..8f1cd53f01 100644 --- a/tree_shaker/src/main/java/com/google/devtools/treeshaker/TypeGraphBuilder.java +++ b/tree_shaker/src/main/java/com/google/devtools/treeshaker/TypeGraphBuilder.java @@ -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; @@ -38,6 +39,25 @@ class TypeGraphBuilder { types = typesByName.values(); } + TypeGraphBuilder(List libraryInfos) { + Map 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 getTypes() { return types; }