chiark / gitweb /
automatically detect various installed JDKs and set JAVA[6-9]_HOME
authorHans-Christoph Steiner <hans@eds.org>
Fri, 5 Feb 2016 23:29:07 +0000 (00:29 +0100)
committerHans-Christoph Steiner <hans@eds.org>
Thu, 11 Feb 2016 20:17:23 +0000 (21:17 +0100)
This checks for which JDKs are installed in common locations, then sets the
JAVA[6-9]_HOME env vars needed by some build environments.

fdroidserver/common.py

index a20aa59b478822ced8a95353ea62bc29fe4e9d55..3473c426929d42dbce4dd86ad2019ee325e12a15 100644 (file)
@@ -62,10 +62,7 @@ default_config = {
         'r10e': "$ANDROID_NDK",
     },
     'build_tools': "23.0.2",
-    'java_paths': {
-        '1.7': "/usr/lib/jvm/java-7-openjdk",
-        '1.8': None,
-    },
+    'java_paths': None,
     'ant': "ant",
     'mvn3': "mvn",
     'gradle': 'gradle',
@@ -131,6 +128,32 @@ def fill_config_defaults(thisconfig):
             thisconfig[k] = exp
             thisconfig[k + '_orig'] = v
 
+    # find all installed JDKs for keytool, jarsigner, and JAVA[6-9]_HOME env vars
+    if thisconfig['java_paths'] is None:
+        thisconfig['java_paths'] = dict()
+        for d in sorted(glob.glob('/usr/lib/jvm/j*[6-9]*')
+                        + glob.glob('/usr/java/jdk1.[6-9]*')
+                        + glob.glob('/System/Library/Java/JavaVirtualMachines/1.[6-9].0.jdk')
+                        + glob.glob('/Library/Java/JavaVirtualMachines/*jdk*[6-9]*')):
+            if os.path.islink(d):
+                continue
+            j = os.path.basename(d)
+            # the last one found will be the canonical one, so order appropriately
+            for regex in (r'1\.([6-9])\.0\.jdk',  # OSX
+                          r'jdk1\.([6-9])\.0_[0-9]+.jdk',  # OSX and Oracle tarball
+                          r'jdk([6-9])-openjdk',  # Arch
+                          r'java-1\.([6-9])\.0-.*',  # RedHat
+                          r'java-([6-9])-oracle',  # Debian WebUpd8
+                          r'jdk-([6-9])-oracle-.*',  # Debian make-jpkg
+                          r'java-([6-9])-openjdk-[^c][^o][^m].*'):  # Debian
+                m = re.match(regex, j)
+                if m:
+                    osxhome = os.path.join(d, 'Contents', 'Home')
+                    if os.path.exists(osxhome):
+                        thisconfig['java_paths'][m.group(1)] = osxhome
+                    else:
+                        thisconfig['java_paths'][m.group(1)] = d
+
     for k in ['ndk_paths', 'java_paths']:
         d = thisconfig[k]
         for k2 in d.copy():
@@ -194,10 +217,8 @@ def read_config(opts, config_file='config.py'):
     for n in ['ANDROID_HOME', 'ANDROID_SDK']:
         env[n] = config['sdk_path']
 
-    for v in ['7', '8']:
-        cpath = config['java_paths']['1.%s' % v]
-        if cpath:
-            env['JAVA%s_HOME' % v] = cpath
+    for k, v in config['java_paths'].items():
+        env['JAVA%s_HOME' % k] = v
 
     for k in ["keystorepass", "keypass"]:
         if k in config: