Skip to content

Commit

Permalink
feat: test building graalvm app using clara rules (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
k13gomez authored Sep 21, 2024
1 parent c73ce98 commit 29bc7ec
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 23 deletions.
32 changes: 19 additions & 13 deletions .github/workflows/clojure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ name: Clojure CI

on:
push:
branches: "*"
branches:
- 'main'
pull_request:
branches: [ "main" ]
branches:
- '**'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Java
uses: actions/setup-java@v3
- name: Tools setup
uses: jdx/mise-action@v2
with:
# see: https://github.com/marketplace/actions/setup-java
distribution: 'corretto'
java-version: '11'
- name: Install clojure tools
uses: DeLaGuardo/setup-clojure@12.1
with:
# see: https://github.com/marketplace/actions/setup-clojure
cli: 1.11.1.1429
install: true
cache: true
- name: Tools config
run: |
echo "JAVA_HOME=$(mise where java)" >> $GITHUB_ENV
echo "GRAALVM_HOME=$(mise where java)" >> $GITHUB_ENV
echo "$(mise where java)/bin" >> $GITHUB_PATH
gu install native-image
- name: Run tests
run: make test
- name: Run coverage tests
Expand All @@ -32,4 +34,8 @@ jobs:
- name: Run clj-kondo linter
run: make lint
- name: Run build jar
run: make clean build
run: make build
- name: Run build app
run: |
make build-native
build/clara-test-app this is a test invocation of native app
5 changes: 5 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[plugins]
clojure = "https://github.com/asdf-community/asdf-clojure.git"
[tools]
java = "graalvm-22.3.1+java11"
clojure = "1.11"
39 changes: 31 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,60 @@

SHELL := /bin/bash

env:
env

compile-main-java:
clojure -T:build compile-main-java

compile-test-java: compile-main-java
clojure -T:build compile-test-java

repl: compile-test-java
clojure -M:dev:test:repl
clojure -M:dev:test:app:repl

test: compile-test-java
clojure -M:dev:test:runner --focus :unit --reporter kaocha.report/documentation --no-capture-output
clojure -M:dev:test:app:runner --focus :unit --reporter kaocha.report/documentation --no-capture-output

test-coverage: compile-test-java
clojure -M:dev:test:runner --focus :coverage --reporter kaocha.report/documentation --no-capture-output
clojure -M:dev:test:app:runner --focus :coverage --reporter kaocha.report/documentation --no-capture-output
jq '.coverage["clara/coverage_ruleset.clj"]|join("_")' target/coverage/codecov.json | grep "_1___1____1_0_1__1__" || echo "Unexpected coverage output for clara.coverage-ruleset." exit 1

test-generative: compile-test-java
clojure -M:dev:test:runner --focus :generative --reporter kaocha.report/documentation --no-capture-output
clojure -M:dev:test:app:runner --focus :generative --reporter kaocha.report/documentation --no-capture-output

test-config:
clojure -M:dev:test:runner --print-config
clojure -M:dev:test:app:runner --print-config

clean:
rm -rf target build

lint: compile-test-java
clojure -M:dev:test:clj-kondo --copy-configs --dependencies --parallel --lint "$(shell clojure -A:dev:test -Spath)"
clojure -M:dev:test:clj-kondo --lint "src/main:src/test" --fail-level "error"
clojure -M:dev:test:app:clj-kondo --copy-configs --dependencies --parallel --lint "$(shell clojure -A:dev:test -Spath)"
clojure -M:dev:test:app:clj-kondo --lint "src/main:src/test" --fail-level "error"

build: compile-main-java
clojure -X:jar :sync-pom true
clojure -X:jar :sync-pom true :jar "build/clara-rules.jar"

build-native: build
mkdir -p build/graalvm-config
clojure -X:uberjar :sync-pom false :jar "build/clara-test-app.jar"
java -agentlib:native-image-agent=config-output-dir=build/graalvm-config -jar "build/clara-test-app.jar" clara.main
native-image -jar "build/clara-test-app.jar" \
"-H:+ReportExceptionStackTraces" \
"-H:+JNI" \
"-H:EnableURLProtocols=http,https,jar" \
"-J-Dclojure.spec.skip-macros=true" \
"-J-Dclojure.compiler.direct-linking=true" \
"--report-unsupported-elements-at-runtime" \
"--verbose" \
"--no-fallback" \
"--no-server" \
"--allow-incomplete-classpath" \
"--trace-object-instantiation=java.lang.Thread" \
"--features=clj_easy.graal_build_time.InitClojureClasses" \
"-H:ConfigurationFileDirectories=build/graalvm-config" \
"build/clara-test-app"

deploy: clean build
clojure -X:deploy-maven
Expand Down
51 changes: 51 additions & 0 deletions app/clara/main.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(ns clara.main
(:require
[clara.rules :refer [defrule
insert!
insert
defquery
defhierarchy
defsession
query
fire-rules-async] :as r]
[futurama.core :refer [!<! !<!! async]])
(:gen-class)
(:import
[java.util.concurrent CompletableFuture]))

(defrule simple-transform-rule
"simple rule that transforms an input into a result"
[:my/input [{:keys [value]}]
(= value ?value)]
=>
(insert! {:fact/type :my/result
:result ?value}))

(defquery simple-output-query
"simple query that returns an output fact"
[]
[?output <- :my/output])

(defhierarchy simple-hierarchy
"simple hierarchy of facts"
:my/thing :my/input
:my/result :my/output
:my/input :my/fact
:my/output :my/fact)

(defsession my-session
'clara.main
:fact-type-fn :fact/type)

(defn -main
"this is a test to compile a bunch of async code into native code, pretty nasty"
[& args]
(println
"async result:"
(!<!!
(async
(let [session (!<! (-> my-session
(insert {:fact/type :my/thing
:value args})
(fire-rules-async)))]
(query session simple-output-query))))))
12 changes: 10 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
org.clojure/math.combinatorics {:mvn/version "0.3.0"}
criterium/criterium {:mvn/version "0.4.6"}}}

:app {:extra-paths ["app"]
:extra-deps {com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"}}}

:clj-kondo {:extra-deps {clj-kondo/clj-kondo {:mvn/version "2024.03.05"}}
:main-opts ["-m" "clj-kondo.main"]}

Expand Down Expand Up @@ -47,8 +50,13 @@
:main-opts ["-m" "cljfmt.main" "check" "src" "dev"]}

:jar {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.303"}}
:exec-fn hf.depstar/jar
:exec-args {:jar "build/clara-rules.jar"}}
:exec-fn hf.depstar/jar}

:uberjar {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.303"}}
:exec-fn hf.depstar/uberjar
:exec-args {:aot true
:aliases [:app]
:main-class clara.main}}

:install-maven {:extra-deps {slipset/deps-deploy {:mvn/version "0.2.2"}}
:exec-fn deps-deploy.deps-deploy/deploy
Expand Down

0 comments on commit 29bc7ec

Please sign in to comment.