generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for macos launcher #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3023f54
WIP: Add support for macos launcher
horakivo 543643d
Adjusting venv_launcher path
horakivo 1e88865
Merging generation of launchers for MacOS and Windows
horakivo c37eb81
Remove left out code
horakivo b3bb06e
Merge branch 'main' into ih/add-support-for-macos-launcher
horakivo 795ef3b
fix: style issues
horakivo 3d84bfb
Merge branch 'ih/add-support-for-macos-launcher' of github.com:horaki…
horakivo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,8 +174,9 @@ public final class VFSUtils { | |
| """; | ||
|
|
||
| private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows"); | ||
| private static final boolean IS_MAC = System.getProperty("os.name").startsWith("Mac"); | ||
|
|
||
| public static final String LAUNCHER_NAME = IS_WINDOWS ? "graalpy.exe" : "graalpy.sh"; | ||
| public static final String LAUNCHER_NAME = IS_WINDOWS ? "graalpy.exe" : IS_MAC ? "graalpy" : "graalpy.sh"; | ||
|
|
||
| private static final String GRAALPY_MAIN_CLASS = "com.oracle.graal.python.shell.GraalPythonMain"; | ||
|
|
||
|
|
@@ -848,9 +849,9 @@ private static Path ensureLauncher(Launcher launcherArgs, BuildToolLog log) thro | |
| } | ||
| } | ||
|
|
||
| private static boolean checkWinLauncherJavaPath(Path venvCfg, Path java) { | ||
| private static boolean checkPyVenvCfgFile(Path pyVenvCfg, Path java) { | ||
| try { | ||
| for (String line : Files.readAllLines(venvCfg)) { | ||
| for (String line : Files.readAllLines(pyVenvCfg)) { | ||
| if (line.trim().startsWith("venvlauncher_command = " + java)) { | ||
| return true; | ||
| } | ||
|
|
@@ -871,30 +872,16 @@ private static void generateLaunchers(Launcher launcherArgs, BuildToolLog log) t | |
| Path java = Paths.get(System.getProperty("java.home"), "bin", "java"); | ||
| String classpath = String.join(File.pathSeparator, launcherArgs.computeClassPath()); | ||
| String extraJavaOptions = String.join(" ", GraalPyRunner.getExtraJavaOptions()); | ||
| if (!IS_WINDOWS) { | ||
| // we do not bother checking if it exists and has correct java home, since it is | ||
| // simple | ||
| // to regenerate the launcher | ||
| var script = formatMultiline(""" | ||
| #!/usr/bin/env bash | ||
| %s --enable-native-access=ALL-UNNAMED %s -classpath %s %s --python.Executable="$0" "$@" | ||
| """, java, extraJavaOptions, String.join(File.pathSeparator, classpath), GRAALPY_MAIN_CLASS); | ||
| try { | ||
| Files.writeString(launcherArgs.launcherPath, script); | ||
| var perms = Files.getPosixFilePermissions(launcherArgs.launcherPath); | ||
| perms.addAll(List.of(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE, | ||
| PosixFilePermission.OTHERS_EXECUTE)); | ||
| Files.setPosixFilePermissions(launcherArgs.launcherPath, perms); | ||
| } catch (IOException e) { | ||
| throw new IOException(String.format("failed to create launcher %s", launcherArgs.launcherPath), e); | ||
| if (IS_MAC || IS_WINDOWS) { | ||
| if (Files.exists(launcherArgs.launcherPath) && checkPyVenvCfgFile(launcherArgs.launcherPath.getParent().resolve("pyvenv.cfg"), java)) { | ||
| return; | ||
| } | ||
| } else if (!Files.exists(launcherArgs.launcherPath) | ||
| || !checkWinLauncherJavaPath(launcherArgs.launcherPath.getParent().resolve("pyenv.cfg"), java)) { | ||
| // on windows, generate a venv launcher that executes the java command | ||
| var launcherFolder = IS_WINDOWS ? "nt" : "macos"; | ||
| var launcherName = IS_WINDOWS ? "graalpy.exe" : "graalpy"; | ||
| var script = formatMultiline(""" | ||
| import os, shutil, struct, venv | ||
| from pathlib import Path | ||
| vl = os.path.join(venv.__path__[0], 'scripts', 'nt', 'graalpy.exe') | ||
| vl = os.path.join(venv.__path__[0], 'scripts', '%s', '%s') | ||
| tl = os.path.join(r'%s') | ||
| os.makedirs(Path(tl).parent.absolute(), exist_ok=True) | ||
| shutil.copy(vl, tl) | ||
|
|
@@ -903,13 +890,14 @@ private static void generateLaunchers(Launcher launcherArgs, BuildToolLog log) t | |
| with open(pyvenvcfg, 'w', encoding='utf-8') as f: | ||
| f.write('venvlauncher_command = ') | ||
| f.write(cmd) | ||
| """, launcherArgs.launcherPath, java, extraJavaOptions, classpath, GRAALPY_MAIN_CLASS); | ||
| """, launcherFolder, launcherName, launcherArgs.launcherPath, java, extraJavaOptions, classpath, GRAALPY_MAIN_CLASS); | ||
| File tmp; | ||
| try { | ||
| tmp = File.createTempFile("create_launcher", ".py"); | ||
| } catch (IOException e) { | ||
| throw new IOException("failed to create tmp launcher", e); | ||
| } | ||
| System.out.println("Created temporary launcher file: " + tmp); | ||
|
||
| tmp.deleteOnExit(); | ||
| try (var wr = new FileWriter(tmp, StandardCharsets.UTF_8)) { | ||
| wr.write(script); | ||
|
|
@@ -923,6 +911,24 @@ with open(pyvenvcfg, 'w', encoding='utf-8') as f: | |
| throw new IOException("failed to run Graalpy launcher", e); | ||
| } | ||
| } | ||
| else { | ||
| // we do not bother checking if it exists and has correct java home, since it is | ||
| // simple | ||
| // to regenerate the launcher | ||
| var script = formatMultiline(""" | ||
| #!/usr/bin/env bash | ||
| %s --enable-native-access=ALL-UNNAMED %s -classpath %s %s --python.Executable="$0" "$@" | ||
| """, java, extraJavaOptions, String.join(File.pathSeparator, classpath), GRAALPY_MAIN_CLASS); | ||
| try { | ||
| Files.writeString(launcherArgs.launcherPath, script); | ||
| var perms = Files.getPosixFilePermissions(launcherArgs.launcherPath); | ||
| perms.addAll(List.of(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE, | ||
| PosixFilePermission.OTHERS_EXECUTE)); | ||
| Files.setPosixFilePermissions(launcherArgs.launcherPath, perms); | ||
| } catch (IOException e) { | ||
| throw new IOException(String.format("failed to create launcher %s", launcherArgs.launcherPath), e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean installWantedPackages(Path venvDirectory, List<String> packages, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you share this code with the Windows branch? Seems like only this line in the script would need parametrization:
otherwise it is the same?
On Windows we try to avoid regenerating the launcher to speed up the build:
do you see any issue with doing that on MacOS?
checkWinLauncherJavaPathshould be renamed, but what it does inside should apply just fine to the MacOS launcher AFAICS.Up to your consideration: since we now have more conditions, I would find this if-else structure more readable: