Skip to content

Commit 9e1e951

Browse files
committed
Fix SIP detection on Windows for native launcher
1 parent 1719919 commit 9e1e951

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package scala.cli.internal;
2+
3+
import com.oracle.svm.core.annotate.Substitute;
4+
import com.oracle.svm.core.annotate.TargetClass;
5+
import org.graalvm.nativeimage.Platform;
6+
import org.graalvm.nativeimage.Platforms;
7+
8+
import java.nio.file.Path;
9+
10+
@TargetClass(className = "scala.cli.internal.Argv0")
11+
@Platforms({Platform.WINDOWS.class})
12+
final class Argv0SubstWindows {
13+
14+
@Substitute
15+
String get(String defaultValue) {
16+
return coursier.jniutils.ModuleFileName.get();
17+
}
18+
19+
}

modules/cli/src/main/scala/scala/cli/ScalaCli.scala

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@ import scala.util.Properties
1616

1717
object ScalaCli {
1818

19+
if (Properties.isWin && isGraalvmNativeImage)
20+
// have to be initialized before running (new Argv0).get because Argv0SubstWindows uses csjniutils library
21+
// The DLL loaded by LoadWindowsLibrary is statically linke/d in
22+
// the Scala CLI native image, no need to manually load it.
23+
coursier.jniutils.LoadWindowsLibrary.assumeInitialized()
24+
1925
val progName = (new Argv0).get("scala-cli")
2026

21-
private def checkName(name: String) =
22-
progName == name ||
23-
progName.endsWith(s"/$name") ||
24-
progName.endsWith(File.separator + name)
27+
private def checkName(name: String) = {
28+
val baseProgName = if (Properties.isWin) progName.stripSuffix(".exe") else progName
29+
baseProgName == name ||
30+
baseProgName.endsWith(s"/$name") ||
31+
baseProgName.endsWith(File.separator + name)
32+
}
2533

2634
private var isSipScala = checkName("scala") || checkName("scala-cli-sip")
2735

@@ -171,11 +179,6 @@ object ScalaCli {
171179
if (!Properties.isWin && isGraalvmNativeImage)
172180
ignoreSigpipe()
173181

174-
if (Properties.isWin && isGraalvmNativeImage)
175-
// The DLL loaded by LoadWindowsLibrary is statically linked in
176-
// the Scala CLI native image, no need to manually load it.
177-
coursier.jniutils.LoadWindowsLibrary.assumeInitialized()
178-
179182
if (Properties.isWin && System.console() != null && coursier.paths.Util.useJni())
180183
// Enable ANSI output in Windows terminal
181184
coursier.jniutils.WindowsAnsiTerminal.enableAnsiOutput()

0 commit comments

Comments
 (0)