Skip to content

Commit 849837d

Browse files
horakivosteve-s
authored andcommitted
Add initial support for macos launcher
1 parent c2d0c87 commit 849837d

File tree

1 file changed

+38
-2
lines changed
  • org.graalvm.python.embedding.tools/src/main/java/org/graalvm/python/embedding/tools/vfs

1 file changed

+38
-2
lines changed

org.graalvm.python.embedding.tools/src/main/java/org/graalvm/python/embedding/tools/vfs/VFSUtils.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ public final class VFSUtils {
175175
""";
176176

177177
private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
178+
private static final boolean IS_MAC = System.getProperty("os.name").startsWith("Mac");
178179

179-
public static final String LAUNCHER_NAME = IS_WINDOWS ? "graalpy.exe" : "graalpy.sh";
180+
public static final String LAUNCHER_NAME = IS_WINDOWS ? "graalpy.exe" : IS_MAC ? "graalpy" : "graalpy.sh";
180181

181182
private static final String GRAALPY_MAIN_CLASS = "com.oracle.graal.python.shell.GraalPythonMain";
182183

@@ -917,7 +918,42 @@ private static void generateLaunchers(Launcher launcherArgs, BuildToolLog log) t
917918
Path java = Paths.get(System.getProperty("java.home"), "bin", "java");
918919
String classpath = String.join(File.pathSeparator, launcherArgs.computeClassPath());
919920
String extraJavaOptions = String.join(" ", GraalPyRunner.getExtraJavaOptions());
920-
if (!IS_WINDOWS) {
921+
if (IS_MAC) {
922+
var script = formatMultiline("""
923+
import os, shutil, struct, venv
924+
from pathlib import Path
925+
adjusted = os.path.dirname(os.path.dirname(venv.__path__[0]))
926+
vl = os.path.join(adjusted, 'venv', 'scripts', 'macos', 'graalpy')
927+
tl = os.path.join(r'%s')
928+
os.makedirs(Path(tl).parent.absolute(), exist_ok=True)
929+
shutil.copy(vl, tl)
930+
cmd = r'%s --enable-native-access=ALL-UNNAMED %s -classpath "%s" %s'
931+
pyvenvcfg = os.path.join(os.path.dirname(tl), "pyvenv.cfg")
932+
with open(pyvenvcfg, 'w', encoding='utf-8') as f:
933+
f.write('venvlauncher_command = ')
934+
f.write(cmd)
935+
""", launcherArgs.launcherPath, java, extraJavaOptions, classpath, GRAALPY_MAIN_CLASS);
936+
File tmp;
937+
try {
938+
tmp = File.createTempFile("create_launcher", ".py");
939+
} catch (IOException e) {
940+
throw new IOException("failed to create tmp launcher", e);
941+
}
942+
System.out.println("Created temporary launcher file: " + tmp);
943+
tmp.deleteOnExit();
944+
try (var wr = new FileWriter(tmp, StandardCharsets.UTF_8)) {
945+
wr.write(script);
946+
} catch (IOException e) {
947+
throw new IOException(String.format("failed to write tmp launcher %s", tmp), e);
948+
}
949+
950+
try {
951+
GraalPyRunner.run(classpath, log, tmp.getAbsolutePath());
952+
} catch (InterruptedException e) {
953+
throw new IOException("failed to run Graalpy launcher", e);
954+
}
955+
}
956+
else if (!IS_WINDOWS) {
921957
// we do not bother checking if it exists and has correct java home, since it is
922958
// simple
923959
// to regenerate the launcher

0 commit comments

Comments
 (0)