aboutsummaryrefslogtreecommitdiff
path: root/cljcc-compiler
diff options
context:
space:
mode:
authorYour Name <agrawalshagun07@gmail.com>2025-04-26 01:35:36 +0530
committerYour Name <agrawalshagun07@gmail.com>2025-04-26 01:35:36 +0530
commit0f07ef8ebfcbb7f9077246eec08fd1435cdaee46 (patch)
treed4b943c77faab3a9c77b107341c02a9754830e4b /cljcc-compiler
parent18ae507a42c1e45b6e552fe95c675d1e4db58737 (diff)
Compile library to WASM using GraalVM.
Add main functions for library. Convert to WASM image. Setup public for cljcc website.
Diffstat (limited to 'cljcc-compiler')
-rw-r--r--cljcc-compiler/build.clj28
-rw-r--r--cljcc-compiler/deps.edn8
-rw-r--r--cljcc-compiler/src/cljcc/cljcc.clj25
3 files changed, 60 insertions, 1 deletions
diff --git a/cljcc-compiler/build.clj b/cljcc-compiler/build.clj
new file mode 100644
index 0000000..68d7f95
--- /dev/null
+++ b/cljcc-compiler/build.clj
@@ -0,0 +1,28 @@
+(ns build
+ (:refer-clojure :exclude [test])
+ (:require [clojure.tools.build.api :as b]))
+
+(def lib 'net.clojars.cljcc-lib/cljcc-lib)
+(def main 'cljcc.cljcc)
+(def class-dir "../target/classes")
+
+(defn- uber-opts [opts]
+ (assoc opts
+ :lib lib :main main
+ :uber-file "../target/lib/cljcc-lib.jar"
+ :basis (b/create-basis {})
+ :class-dir class-dir
+ :src-dirs ["src"]
+ :ns-compile [main]))
+
+(defn ci "Run the CI pipeline of tests (and build the uberjar)." [opts]
+ (b/delete {:path "../target"})
+ (let [opts (uber-opts opts)]
+ (println "\nCopying source...")
+ (b/copy-dir {:src-dirs ["resources" "src"] :target-dir class-dir})
+ (println (str "\nCompiling " main "..."))
+ (b/compile-clj opts)
+ (println "\nBuilding JAR...")
+ (b/uber opts)
+ (println "\nJAR built."))
+ opts)
diff --git a/cljcc-compiler/deps.edn b/cljcc-compiler/deps.edn
index 52fd6d7..2d5cfc3 100644
--- a/cljcc-compiler/deps.edn
+++ b/cljcc-compiler/deps.edn
@@ -1,4 +1,10 @@
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/core.match {:mvn/version "1.1.0"}
- metosin/malli {:mvn/version "0.16.4"}}}
+ com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"}
+ metosin/malli {:mvn/version "0.16.4"}}
+ :aliases {:run {:main-opts ["-m" "cljcc.cljcc"]}
+ :build {:deps {io.github.clojure/tools.build
+ {:mvn/version "0.10.3"}}
+ :jvm-opts ["-Dclojure.compiler.direct-linking=true"]
+ :ns-default build}}}
diff --git a/cljcc-compiler/src/cljcc/cljcc.clj b/cljcc-compiler/src/cljcc/cljcc.clj
index fc088fc..e5c1e69 100644
--- a/cljcc-compiler/src/cljcc/cljcc.clj
+++ b/cljcc-compiler/src/cljcc/cljcc.clj
@@ -9,6 +9,8 @@
[cljcc.emit :as emit])
(:gen-class))
+(set! *warn-on-reflection* true)
+
(def valid-os-targets #{:mac :linux})
(def valid-stages #{:lex :parse :validate :tacky :codegen :emit})
@@ -47,3 +49,26 @@
stages-to-run (vec (take stage-idx stages))]
(reduce (fn [acc f] (f acc)) source stages-to-run)))
+(defn- validate-os [os]
+ (let [valid-os #{"mac" "linux"}]
+ (assert (valid-os os) (str "OS: " os "is not valid." "OS type is not valid. Valid os: " (vec valid-os))))
+ os)
+
+(defn- validate-stage [stage]
+ (let [valid-stages #{"lex" "parse" "validate" "tacky" "codegen" "emit"}]
+ (assert (valid-stages stage) (str "Stage is not valid. Valid stages: " (vec valid-stages)))
+ stage))
+
+(defn -main [& args]
+ (let [[source os stage] args
+ source (or source "")
+ os (or os "")
+ stage (or stage "")]
+ (validate-os os)
+ (validate-stage stage)
+ (println (run source {:options {:os (keyword os)
+ :stage (keyword stage)}}))))
+
+(comment
+
+ (-main "int main(void) {return 42;}" "linux" "lex"))