From 0f07ef8ebfcbb7f9077246eec08fd1435cdaee46 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 26 Apr 2025 01:35:36 +0530 Subject: Compile library to WASM using GraalVM. Add main functions for library. Convert to WASM image. Setup public for cljcc website. --- cljcc-compiler/build.clj | 28 ++++++++++++++++++++++++++++ cljcc-compiler/deps.edn | 8 +++++++- cljcc-compiler/src/cljcc/cljcc.clj | 25 +++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 cljcc-compiler/build.clj (limited to 'cljcc-compiler') 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")) -- cgit v1.2.3