From 79bf9bbdc8d5a76026ae10b4539d0bed8446b996 Mon Sep 17 00:00:00 2001
From: Nick Pavlov <gurinderu@gmail.com>
Date: Sun, 21 Jun 2020 18:32:19 +0300
Subject: [PATCH] Add build with musl libc

---
 .github/workflows/ci.yml                      | 30 +++++++++++++++----
 bin/build-native-image.sh                     |  3 --
 build.sbt                                     | 28 +++++++----------
 .../scalafmt-cli/native-image.properties      | 13 ++++++++
 .../scalafmt-cli}/reflection.json             |  0
 5 files changed, 47 insertions(+), 27 deletions(-)
 create mode 100644 scalafmt-cli/src/main/resources/META-INF/native-image/org.scalafmt/scalafmt-cli/native-image.properties
 rename scalafmt-cli/src/main/{graal => resources/META-INF/native-image/org.scalafmt/scalafmt-cli}/reflection.json (100%)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 29eecfeb18..dc0852b23e 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -6,7 +6,7 @@ on:
     tags:
       - '*'
   pull_request:
-  
+
 jobs:
   test:
     strategy:
@@ -40,17 +40,35 @@ jobs:
       fail-fast: false
       matrix:
         os: [macOS-latest, ubuntu-latest]
+        libc: [default, musl]
+        exclude:
+          - os: macOS-latest
+            libc: musl
         include:
           - os: macOS-latest
-            url: https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-20.0.0/graalvm-ce-java11-darwin-amd64-20.0.0.tar.gz
             artifact: scalafmt-macos
           - os: ubuntu-latest
-            url: https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-20.0.0/graalvm-ce-java11-linux-amd64-20.0.0.tar.gz
-            artifact: scalafmt-linux
+            libc: default
+            artifact: scalafmt-linux-glibc
+            env:
+              - NATIVE_IMAGE_STATIC: true
+          - os: ubuntu-latest
+            libc: musl
+            artifact: scalafmt-linux-musl
+            env:
+              - NATIVE_IMAGE_STATIC: true
+              - NATIVE_IMAGE_MUSL: ${GITHUB_WORKSPACE}/bundle
     steps:
       - uses: actions/checkout@v2
-      - uses: olafurpg/setup-scala@v6
-      - run: jabba install graal-custom@20.0=tgz+${{ matrix.url }}
+      - uses: olafurpg/setup-scala@v7
+        with:
+          java-version: graalvm-ce-java11@20.1.0
+      - if: matrix.libc == 'musl'
+        name: Install musl bundle
+        run: |
+          wget https://github.com/gradinac/musl-bundle-example/releases/download/v1.0/musl.tar.gz
+          tar xvf musl.tar.gz
+
       - run: bin/build-native-image.sh
         env:
           CI: true
diff --git a/bin/build-native-image.sh b/bin/build-native-image.sh
index 53548da372..26946df82b 100755
--- a/bin/build-native-image.sh
+++ b/bin/build-native-image.sh
@@ -1,8 +1,5 @@
 set -eux
 # NOTE(olafur): for some reason `jabba use ...` doesn't seem to work on GH Actions
-export JAVA_HOME=$(jabba which --home graal-custom@20.0)
-export PATH=$JAVA_HOME/bin:$PATH
-echo $JAVA_HOME
 which gu
 gu install native-image
 sbt cli/graalvm-native-image:packageBin
diff --git a/build.sbt b/build.sbt
index e7812213dd..7ae7cb4ac0 100644
--- a/build.sbt
+++ b/build.sbt
@@ -182,24 +182,16 @@ lazy val cli = project
     scalacOptions ++= scalacJvmOptions.value,
     mainClass in GraalVMNativeImage := Some("org.scalafmt.cli.Cli"),
     graalVMNativeImageOptions ++= {
-      val reflectionFile =
-        Keys.sourceDirectory.in(Compile).value./("graal")./("reflection.json")
-      assert(reflectionFile.exists, "no such file: " + reflectionFile)
-      List(
-        "-H:+ReportUnsupportedElementsAtRuntime",
-        "-Dscalafmt.native-image=true",
-        "--initialize-at-build-time",
-        "--no-server",
-        "--enable-http",
-        "--enable-https",
-        "-H:EnableURLProtocols=http,https",
-        "--enable-all-security-services",
-        "--no-fallback",
-        s"-H:ReflectionConfigurationFiles=$reflectionFile",
-        "--allow-incomplete-classpath",
-        "-H:+ReportExceptionStackTraces"
-        //"--initialize-at-build-time=scala.Function1"
-      )
+      sys.env
+        .get("NATIVE_IMAGE_MUSL")
+        .map(path => s"-H:UseMuslC=$path")
+        .toSeq ++
+        sys.env
+          .get("NATIVE_IMAGE_STATIC")
+          .map(_.toBoolean)
+          .filter(identity)
+          .map(_ => "--static")
+          .toSeq
     }
   )
   .dependsOn(coreJVM, dynamic)
diff --git a/scalafmt-cli/src/main/resources/META-INF/native-image/org.scalafmt/scalafmt-cli/native-image.properties b/scalafmt-cli/src/main/resources/META-INF/native-image/org.scalafmt/scalafmt-cli/native-image.properties
new file mode 100644
index 0000000000..6883e8f348
--- /dev/null
+++ b/scalafmt-cli/src/main/resources/META-INF/native-image/org.scalafmt/scalafmt-cli/native-image.properties
@@ -0,0 +1,13 @@
+Args=--no-server \
+  -Dscalafmt.native-image=true \
+  --no-fallback \
+  --enable-http \
+  --enable-https \
+  --enable-all-security-services \
+  --install-exit-handlers \
+  --initialize-at-build-time \
+  --verbose \
+  -H:EnableURLProtocols=http,https \
+  -H:+TraceClassInitialization \
+  -H:+RemoveSaturatedTypeFlows \
+  -H:+ReportExceptionStackTraces
diff --git a/scalafmt-cli/src/main/graal/reflection.json b/scalafmt-cli/src/main/resources/META-INF/native-image/org.scalafmt/scalafmt-cli/reflection.json
similarity index 100%
rename from scalafmt-cli/src/main/graal/reflection.json
rename to scalafmt-cli/src/main/resources/META-INF/native-image/org.scalafmt/scalafmt-cli/reflection.json