From 566a47173b4019d951a326271763ce5e7a5b88a0 Mon Sep 17 00:00:00 2001 From: Olafur Pall Geirsson Date: Wed, 9 Sep 2020 09:31:23 +0200 Subject: [PATCH] Try to support automatic Windows installation. This commits moves to using `gu install native-image` instead of `coursier launch --jvm graalvm svm`. The second approach doesn't appear to work well with Java 11. --- .github/workflows/ci.yml | 31 +++---------- .github/workflows/native.yml | 46 +++---------------- .../sbtnativeimage/NativeImagePlugin.scala | 37 ++++++++++----- 3 files changed, 38 insertions(+), 76 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 16c5acd..c39cb29 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,14 +11,16 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest] + os: [ubuntu-latest, windows-latest] java: [8] steps: - uses: actions/checkout@v2 - uses: olafurpg/setup-scala@v7 with: java-version: "adopt@1.${{ matrix.java }}" - - shell: bash + - name: sbt test plugin/scripted + if: ${{ matrix.os }} != windows-latest + shell: bash run: | export JABBA=/home/runner/bin/jabba $JABBA install graalvm@20.1.0 @@ -26,29 +28,10 @@ jobs: $GRAALVM_HOME/bin/gu install native-image export NATIVE_IMAGE_COMMAND=$GRAALVM_HOME/bin/native-image sbt test plugin/scripted - windows: - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [windows-latest] - steps: - - uses: actions/checkout@v2 - - name: Configure Pagefile - uses: al-cheb/configure-pagefile-action@v1.2 - - name: Refresh Pagefile - run: | - (Get-CimInstance Win32_PageFileUsage).AllocatedBaseSize - - uses: olafurpg/setup-scala@v7 - with: - java-version: graalvm-ce-java11@20.1.0 - - name: gu install native-image - shell: bash - run: | - echo $JAVA_HOME - "$JAVA_HOME\bin\gu.cmd" install native-image - name: sbt test shell: cmd + if: ${{ matrix.os }} == windows-latest run: >- "C:\Program Files (x86)\Microsoft Visual - Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && sbt test + Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && sbt + example/nativeImage diff --git a/.github/workflows/native.yml b/.github/workflows/native.yml index cfbe073..ecbd982 100644 --- a/.github/workflows/native.yml +++ b/.github/workflows/native.yml @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: false matrix: - os: [macOS-latest, ubuntu-latest] + os: [macOS-latest, ubuntu-latest, windows-latest] include: # Replace "example" with the name your binary - os: macOS-latest @@ -21,53 +21,19 @@ jobs: - os: ubuntu-latest artifact: example-x86_64-pc-linux binary: example/target/native-image/example - steps: - - uses: actions/checkout@v2 - - uses: olafurpg/setup-scala@v6 - - run: git fetch --tags || true - - run: sbt example/nativeImage - - uses: actions/upload-artifact@master - with: - path: ${{ matrix.binary }} - name: ${{ matrix.artifact }} - - name: Upload release - if: github.event_name == 'release' - uses: actions/upload-release-asset@v1.0.1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ github.event.release.upload_url }} - asset_path: ${{ matrix.binary }} - asset_name: ${{ matrix.artifact }} - asset_content_type: application/zip - windows: - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [windows-latest] - include: - # Replace "example" with the name your binary - os: windows-latest artifact: example-x86_64-pc-win32.exe binary: example\target\native-image\example.exe steps: - uses: actions/checkout@v2 - - name: Configure Pagefile - uses: al-cheb/configure-pagefile-action@v1.2 - - name: Refresh Pagefile - run: | - (Get-CimInstance Win32_PageFileUsage).AllocatedBaseSize - - uses: olafurpg/setup-scala@v7 - with: - java-version: graalvm-ce-java11@20.1.0 - - name: gu install native-image + - uses: olafurpg/setup-scala@v6 + - run: git fetch --tags || true + - run: sbt example/nativeImage shell: bash - run: | - echo $JAVA_HOME - "$JAVA_HOME\bin\gu.cmd" install native-image + if: ${{ matrix.os }} != windows-latest - name: sbt test shell: cmd + if: ${{ matrix.os }} == windows-latest run: >- "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && sbt diff --git a/plugin/src/main/scala/sbtnativeimage/NativeImagePlugin.scala b/plugin/src/main/scala/sbtnativeimage/NativeImagePlugin.scala index 9402e88..88e6b6c 100644 --- a/plugin/src/main/scala/sbtnativeimage/NativeImagePlugin.scala +++ b/plugin/src/main/scala/sbtnativeimage/NativeImagePlugin.scala @@ -93,9 +93,12 @@ object NativeImagePlugin extends AutoPlugin { { () => this.alertUser(s, "Native image ready!") } }, mainClass.in(NativeImage) := mainClass.in(Compile).value, - nativeImageJvm := "graalvm", - nativeImageVersion := "19.3.2", - name.in(NativeImage) := name.value, + nativeImageJvm := "graalvm-java11", + nativeImageVersion := "20.1.0", + name.in(NativeImage) := { + val exe = if (Properties.isWin) ".exe" else "" + name.value + exe + }, mainClass.in(NativeImage) := mainClass.in(Compile).value, nativeImageOptions := List(), nativeImageCoursier := { @@ -109,7 +112,6 @@ object NativeImagePlugin extends AutoPlugin { }, nativeImageCommand := Def.taskDyn { if ( - Properties.isWin || "true".equalsIgnoreCase(System.getProperty("native-image-installed")) || "true".equalsIgnoreCase(System.getenv("NATIVE_IMAGE_INSTALLED")) ) { @@ -120,15 +122,26 @@ object NativeImagePlugin extends AutoPlugin { Def.task(List[String](path.toString())) } else { Def.task { - val svmVersion = nativeImageVersion.value - List( - nativeImageCoursier.value.absolutePath, - "launch", - "--jvm", - s"${nativeImageJvm.value}:$svmVersion", - s"org.graalvm.nativeimage:svm-driver:$svmVersion", - "--" + val coursier = nativeImageCoursier.value.absolutePath + val svm = nativeImageVersion.value + val jvm = nativeImageJvm.value + val javaHome = Paths.get( + Process(List(coursier, "java-home", "--jvm", s"$jvm:$svm")).!!.trim ) + val cmd = if (Properties.isWin) ".cmd" else "" + val ni = javaHome.resolve("bin").resolve(s"native-image$cmd") + if (!Files.isExecutable(ni)) { + val gu = ni.resolveSibling(s"gu$cmd") + Process(List(gu.toString, "install", "native-image")).! + } + if (!Files.isExecutable(ni)) { + throw new MessageOnlyException( + "Failed to automatically install native-image. " + + "To fix this problem, install native-image manually and start sbt with " + + "the environment variable 'NATIVE_IMAGE_INSTALLED=true'" + ) + } + List(ni.toString()) } } }.value,