forked from clojure-lsp/clojure-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.clj
184 lines (164 loc) · 6.86 KB
/
build.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
(ns build
(:require
[babashka.fs :as fs]
[clojure.java.io :as io]
[clojure.string :as string]
[clojure.tools.build.api :as b]))
(def standalone-lib 'com.github.clojure-lsp/clojure-lsp-standalone)
(def server-lib 'com.github.clojure-lsp/clojure-lsp-server)
(def current-version (string/trim (slurp (io/resource "CLOJURE_LSP_VERSION"))))
(def class-dir "target/classes")
(def basis {:project "deps.edn"
:extra "../lib/deps.edn"})
(def server-file "target/clojure-lsp-server.jar")
(def standalone-file "target/clojure-lsp-standalone.jar")
(def ^:private aarch64?
(-> (System/getProperty "os.arch")
(string/lower-case)
(string/includes? "aarch64")))
(def ^:private linux?
(-> (System/getProperty "os.name")
(string/lower-case)
(string/includes? "linux")))
(defn clean [_]
(b/delete {:path "target"}))
(defn pom [opts]
(let [lib (or (:lib opts) server-lib)]
(b/write-pom {:class-dir class-dir
:lib lib
:src-pom "./pom.xml"
:version current-version
:basis (b/create-basis (update basis :aliases concat (:extra-aliases opts)))
:src-dirs ["src" "../lib/src"]
:resource-dirs ["resources" "../lib/resources"]
:scm {:tag current-version}})
(b/copy-file {:src (str class-dir "/META-INF/maven/" lib "/pom.xml") :target "pom.xml"})
(b/copy-file {:src (str class-dir "/META-INF/maven/" lib "/pom.properties") :target "pom.properties"})))
(defn ^:private standalone-jar [opts]
(clean opts)
(pom (assoc opts :lib standalone-lib))
(b/copy-dir {:src-dirs ["src" "../lib/src" "resources" "../lib/resources"]
:target-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file standalone-file
:main 'clojure-lsp.main
:basis (b/create-basis (update basis :aliases concat (:extra-aliases opts)))}))
(defn ^:private standalone-aot-jar [opts]
(clean opts)
(println "Building uberjar...")
(let [basis (b/create-basis (update basis :aliases concat (:extra-aliases opts)))
src-dirs (into ["src" "resources"] (:extra-dirs opts))]
(b/copy-dir {:src-dirs src-dirs
:target-dir class-dir})
(b/compile-clj {:basis basis
:src-dirs src-dirs
:java-opts ["-Xmx2g" "-server"]
:class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file standalone-file
:main 'clojure-lsp.main
:basis basis})))
(defn ^:private bin
"Create an executable `clojure-lsp` script out of UBER-FILE jar with
the given OPTS.
OPTS can be a map of
:jvm-opts A vector of options ot pass to the JVM."
[opts]
(println "Generating bin...")
(let [jvm-opts (concat (:jvm-opts opts []) ["-Xmx2g" "-server"])]
((requiring-resolve 'deps-bin.impl.bin/build-bin)
{:jar standalone-file
:name "clojure-lsp"
:jvm-opts jvm-opts
:skip-realign true})))
(defn server-jar [opts]
(clean opts)
(pom (assoc opts :lib server-lib))
(println "Building jar...")
(b/copy-dir {:src-dirs ["../lib/src" "../lib/resources" "src" "resources"]
:target-dir class-dir})
(b/jar {:class-dir class-dir
:jar-file server-file}))
(defn server-install [opts]
(server-jar opts)
(println "Installing to local mvn repo...")
(b/install {:basis (b/create-basis (update basis :aliases concat [:debug :test]))
:lib server-lib
:version current-version
:jar-file server-file
:class-dir class-dir}))
(defn standalone-install [opts]
(standalone-jar opts)
(println "Installing to local mvn repo...")
(b/install {:basis (b/create-basis (update basis :aliases concat [:debug :test]))
:lib standalone-lib
:version current-version
:jar-file standalone-file
:class-dir class-dir}))
(defn debug-jar [opts]
(standalone-aot-jar (merge opts {:extra-aliases [:debug :test]
:extra-dirs ["dev"]})))
(defn debug-cli [opts]
(standalone-aot-jar (merge opts {:extra-aliases [:debug :test]
:extra-dirs ["dev"]}))
(bin {:jvm-opts ["-XX:-OmitStackTraceInFastThrow"
"-Djdk.attach.allowAttachSelf=true"
"-Dclojure.core.async.go-checking=true"]}))
(defn prod-jar [opts]
(standalone-aot-jar (merge opts {:extra-aliases [:native]})))
(defn prod-cli [opts]
(standalone-aot-jar opts)
(bin {}))
(defn native-cli [opts]
(println "Building native image...")
(if-let [graal-home (System/getenv "GRAALVM_HOME")]
(let [jar (or (System/getenv "CLOJURE_LSP_JAR")
(do (prod-jar opts)
standalone-file))
native-image (if (fs/windows?) "native-image.cmd" "native-image")
command (->> [(str (io/file graal-home "bin" native-image))
"-jar" jar
"clojure-lsp"
"-H:+ReportExceptionStackTraces"
"--verbose"
"--no-fallback"
"--native-image-info"
"--features=clj_easy.graal_build_time.InitClojureClasses"
(when-not (fs/windows?) "-march=compatibility")
"-O1"
(when-not (or (:pgo-instrument opts)
(fs/windows?)) "--pgo=graalvm/default.iprof")
(or (System/getenv "CLOJURE_LSP_XMX")
"-J-Xmx8g")
(when (and linux? aarch64?)
["-Djdk.lang.Process.launchMechanism=vfork"
"-H:PageSize=65536"])
(when (= "true" (System/getenv "CLOJURE_LSP_STATIC"))
["--static"
(if (= "true" (System/getenv "CLOJURE_LSP_MUSL"))
["--libc=musl" "-H:CCompilerOption=-Wl,-z,stack-size=2097152"]
["-H:+StaticExecutableWithDynamicLibC"])])
(:extra-args opts)]
(flatten)
(remove nil?))
{:keys [exit]} (b/process {:command-args command})]
(when-not (= 0 exit)
(System/exit exit)))
(println "Set GRAALVM_HOME env")))
(defn native-cli-pgo-instrument [opts]
(native-cli (merge opts {:pgo-instrument true
:extra-args ["--pgo-instrument"]})))
(defn deploy-clojars [opts]
(server-jar opts)
((requiring-resolve 'deps-deploy.deps-deploy/deploy)
(merge {:installer :remote
:artifact server-file
:pom-file "pom.xml"}
opts))
(standalone-jar opts)
((requiring-resolve 'deps-deploy.deps-deploy/deploy)
(merge {:installer :remote
:artifact standalone-file
:pom-file "pom.xml"}
opts))
opts)