| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import subprocess |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
from briefcase.exceptions import ( |
| 9 |
BriefcaseCommandError, |
| 10 |
IncompatibleToolError, |
| 11 |
MissingToolError, |
| 12 |
) |
| 13 |
from briefcase.integrations.base import ManagedTool, ToolCache |
| 14 |
|
| 15 |
|
| 16 |
class JDK(ManagedTool): |
| 17 |
name = "java" |
| 18 |
full_name = "Java JDK" |
| 19 |
|
| 20 |
# Latest OpenJDK as of January 2025: https://adoptium.net/temurin/releases/ |
| 21 |
JDK_MAJOR_VER = "17" |
| 22 |
JDK_RELEASE = "17.0.17" |
| 23 |
JDK_BUILD = "10" |
| 24 |
JDK_INSTALL_DIR_NAME = f"java{JDK_MAJOR_VER}" |
| 25 |
|
| 26 |
def __init__(self, tools: ToolCache, java_home: Path): |
| 27 |
super().__init__(tools=tools) |
| 28 |
self.java_home = java_home |
| 29 |
|
| 30 |
@property |
| 31 |
def OpenJDK_download_url(self): |
| 32 |
"""The OpenJDK download URL appropriate for the current machine.""" |
| 33 |
system_arch = self.tools.host_arch |
| 34 |
# use a 32bit JDK if using 32bit Python on 64bit hardware |
| 35 |
if self.tools.is_32bit_python and self.tools.host_arch == "aarch64": |
| 36 |
system_arch = "armv7l" |
| 37 |
|
| 38 |
try: |
| 39 |
platform, arch, extension = { |
| 40 |
"Darwin": { |
| 41 |
"arm64": ("mac", "aarch64", "tar.gz"), |
| 42 |
"x86_64": ("mac", "x64", "tar.gz"), |
| 43 |
}, |
| 44 |
"Linux": { |
| 45 |
"armv7l": ("linux", "arm", "tar.gz"), |
| 46 |
"armv8l": ("linux", "arm", "tar.gz"), |
| 47 |
"aarch64": ("linux", "aarch64", "tar.gz"), |
| 48 |
"x86_64": ("linux", "x64", "tar.gz"), |
| 49 |
}, |
| 50 |
"Windows": { |
| 51 |
"AMD64": ("windows", "x64", "zip"), |
| 52 |
}, |
| 53 |
}[self.tools.host_os][system_arch] |
| 54 |
except KeyError as e: |
| 55 |
raise IncompatibleToolError(tool=self.full_name, env_var="JAVA_HOME") from e |
| 56 |
|
| 57 |
return ( |
| 58 |
f"https://github.com/adoptium/temurin{self.JDK_MAJOR_VER}-binaries/" |
| 59 |
f"releases/download/jdk-{self.JDK_RELEASE}+{self.JDK_BUILD}/" |
| 60 |
f"OpenJDK{self.JDK_MAJOR_VER}U-jdk_{arch}_{platform}_hotspot_" |
| 61 |
f"{self.JDK_RELEASE}_{self.JDK_BUILD}.{extension}" |
| 62 |
) |
| 63 |
|
| 64 |
@classmethod |
| 65 |
def version_from_path(cls, tools: ToolCache, java_path: str | Path) -> str: |
| 66 |
"""Return a JDK's version from a path by running ``<java_path>/bin/javac``. |
| 67 |
|
| 68 |
This will fail if the path contains a JRE instead of a JDK. |
| 69 |
|
| 70 |
Several exceptions can be raised for issues: |
| 71 |
- OSError - the ``javac`` executable doesn't exist |
| 72 |
- CalledProcessError - ``javac`` returned a non-zero value |
| 73 |
- IndexError - unparsable version value from ``javac`` |
| 74 |
|
| 75 |
:param tools: ToolCache of available tools |
| 76 |
:param java_path: File path to a candidate JDK install |
| 77 |
:return: JDK release version; e.g. "17.0.X" |
| 78 |
""" |
| 79 |
output = tools.subprocess.check_output( |
| 80 |
[Path(java_path) / "bin/javac", "-version"] |
| 81 |
) |
| 82 |
# javac's output should look like "javac 17.0.X\n" |
| 83 |
return output.strip("\n").split(" ")[1] |
| 84 |
|
| 85 |
@classmethod |
| 86 |
def verify_install(cls, tools: ToolCache, install: bool = True, **kwargs) -> JDK: |
| 87 |
"""Verify that a Java JDK exists. |
| 88 |
|
| 89 |
If ``JAVA_HOME`` is set, try that version. If it is a JRE, or its *not* |
| 90 |
a Java JDK, download one. |
| 91 |
|
| 92 |
On macOS, also try invoking /usr/libexec/java_home. If that location |
| 93 |
points to a Java JDK, use it. |
| 94 |
|
| 95 |
Otherwise, download a JDK from OpenJDK and unpack it into the |
| 96 |
briefcase data directory. |
| 97 |
|
| 98 |
:param tools: ToolCache of available tools |
| 99 |
:param install: Should the tool be installed if it is not found? |
| 100 |
:returns: A valid Java JDK wrapper. If a JDK is not available, and was |
| 101 |
not installed, raises MissingToolError. |
| 102 |
""" |
| 103 |
# short circuit since already verified and available |
| 104 |
if hasattr(tools, "java"): |
| 105 |
return tools.java |
| 106 |
|
| 107 |
java = None |
| 108 |
install_message = None |
| 109 |
|
| 110 |
if java_home := tools.os.environ.get("JAVA_HOME", ""): |
| 111 |
tools.console.debug("Evaluating JAVA_HOME...", prefix=cls.full_name) |
| 112 |
tools.console.debug(f"JAVA_HOME={java_home}") |
| 113 |
try: |
| 114 |
version_str = cls.version_from_path(tools, java_home) |
| 115 |
if version_str.split(".")[0] == cls.JDK_MAJOR_VER: |
| 116 |
java = JDK(tools, java_home=Path(java_home)) |
| 117 |
else: |
| 118 |
install_message = f""" |
| 119 |
************************************************************************* |
| 120 |
** WARNING: JAVA_HOME does not point to a Java {cls.JDK_MAJOR_VER} JDK ** |
| 121 |
************************************************************************* |
| 122 |
|
| 123 |
Android requires a Java {cls.JDK_MAJOR_VER} JDK, but the location pointed to by the |
| 124 |
JAVA_HOME environment variable: |
| 125 |
|
| 126 |
{java_home} |
| 127 |
|
| 128 |
isn't a Java {cls.JDK_MAJOR_VER} JDK (it appears to be Java {version_str}). |
| 129 |
|
| 130 |
Briefcase will proceed using its own JDK instance. |
| 131 |
|
| 132 |
************************************************************************* |
| 133 |
""" |
| 134 |
|
| 135 |
except OSError: |
| 136 |
install_message = f""" |
| 137 |
************************************************************************* |
| 138 |
** WARNING: JAVA_HOME does not point to a JDK ** |
| 139 |
************************************************************************* |
| 140 |
|
| 141 |
The location pointed to by the JAVA_HOME environment variable: |
| 142 |
|
| 143 |
{java_home} |
| 144 |
|
| 145 |
does not appear to be a JDK. It may be a Java Runtime Environment. |
| 146 |
|
| 147 |
If JAVA_HOME is a JDK, ensure it is the root directory of the JDK |
| 148 |
instance such that $JAVA_HOME/bin/javac is a valid filepath. |
| 149 |
|
| 150 |
Briefcase will proceed using its own JDK instance. |
| 151 |
|
| 152 |
************************************************************************* |
| 153 |
""" |
| 154 |
|
| 155 |
except subprocess.CalledProcessError: |
| 156 |
install_message = f""" |
| 157 |
************************************************************************* |
| 158 |
** WARNING: Unable to invoke the Java compiler ** |
| 159 |
************************************************************************* |
| 160 |
|
| 161 |
Briefcase received an unexpected error when trying to invoke javac, |
| 162 |
the Java compiler, at the location indicated by the JAVA_HOME |
| 163 |
environment variable. |
| 164 |
|
| 165 |
Briefcase will continue by downloading and using its own JDK. |
| 166 |
|
| 167 |
Please report this as a bug at: |
| 168 |
|
| 169 |
https://github.com/beeware/briefcase/issues/new |
| 170 |
|
| 171 |
|
| 172 |
In your report, please including the output from running: |
| 173 |
|
| 174 |
{java_home}/bin/javac -version |
| 175 |
|
| 176 |
from the command prompt. |
| 177 |
|
| 178 |
************************************************************************* |
| 179 |
""" |
| 180 |
|
| 181 |
except IndexError: |
| 182 |
install_message = f""" |
| 183 |
************************************************************************* |
| 184 |
** WARNING: Unable to determine the version of Java that is installed ** |
| 185 |
************************************************************************* |
| 186 |
|
| 187 |
Briefcase was unable to interpret the version information returned |
| 188 |
by the Java compiler at the location indicated by the JAVA_HOME |
| 189 |
environment variable. |
| 190 |
|
| 191 |
Briefcase will continue by downloading and using its own JDK. |
| 192 |
|
| 193 |
Please report this as a bug at: |
| 194 |
|
| 195 |
https://github.com/beeware/briefcase/issues/new |
| 196 |
|
| 197 |
|
| 198 |
In your report, please including the output from running: |
| 199 |
|
| 200 |
{java_home}/bin/javac -version |
| 201 |
|
| 202 |
from the command prompt. |
| 203 |
|
| 204 |
************************************************************************* |
| 205 |
""" |
| 206 |
|
| 207 |
# macOS has a helpful system utility to determine JAVA_HOME. Try it. |
| 208 |
elif tools.host_os == "Darwin": |
| 209 |
tools.console.debug( |
| 210 |
"Evaluating /usr/libexec/java_home...", prefix=cls.full_name |
| 211 |
) |
| 212 |
try: |
| 213 |
# If /usr/libexec/java_home doesn't exist, OSError will be raised |
| 214 |
# If no JRE/JDK is installed, /usr/libexec/java_home raises an error |
| 215 |
java_home = tools.subprocess.check_output( |
| 216 |
["/usr/libexec/java_home"], |
| 217 |
quiet=1, |
| 218 |
).strip("\n") |
| 219 |
except (OSError, subprocess.CalledProcessError): |
| 220 |
tools.console.debug("An existing JDK/JRE was not returned") |
| 221 |
else: |
| 222 |
try: |
| 223 |
version_str = cls.version_from_path(tools, java_home) |
| 224 |
if version_str.split(".")[0] == cls.JDK_MAJOR_VER: |
| 225 |
java = JDK(tools, java_home=Path(java_home)) |
| 226 |
except (OSError, subprocess.CalledProcessError, IndexError): |
| 227 |
pass # do not alert user if macOS found an unqualified JDK |
| 228 |
|
| 229 |
if java is None: |
| 230 |
# Inform the user if the user-specified JDK wasn't valid |
| 231 |
if install_message: |
| 232 |
tools.console.warning(install_message) |
| 233 |
|
| 234 |
# Use the Briefcase JDK install |
| 235 |
java_home = tools.base_path / cls.JDK_INSTALL_DIR_NAME |
| 236 |
|
| 237 |
# The macOS download has a weird layout (inherited from the official Oracle |
| 238 |
# release). The actual JAVA_HOME is deeper inside the directory structure. |
| 239 |
if tools.host_os == "Darwin": |
| 240 |
java_home = java_home / "Contents/Home" |
| 241 |
|
| 242 |
java = JDK(tools=tools, java_home=java_home) |
| 243 |
|
| 244 |
if not java.exists(): |
| 245 |
if install: |
| 246 |
tools.console.info( |
| 247 |
( |
| 248 |
f"A Java {cls.JDK_MAJOR_VER} JDK was not found; " |
| 249 |
"downloading and installing..." |
| 250 |
), |
| 251 |
prefix=cls.name, |
| 252 |
) |
| 253 |
tools.console.info( |
| 254 |
f"To use an existing JDK {cls.JDK_MAJOR_VER} instance, " |
| 255 |
f"specify its root directory path in the JAVA_HOME environment " |
| 256 |
f"variable." |
| 257 |
) |
| 258 |
tools.console.info() |
| 259 |
java.install() |
| 260 |
else: |
| 261 |
raise MissingToolError("Java") |
| 262 |
|
| 263 |
tools.console.debug(f"Using JDK at {java.java_home}") |
| 264 |
tools.java = java |
| 265 |
return java |
| 266 |
|
| 267 |
def exists(self) -> bool: |
| 268 |
return (self.java_home / "bin").exists() |
| 269 |
|
| 270 |
@property |
| 271 |
def managed_install(self) -> bool: |
| 272 |
try: |
| 273 |
# Determine if java_home is relative to the briefcase data directory. |
| 274 |
# If java_home isn't inside this directory, this will raise a ValueError, |
| 275 |
# indicating it is a non-managed install. |
| 276 |
self.java_home.relative_to(self.tools.base_path) |
| 277 |
return True |
| 278 |
except ValueError: |
| 279 |
return False |
| 280 |
|
| 281 |
def install(self): |
| 282 |
"""Download and install a JDK.""" |
| 283 |
jdk_zip_path = self.tools.file.download( |
| 284 |
url=self.OpenJDK_download_url, |
| 285 |
download_path=self.tools.base_path, |
| 286 |
role=f"Java {self.JDK_MAJOR_VER} JDK", |
| 287 |
) |
| 288 |
|
| 289 |
with self.tools.console.wait_bar("Installing OpenJDK..."): |
| 290 |
try: |
| 291 |
self.tools.file.unpack_archive( |
| 292 |
os.fsdecode(jdk_zip_path), |
| 293 |
extract_dir=os.fsdecode(self.tools.base_path), |
| 294 |
) |
| 295 |
except (shutil.ReadError, EOFError) as e: |
| 296 |
raise BriefcaseCommandError( |
| 297 |
f"""\ |
| 298 |
Unable to unpack OpenJDK ZIP file. The download may have been interrupted |
| 299 |
or corrupted. |
| 300 |
|
| 301 |
Delete {jdk_zip_path} and run briefcase again. |
| 302 |
""" |
| 303 |
) from e |
| 304 |
|
| 305 |
jdk_zip_path.unlink() # Zip file no longer needed once unpacked. |
| 306 |
|
| 307 |
# The tarball will unpack into <briefcase data dir>/tools/jdk-17.0.X+7 |
| 308 |
# (or whatever name matches the current release). We turn this |
| 309 |
# into <briefcase data dir>/tools/java so we have a consistent name. |
| 310 |
java_unpack_path = ( |
| 311 |
self.tools.base_path / f"jdk-{self.JDK_RELEASE}+{self.JDK_BUILD}" |
| 312 |
) |
| 313 |
java_unpack_path.rename(self.tools.base_path / self.JDK_INSTALL_DIR_NAME) |
| 314 |
|
| 315 |
def uninstall(self): |
| 316 |
"""Uninstall a JDK.""" |
| 317 |
try: |
| 318 |
with self.tools.console.wait_bar("Removing old JDK install..."): |
| 319 |
if self.tools.host_os == "Darwin": |
| 320 |
jdk_location = self.java_home.parent.parent |
| 321 |
else: |
| 322 |
jdk_location = self.java_home |
| 323 |
self.tools.shutil.rmtree(jdk_location) |
| 324 |
|
| 325 |
except PermissionError as e: |
| 326 |
raise BriefcaseCommandError( |
| 327 |
f"""\ |
| 328 |
Permission denied when trying to remove {jdk_location!r} |
| 329 |
|
| 330 |
Ensure no Java processes are running and try again. |
| 331 |
""" |
| 332 |
) from e |