/src/clj/spire/config.clj

https://github.com/epiccastle/spire · Clojure · 56 lines · 45 code · 7 blank · 4 comment · 9 complexity · a4af7753268a7a4c50a208b563c55f33 MD5 · raw file

  1. (ns spire.config
  2. (:require [clojure.java.io :as io])
  3. (:import [java.util Base64]))
  4. (def libs-set ["libspire.dylib" "libspire.so"])
  5. (defn path-split
  6. "give a full path filename, return a tuple of
  7. [path basename]
  8. eg \"blog/posts/1/main.yml\" -> [\"blog/posts/1\" \"main.yml\"]
  9. "
  10. [filename]
  11. (let [file (io/file filename)]
  12. [(.getParent file) (.getName file)]))
  13. (defn path-join
  14. "given multiple file path parts, join them all together with the
  15. file separator"
  16. [& parts]
  17. (.getPath (apply io/file parts)))
  18. (defn setup
  19. "Copy any of the bundled dynamic libs from resources to the
  20. run time lib directory"
  21. [libs-dir]
  22. (doseq [filename libs-set]
  23. (when-let [file (io/resource filename)]
  24. (let [[_ name] (path-split (.getFile file))
  25. dest-path (path-join libs-dir name)
  26. resource-size (with-open [out (java.io.ByteArrayOutputStream.)]
  27. (io/copy (io/input-stream file) out)
  28. (count (.toByteArray out)))]
  29. ;; writing to a library while running its code can result in segfault
  30. ;; only write if filesize is different or it doesnt exist
  31. (when (or (not (.exists (io/file dest-path)))
  32. (not= (.length (io/file dest-path)) resource-size))
  33. (io/copy (io/input-stream file) (io/file dest-path)))))))
  34. (defn get-libs-dir []
  35. (let [home-dir (System/getenv "HOME")
  36. config-dir (path-join home-dir ".spire")
  37. libs-dir (path-join config-dir "libs")]
  38. libs-dir))
  39. (defn init! []
  40. (let [native-image?
  41. (and (= "Substrate VM" (System/getProperty "java.vm.name"))
  42. (= "runtime" (System/getProperty "org.graalvm.nativeimage.imagecode")))
  43. libs-dir (get-libs-dir)]
  44. (.mkdirs (io/as-file libs-dir))
  45. (setup libs-dir)
  46. (when native-image?
  47. ;; most JVM implementations: this property needs to be set on launch
  48. ;; but graalvm allows us to dynamically change it
  49. (System/setProperty "java.library.path" libs-dir))))