|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.resources; |
| 42 | + |
| 43 | +import java.io.IOException; |
| 44 | +import java.io.InputStream; |
| 45 | +import java.nio.file.InvalidPathException; |
| 46 | +import java.nio.file.Path; |
| 47 | +import java.util.List; |
| 48 | + |
| 49 | +import com.oracle.truffle.api.CompilerDirectives; |
| 50 | +import com.oracle.truffle.api.InternalResource; |
| 51 | +import com.oracle.truffle.api.InternalResource.Id; |
| 52 | + |
| 53 | +/** |
| 54 | + * This code needs to be kept in sync somehow with the suite.py code. In particular, the layouts |
| 55 | + * produced by the unpacking logic in {@link #unpackFiles} should produce the same layout as the |
| 56 | + * GRAALPYTHON_GRAALVM_SUPPORT distribution in the suite.py. |
| 57 | + */ |
| 58 | +@Id("python-home") |
| 59 | +public final class PythonResource implements InternalResource { |
| 60 | + private static final int PYTHON_MAJOR; |
| 61 | + private static final int PYTHON_MINOR; |
| 62 | + private static final int GRAALVM_MAJOR; |
| 63 | + private static final int GRAALVM_MINOR; |
| 64 | + |
| 65 | + static { |
| 66 | + try (InputStream is = PythonResource.class.getResourceAsStream("/graalpy_versions")) { |
| 67 | + PYTHON_MAJOR = is.read() - ' '; |
| 68 | + PYTHON_MINOR = is.read() - ' '; |
| 69 | + is.read(); // skip python micro version |
| 70 | + GRAALVM_MAJOR = is.read() - ' '; |
| 71 | + GRAALVM_MINOR = is.read() - ' '; |
| 72 | + } catch (IOException e) { |
| 73 | + throw new RuntimeException(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static final Path BASE_PATH = Path.of("META-INF", "resources"); |
| 78 | + private static final String LIBGRAALPY = "libgraalpy"; |
| 79 | + private static final String LIBPYTHON = "libpython"; |
| 80 | + private static final String INCLUDE = "include"; |
| 81 | + private static final String LIBGRAALPY_FILES = LIBGRAALPY + ".files"; |
| 82 | + private static final String LIBPYTHON_FILES = LIBPYTHON + ".files"; |
| 83 | + private static final String INCLUDE_FILES = "include.files"; |
| 84 | + private static final String NI_FILES = "ni.files"; |
| 85 | + private static final String NATIVE_FILES = "native.files"; |
| 86 | + private static final Path LIBGRAALPY_SHA256 = Path.of(LIBGRAALPY + ".sha256"); |
| 87 | + private static final Path LIBPYTHON_SHA256 = Path.of(LIBPYTHON + ".sha256"); |
| 88 | + private static final Path INCLUDE_SHA256 = Path.of("include.sha256"); |
| 89 | + private static final Path NI_SHA256 = Path.of("ni.sha256"); |
| 90 | + private static final Path NATIVE_SHA256 = Path.of("native.sha256"); |
| 91 | + |
| 92 | + @Override |
| 93 | + public void unpackFiles(Env env, Path targetDirectory) throws IOException { |
| 94 | + OS os = env.getOS(); |
| 95 | + Path osArch = Path.of(os.toString()).resolve(env.getCPUArchitecture().toString()); |
| 96 | + if (os.equals(OS.WINDOWS)) { |
| 97 | + env.unpackResourceFiles(BASE_PATH.resolve(LIBPYTHON_FILES), targetDirectory.resolve("Lib"), BASE_PATH.resolve(LIBPYTHON)); |
| 98 | + env.unpackResourceFiles(BASE_PATH.resolve(LIBGRAALPY_FILES), targetDirectory.resolve("lib-graalpython"), BASE_PATH.resolve(LIBGRAALPY)); |
| 99 | + env.unpackResourceFiles(BASE_PATH.resolve(INCLUDE_FILES), targetDirectory.resolve("Include"), BASE_PATH.resolve(INCLUDE)); |
| 100 | + } else { |
| 101 | + String pythonMajMin = "python" + PYTHON_MAJOR + "." + PYTHON_MINOR; |
| 102 | + env.unpackResourceFiles(BASE_PATH.resolve(LIBPYTHON_FILES), targetDirectory.resolve("lib").resolve(pythonMajMin), BASE_PATH.resolve(LIBPYTHON)); |
| 103 | + env.unpackResourceFiles(BASE_PATH.resolve(LIBGRAALPY_FILES), targetDirectory.resolve("lib").resolve("graalpy" + GRAALVM_MAJOR + "." + GRAALVM_MINOR), BASE_PATH.resolve(LIBGRAALPY)); |
| 104 | + env.unpackResourceFiles(BASE_PATH.resolve(INCLUDE_FILES), targetDirectory.resolve("include").resolve(pythonMajMin), BASE_PATH.resolve(INCLUDE)); |
| 105 | + } |
| 106 | + // ni files are in the same place on all platforms |
| 107 | + env.unpackResourceFiles(BASE_PATH.resolve(NI_FILES), targetDirectory, BASE_PATH); |
| 108 | + // native files already have the correct structure |
| 109 | + env.unpackResourceFiles(BASE_PATH.resolve(osArch).resolve(NATIVE_FILES), targetDirectory, BASE_PATH.resolve(osArch)); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String versionHash(Env env) { |
| 114 | + StringBuilder sb = new StringBuilder(); |
| 115 | + for (var s : List.of(LIBGRAALPY_SHA256, LIBPYTHON_SHA256, NI_SHA256, INCLUDE_SHA256, Path.of(env.getOS().toString()).resolve(env.getCPUArchitecture().toString()).resolve(NATIVE_SHA256))) { |
| 116 | + try { |
| 117 | + sb.append(env.readResourceLines(BASE_PATH.resolve(s)).get(0).substring(0, 8)); |
| 118 | + } catch (IOException | IndexOutOfBoundsException | InvalidPathException e) { |
| 119 | + throw CompilerDirectives.shouldNotReachHere(e); |
| 120 | + } |
| 121 | + } |
| 122 | + return sb.toString(); |
| 123 | + } |
| 124 | +} |
0 commit comments