-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.nims
More file actions
executable file
·232 lines (177 loc) · 5.1 KB
/
Copy pathbuild.nims
File metadata and controls
executable file
·232 lines (177 loc) · 5.1 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#! /usr/bin/env nim --hints:off
import std/[
os,
strformat,
macros,
strutils,
sets
]
const
appname = "page"
bindir = "bin"
distdir = "dist"
nimc = "nim c"
proc cmd(str: string) =
echo "Running `", str, "`"
exec str
proc getPack(targetpair: string): (string, proc()) =
let outpath = bindir / targetpair
return (
outpath / (if "windows" in targetpair: appname & ".exe" else: appname),
proc() =
cmd fmt"cp -R {outpath} ."
cmd fmt"zip -r {targetpair} {targetpair}"
cmd fmt"mv {targetpair}.zip {distdir}"
cmd fmt"rm -rf {targetpair}"
)
type Cmd = ref object
buf: string
defines: HashSet[string]
proc addf(self: Cmd, name: string) =
self.buf &= " "
if name.len == 1:
self.buf &= "-"
else:
self.buf &= "--"
self.buf &= name
proc addf(self: Cmd, name, value: string, quoted: bool = false) =
self.addf(name)
self.buf &= ":"
self.buf &= (if quoted: "\"" & value & "\"" else: value)
macro addd(buf, name: untyped): untyped =
result = newCall(ident"addf", buf, newLit("define"), name.toStrLit)
proc addp(self: Cmd, pkg: string) =
self.addf "p", "nimbledeps" / "pkgs2" / pkg, true
proc checkEnv(self: Cmd) =
if (let def = getEnv("DEF"); def.len > 0):
self.defines = def.split(' ').toHashSet()
proc addExtras(self: Cmd) =
if "nohttp" notin self.defines:
self.addd ssl
self.addp "nargparse-1.0.0-d77b6d27d997463cb62f2067272cddcc7d82de87"
self.addp "noise-0.1.10-c0cbecd0917a5c13cab331cb959a5280acd3401e"
self.addp "regex-0.26.3-4d24e7d7441137cd202e16f2359a5807ddbdc31f"
self.addp "unicodedb-0.13.2-739102d885d99bb4571b1955f5f12aee423c935b"
self.addp "checksums-0.2.1-d3c7a1d0c0dee8fa089bd7f4d474e005877b608a"
proc run(self: Cmd) =
self.checkEnv()
for def in self.defines:
self.addf "define", def, true
self.addExtras()
self.buf &= " src" / "page.nim"
cmd self.buf
let paramc = paramCount() - 2
if not dirExists(distdir):
mkdir distdir
if paramc == 0:
let buf = Cmd(buf: nimc)
buf.addd debug
buf.addf "out", bindir / "page"
buf.run()
else:
let cmd = paramStr(3)
case cmd
of "host":
let buf = Cmd(buf: nimc)
buf.addd release
buf.addf "forceBuild", "on"
let (exepath, _) = getPack("host")
buf.addf("out", exepath)
buf.run()
of "macos":
let buf = Cmd(buf: nimc)
buf.addd release
buf.addf "out", bindir / "host" / "page"
buf.addf "cc", "clang"
buf.addf "clang.exe", "clang"
buf.addf "clang.linkerexe", "clang"
buf.addf "forceBuild", "on"
buf.addf "os", "macosx"
buf.addf "cpu", "arm64"
let (exepath, pack) = getPack("aarch64-macos")
buf.addf("out", exepath)
buf.run()
pack()
of "target", "ctarget":
let ct = cmd[0] == 'c'
let buf = Cmd(buf: nimc)
buf.addd release
buf.addf "cc", "clang"
buf.addf "clang.exe", "zigcc"
buf.addf "clang.linkerexe", "zigcc"
buf.addf "passC", fmt"-target {paramStr(6)}", true
buf.addf "passL", fmt"-target {paramStr(6)}", true
buf.addf "os", paramStr(4)
buf.addf "cpu", paramStr(5)
buf.addf "forceBuild", "on"
if ct:
buf.addf "c"
let (exepath, pack) = getPack(fmt"{cmd}_{paramStr(4)}_{paramStr(5)}_{paramStr(6)}")
buf.addf(
if ct: "nimcache" else: "out",
if (let output = getEnv("OUT"); output.len > 0):
output
else:
exepath.parentDir()
)
buf.run()
if ct:
pack()
of "some":
let targets = @[
("linux", @[
("amd64", "x86_64-linux-gnu"),
("i386", "x86-linux-gnu"),
("arm64", "aarch64-linux-gnu"),
("amd64", "x86_64-linux-musl"),
("i386", "x86-linux-musl"),
("arm64", "aarch64-linux-musl")
]),
("windows", @[
("arm64", "aarch64-windows"),
("amd64", "x86_64-windows"),
("i386", "x86-windows")
])
]
for (os, pairs) in targets:
for (cpu, triple) in pairs:
let buf = Cmd(buf: nimc)
buf.addd release
buf.addf "cc", "clang"
buf.addf "clang.exe", "zigcc"
buf.addf "clang.linkerexe", "zigcc"
buf.addf "passC", fmt"-target {triple}", true
buf.addf "passL", fmt"-target {triple}", true
buf.addf "os", os
buf.addf "cpu", cpu
buf.addf "forceBuild", "on"
let (exepath, pack) = getPack(triple)
buf.addf("out", exepath)
buf.run()
pack()
of "help":
echo """
Usage:
./build.nims help
- Shows this message.
./build.nims some
- Builds for all non-MacOS.
./build.nims target <os> <cpu> <llvm triple>
- builds for the specified target.
./build.nims ctarget <os> <cpu> <llvm triple>
- builds for the specified target, but doesn't build or link the resulting C.
./build.nims macos
- builds for the MacOS target (only works on MacOS systems).
./build.nims host
- builds for the host system.
./build.nims
- builds in debug mode for the host system.
Vars:
'DEF'
- Sets the defines; it should be a list of space-separated symbols.
'OUT'
- Sets the output for the 'target subcommand'
"""
else:
echo fmt"Unknown subcommand '{cmd}'"
quit 1