From ffb1fc37dfa5ce4b9608b5e0c881f3dde113b184 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Mon, 25 Feb 2013 15:37:33 +0000 Subject: [PATCH] Java API: New JDK detection routines. Signed-off-by: Christoph M. Wintersteiger --- scripts/mk_util.py | 190 ++++++++++++++++++++++----------------------- 1 file changed, 94 insertions(+), 96 deletions(-) diff --git a/scripts/mk_util.py b/scripts/mk_util.py index 5d2fb4e17..c70b1d527 100644 --- a/scripts/mk_util.py +++ b/scripts/mk_util.py @@ -30,13 +30,12 @@ CC=getenv("CC", None) CPPFLAGS=getenv("CPPFLAGS", "") CXXFLAGS=getenv("CXXFLAGS", "") LDFLAGS=getenv("LDFLAGS", "") -JAVA=getenv("JAVA", "java") -JAVAC=getenv("JAVAC", "javac") -JAVA_HOME=getenv("JAVA_HOME", None) +JDK_HOME=getenv("JDK_HOME", None) JNI_HOME=getenv("JNI_HOME", None) CXX_COMPILERS=['g++', 'clang++'] C_COMPILERS=['gcc', 'clang'] +JAVAC=None PYTHON_PACKAGE_DIR=distutils.sysconfig.get_python_lib() BUILD_DIR='build' REV_BUILD_DIR='..' @@ -207,24 +206,6 @@ def test_openmp(cc): t.commit() return exec_compiler_cmd([cc, CPPFLAGS, 'tstomp.cpp', LDFLAGS, '-fopenmp']) == 0 -def check_java(): - t = TempFile('Hello.java') - t.add('public class Hello { public static void main(String[] args) { System.out.println("Hello, World"); }}\n') - t.commit() - if is_verbose(): - print("Testing %s..." % JAVAC) - r = exec_cmd([JAVAC, 'Hello.java']) - if r != 0: - raise MKException('Failed testing Java compiler. Set environment variable JAVAC with the path to the Java compiler') - if is_verbose(): - print("Testing %s..." % JAVA) - r = exec_cmd([JAVA, 'Hello']) - rmf('Hello.class') - if r != 0: - raise MKException('Failed testing Java program. Set environment variable JAVA with the path to the Java virtual machine') - find_java_home() - find_jni_home() - def find_jni_h(path): for root, dirs, files in os.walk(path): for f in files: @@ -232,76 +213,97 @@ def find_jni_h(path): return root return False -def find_java_home(): - global JAVA_HOME - if JAVA_HOME != None: - if IS_WINDOWS: - ind = '%s%s' % (JAVA_HOME, '\\bin\\java.exe') - else: - ind = '%s%s' % (JAVA_HOME, '/bin/java') - if not os.path.exists(ind): - raise MKException("Failed to detect java at '%s'.Possible solution: set JAVA_HOME with the path to JDK." % os.path.join(JAVA_HOME)) - else: - return - if is_verbose(): - print("Finding JAVA_HOME...") - t = TempFile('output') - null = open(os.devnull, 'wb') - try: - subprocess.call([JAVA, '-verbose'], stdout=t.fname, stderr=null) - t.commit() - except: - raise MKException('Failed to find JAVA_HOME') - open_pat = re.compile("\[Opened (.*)\]") - t = open('output', 'r') - for line in t: - m = open_pat.match(line) - if m: - # Remove last 3 directives from m.group(1) - tmp = m.group(1).split(os.sep) - path = string.join(tmp[:len(tmp) - 3], os.sep) - if IS_WINDOWS: - ind = '%s%s' % (path, '\\bin\\java.exe') - else: - ind = '%s%s' % (path, '/bin/java') - if os.path.exists(ind): - JAVA_HOME = path - return - if IS_OSX: - path = '%s%s' % (path, '/Contents/Home/') - ind = '%s%s' % (path, 'bin/java') - if os.path.exists(ind): - JAVA_HOME = path - return - raise MKException("Failed to detect java at '%s'.Possible solution: set JAVA_HOME with the path to JDK." % os.path.join(path)) - return - raise MKException('Failed to find JAVA_HOME') - -def find_jni_home(): +def check_java(): + global JDK_HOME global JNI_HOME - if JNI_HOME != None: - if is_verbose(): - print("Checking jni.h...") - path = JNI_HOME - fn = os.path.join(path, 'jni.h') - print("Checking for jni.h in %s..." % JNI_HOME) - if os.path.exists(fn): - return + global JAVAC + + if is_verbose(): + print("Finding JDK_HOME...") + + if JDK_HOME != None: + if IS_WINDOWS: + JAVAC = os.path.join(JDK_HOME, 'bin', 'javac.exe') + else: + JAVAC = os.path.join(JDK_HOME, 'bin', 'javac') + + if not os.path.exists(JAVAC): + raise MKException("Failed to detect javac at '%s/bin'; the environment variable JDK_HOME is probably set to the wrong path." % os.path.join(JDK_HOME)) else: - path = '%s%s' % (JAVA_HOME, '/include/') - fn = '%s%s' % (path, 'jni.h') - print("Checking for jni.h in %s..." % path) - if os.path.exists(fn): - JNI_HOME = find_jni_h(path) - elif IS_OSX: - # Apparently Apple knows best where to put stuff... - path = '/System/Library/Frameworks/JavaVM.framework/Headers/' - fn = '%s%s' % (path, 'jni.h') - print("Checking for jni.h in %s..." % path) - if os.path.exists(fn): - JNI_HOME = find_jni_h(path) - if JNI_HOME == None: - raise MKException("Failed to detect jni.h. Possible solution: set JNI_HOME with the path to JDK.") + # Search for javac in the path. + ind = 'javac'; + if IS_WINDOWS: + ind = ind + '.exe' + paths = os.getenv('path', None) + spaths = paths.split(os.pathsep) + for i in range(0, len(spaths)): + cmb = os.path.join(spaths[i], ind) + if os.path.exists(cmb): + JAVAC = cmb + break + + if JAVAC == None: + raise MKException('No java compiler in the path, please adjust your PATH or set JDK_HOME to the location of the JDK.') + + if is_verbose(): + print("Testing %s..." % JAVAC) + + t = TempFile('Hello.java') + t.add('public class Hello { public static void main(String[] args) { System.out.println("Hello, World"); }}\n') + t.commit() + + oo = TempFile('output') + eo = TempFile('errout') + try: + subprocess.call([JAVAC, 'Hello.java', '-verbose'], stdout=oo.fname, stderr=eo.fname) + oo.commit() + eo.commit() + except: + raise MKException('Found, but failed to run Java compiler at %s' % (JAVAC)) + + os.remove('Hello.class') + + if is_verbose(): + print("Finding jni.h...") + + if JNI_HOME != None: + if not os.path.exists(path.join(JNI_HOME, 'jni.h')): + raise MKException("Failed to detect jni.h '%s'; the environment variable JNI_HOME is probably set to the wrong path." % os.path.join(JNI_HOME)) + else: + # Search for jni.h in the library directories... + t = open('errout', 'r') + open_pat = re.compile("\[search path for class files: (.*)\]") + cdirs = [] + for line in t: + m = open_pat.match(line) + if m: + libdirs = m.group(1).split(',') + for libdir in libdirs: + q = os.path.dirname(libdir) + if cdirs.count(q) == 0: + cdirs.append(q) + + # ... plus some heuristic ones. + extra_dirs = [] + + # For the libraries, even the JDK usually uses a JRE that comes with it. To find the + # headers we have to go a little bit higher up. + for dir in cdirs: + extra_dirs.append(os.path.abspath(os.path.join(dir, '..'))) + + if IS_OSX: # Apparently Apple knows best where to put stuff... + extra_dirs.append('/System/Library/Frameworks/JavaVM.framework/Headers/') + + cdirs[len(cdirs):] = extra_dirs + + for dir in cdirs: + print dir + q = find_jni_h(dir) + if q != False: + JNI_HOME = q + + if JNI_HOME == None: + raise MKException("Failed to detect jni.h. Possible solution: set JNI_HOME with the path to JDK.") def is64(): return sys.maxsize >= 2**32 @@ -436,9 +438,7 @@ def display_help(exit_code): print(" LDFLAGS Linker flags, e.g., -L if you have libraries in a non-standard directory") print(" CPPFLAGS Preprocessor flags, e.g., -I if you have header files in a non-standard directory") print(" CXXFLAGS C++ compiler flags") - print(" JAVA Java virtual machine (only relevant if -j or --java option is provided)") - print(" JAVAC Java compiler (only relevant if -j or --java option is provided)") - print(" JAVA_HOME JDK installation directory (only relevant if -j or --java option is provided)") + print(" JDK_HOME JDK installation directory (only relevant if -j or --java option is provided)") print(" JNI_HOME JNI bindings directory (only relevant if -j or --java option is provided)") exit(exit_code) @@ -1425,10 +1425,9 @@ def mk_config(): if is_verbose(): print('64-bit: %s' % is64()) if is_java_enabled(): - print('Java Home: %s' % JAVA_HOME) + print('JDK Home: %s' % JDK_HOME) print('JNI Home: %s' % JNI_HOME) print('Java Compiler: %s' % JAVAC) - print('Java VM: %s' % JAVA) else: global CXX, CC, GMP, CPPFLAGS, CXXFLAGS, LDFLAGS ARITH = "internal" @@ -1530,10 +1529,9 @@ def mk_config(): print('gprof: enabled') print('Python version: %s' % distutils.sysconfig.get_python_version()) if is_java_enabled(): - print('Java Home: %s' % JAVA_HOME) + print('JDK Home: %s' % JDK_HOME) print('JNI Home: %s' % JNI_HOME) print('Java Compiler: %s' % JAVAC) - print('Java VM: %s' % JAVA) def mk_install(out): out.write('install:\n')