Skip to content

Commit

Permalink
Fix Mac OSX + OpenJDK builds where JAVA_HOME contains libexec but…
Browse files Browse the repository at this point in the history
… not `lib` (#119)

* Allow JAVA_HOME values to contain libexec and not lib.

* Fixed bug

* Python 3.12 build

* Remove possibly unnecessary JAVA_HOME checks and improve logging.

* Recursively find the directory containing the relevant JDK files.

* Upated readme

* Better java home error message

* Fixed usage of walk.
  • Loading branch information
chipkent authored Dec 18, 2023
1 parent 4f69ed8 commit b502d4c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Install a JDK 8, preferably the Oracle distribution. Set `JDK_HOME` or

$ export JDK_HOME=<your-jdk-dir>
$ export JAVA_HOME=$JDK_HOME
$ pip install setuptools wheel
$ python setup.py build maven bdist_wheel

On success, the wheel is found in the `dist` directory.
Expand All @@ -68,12 +69,14 @@ SDK 7.1::
> SET DISTUTILS_USE_SDK=1
> C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\setenv /x64 /release
> SET JDK_HOME=<your-jdk-dir>
> pip install setuptools wheel
> python setup.py build maven bdist_wheel

With Visual Studio 14 and higher it is much easier::

> SET VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\
> SET JDK_HOME=<your-jdk-dir>
> pip install setuptools wheel
> python setup.py build maven bdist_wheel

On success, the wheel can be found in the `dist` directory.
Expand Down
42 changes: 37 additions & 5 deletions jpyutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,42 @@ def find_jdk_home_dir():
dedicated environment variables.
:return: pathname if found, else None
"""

def is_jdk_dir(path):
rst = os.path.exists(os.path.join(path, 'include')) and os.path.exists(os.path.join(path, 'lib'))
logger.debug(f'Checking {path} for JDK...: {"yes" if rst else "no"}')
return rst

def walk_to_jdk(path):
if is_jdk_dir(path):
return path

for root, dir_names, file_names in os.walk(path):
for d in dir_names:
p = os.path.join(root, d)

if p and is_jdk_dir(p):
return p

return None

for name in JDK_HOME_VARS:
jdk_home_dir = os.environ.get(name, None)
if jdk_home_dir \
and os.path.exists(os.path.join(jdk_home_dir, 'include')) \
and os.path.exists(os.path.join(jdk_home_dir, 'lib')):
return jdk_home_dir
if jdk_home_dir:
logger.debug(f'JAVA_HOME set by environment variable to {jdk_home_dir}')

if is_jdk_dir(jdk_home_dir):
return jdk_home_dir

jdk_dir = walk_to_jdk(jdk_home_dir)

if jdk_dir:
logger.error(f'JAVA_HOME set by environment variable to {jdk_home_dir} but no no "include" or "lib" directory found. Possibly you meant {jdk_dir}?')
else:
logger.error(f'JAVA_HOME set by environment variable to {jdk_home_dir} but no no "include" or "lib" directory found. Does not appear to be a JDK directory.')

exit(1)

logger.debug('Checking Maven for JAVA_HOME...')
try:
output = subprocess.check_output(['mvn', '-v'])
Expand All @@ -152,7 +182,9 @@ def find_jdk_home_dir():
if part.startswith('Java home:'):
path = part.split(':')[1].strip()
if path.endswith('jre'):
return path[0:-3]
java_home = path[0:-3]
logger.debug(f'JAVA_HOME set by maven to {java_home}')
return java_home

except Exception:
# maven probably isn't installed or not on PATH
Expand Down

0 comments on commit b502d4c

Please sign in to comment.