taylorwood/clj.native-image

Fallback to native-image on PATH?

sogaiu opened this issue · 3 comments

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.)

(defn native-image-bin-path []
(-> (io/file (System/getenv "GRAALVM_HOME") "bin/native-image")
(.getAbsolutePath)))

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

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 "-" "_"))

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

It appears to work fine!

Thanks a lot :)