GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — detroit-python/JextractPython.java at master · openjdk/detroit-python Expand file tree/ Copy pathJextractPython.javaMore file actions 450 lines (400 loc) · 17.6 KB / Copy pathJextractPython.javaFile metadata and controls450 lines (400 loc) · 17.6 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 /* * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collections; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; public class JextractPython { // do not create! private JextractPython() { } public enum OS { WINDOWS, LINUX, MAC, UNKNOWN } // detect the current OS private static OS getCurrentOS() { String osName = System.getProperty("os.name").toLowerCase(); if (osName.contains("linux")) { return OS.LINUX; } else if (osName.contains("mac")) { return OS.MAC; } else if (osName.contains("win")) { return OS.WINDOWS; } else { return OS.UNKNOWN; } } private static final OS THE_OS = getCurrentOS(); // result from a child Process record ExecResult(String output, int exitCode) { } private static ExecResult runCommand(String... commands) throws IOException, InterruptedException { return runCommand(false, commands); } // run a command in a child process and get its output private static ExecResult runCommand(boolean firstLine, String... commands) throws IOException, InterruptedException { ProcessBuilder builder = new ProcessBuilder(commands); builder.redirectErrorStream(true); Process process = builder.start(); // collect all output lines var lines = new ArrayList<String>(); try (var reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { String line; while ((line = reader.readLine()) != null) { lines.add(line); } } return new ExecResult(firstLine ? (lines.isEmpty() ? null : lines.get(0)) : lines.stream().collect(Collectors.joining("\n")), process.waitFor()); } // find a OS command using OS specific command search command private static String findCommand(String command) throws IOException, InterruptedException { ExecResult execResult; if (THE_OS == OS.WINDOWS) { execResult = runCommand(true, "where.exe", command); } else { execResult = runCommand(true, "which", command); } return execResult.exitCode() == 0 ? execResult.output() : null; } // check python version is expected minor version or not private static boolean checkPythonVersion(String pythonBin, String pythonMinor) throws IOException, InterruptedException { // Run 'python --version' and capture output (both stdout and stderr) ExecResult result = runCommand(pythonBin, "--version"); String versionStr = result.output(); if (versionStr == null || versionStr.trim().isEmpty()) { IO.println("Could not parse Python version."); return false; } // Regex to match 'Python 3.14.2' (or similar) var pattern = Pattern.compile("Python\\s+([0-9]+)\\.([0-9]+)\\.[0-9]+"); var matcher = pattern.matcher(versionStr); if (matcher.matches()) { String major = matcher.group(1); String minor = matcher.group(2); if ("3".equals(major) && pythonMinor.equals(minor)) { IO.println("Python 3." + pythonMinor + ".x detected: " + versionStr); return true; } else { IO.println("Python is not 3." + pythonMinor + ".x: " + versionStr); return false; } } else { IO.println("Could not parse Python version."); return false; } } private static String getPythonBasePrefix(String pythonBin) throws IOException, InterruptedException { // get Python's sys.base_prefix value ExecResult execResult = runCommand(pythonBin, "-c", "import sys; print(sys.base_prefix)"); if (execResult.exitCode != 0) { throw new IOException("getting python sys.base_prefix failed"); } return execResult.output(); } // post process jextract include options file private static void postProcessOptionsFile(Path input, Path output) throws IOException { output.getParent().toFile().mkdirs(); var lines = new ArrayList<String>(); try (var reader = Files.newBufferedReader(input); var writer = Files.newBufferedWriter(output)) { reader.lines().forEach(line -> { // ignore blank files if (line.isEmpty()) { return; } // skip lines not from the "python" headers if (!line.contains("python")) { return; } // Windows issue if (line.contains("_setjmp")) { // if _setjmp is included, we get the following error: // setjmp depends on _SETJMP_FLOAT128 which has been excluded return; } // get rid of comments and trim lines int hashIdx = line.indexOf('#'); if (hashIdx != -1) { line = line.substring(0, hashIdx).trim(); } lines.add(line); }); // sort the list so that saved jextract_python_includes.txt // is stable regardless of jextract option dump order change // between sessions. Collections.sort(lines); lines.stream(). filter(((Predicate<String>) String::isEmpty).negate()). forEach(str -> { try { writer.append(str); writer.append('\n'); } catch (IOException exp) { throw new UncheckedIOException(exp); } }); // add 'malloc' and 'free' in additon to Python symbols writer.append("# othen than C Python API\n"); writer.append("--include-function malloc\n"); writer.append("--include-function free\n"); } } // post process generated java source file for SymbolLookup initialisation private static boolean postProcessForSymbolLookup(Path srcDir) throws IOException, InterruptedException { String symbolLookupLine = "static final SymbolLookup SYMBOL_LOOKUP ="; Path[] found = new Path[1]; try (Stream<Path> paths = Files.walk(srcDir)) { paths.filter(Files::isRegularFile). forEach(path -> { if (found[0] != null) { return; } try { if (Files.lines(path).anyMatch(line -> line.contains(symbolLookupLine))) { found[0] = path; } } catch (IOException e) { System.err.println(e); } }); } if (found[0] == null) { return false; } // Customise SymbolLookup initialisation file Path symbolLookupInitJava = found[0]; Path tempFile = Files.createTempFile(symbolLookupInitJava.getFileName().toString(), "temp"); // we use PythonLibraryLoader class and so add an import for that class String importLine = "\nimport org.openjdk.engine.python.impl.PythonLibraryLoader;\n"; // replace original init with this line String replacementLine = " static final SymbolLookup SYMBOL_LOOKUP = PythonLibraryLoader.load(LIBRARY_ARENA)"; // we create a temporary to make modifications try (var writer = Files.newBufferedWriter(tempFile)) { // have we inserted import line already? boolean importInserted = false; for (String line : Files.readAllLines(symbolLookupInitJava)) { if (!importInserted && line.strip().startsWith("package ")) { writer.append(line); writer.append('\n'); writer.append(importLine); importInserted = true; } else if (line.contains(symbolLookupLine)) { writer.append(replacementLine); } else { writer.append(line); } writer.append('\n'); } } // copy the temporary modified file to the original .java file Files.copy(tempFile, symbolLookupInitJava, StandardCopyOption.REPLACE_EXISTING); return true; } private static final String COPYRIGHT_TEXT = """ /* * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ """; // post process generated java source file copyright addition private static boolean postProcessForCopyright(Path srcDir) throws IOException, InterruptedException { try (Stream<Path> paths = Files.walk(srcDir)) { boolean[] res = { true }; paths.filter(Files::isRegularFile). forEach(path -> { try { var origContent = Files.readString(path, StandardCharsets.UTF_8); var newContent = COPYRIGHT_TEXT + origContent; Files.writeString(path, newContent, StandardCharsets.UTF_8); } catch (IOException e) { res[0] = false; System.err.println(e); } }); return res[0]; } } private static final String JEXTRACT_OPTIONS_FILE_NAME = "jextract_python_includes.txt"; public static void main(String[] args) { // should pass version line 3.14 or 3.13 etc. Only major // and minor versions. Do not pass 3.13.2 for example! if (args.length != 2) { System.err.println("java JextractHelper <python-version> <osdir>"); System.exit(1); } String pythonVersion = args[0]; String osDir = args[1]; if (THE_OS == OS.UNKNOWN) { System.err.println("Unsupported OS: " + System.getProperty("os.name")); System.exit(2); } try { String jextractPath = findCommand(THE_OS == OS.WINDOWS ? "jextract.bat" : "jextract"); if (jextractPath != null) { IO.println("Using jextract @ " + jextractPath); } else { IO.println("Cannot find jextract in PATH"); System.exit(3); } // Try python version specified, else 3, else python String pythonBin = findCommand("python" + pythonVersion); if (pythonBin == null) { pythonBin = findCommand("python3"); } if (pythonBin == null) { pythonBin = findCommand("python"); } if (pythonBin != null) { IO.println("Using Python @ " + pythonBin); } else { IO.println("Python not found!"); System.exit(4); } // make sure that we are on expected python version if (!checkPythonVersion(pythonBin, pythonVersion.substring("3.".length()))) { System.exit(5); } // find Python's sys.base_prefix String pythonBasePrefix = getPythonBasePrefix(pythonBin); var targetDir = Paths.get("target"); targetDir.toFile().mkdirs(); var pythonIncludeDir = pythonBasePrefix + (THE_OS == OS.WINDOWS ? "\\include" : ("/include/python" + pythonVersion + "/")); IO.println("extracting python option includes file from " + pythonIncludeDir); // dump include options from Python.h using jextract var optionsFile = targetDir.resolve(JEXTRACT_OPTIONS_FILE_NAME); ExecResult execResult; if (THE_OS == OS.WINDOWS) { execResult = runCommand("cmd.exe", "/c", jextractPath, "-D_Float16=short", "-I", pythonIncludeDir, "\"<Python.h>\"", "--dump-includes", optionsFile.toString()); } else { execResult = runCommand(jextractPath, "-D_Float16=short", "-I", pythonIncludeDir, "<Python.h>", "--dump-includes", optionsFile.toString()); } if (execResult.exitCode() != 0) { System.err.println("jextract generate include options file step failed"); System.err.println(execResult.output()); System.err.println("Process exited with " + execResult.exitCode()); System.exit(6); } Path javaSrcDir = Paths.get("src", "main", "profiles", osDir, "java"); // post process jextract include options file var modifiedOptionsFile = javaSrcDir.resolve(JEXTRACT_OPTIONS_FILE_NAME); postProcessOptionsFile(optionsFile, modifiedOptionsFile); // run jextract with the post processed options file if (THE_OS == OS.WINDOWS) { execResult = runCommand("cmd.exe", "/c", jextractPath, "--output", javaSrcDir.toString(), "-D_Float16=short", "-I", pythonIncludeDir, "@" + modifiedOptionsFile, "-t", "org.openjdk.engine.python.bindings", "\"<Python.h>\""); } else { execResult = runCommand(jextractPath, "--output", javaSrcDir.toString(), "-D_Float16=short", "-I", pythonIncludeDir, "@" + modifiedOptionsFile, "-t", "org.openjdk.engine.python.bindings", "<Python.h>"); } IO.println(execResult.output()); if (execResult.exitCode() != 0) { System.err.println("jextract step failed"); System.err.println("Process exited with " + execResult.exitCode()); System.exit(7); } // post process jextracted sources for SymbolLookup initialisation if (!postProcessForSymbolLookup(javaSrcDir)) { System.err.println("post processing for SymbolLookup init failed"); System.exit(8); } // post process jextracted sources for copyright addition if (!postProcessForCopyright(javaSrcDir)) { System.err.println("post processing for copyright addition failed"); System.exit(9); } } catch (IOException | InterruptedException ex) { ex.printStackTrace(System.err); System.exit(9); } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |