Skip to content

Commit

Permalink
Add a "native-extensions" option to badigeon.bundle/extract-native-de…
Browse files Browse the repository at this point in the history
…pendencies
  • Loading branch information
EwenG committed May 17, 2019
1 parent 1ed8c7b commit 6b3dda9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
4 changes: 3 additions & 1 deletion sample/badigeon/sample.clj
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@
;; The directory where native dependencies are copied.
:native-path "lib"
;; The paths where native dependencies should be searched. The native-prefix is excluded from the output path of the native dependency.
:native-prefixes {'org.lwjgl.lwjgl/lwjgl-platform ""}})
:native-prefixes {'org.lwjgl.lwjgl/lwjgl-platform ""}
;; A collection of native extension regexp. Files which name match one of these regexps are considered a native dependency. Default to badigeon.bundle/native-extensions.
:native-extensions #{#"\.so$"}})

;; Requires a JDK9+
;; Embeds a custom JRE runtime into the bundle.
Expand Down
59 changes: 32 additions & 27 deletions src/badigeon/bundle.clj
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,29 @@
#"\.dll$"
#"\.a$"
#"\.lib$"
;; overtone resources
#"\.scx$"})

(defn- do-extract-native-dependencies
([native-prefix coords out-path]
(do-extract-native-dependencies native-prefix coords out-path
(Paths/get "lib" (make-array 0))))
([native-prefix {:keys [paths]} ^Path out-path ^Path native-path]
(let [^Path native-prefix (if (instance? Path native-prefix)
native-prefix
(Paths/get (str native-prefix) (make-array String 0)))
native-path (.resolve out-path native-path)]
(doseq [^String path paths]
(let [f (io/file path)]
(when (and (.exists f) (not (.isDirectory f))
(.endsWith path ".jar"))
(let [jar-file (JarFile. path)
entries (enumeration-seq (.entries jar-file))]
(doseq [^JarEntry entry entries]
(let [entry-path (.getName entry)]
(when (and (some #(re-find % entry-path) native-extensions)
(.startsWith entry-path (str native-prefix)))
(let [entry-path (.relativize native-prefix (Paths/get (.getName entry) (make-array String 0)))
f-path (.resolve native-path entry-path)]
(Files/createDirectories (.getParent f-path) (make-array FileAttribute 0))
(io/copy (.getInputStream jar-file entry) (.toFile f-path)))))))))))))
[native-prefix {:keys [paths]} ^Path out-path ^Path native-path extensions]
(let [^Path native-prefix (if (instance? Path native-prefix)
native-prefix
(Paths/get (str native-prefix) (make-array String 0)))
native-path (.resolve out-path native-path)]
(doseq [^String path paths]
(let [f (io/file path)]
(when (and (.exists f) (not (.isDirectory f))
(.endsWith path ".jar"))
(let [jar-file (JarFile. path)
entries (enumeration-seq (.entries jar-file))]
(doseq [^JarEntry entry entries]
(let [entry-path (.getName entry)]
(when (and (some #(re-find % entry-path) extensions)
(.startsWith entry-path (str native-prefix)))
(let [entry-path (.relativize native-prefix (Paths/get (.getName entry) (make-array String 0)))
f-path (.resolve native-path entry-path)]
(Files/createDirectories (.getParent f-path) (make-array FileAttribute 0))
(io/copy (.getInputStream jar-file entry) (.toFile f-path))))))))))))

(defn copy-directory [from to-directory]
(let [to-directory (if (string? to-directory)
Expand Down Expand Up @@ -182,18 +180,20 @@
out-path)))

(defn extract-native-dependencies
"Extract native dependencies (.so, .dylib, .dll, .a, .lib files) from jar dependencies. By default native dependencies are extracted to a \"lib\" folder under the output directory.
"Extract native dependencies (.so, .dylib, .dll, .a, .lib, .scx files) from jar dependencies. By default native dependencies are extracted to a \"lib\" folder under the output directory.
- out-path: The path of the output directory.
- deps-map: A map with the same format than a deps.edn map. The dependencies with a jar format resolved from this map are searched for native dependencies. Default to the deps.edn map of the project (without merging the system-level and user-level deps.edn maps), with the addition of the maven central and clojars repository.
- allow-unstable-deps: A boolean. When set to true, the project can depend on local dependencies or a SNAPSHOT version of a dependency. Default to false.
- native-path: The path of the folder where native dependencies are extracted, relative to the output folder. Default to \"lib\".
- native-prefixes: A map from libs (symbol) to a path prefix (string). Libs with a specified native-prefix are searched for native dependencies under the path of the native prefix only. The native-prefix is excluded from the output path of the native dependency."
- native-prefixes: A map from libs (symbol) to a path prefix (string). Libs with a specified native-prefix are searched for native dependencies under the path of the native prefix only. The native-prefix is excluded from the output path of the native dependency.
- native-extensions: A collection of native extension regexp. Files which name match one of these regexps are considered a native dependency. Default to badigeon.bundle/native-extensions."
([out-path]
(extract-native-dependencies out-path nil))
([out-path {:keys [deps-map
allow-unstable-deps?
native-path
native-prefixes]}]
native-prefixes
native-extensions] :as opts}]
(let [deps-map (update deps-map :mvn/repos utils/with-standard-repos)
resolved-deps (deps/resolve-deps deps-map nil)
^Path out-path (if (string? out-path)
Expand All @@ -203,13 +203,18 @@
(if (string? native-path)
(Paths/get native-path (make-array String 0))
native-path)
(Paths/get "lib" (make-array String 0)))]
(Paths/get "lib" (make-array String 0)))
native-extensions (if (contains? opts :native-extensions)
native-extensions
badigeon.bundle/native-extensions)]
(when-not allow-unstable-deps?
(utils/check-for-unstable-deps #(utils/snapshot-dep? %) resolved-deps))
(Files/createDirectories (.resolve out-path native-path) (make-array FileAttribute 0))
(doseq [[lib {:keys [paths] :as coords}] resolved-deps]
(when (contains? native-prefixes lib)
(do-extract-native-dependencies (get native-prefixes lib) coords out-path native-path)))
(do-extract-native-dependencies
(get native-prefixes lib)
coords out-path native-path native-extensions)))
out-path)))

(def ^:const windows-like :windows-like)
Expand Down

0 comments on commit 6b3dda9

Please sign in to comment.