|
| 1 | +"""Local hatchling plugins for the codeanalyzer-java wheel. |
| 2 | +
|
| 3 | +Two concerns live here: |
| 4 | +
|
| 5 | +1. **Version lockstep.** The wheel version is read from the Java project's |
| 6 | + ``gradle.properties`` so the Python package and the native binary it ships |
| 7 | + can never drift apart. |
| 8 | +
|
| 9 | +2. **Impure, platform-tagged wheels.** The wheel carries a prebuilt native |
| 10 | + binary plus the JDK ``.jmod`` files it needs at runtime. The build hook |
| 11 | + force-includes those assets, marks the wheel non-purelib, and stamps a |
| 12 | + concrete ``py3-none-<platform>`` tag so pip resolves the correct artifact |
| 13 | + per OS/arch instead of a universal ``py3-none-any`` wheel. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import os |
| 19 | +import sysconfig |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +from hatchling.builders.hooks.plugin.interface import BuildHookInterface |
| 23 | +from hatchling.metadata.plugin.interface import MetadataHookInterface |
| 24 | + |
| 25 | +_BINARY_NAMES = ("codeanalyzer", "codeanalyzer.exe") |
| 26 | + |
| 27 | +# Bundling every JDK jmod (~104 MB) pushes the wheel over PyPI's default 100 MB |
| 28 | +# per-file limit, and the native binary does not compress further (~23.5 MB), |
| 29 | +# leaving room for ~80 MB of jmods. We therefore drop only the largest modules |
| 30 | +# that static type resolution never needs, keeping 79 of 83 (~90 MB wheel): |
| 31 | +# - jdk.localedata locale resource *data*, not API types |
| 32 | +# - jdk.compiler com.sun.tools.javac.* internals (annotation- |
| 33 | +# processor sources are the only mild compromise) |
| 34 | +# - jdk.internal.vm.compiler Graal compiler internals, never referenced |
| 35 | +# - jdk.hotspot.agent Serviceability Agent internals, never referenced |
| 36 | +# Set CODEANALYZER_BUNDLE_ALL_JMODS=1 to bundle all 83 (needs a PyPI size bump). |
| 37 | +_EXCLUDED_JMODS = frozenset( |
| 38 | + { |
| 39 | + "jdk.localedata.jmod", |
| 40 | + "jdk.compiler.jmod", |
| 41 | + "jdk.internal.vm.compiler.jmod", |
| 42 | + "jdk.hotspot.agent.jmod", |
| 43 | + } |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +def _bundle_all_jmods() -> bool: |
| 48 | + return os.environ.get("CODEANALYZER_BUNDLE_ALL_JMODS", "").lower() in {"1", "true", "yes"} |
| 49 | + |
| 50 | + |
| 51 | +def _select_jmods(jmod_files: list[Path]) -> list[Path]: |
| 52 | + if _bundle_all_jmods(): |
| 53 | + return jmod_files |
| 54 | + return [jmod for jmod in jmod_files if jmod.name not in _EXCLUDED_JMODS] |
| 55 | + |
| 56 | + |
| 57 | +def read_gradle_version(repo_root: Path) -> str: |
| 58 | + """Return the ``version=`` value from ``<repo_root>/gradle.properties``.""" |
| 59 | + gradle_properties = repo_root / "gradle.properties" |
| 60 | + for line in gradle_properties.read_text(encoding="utf-8").splitlines(): |
| 61 | + line = line.strip() |
| 62 | + if line.startswith("version="): |
| 63 | + return line.split("=", 1)[1].strip() |
| 64 | + raise RuntimeError(f"no 'version=' entry found in {gradle_properties}") |
| 65 | + |
| 66 | + |
| 67 | +def _wheel_platform_tag() -> str: |
| 68 | + """Concrete wheel tag for the current platform, e.g. ``py3-none-linux_x86_64``. |
| 69 | +
|
| 70 | + Linux wheels are emitted with the plain ``linux_*`` platform; CI runs |
| 71 | + ``auditwheel repair`` to relabel them to a manylinux/musllinux policy. |
| 72 | + """ |
| 73 | + platform = sysconfig.get_platform().replace("-", "_").replace(".", "_") |
| 74 | + return f"py3-none-{platform}" |
| 75 | + |
| 76 | + |
| 77 | +def _resolve_binary(repo_root: Path) -> Path: |
| 78 | + override = os.environ.get("CODEANALYZER_NATIVE_BINARY") |
| 79 | + if override: |
| 80 | + candidate = Path(override) |
| 81 | + if candidate.is_file(): |
| 82 | + return candidate |
| 83 | + raise RuntimeError( |
| 84 | + f"CODEANALYZER_NATIVE_BINARY is set to '{override}' but no file exists there." |
| 85 | + ) |
| 86 | + native_dir = repo_root / "build" / "native" / "nativeCompile" |
| 87 | + for name in _BINARY_NAMES: |
| 88 | + candidate = native_dir / name |
| 89 | + if candidate.is_file(): |
| 90 | + return candidate |
| 91 | + raise RuntimeError( |
| 92 | + "no prebuilt codeanalyzer native binary found for this platform/arch.\n" |
| 93 | + f"Looked for {_BINARY_NAMES} under {native_dir}.\n" |
| 94 | + "Build it first with `./gradlew nativeCompile`, or point " |
| 95 | + "CODEANALYZER_NATIVE_BINARY at an existing binary. " |
| 96 | + "codeanalyzer-java ships only prebuilt wheels; there is no from-source " |
| 97 | + "build path for unsupported platforms." |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def _resolve_jmods() -> Path: |
| 102 | + override = os.environ.get("CODEANALYZER_JMODS_DIR") |
| 103 | + if override: |
| 104 | + candidate = Path(override) |
| 105 | + if candidate.is_dir(): |
| 106 | + return candidate |
| 107 | + raise RuntimeError( |
| 108 | + f"CODEANALYZER_JMODS_DIR is set to '{override}' but it is not a directory." |
| 109 | + ) |
| 110 | + java_home = os.environ.get("JAVA_HOME") |
| 111 | + if java_home: |
| 112 | + candidate = Path(java_home) / "jmods" |
| 113 | + if candidate.is_dir(): |
| 114 | + return candidate |
| 115 | + raise RuntimeError( |
| 116 | + "could not locate JDK .jmod files to bundle. Set CODEANALYZER_JMODS_DIR " |
| 117 | + "to a directory of .jmod files, or JAVA_HOME to a JDK that has a jmods/ " |
| 118 | + "directory (a JDK 9+ image, not a JRE)." |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +class CustomMetadataHook(MetadataHookInterface): |
| 123 | + """Inject the version read from gradle.properties.""" |
| 124 | + |
| 125 | + def update(self, metadata: dict) -> None: |
| 126 | + metadata["version"] = read_gradle_version(Path(self.root).parent) |
| 127 | + |
| 128 | + |
| 129 | +class CustomBuildHook(BuildHookInterface): |
| 130 | + """Bundle the native binary + jmods and force an impure platform wheel.""" |
| 131 | + |
| 132 | + def initialize(self, version: str, build_data: dict) -> None: |
| 133 | + if self.target_name != "wheel": |
| 134 | + return |
| 135 | + |
| 136 | + repo_root = Path(self.root).parent |
| 137 | + binary = _resolve_binary(repo_root) |
| 138 | + jmods_dir = _resolve_jmods() |
| 139 | + jmod_files = sorted(jmods_dir.glob("*.jmod")) |
| 140 | + if not jmod_files: |
| 141 | + raise RuntimeError(f"no .jmod files found in {jmods_dir}") |
| 142 | + selected = _select_jmods(jmod_files) |
| 143 | + if not selected: |
| 144 | + raise RuntimeError(f"jmod selection is empty (from {jmods_dir})") |
| 145 | + |
| 146 | + force_include = build_data["force_include"] |
| 147 | + force_include[str(binary)] = f"codeanalyzer_java/_vendor/bin/{binary.name}" |
| 148 | + for jmod in selected: |
| 149 | + force_include[str(jmod)] = f"codeanalyzer_java/_vendor/jmods/{jmod.name}" |
| 150 | + |
| 151 | + build_data["pure_python"] = False |
| 152 | + build_data["infer_tag"] = False |
| 153 | + build_data["tag"] = _wheel_platform_tag() |
| 154 | + |
| 155 | + self.app.display_info( |
| 156 | + f"codeanalyzer-java: bundling {binary.name} + {len(selected)}/" |
| 157 | + f"{len(jmod_files)} jmods as {build_data['tag']}" |
| 158 | + ) |
0 commit comments