Revert "Clean-up top-level r8 entry-points to not use shadow-jar"

This reverts commit 34b678c668e0333482bd419c2ca99e0a7688f716.

Revert "Use relocator for relocating in R8"

This reverts commit 8c812bd47a2742f46ab0860f951a03d07dcccf92.

Revert "Use r8withrelocateddeps for r8lib"

This reverts commit aa44a9bab513651ffb2c460d3c4ecc9b8846bb02.

Change-Id: I86aadada59cd9b55840824fc7bff24f8a1ac41bf
diff --git a/build.gradle b/build.gradle
index 7892722..12d14c9 100644
--- a/build.gradle
+++ b/build.gradle
@@ -284,6 +284,7 @@
 def r8LibPath = "$buildDir/libs/r8lib.jar"
 def r8LibExludeDepsPath = "$buildDir/libs/r8lib-exclude-deps.jar"
 def r8LibGeneratedKeepRulesPath = "$buildDir/generated/keep.txt"
+def r8LibGeneratedKeepRulesExcludeDepsPath = "$buildDir/generated/keep-exclude-deps.txt"
 def r8LibTestPath = "$buildDir/classes/r8libtest"
 def java11ClassFiles = "build/classes/java/mainJava11"
 
@@ -694,9 +695,33 @@
     }
 }
 
+static configureRelocations(ShadowJar task) {
+    task.relocate('com.google.common', 'com.android.tools.r8.com.google.common')
+    task.relocate('com.google.gson', 'com.android.tools.r8.com.google.gson')
+    task.relocate('com.google.thirdparty', 'com.android.tools.r8.com.google.thirdparty')
+    task.relocate('joptsimple', 'com.android.tools.r8.joptsimple')
+    task.relocate('org.objectweb.asm', 'com.android.tools.r8.org.objectweb.asm')
+    task.relocate('it.unimi.dsi.fastutil', 'com.android.tools.r8.it.unimi.dsi.fastutil')
+    task.relocate('kotlin', 'com.android.tools.r8.jetbrains.kotlin')
+    task.relocate('kotlinx', 'com.android.tools.r8.jetbrains.kotlinx')
+    task.relocate('org.jetbrains', 'com.android.tools.r8.org.jetbrains')
+    task.relocate('org.intellij', 'com.android.tools.r8.org.intellij')
+}
+
+task repackageDepsNoRelocate(type: ShadowJar) {
+    configurations = [project.configurations.runtimeClasspath]
+    mergeServiceFiles(it)
+    exclude { it.getRelativePath().getPathString() == "module-info.class" }
+    exclude { it.getRelativePath().getPathString().startsWith("META-INF/maven/") }
+    baseName 'deps-not-relocated'
+}
+
 task repackageDeps(type: ShadowJar) {
     configurations = [project.configurations.runtimeClasspath]
     mergeServiceFiles(it)
+    if (!project.hasProperty('lib_no_relocate')) {
+        configureRelocations(it)
+    }
     exclude { it.getRelativePath().getPathString() == "module-info.class" }
     exclude { it.getRelativePath().getPathString().startsWith("META-INF/maven/") }
     baseName 'deps'
@@ -705,6 +730,9 @@
 task repackageSources(type: ShadowJar) {
     from sourceSets.main.output
     mergeServiceFiles(it)
+    if (!project.hasProperty('lib_no_relocate')) {
+        configureRelocations(it)
+    }
     baseName 'sources'
 }
 
@@ -712,161 +740,100 @@
     dependsOn compileMainWithJava11
     from file(java11ClassFiles)
     mergeServiceFiles(it)
+    if (!project.hasProperty('lib_no_relocate')) {
+        configureRelocations(it)
+    }
     baseName 'sources11'
 }
 
-def r8CreateTask(name, baseNameName, sources, includeSwissArmyKnife) {
-    return tasks.create("r8Create${name}", ShadowJar) {
-        from consolidatedLicense.outputs.files
-        from sources
-        baseName baseNameName
-        classifier = null
-        version = null
-        if (includeSwissArmyKnife) {
-            manifest {
-                attributes 'Main-Class': 'com.android.tools.r8.SwissArmyKnife'
-            }
-        }
-        exclude "META-INF/*.kotlin_module"
-        exclude "**/*.kotlin_metadata"
+task r8WithRelocatedDeps(type: ShadowJar) {
+    from consolidatedLicense.outputs.files
+    baseName 'r8_with_relocated_deps'
+    classifier = null
+    version = null
+    manifest {
+        attributes 'Main-Class': 'com.android.tools.r8.SwissArmyKnife'
     }
+    from repackageSources.outputs.files
+    from repackageDeps.outputs.files
+    configureRelocations(it)
+    exclude "META-INF/*.kotlin_module"
+    exclude "**/*.kotlin_metadata"
 }
 
-def r8RelocateTask(r8Task, output) {
-    return tasks.create("r8Relocate_${r8Task.name}", Exec) {
-        dependsOn r8WithDeps
-        dependsOn r8Task
-        outputs.file output
-        workingDir = projectDir
-        inputs.files r8Task.outputs.files + r8WithDeps.outputs.files
-        commandLine baseR8CommandLine([
-                "relocator",
-                "--input",
-                r8Task.outputs.files[0],
-                "--output",
-                output,
-                "--map",
-                "com.google.common->com.android.tools.r8.com.google.common",
-                "--map",
-                "com.google.gson->com.android.tools.r8.com.google.gson",
-                "--map",
-                "com.google.thirdparty->com.android.tools.r8.com.google.thirdparty",
-                "--map",
-                "joptsimple->com.android.tools.r8.joptsimple",
-                "--map",
-                "org.objectweb.asm->com.android.tools.r8.org.objectweb.asm",
-                "--map",
-                "it.unimi.dsi.fastutil->com.android.tools.r8.it.unimi.dsi.fastutil",
-                "--map",
-                "kotlin->com.android.tools.r8.jetbrains.kotlin",
-                "--map",
-                "kotlinx->com.android.tools.r8.jetbrains.kotlinx",
-                "--map",
-                "org.jetbrains->com.android.tools.r8.org.jetbrains",
-                "--map",
-                "org.intellij->com.android.tools.r8.org.intellij"
-        ])
+task r8WithRelocatedDeps11(type: ShadowJar) {
+    from consolidatedLicense.outputs.files
+    baseName 'r8_with_relocated_deps_11'
+    classifier = null
+    version = null
+    manifest {
+        attributes 'Main-Class': 'com.android.tools.r8.SwissArmyKnife'
     }
+    from repackageSources11.outputs.files
+    from repackageDeps.outputs.files
+    configureRelocations(it)
+    exclude "META-INF/*.kotlin_module"
+    exclude "**/*.kotlin_metadata"
 }
 
-task r8WithDeps {
-    dependsOn repackageSources11
-    dependsOn repackageDeps
-    def r8Task = r8CreateTask(
-            'WithDeps',
-            'r8_with_deps',
-            repackageSources.outputs.files + repackageDeps.outputs.files,
-            true)
-    dependsOn r8Task
-    outputs.files r8Task.outputs.files
+task r8WithoutDeps(type: ShadowJar) {
+    from consolidatedLicense.outputs.files
+    baseName 'r8_without_deps'
+    classifier = null
+    version = null
+    manifest {
+        attributes 'Main-Class': 'com.android.tools.r8.SwissArmyKnife'
+    }
+    from sourceSets.main.output
 }
 
-task r8WithDeps11 {
-    dependsOn repackageSources
-    dependsOn repackageDeps
-    def r8Task = r8CreateTask(
-            'WithDeps11',
-            'r8_with_deps_11',
-            repackageSources.outputs.files + repackageDeps.outputs.files,
-            true)
-    dependsOn r8Task
-    outputs.files r8Task.outputs.files
+task R8(type: ShadowJar) {
+    from consolidatedLicense.outputs.files
+    baseName 'r8'
+    classifier = null
+    version = null
+    manifest {
+        attributes 'Main-Class': 'com.android.tools.r8.SwissArmyKnife'
+    }
+    // In order to build without dependencies, pass the exclude_deps property using:
+    // gradle -Pexclude_deps R8
+    if (!project.hasProperty('exclude_deps')) {
+        from repackageSources.outputs.files
+        from repackageDeps.outputs.files
+    } else {
+        from sourceSets.main.output
+    }
+    exclude "META-INF/*.kotlin_module"
+    exclude "**/*.kotlin_metadata"
 }
 
-task r8WithRelocatedDeps {
-    def output = "${buildDir}/libs/r8_with_relocated_deps.jar"
-    dependsOn r8RelocateTask(r8WithDeps, output)
-    outputs.file output
+task R8NoManifestNoDeps(type: ShadowJar) {
+    from consolidatedLicense.outputs.files
+    baseName 'r8nomanifest-exclude-deps'
+    classifier = null
+    version = null
+    from sourceSets.main.output
 }
 
-task r8WithRelocatedDeps11 {
-    def output = "${buildDir}/libs/r8_with_relocated_deps_11.jar"
-    dependsOn r8RelocateTask(r8WithDeps11, output)
-    outputs.file output
-}
-
-task r8WithoutDeps {
-    dependsOn repackageSources
-    def r8Task = r8CreateTask(
-            'WithoutDeps',
-            'r8_without_deps',
-            repackageSources.outputs.files,
-            true)
-    dependsOn r8Task
-    outputs.files r8Task.outputs.files
-}
-
-task r8(type: Copy) {
-    def r8Task = project.hasProperty("exclude_deps")
-            ? r8WithoutDeps : r8WithRelocatedDeps
-    dependsOn r8Task
-    from r8Task.outputs.files[0]
-    into file("${buildDir}/libs")
-    rename { String fileName -> "r8.jar" }
-    outputs.file "${buildDir}/libs/r8.jar"
-}
-
-task r8NoManifestWithoutDeps {
-    dependsOn repackageSources
-    def r8Task = r8CreateTask(
-            'NoManifestWithoutDeps',
-            'r8_no_manifest_without_deps',
-            repackageSources.outputs.files,
-            false)
-    dependsOn r8Task
-    outputs.files r8Task.outputs.files
-}
-
-task r8NoManifestWithDeps {
-    dependsOn repackageSources
-    def r8Task = r8CreateTask(
-            'NoManifestWithDeps',
-            'r8_no_manifest_with_deps',
-            repackageSources.outputs.files + repackageDeps.outputs.files,
-            false)
-    dependsOn r8Task
-    outputs.files r8Task.outputs.files
-}
-
-task r8NoManifestWithRelocatedDeps {
-    def output = "${buildDir}/libs/r8_no_manifest_with_relocated_deps.jar"
-    dependsOn r8RelocateTask(r8NoManifestWithDeps, output)
-    outputs.file output
-}
-
-task r8NoManifest(type: Copy) {
-    def r8Task = project.hasProperty("exclude_deps")
-            ? r8NoManifestWithoutDeps : r8NoManifestWithRelocatedDeps
-    dependsOn r8Task
-    from r8Task.outputs.files[0]
-    into file("${buildDir}/libs")
-    rename { String fileName -> "r8_no_manifest.jar" }
-    outputs.file "${buildDir}/libs/r8_no_manifest.jar"
+task R8NoManifest(type: ShadowJar) {
+    from consolidatedLicense.outputs.files
+    baseName 'r8nomanifest'
+    classifier = null
+    version = null
+    // In order to build without dependencies, pass the exclude_deps property using:
+    // gradle -Pexclude_deps R8
+    if (!project.hasProperty('exclude_deps')) {
+        from repackageSources.outputs.files
+        from repackageDeps.outputs.files
+    } else {
+        from sourceSets.main.output
+    }
+    exclude "META-INF/*.kotlin_module"
+    exclude "**/*.kotlin_metadata"
 }
 
 task D8(type: ShadowJar) {
-    dependsOn r8
-    from r8.outputs.files[0]
+    from R8.outputs.files
     baseName 'd8'
     manifest {
         attributes 'Main-Class': 'com.android.tools.r8.D8'
@@ -874,10 +841,10 @@
 }
 
 def baseR8CommandLine(args = []) {
-    // Execute r8 commands against a stable r8 with dependencies.
+    // Execute r8 commands against a stable r8 with relocated dependencies.
     // TODO(b/139725780): See if we can remove or lower the heap size (-Xmx6g).
     return [org.gradle.internal.jvm.Jvm.current().getJavaExecutable(),
-            "-Xmx8g", "-ea", "-jar", r8WithDeps.outputs.files[0]] + args
+            "-Xmx8g", "-ea", "-jar", r8WithRelocatedDeps.outputs.files[0]] + args
 }
 
 def r8CfCommandLine(input, output, pgConfs = [], args = ["--release"], libs = []) {
@@ -923,23 +890,23 @@
         // TODO(b/154785341): We should remove this.
         standardOutput new FileOutputStream(r8LibGeneratedKeepRulesPath)
     }
-    dependsOn r8NoManifest
+    dependsOn R8NoManifest
     dependsOn r8WithRelocatedDeps
     dependsOn testJar
     dependsOn downloadOpenJDKrt
-    inputs.files ([r8WithRelocatedDeps.outputs, r8NoManifest.outputs, testJar.outputs])
+    inputs.files ([r8WithRelocatedDeps.outputs, R8NoManifest.outputs, testJar.outputs])
     outputs.file r8LibGeneratedKeepRulesPath
     commandLine baseR8CommandLine([
             "printuses",
             "--keeprules-allowobfuscation",
             "third_party/openjdk/openjdk-rt-1.8/rt.jar",
-            r8NoManifest.outputs.files[0],
+            R8NoManifest.outputs.files[0],
             testJar.outputs.files[0]])
     workingDir = projectDir
 }
 
 task R8LibApiOnly {
-    dependsOn r8LibCreateTask("Api", ["src/main/keep.txt"], r8NoManifest, r8LibPath)
+    dependsOn r8LibCreateTask("Api", ["src/main/keep.txt"], R8NoManifest, r8LibPath)
     outputs.file r8LibPath
 }
 
@@ -949,7 +916,7 @@
             ["src/main/keep.txt",
              "src/main/keep-applymapping.txt",
              generateR8LibKeepRules.outputs.files[0]],
-            r8NoManifestWithRelocatedDeps,
+            R8NoManifest,
             r8LibPath,
     ).dependsOn(generateR8LibKeepRules)
     outputs.file r8LibPath
@@ -959,11 +926,11 @@
     dependsOn r8LibCreateTask(
             "NoDeps",
             ["src/main/keep.txt", "src/main/keep-applymapping.txt"],
-            r8NoManifestWithoutDeps,
+            R8NoManifestNoDeps,
             r8LibExludeDepsPath,
             "--release",
-            repackageDeps.outputs.files
-    ).dependsOn(repackageDeps)
+            repackageDepsNoRelocate.outputs.files
+    ).dependsOn(repackageDepsNoRelocate)
     outputs.file r8LibExludeDepsPath
 }
 
@@ -1772,7 +1739,7 @@
 task buildR8LibCfTestDeps(type: Exec) {
     def outputPath = "build/libs/r8libtestdeps-cf.jar"
     dependsOn downloadDeps
-    dependsOn r8NoManifest
+    dependsOn R8NoManifest
     dependsOn R8Lib
     dependsOn generateR8TestKeepRules
     dependsOn testJar
@@ -1787,8 +1754,8 @@
             testJar.outputs.files[0],
             outputPath,
             [generateR8TestKeepRules.outputs.files[0]],
-            ["--debug", "--classpath", r8NoManifest.outputs.files[0]],
-            r8NoManifest.outputs.files + addedLibraries)
+            ["--debug", "--classpath", R8NoManifest.outputs.files[0]],
+            R8NoManifest.outputs.files + addedLibraries)
     workingDir = projectDir
     outputs.file outputPath
 }
@@ -1856,7 +1823,7 @@
     dependsOn buildLibraryDesugarConversions
     dependsOn getJarsFromSupportLibs
     // R8.jar is required for running bootstrap tests.
-    dependsOn r8
+    dependsOn R8
     testLogging.exceptionFormat = 'full'
     if (project.hasProperty('print_test_stdout')) {
         testLogging.showStandardStreams = true
diff --git a/src/test/java/com/android/tools/r8/relocator/RelocatorTest.java b/src/test/java/com/android/tools/r8/relocator/RelocatorTest.java
index 9796387..5e66581 100644
--- a/src/test/java/com/android/tools/r8/relocator/RelocatorTest.java
+++ b/src/test/java/com/android/tools/r8/relocator/RelocatorTest.java
@@ -251,8 +251,6 @@
   private void inspectAllClassesRelocated(
       Path original, Path relocated, String originalPrefix, String newPrefix)
       throws IOException, ExecutionException {
-    assert !originalPrefix.endsWith("" + DescriptorUtils.JAVA_PACKAGE_SEPARATOR)
-        && !newPrefix.endsWith("" + DescriptorUtils.JAVA_PACKAGE_SEPARATOR);
     CodeInspector originalInspector = new CodeInspector(original);
     CodeInspector relocatedInspector = new CodeInspector(relocated);
     for (FoundClassSubject clazz : originalInspector.allClasses()) {