Bug 1352599 - Part 2: Add PROGUARD_JAR configure option. r=chmanchester draft
authorNick Alexander <nalexander@mozilla.com>
Mon, 02 Oct 2017 15:26:25 -0700
changeset 676207 eb1bc4ddbb23ca2ccf18760e2e53b01e8b5517e3
parent 676206 db3fab1b245188309681e17ba32497d24d77a2d2
child 676208 a903fc69edaed33c346323d99198d9a6b4dba36a
push id83424
push usernalexander@mozilla.com
push dateFri, 06 Oct 2017 20:34:02 +0000
reviewerschmanchester
bugs1352599
milestone58.0a1
Bug 1352599 - Part 2: Add PROGUARD_JAR configure option. r=chmanchester MozReview-Commit-ID: 9ZHaGfmrjNI
build/moz.configure/java.configure
mobile/android/base/Makefile.in
mobile/android/config/mozconfigs/common
--- a/build/moz.configure/java.configure
+++ b/build/moz.configure/java.configure
@@ -35,17 +35,17 @@ def check_java_tool(tool):
         if result is None:
             die("The program %s was not found.  Set $JAVA_HOME to your Java "
                 "SDK directory or use '--with-java-bin-path={java-bin-dir}'"
                 % tool)
         return result
 
     return require_tool
 
-check_java_tool('java')
+java = check_java_tool('java')
 check_java_tool('javah')
 check_java_tool('jar')
 check_java_tool('jarsigner')
 check_java_tool('keytool')
 javac = check_java_tool('javac')
 
 @depends(javac)
 @checking('for javac version')
@@ -55,8 +55,58 @@ def javac_version(javac):
         output = subprocess.check_output([javac, '-version'],
                                          stderr=subprocess.STDOUT).rstrip()
         version = Version(output.split(' ')[-1])
         if version < '1.8':
             die('javac 1.8 or higher is required (found %s). Check the JAVA_HOME environment variable.' % version)
         return version
     except subprocess.CalledProcessError as e:
         die('Failed to get javac version: %s', e.output)
+
+# Proguard detection
+# ========================================================
+@dependable
+@imports('os')
+def proguard_jar_default():
+    # By default, look for proguard.jar in the location to which `mach
+    # bootstrap` or `mach artifact toolchain` will install Proguard.
+    mozbuild_state_dir = os.environ.get('MOZBUILD_STATE_PATH',
+                                        os.path.expanduser(os.path.join('~', '.mozbuild')))
+    return os.path.join(mozbuild_state_dir, 'proguard', 'lib', 'proguard.jar')
+
+# Proguard is really required; we provide a good error message when
+# validating.
+option(env='PROGUARD_JAR', nargs=1, default=proguard_jar_default, help='Path to proguard.jar')
+
+@depends(java, 'PROGUARD_JAR')
+@checking('for proguard.jar version')
+@imports('os')
+@imports('subprocess')
+# Not Python 3 compatible, but neither is the subprocess invocation.
+@imports(_from='exceptions', _import='Exception')
+def valid_proguard(java, proguard_jar):
+    if not proguard_jar or not os.path.isfile(proguard_jar[0]):
+        die('proguard.jar 5.3.3 or higher is required (looked for %s). '
+            'Run |mach bootstrap| or add '
+            '`export PROGUARD_JAR=/path/to/proguard.jar` to your mozconfig.' % proguard_jar[0])
+
+    try:
+        # Exit code zero shouldn't happen, but roll with it.
+        output = subprocess.check_output([java, '-jar', proguard_jar[0]])
+
+    except subprocess.CalledProcessError as e:
+        # Exit code is non zero and output is like
+        # ProGuard, version 5.3.3
+        # Usage: java proguard.ProGuard [options ...]
+        output = e.output
+
+    try:
+        version = Version(e.output.splitlines()[0].split(' ')[-1])
+        if version < '5.3.3':
+            die('proguard.jar 5.3.3 or higher is required (found %s). '
+                'Run |mach bootstrap| to upgrade. ' % version)
+
+        return proguard_jar[0]
+
+    except Exception as e:
+        die('Failed to determine proguard.jar version: %s' % e)
+
+set_config('PROGUARD_JAR', valid_proguard)
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -280,37 +280,37 @@ classycle_jar := $(topsrcdir)/mobile/and
 # sometimes corrupt this information if present (which it does for some of the
 # included libraries). This corruption prevents dex from completing, so we need
 # to get rid of it.  This prevents us from seeing line numbers in stack traces
 # for stack frames inside libraries.
 #
 # This step can occur much earlier than the main Proguard pass: it needs only
 # gecko-R.jar to have been compiled (as that's where the library R.java files
 # end up), but it does block the main Proguard pass.
-.bundled.proguard.deps: gecko-R.jar $(proguard_config_dir)/strip-libs.cfg
+.bundled.proguard.deps: gecko-R.jar $(proguard_config_dir)/strip-libs.cfg $(PROGUARD_JAR)
 	$(REPORT_BUILD)
 	@$(TOUCH) $@
 	$(JAVA) \
 		-Xmx512m -Xms128m \
-		-jar $(ANDROID_SDK_ROOT)/tools/proguard/lib/proguard.jar \
+		-jar $(PROGUARD_JAR) \
 		@$(proguard_config_dir)/strip-libs.cfg \
 		-injars $(subst ::,:,$(java_bundled_libs))\
 		-outjars bundled-jars-nodebug \
 		-libraryjars $(library_jars):gecko-R.jar
 
 # We touch the target file before invoking Proguard so that Proguard's
 # outputs are fresher than the target, preventing a subsequent
 # invocation from thinking Proguard's outputs are stale.  This is safe
 # because Make removes the target file if any recipe command fails.
-.proguard.deps: .geckoview.deps .bundled.proguard.deps $(ALL_JARS) $(proguard_config_dir)/proguard.cfg
+.proguard.deps: .geckoview.deps .bundled.proguard.deps $(ALL_JARS) $(proguard_config_dir)/proguard.cfg $(PROGUARD_JAR)
 	$(REPORT_BUILD)
 	@$(TOUCH) $@
 	$(JAVA) \
 		-Xmx512m -Xms128m \
-		-jar $(ANDROID_SDK_ROOT)/tools/proguard/lib/proguard.jar \
+		-jar $(PROGUARD_JAR) \
 		@$(proguard_config_dir)/proguard.cfg \
 		-optimizationpasses $(PROGUARD_PASSES) \
 		-injars $(subst ::,:,$(all_jars_classpath)):bundled-jars-nodebug \
 		-outjars jars-proguarded \
 		-libraryjars $(library_jars)
 
 ANNOTATION_PROCESSOR_JAR_FILES := $(abspath $(DEPTH)/build/annotationProcessors/annotationProcessors.jar)
 
--- a/mobile/android/config/mozconfigs/common
+++ b/mobile/android/config/mozconfigs/common
@@ -103,8 +103,10 @@ fi
 export SOCORRO_SYMBOL_UPLOAD_TOKEN_FILE=/builds/crash-stats-api.token
 
 # Package js shell.
 export MOZ_PACKAGE_JSSHELL=1
 
 . "$topsrcdir/build/unix/mozconfig.stdcxx"
 
 JS_BINARY="$topsrcdir/mobile/android/config/js_wrapper.sh"
+
+export PROGUARD_JAR="$topsrcdir/proguard/lib/proguard.jar"