1919import platform
2020import re
2121import os
22+ import subprocess
2223
2324KNOWN_PLATFORMS = (
2425 'linux-x86' , 'linux-x86_64' , 'android-armv6' ,
@@ -267,19 +268,34 @@ def validate_gyp_settings(opts):
267268 if opts ['BUILD_EDITION' ] is None :
268269 opts ['BUILD_EDITION' ] = 'community'
269270
270- def guess_java_home (os ):
271+ def guess_java_home (platform ):
272+ if platform .startswith ('linux' ):
273+ try :
274+ javac_str = '/bin/javac'
275+ javac_path = subprocess .check_output (['/usr/bin/env' ,
276+ 'readlink' , '-f' , '/usr' + javac_str ]).strip ()
277+ if (os .path .isfile (javac_path ) and
278+ javac_path .endswith (javac_str )):
279+ return javac_path [:- len (javac_str )]
280+ except subprocess .CalledProcessError as e :
281+ print (e )
282+ pass # Fall through to other ways of guessing
283+
284+ # More guesses
271285 try :
272- return subprocess .check_output ('/usr/libexec/java_home' )
273- except CalledProcessError as e :
286+ if os .path .isfile ('/usr/libexec/java_home' ):
287+ return subprocess .check_output ('/usr/libexec/java_home' ).strip ()
288+ except subprocess .CalledProcessError as e :
289+ print (e )
274290 pass
275291 for d in ('/usr/lib/jvm/default' , '/usr/lib/jvm/default-java' ):
276292 if os .path .isdir (d ):
277293 return d
278294
279295def validate_java_tools (opts ):
280296 if opts ['JAVA_SDK' ] is None :
281- validate_os (opts )
282- sdk = guess_java_home (os )
297+ validate_platform (opts )
298+ sdk = guess_java_home (opts [ 'PLATFORM' ] )
283299 if sdk is None :
284300 error ('Java SDK not found; set $JAVA_SDK' )
285301 opts ['JAVA_SDK' ] = sdk
@@ -370,7 +386,7 @@ def guess_android_tooldir(name):
370386# in the SDK's build-tools subdirectory. This is pretty fragile if someone
371387# has (potentially?) multiple sets of build tools installed.
372388def guess_android_build_tools (sdkdir ):
373- dirs = listdir (os .path .join (sdkdir , 'build-tools' ))
389+ dirs = os . listdir (os .path .join (sdkdir , 'build-tools' ))
374390 if len (dirs ) == 1 :
375391 return dirs [0 ]
376392 return None
@@ -473,7 +489,9 @@ def gyp_define_args(opts, names):
473489
474490def configure_linux (opts ):
475491 validate_target_arch (opts )
476- args = core_gyp_args (opts ) + ['-Dtarget_arch=' + opts ['TARGET_ARCH' ]]
492+ validate_java_tools (opts )
493+ args = core_gyp_args (opts ) + ['-Dtarget_arch=' + opts ['TARGET_ARCH' ],
494+ '-Djavahome=' + opts ['JAVA_SDK' ]]
477495 exec_gyp (args + opts ['GYP_OPTIONS' ])
478496
479497def configure_emscripten (opts ):
@@ -495,7 +513,8 @@ def configure_android(opts):
495513 'OBJDUMP' , 'STRIP' ))
496514 args = core_gyp_args (opts ) + ['-Dtarget_arch=' + opts ['TARGET_ARCH' ],
497515 '-Dcross_compile=1' ,
498- '-Gandroid_ndk_version=' + opts ['ANDROID_NDK_VERSION' ]]
516+ '-Gandroid_ndk_version=' + opts ['ANDROID_NDK_VERSION' ],
517+ '-Djavahome=' + opts ['JAVA_SDK' ]]
499518 exec_gyp (args + opts ['GYP_OPTIONS' ])
500519
501520def configure_win (opts ):
@@ -515,10 +534,12 @@ def configure_win(opts):
515534def configure_mac (opts ):
516535 validate_target_arch (opts )
517536 validate_xcode_sdks (opts )
537+ validate_java_tools (opts )
518538
519539 args = core_gyp_args (opts ) + ['-Dtarget_sdk=' + opts ['XCODE_TARGET_SDK' ],
520540 '-Dhost_sdk=' + opts ['XCODE_HOST_SDK' ],
521- '-Dtarget_arch=' + opts ['TARGET_ARCH' ]]
541+ '-Dtarget_arch=' + opts ['TARGET_ARCH' ],
542+ '-Djavahome=' + opts ['JAVA_SDK' ]]
522543 exec_gyp (args + opts ['GYP_OPTIONS' ])
523544
524545def configure_ios (opts ):
0 commit comments