taylorwood / clj.native-image

Build GraalVM native images with Clojure Deps and CLI tools
MIT License
271 stars 20 forks source link

Fallback to native-image on PATH? #10

Closed sogaiu closed 5 years ago

sogaiu commented 5 years ago

I have native-image in my PATH but it isn't used. (As the documentation mentions, setting GRAALVM_HOME or having the bin subdirectory of my project contain a symlink to native-image works.)

https://github.com/taylorwood/clj.native-image/blob/567176ddb0f7507c8b0969e0a10f60f848afaf7d/src/clj/native_image.clj#L57-L59

Does it sound reasonable for clj.native-image to make use of a native-image binary that's on one's PATH?

sogaiu commented 5 years ago

How is something like this?

diff --git a/src/clj/native_image.clj b/src/clj/native_image.clj
index 76766be..6cc8097 100644
--- a/src/clj/native_image.clj
+++ b/src/clj/native_image.clj
@@ -59,11 +59,17 @@
     (.mkdir compile-path)))

 (defn native-image-bin-path []
-  (-> (io/file (System/getenv "GRAALVM_HOME")
-               (if windows?
-                 "bin/native-image.cmd"
-                 "bin/native-image"))
-      (.getAbsolutePath)))
+  (let [path (->> (clojure.string/split (System/getenv "PATH") #":")
+                  (map #(str % "/native-image"))
+                  (filter #(.isFile (java.io.File. %)))
+                  first)]
+    (if (string? path)
+      path
+      (-> (io/file (System/getenv "GRAALVM_HOME")
+                   (if windows?
+                     "bin/native-image.cmd"
+                     "bin/native-image"))
+          (.getAbsolutePath)))))

 (defn- munge-class-name [class-name]
   (cs/replace class-name "-" "_"))
taylorwood commented 5 years ago

@sogaiu let me know if latest commit works for you. Thanks!

sogaiu commented 5 years ago

It appears to work fine!

Thanks a lot :)