Skip to content

Commit f611167

Browse files
committed
generate graalvm and python versions for use in java at build time
1 parent cb107cf commit f611167

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed

graalpython/com.oracle.graal.python.resources/src/com/oracle/graal/python/resources/PythonResource.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package com.oracle.graal.python.resources;
4242

4343
import java.io.IOException;
44+
import java.io.InputStream;
4445
import java.nio.file.InvalidPathException;
4546
import java.nio.file.Path;
4647
import java.util.List;
@@ -56,12 +57,22 @@
5657
*/
5758
@Id("python-home")
5859
public final class PythonResource implements InternalResource {
59-
// These fields should be initialized by the language using these resource so the layout will
60-
// match up
61-
public static int PYTHON_MAJOR = 0;
62-
public static int PYTHON_MINOR = 0;
63-
public static int GRAALVM_MAJOR = 0;
64-
public static int GRAALVM_MINOR = 0;
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+
}
6576

6677
private static final Path BASE_PATH = Path.of("META-INF", "resources");
6778
private static final String LIBGRAALPY = "libgraalpy";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
3535

3636
import java.io.IOException;
37+
import java.io.InputStream;
3738
import java.nio.file.InvalidPathException;
3839
import java.util.ArrayList;
3940
import java.util.Arrays;
@@ -180,9 +181,8 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
180181

181182
/**
182183
* GraalVM version. Unfortunately, we cannot just use {@link Version#getCurrent} as it relies on
183-
* a GraalVM build, but we may run from Jar files directly during development. So we hardcode
184-
* the version here. It will fail at runtime if it doesn't match the distribution layout that is
185-
* derived from the actual version.
184+
* a GraalVM build, but we may run from Jar files directly during development. We generate the
185+
* version during the build that are checked against these constants.
186186
*/
187187
public static final int GRAALVM_MAJOR = 23;
188188
public static final int GRAALVM_MINOR = 1;
@@ -191,21 +191,11 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
191191

192192
@SuppressWarnings("unchecked")
193193
private static Class<PythonResource> getPythonResourceClass() {
194-
Class<PythonResource> pr;
195194
try {
196-
pr = (Class<PythonResource>) Class.forName("com.oracle.graal.python.resources.PythonResource");
195+
return (Class<PythonResource>) Class.forName("com.oracle.graal.python.resources.PythonResource");
197196
} catch (ClassNotFoundException e) {
198197
return null;
199198
}
200-
try {
201-
pr.getDeclaredField("PYTHON_MAJOR").set(pr, MAJOR);
202-
pr.getDeclaredField("PYTHON_MINOR").set(pr, MINOR);
203-
pr.getDeclaredField("GRAALVM_MAJOR").set(pr, GRAALVM_MAJOR);
204-
pr.getDeclaredField("GRAALVM_MINOR").set(pr, GRAALVM_MINOR);
205-
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
206-
return null;
207-
}
208-
return pr;
209199
}
210200

211201
static {
@@ -225,6 +215,25 @@ private static Class<PythonResource> getPythonResourceClass() {
225215
}
226216

227217
PYTHON_RESOURCE_CLASS = getPythonResourceClass();
218+
try (InputStream is = PythonLanguage.class.getResourceAsStream("/graalpy_versions")) {
219+
if (MAJOR != is.read()) {
220+
throw new RuntimeException("suite.py version info does not match PythonLanguage#MAJOR");
221+
}
222+
if (MINOR != is.read()) {
223+
throw new RuntimeException("suite.py version info does not match PythonLanguage#MINOR");
224+
}
225+
if (MICRO != is.read()) {
226+
throw new RuntimeException("suite.py version info does not match PythonLanguage#MICRO");
227+
}
228+
if (GRAALVM_MAJOR != is.read()) {
229+
throw new RuntimeException("suite.py version info does not match PythonLanguage#GRAALVM_MAJOR");
230+
}
231+
if (GRAALVM_MINOR != is.read()) {
232+
throw new RuntimeException("suite.py version info does not match PythonLanguage#GRAALVM_MINOR");
233+
}
234+
} catch (IOException e) {
235+
throw new RuntimeException(e);
236+
}
228237
}
229238
public static final int RELEASE_SERIAL = 0;
230239
public static final int VERSION_HEX = MAJOR << 24 |

mx.graalpython/mx_graalpython.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@
8484
SUITE_COMPILER = mx.suite("compiler", fatalIfMissing=False)
8585
SUITE_SULONG = mx.suite("sulong")
8686

87-
GRAAL_VERSION = ".".join(SUITE.suiteDict['version'].split('.')[:2])
88-
GRAAL_VERSION_NODOT = GRAAL_VERSION.replace('.', '')
89-
PYTHON_VERSION = "3.10"
90-
PYTHON_VERSION_NODOT = "3.10".replace('.', '')
87+
GRAAL_VERSION = SUITE.suiteDict['version']
88+
GRAAL_VERSION_MAJ_MIN = ".".join(GRAAL_VERSION.split(".")[:2])
89+
PYTHON_VERSION = SUITE.suiteDict[f'{SUITE.name}:pythonVersion']
90+
PYTHON_VERSION_MAJ_MIN = ".".join(PYTHON_VERSION.split('.')[:2])
9191

9292
GRAALPYTHON_MAIN_CLASS = "com.oracle.graal.python.shell.GraalPythonMain"
9393

@@ -1746,14 +1746,21 @@ def _get_output_root(projectname):
17461746

17471747
def py_version_short(variant=None, **kwargs):
17481748
if variant == 'major_minor_nodot':
1749-
return PYTHON_VERSION_NODOT
1750-
return PYTHON_VERSION
1749+
return PYTHON_VERSION_MAJ_MIN.replace(".", "")
1750+
elif variant == 'binary':
1751+
return "".join([chr(int(p)) for p in PYTHON_VERSION.split(".")])
1752+
else:
1753+
return PYTHON_VERSION_MAJ_MIN
17511754

17521755

17531756
def graal_version_short(variant=None, **kwargs):
17541757
if variant == 'major_minor_nodot':
1755-
return GRAAL_VERSION_NODOT
1756-
return GRAAL_VERSION
1758+
return GRAAL_VERSION_MAJ_MIN.replace(".", "")
1759+
elif variant == 'binary':
1760+
return "".join([chr(int(p)) for p in GRAAL_VERSION.split(".")])
1761+
else:
1762+
return GRAAL_VERSION_MAJ_MIN
1763+
17571764

17581765
def graalpy_ext(llvm_mode, **kwargs):
17591766
if not llvm_mode:
@@ -1774,7 +1781,7 @@ def graalpy_ext(llvm_mode, **kwargs):
17741781
# https://github.com/python/cpython/issues/37510
17751782
ext = 'pyd' if os == 'windows' else 'so'
17761783

1777-
return f'.graalpy{GRAAL_VERSION_NODOT}-{PYTHON_VERSION_NODOT}-{llvm_mode}-{arch}-{pyos}.{ext}'
1784+
return f'.graalpy{GRAAL_VERSION_MAJ_MIN.replace(".", "")}-{PYTHON_VERSION_MAJ_MIN.replace(".", "")}-{llvm_mode}-{arch}-{pyos}.{ext}'
17781785

17791786

17801787
mx_subst.path_substitutions.register_with_arg('suite', _get_suite_dir)

mx.graalpython/suite.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"versionConflictResolution": "latest",
1111

1212
"version": "23.1.0",
13+
"graalpython:pythonVersion": "3.10.8",
1314
"release": False,
1415
"groupId": "org.graalvm.graalpython",
1516
"url": "http://www.graalvm.org/",
@@ -790,6 +791,18 @@
790791
#
791792
# --------------------------------------------------------------------------------------------------------------
792793
"distributions": {
794+
# We need the versions twice (even though that's silly), because
795+
# otherwise they will not get included in both the resources jar and
796+
# the language jar.
797+
"GRAALPYTHON_VERSIONS_RES": {
798+
"type": "dir",
799+
"layout": {"./graalpy_versions": ["string:<py_ver:binary><graal_ver:binary>"]},
800+
},
801+
"GRAALPYTHON_VERSIONS_MAIN": {
802+
"type": "dir",
803+
"layout": {"./graalpy_versions": ["string:<py_ver:binary><graal_ver:binary>"]},
804+
},
805+
793806
"GRAALPYTHON-LAUNCHER": {
794807
"moduleInfo": {
795808
"name": "org.graalvm.py.launcher",
@@ -871,6 +884,7 @@
871884
"useModulePath": True,
872885
"dependencies": [
873886
"com.oracle.graal.python.resources",
887+
"GRAALPYTHON_VERSIONS_RES",
874888
"GRAALPYTHON_LIBPYTHON_RESOURCES",
875889
"GRAALPYTHON_LIBGRAALPY_RESOURCES",
876890
"GRAALPYTHON_INCLUDE_RESOURCES",
@@ -911,6 +925,7 @@
911925
},
912926
"useModulePath": True,
913927
"dependencies": [
928+
"GRAALPYTHON_VERSIONS_MAIN",
914929
"com.oracle.graal.python",
915930
"com.oracle.graal.python.frozen",
916931
],

0 commit comments

Comments
 (0)