Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add graal test #362

Merged
merged 2 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/graal_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: graal-test

on: [push, pull_request]

jobs:

clojure:

strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- name: Checkout
uses: actions/checkout@v3

# It is important to install java before installing clojure tools which needs java
# exclusions: babashka, clj-kondo and cljstyle
- uses: graalvm/setup-graalvm@v1
with:
version: 'latest'
java-version: '17'
components: 'native-image'
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Install clojure tools
uses: DeLaGuardo/setup-clojure@10.0
with:
lein: 2.9.1 # Leiningen
bb: latest # Babashka

# Optional step:
- name: Cache clojure dependencies
uses: actions/cache@v3
with:
path: |
~/.m2/repository
~/.gitlibs
~/.deps.clj
# List all files containing dependencies:
key: cljdeps-${{ hashFiles('deps.edn') }}
# key: cljdeps-${{ hashFiles('deps.edn', 'bb.edn') }}
# key: cljdeps-${{ hashFiles('project.clj') }}
# key: cljdeps-${{ hashFiles('build.boot') }}
restore-keys: cljdeps-

- name: Execute graal-test
run: bb test:graal
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ pom.xml*
/doc/
.idea/
*.iml
graal_test
graal_test.build_artifacts.txt
.cache
6 changes: 6 additions & 0 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{:paths ["bb"]
:tasks {:requires ([graal-test])
test:graal {:doc "Run native-image tests"
:task (do (graal-test/uberjar)
(graal-test/native-image)
(graal-test/test-native-image))}}}
37 changes: 37 additions & 0 deletions bb/graal_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bb

(ns graal-test
(:require
[babashka.fs :as fs]
[babashka.process :refer [shell]]
[clojure.string :as str]))

(defn uberjar []
(let [command "lein with-profiles +graal-test uberjar"
command (if (fs/windows?)
(if (fs/which "lein")
command
;; assume powershell module
(str "powershell.exe -command " (pr-str command)))
command)]
(shell command)))

(defn executable [dir name]
(-> (fs/glob dir (if (fs/windows?)
(str name ".{exe,bat,cmd}")
name))
first
fs/canonicalize
str))

(defn native-image []
(let [graalvm-home (System/getenv "GRAALVM_HOME")
bin-dir (str (fs/file graalvm-home "bin"))]
(shell (executable bin-dir "gu") "install" "native-image")
(shell (executable bin-dir "native-image") "-jar" "target/graal.jar" "--no-fallback" "graal_test")))

(defn test-native-image []
(let [{:keys [out]}
(shell {:out :string} (executable "." "graal_test") "1" "2" "3")]
(assert (str/includes? out (str '("1" "2" "3"))) out)
(println "Native image works!")))
9 changes: 8 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@
[com.taoensso/nippy "3.2.0"]
[com.taoensso/carmine "3.1.0"
:exclusions [com.taoensso/timbre]]
[com.draines/postal "2.0.5"]]}}
[com.draines/postal "2.0.5"]]}

:graal-test
{:dependencies [[org.clojure/clojure "1.11.1"]
[com.github.clj-easy/graal-build-time "0.1.4"]]
:main taoensso.timbre.graal-test
:aot [taoensso.timbre.graal-test]
:uberjar-name "graal.jar"}}

:test-paths ["test" #_"src"]

Expand Down
18 changes: 6 additions & 12 deletions src/taoensso/timbre.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,9 @@

(defn- fline [and-form] (:line (meta and-form)))

;; Try enable reproducible builds by ensuring that `log!` macro expansion
;; produces deterministic callsite-ids, Ref. #354
#?(:cljs (def ^:private deterministic-rand rand) ; Dummy, non-deterministic
:clj (let [;; Must be delayed for GraalVM compatibility, Ref. #360
rand_ (delay (java.util.Random. 715873))]
(defn- deterministic-rand [] (.nextDouble ^java.util.Random @rand_))))
(enc/defonce ^:private callsite-counter
"Simple counter, used to uniquely identify each log macro expansion."
(enc/counter))

(defmacro log! ; Public wrapper around `-log!`
"Core low-level log macro. Useful for tooling/library authors, etc.
Expand Down Expand Up @@ -786,12 +783,9 @@

?file (when (not= ?file "NO_SOURCE_PATH") ?file)

;; Identifies this particular macro expansion; note that this'll
;; be fixed for any fns wrapping `log!` (notably `tools.logging`,
;; `slf4j-timbre`, etc.):
callsite-id
(hash [level msg-type args ; Unevaluated args (arg forms)
?ns-str ?file ?line (deterministic-rand)])
;; Note that this'll be const for any fns wrapping `log!`
;; (notably `tools.logging`, `slf4j-timbre`, etc.)
callsite-id (callsite-counter)

vargs-form
(if (symbol? args)
Expand Down
6 changes: 6 additions & 0 deletions test/taoensso/timbre/graal_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(ns taoensso.timbre.graal-test
(:require [taoensso.timbre :refer [info]])
(:gen-class))

(defn -main [& args]
(info args))