Extend the tool extract embedded R8 rules to extract for any version
This added the CLI option `--compiler-version` and removes "fake"
from the API.
This can be tested in the R8 repo with something like this:
```
java -cp build/libs/r8.jar com.android.tools.r8.ExtractR8Rules \
third_party/kotlin/kotlin-compiler-2.1.0-Beta1/kotlinc/lib/kotlin-reflect.jar \
third_party/kotlin/kotlin-compiler-2.1.0-Beta1/kotlinc/lib/kotlinx-coroutines-core-jvm.jar \
--include-origin-comments \
--compiler-version 1.0.0 \
--rules-output extracted.rules
```
And the content of `extracted.rules` compared to using other
`--compiler-version` values (e.g >= 1.6.0).
Bug: b/377144587
Change-Id: I7be9bd58192685bb1d747ae38f594e2f826764fd
diff --git a/src/main/java/com/android/tools/r8/ExtractR8Rules.java b/src/main/java/com/android/tools/r8/ExtractR8Rules.java
index 6d97afb..fd54ac7 100644
--- a/src/main/java/com/android/tools/r8/ExtractR8Rules.java
+++ b/src/main/java/com/android/tools/r8/ExtractR8Rules.java
@@ -25,11 +25,11 @@
AndroidApp app,
StringConsumer consumer,
boolean includeOriginComments,
- SemanticVersion fakeCompilerVersion,
+ SemanticVersion compilerVersion,
Reporter reporter) {
Supplier<SemanticVersion> semanticVersionSupplier =
SemanticVersionUtils.compilerVersionSemanticVersionSupplier(
- fakeCompilerVersion,
+ compilerVersion,
"Using an artificial version newer than any known version for selecting"
+ " Proguard configurations embedded under META-INF/. This means that"
+ " all rules with a '-upto-' qualifier will be excluded and all"
@@ -72,12 +72,12 @@
AndroidApp app = command.getInputApp();
StringConsumer rulesConsumer = command.getRulesConsumer();
boolean includeOriginComments = command.getIncludeOriginComments();
- SemanticVersion fakeCompilerVersion = command.getFakeCompilerVersion();
+ SemanticVersion compilerVersion = command.getCompilerVersion();
InternalOptions options = command.getInternalOptions();
ExceptionUtils.withCompilationHandler(
options.reporter,
() -> {
- run(app, rulesConsumer, includeOriginComments, fakeCompilerVersion, options.reporter);
+ run(app, rulesConsumer, includeOriginComments, compilerVersion, options.reporter);
});
}
diff --git a/src/main/java/com/android/tools/r8/ExtractR8RulesCommand.java b/src/main/java/com/android/tools/r8/ExtractR8RulesCommand.java
index 3a55917..f8acec9 100644
--- a/src/main/java/com/android/tools/r8/ExtractR8RulesCommand.java
+++ b/src/main/java/com/android/tools/r8/ExtractR8RulesCommand.java
@@ -22,7 +22,7 @@
private final StringConsumer rulesConsumer;
private final boolean includeOriginComments;
- private final SemanticVersion fakeCompilerVersion;
+ private final SemanticVersion compilerVersion;
private final DexItemFactory factory;
private final Reporter reporter;
@@ -32,7 +32,7 @@
private final DexItemFactory factory = new DexItemFactory();
private StringConsumer rulesConsumer = null;
private boolean includeOriginComments = false;
- private SemanticVersion fakeCompilerVersion = null;
+ private SemanticVersion compilerVersion = null;
private Builder() {}
@@ -64,8 +64,8 @@
}
/** TBD */
- public Builder setFakeCompilerVersion(SemanticVersion version) {
- fakeCompilerVersion = version;
+ public Builder setCompilerVersion(SemanticVersion version) {
+ compilerVersion = version;
return self();
}
@@ -81,7 +81,7 @@
getAppBuilder().build(),
rulesConsumer,
includeOriginComments,
- fakeCompilerVersion,
+ compilerVersion,
getReporter());
}
}
@@ -90,6 +90,7 @@
StringUtils.lines(
"Usage: TBD",
" --rules-output <file> # Output the extracted keep rules.",
+ " --compiler-version <version> # Output the proguard rules extracted.",
" --include-origin-comments # Include comments with origin for extracted rules.",
" --version # Print the version.",
" --help # Print this message.");
@@ -116,8 +117,8 @@
return includeOriginComments;
}
- public SemanticVersion getFakeCompilerVersion() {
- return fakeCompilerVersion;
+ public SemanticVersion getCompilerVersion() {
+ return compilerVersion;
}
Reporter getReporter() {
@@ -135,6 +136,8 @@
builder.setPrintVersion(true);
} else if (arg.equals("--rules-output")) {
builder.setRulesOutputPath(Paths.get(args[++i]));
+ } else if (arg.equals("--compiler-version")) {
+ builder.setCompilerVersion(SemanticVersion.parse(args[++i]));
} else if (arg.equals("--include-origin-comments")) {
builder.setIncludeOriginComments(true);
} else {
@@ -154,13 +157,13 @@
AndroidApp inputApp,
StringConsumer rulesConsumer,
boolean includeOriginComments,
- SemanticVersion fakeCompilerVersion,
+ SemanticVersion compilerVersion,
Reporter reporter) {
super(inputApp);
this.factory = factory;
this.rulesConsumer = rulesConsumer;
this.includeOriginComments = includeOriginComments;
- this.fakeCompilerVersion = fakeCompilerVersion;
+ this.compilerVersion = compilerVersion;
this.reporter = reporter;
}
@@ -169,7 +172,7 @@
this.factory = new DexItemFactory();
this.rulesConsumer = null;
this.includeOriginComments = false;
- this.fakeCompilerVersion = null;
+ this.compilerVersion = null;
this.reporter = new Reporter();
}
diff --git a/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesExtractR8RulesTest.java b/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesExtractR8RulesTest.java
index 36e68ca..62451c9 100644
--- a/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesExtractR8RulesTest.java
+++ b/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesExtractR8RulesTest.java
@@ -109,7 +109,7 @@
builder
.setRulesConsumer((s, h) -> resultBuilder.append(s))
.setIncludeOriginComments(includeOriginComments)
- .setFakeCompilerVersion(compilerVersion)
+ .setCompilerVersion(compilerVersion)
.build();
ExtractR8Rules.run(command);
String extractedRules = resultBuilder.toString();
@@ -129,7 +129,7 @@
builder
.setRulesOutputPath(rulesOutput)
.setIncludeOriginComments(includeOriginComments)
- .setFakeCompilerVersion(compilerVersion)
+ .setCompilerVersion(compilerVersion)
.build();
ExtractR8Rules.run(command);
String extractedRules = FileUtils.readTextFile(rulesOutput, StandardCharsets.UTF_8);
diff --git a/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesR8SpecificTest.java b/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesR8SpecificTest.java
index 6ff21ff..148af6a 100644
--- a/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesR8SpecificTest.java
+++ b/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesR8SpecificTest.java
@@ -148,7 +148,7 @@
builder
.addProgramFiles(library)
.setRulesConsumer((s, h) -> resultBuilder.append(s))
- .setFakeCompilerVersion(compilerVersion)
+ .setCompilerVersion(compilerVersion)
.build();
ExtractR8Rules.run(command);
assertEquals(expected, resultBuilder.toString());