-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
build.nims
executable file
·616 lines (473 loc) · 16.9 KB
/
build.nims
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/env nim
######################################################
# Arturo
# Programming Language + Bytecode VM compiler
# (c) 2019-2024 Yanis Zafirópulos
#
# @file: build.nims
######################################################
# initial conversion to NimScript thanks to:
# - Patrick (skydive241@gmx.de)
#=======================================
# Libraries
#=======================================
import std/json, os
import strformat, strutils
import ".config/utils/ui.nims"
import ".config/utils/cli.nims"
#=======================================
# Initialize globals
#=======================================
mode = ScriptMode.Silent
--hints:off
#=======================================
# Flag system
#=======================================
include ".config/utils/flags.nims"
include ".config/arch.nims"
include ".config/buildmode.nims"
include ".config/devtools.nims"
include ".config/who.nims"
#=======================================
# Constants
#=======================================
let
targetDir = getHomeDir()/".arturo"
paths: tuple = (
targetBin: targetDir/"bin",
targetLib: targetDir/"lib",
targetStores: targetDir/"stores",
mainFile: "src"/"arturo.nim",
)
#=======================================
# Types
#=======================================
type BuildConfig = tuple
binary, version, bundle: string
shouldCompress, shouldInstall, shouldLog, generateBundle, isDeveloper: bool
func webVersion(config: BuildConfig): bool
func backend(config: BuildConfig): string =
result = "c"
if config.webVersion:
return "js"
func silentCompilation(config: BuildConfig): bool =
## CI and User builds should actually be silent,
## the most important is the exit code.
## But for developers, it's useful to have a detailed log.
not (config.isDeveloper or config.shouldLog)
func webVersion(config: BuildConfig): bool =
config.version == "@web"
func buildConfig(): BuildConfig =
(
binary: "bin/arturo".toExe,
version: "@full",
bundle: "",
shouldCompress: true,
shouldInstall: false,
shouldLog: false,
generateBundle: false,
isDeveloper: false,
)
#=======================================
# Helpers
#=======================================
func toErrorCode(a: bool): int =
if a:
return QuitSuccess
else:
return QuitFailure
template unless(condition: bool, body: untyped) =
if not condition:
body
# TODO(build.nims) JavaScript compression not working correctly
# labels: web,bug
proc recompressJS*(jsFile: string, config: BuildConfig) =
var js: string
"testsed.txt".writeFile("""
s/Field([0-5])/F\1/g
s/field [^\"]+ is not accessible [^\"]+//g
""")
let CompressionResult =
gorgeEx fmt"""
sed -E -f testsed.txt {jsFile}
"""
if CompressionResult.exitCode != QuitSuccess:
js = readFile(jsFile)
.replaceWord("Field0", "F0")
.replaceWord("Field1", "F1")
.replaceWord("Field2", "F2")
.replaceWord("Field3", "F3")
else:
js = CompressionResult.output
jsFile.writeFile js
proc miniBuild*() =
# all the necessary "modes" for mini builds
miniBuildConfig()
# plus, shrinking + the MINI flag
if hostOS=="freebsd" or hostOS=="openbsd" or hostOS=="netbsd":
--verbosity:3
proc compressBinary(config: BuildConfig) =
if (not config.shouldCompress) or (not config.webVersion):
return
section "Post-processing..."
log "compressing binary..."
let minBin = config.binary.replace(".js",".min.js")
let CompressionResult =
gorgeEx fmt"uglifyjs {config.binary} -c -m ""toplevel,reserved=['A$']"" -c -o {minBin}"
if CompressionResult.exitCode != QuitSuccess:
warn "uglifyjs: 3rd-party tool not available"
minBin.writeFile readFile(config.binary)
recompressJS(minBin, config)
proc verifyDirectories*() =
## Create target dirs recursively, if they don't exist
log "setting up directories..."
for path in [paths.targetBin, paths.targetLib, paths.targetStores]:
mkdir path
proc updateBuild*() =
## Increment the build version by one and perform a commit.
proc commit(file: string): string =
let cmd = fmt"git commit -m 'build update' {file}"
cmd.gorgeEx().output
proc increaseVersion(file: string) =
let buildVersion: int = file.readFile()
.strip()
.parseInt()
.succ()
file.writeFile $buildVersion
proc main() =
let buildFile = "version/build"
increaseVersion(buildFile)
for line in commit(buildFile).splitLines:
echo line.strip()
main()
proc compile*(config: BuildConfig, showFooter: bool = false): int
{. raises: [OSError, ValueError, Exception] .} =
proc windowsHostSpecific() =
if config.isDeveloper and not flags.contains("NOWEBVIEW"):
discard gorgeEx "src\\extras\\webview\\deps\\build.bat"
#discard gorgeEx "src\\extras\\webview\\deps\\build-new.bat"
--passL:"\"-static-libstdc++ -static-libgcc -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic\""
--gcc.linkerexe:"g++"
proc unixHostSpecific() =
--passL:"\"-lm\""
result = QuitSuccess
let
params = flags.join(" ")
cmd = fmt"nim {config.backend} {params} -o:{config.binary} {paths.mainFile}"
if "windows" == hostOS:
windowsHostSpecific()
else:
unixHostSpecific()
if config.silentCompilation:
return cmd.gorgeEx().exitCode
else:
echo fmt"{colors.gray}"
cmd.exec()
proc installAll*(config: BuildConfig, targetFile: string) =
# Helper functions
proc copy(file: string, source: string, target: string) =
cpFile source.joinPath(file), target.joinPath(file)
# Methods
proc copyWebView() =
let
sourcePath = "src\\extras\\webview\\deps\\dlls\\x64\\"
targetPath = "bin"
log "copying webview..."
"webview.dll".copy(sourcePath, targetPath)
"WebView2Loader.dll".copy(sourcePath, targetPath)
proc copyArturo(config: BuildConfig, targetFile: string) =
log "copying files..."
cpFile(config.binary, targetFile)
proc giveBinaryPermission(targetFile: string) =
exec fmt"chmod +x {targetFile}"
proc main(config: BuildConfig) =
if not config.shouldInstall:
return
section "Installing..."
if config.webVersion:
panic "Web builds can't be installed, please don't use --install"
verifyDirectories()
config.copyArturo(targetFile)
if hostOS != "windows":
giveBinaryPermission(targetFile)
else:
copyWebView()
log fmt"deployed to: {targetDir}"
main(config)
proc showBuildInfo*(config: BuildConfig) =
let
params = flags.join(" ")
version = "version/version".staticRead()
build = "version/build".staticRead()
if config.generateBundle:
section "Bundling..."
else:
section "Building..."
log fmt"version: {version}/{build}"
log fmt"config: {config.version}"
if not config.silentCompilation:
log fmt"flags: {params}"
#=======================================
# Methods
#=======================================
proc buildArturo*(config: BuildConfig, targetFile: string) =
# Methods
proc showInfo(config: BuildConfig) =
showEnvironment()
config.showBuildInfo()
proc setDevmodeUp() =
section "Updating build..."
updateBuild()
devConfig()
proc setBundlemodeUp() =
bundleConfig()
putEnv "BUNDLE_CONFIG", config.bundle
proc tryCompilation(config: BuildConfig) =
## Panics if can't compile.
if (let cd = config.compile(showFooter=true); cd != 0):
panic "Compilation failed. Please try again with --log and report it.", cd
proc main() =
showHeader "install"
if config.isDeveloper:
setDevmodeUp()
if config.generateBundle:
setBundlemodeUp()
config.showInfo()
config.tryCompilation()
config.compressBinary()
if config.shouldInstall:
config.installAll(targetFile)
showFooter()
main()
proc buildPackage*(config: BuildConfig) =
# Helper functions
proc dataFile(package: string): string =
return fmt"{package}.data.json"
proc file(package: string): string =
return fmt"{package}.art"
proc info(package: string): string =
staticExec fmt"arturo --package-info {package.file}"
# Subroutines
proc generateData(package: string) =
section "Processing data..."
(package.dataFile).writeFile(package.info)
log fmt"written to: {package.dataFile}"
proc setEnvUp(package: string) =
section "Setting up options..."
putEnv "PORTABLE_INPUT", package.file
putEnv "PORTABLE_DATA", package.dataFile
log fmt"done!"
proc setFlagsUp() =
--forceBuild:on
--opt:size
--define:NOERRORLINES
--define:PORTABLE
proc showFlags() =
let params = flags.join(" ")
log fmt"FLAGS: {params}"
echo ""
proc cleanUp(package: string) =
rmFile package.dataFile
echo fmt"{styles.clear}"
proc main() =
let package = config.binary
showHeader "package"
package.generateData()
package.setEnvUp()
showEnvironment()
config.showBuildInfo()
setFlagsUp()
showFlags()
if (let cd = compile(config, showFooter=false); cd != 0):
panic "Package building failed. Please try again with --log and report it.", cd
package.cleanUp()
main()
proc buildDocs*() =
let
params = flags.join(" ")
genDocs = fmt"nim doc --project --index:on --outdir:dev-docs {params} src/arturo.nim"
genIndex = "nim buildIndex -o:dev-docs/theindex.html dev-docs"
showHeader "docs"
section "Generating documentation..."
genDocs.exec()
genIndex.exec()
proc performTests*(binary: string): bool =
result = true
showHeader "test"
try:
exec fmt"{binary} ./tools/tester.art"
except:
return false
proc performBenchmarks*(binary: string): bool =
result = true
showHeader "benchmark"
try:
exec fmt"{binary} ./tools/benchmarker.art"
except:
return false
#=======================================
# Main
#=======================================
cliInstance.header = getLogo()
cliInstance.defaultCommand = "build"
let
args = cliInstance.args
cmd build, "[default] Build arturo and optionally install the executable":
## build:
## Provides a cross-compilation for the Arturo's binary.
##
## --arch -a: $hostCPU chooses the target CPU
## [amd64, arm, arm64, i386, x86]
## --as: arturo changes the name of the binary
## --mode -m: full chooses the target Build Version
## [full, mini, web]
## --os: $hostOS chooses the target OS
## [freebsd, linux, openbsd, mac, macos, macosx, netbsd, win, windows]
## --profiler -p: none defines which profiler use
## [default, mem, native, none, profile]
## --who: none defines who is compiling the code
## [dev, user]
## --debug -d enables debugging
## --install -i installs the final binary
## --log -l shows compilation logs
## --raw disables compression
## --release enable release config mode
## --help
let
availableCPUs = @["amd-64", "x64", "x86-64", "arm-64", "i386", "x86",
"x86-32", "arm", "arm-32"]
availableOSes = @["freebsd", "openbsd", "netbsd", "linux", "mac",
"macos", "macosx", "win", "windows",]
availableBuilds = @["full", "mini", "safe", "web"]
availableProfilers = @["default", "mem", "native", "profile"]
var config = buildConfig()
config.binary = "bin"/args.getOptionValue("as", default="arturo").toExe
match args.getOptionValue("arch", short="a",
default=hostCPU,
into=availableCPUs):
let
amd64 = availableCPUs[0..2]
arm64 = [availableCPUs[3]]
x86 = availableCPUs[4..6]
arm32 = availableCPUs[7..8]
>> amd64: amd64Config()
>> arm64: arm64Config()
>> x86: arm64Config()
>> arm32: arm32Config()
match args.getOptionValue("mode", short="m", default="full", into=availableBuilds):
>> ["full"]:
fullBuildConfig()
>> ["mini"]:
miniBuildConfig()
config.version = "@mini"
miniBuild()
>> ["safe"]:
safeBuildConfig()
miniBuild()
>> ["web"]:
config.binary = config.binary.replace(".exe", "") & ".js"
config.version = "@web"
webBuildConfig()
miniBuild()
match args.getOptionValue("os", default=hostOS, into=availableOSes):
let
bsd = availableOSes[0..2]
linux = [availableOSes[3]]
macos = availableOSes[4..6]
windows = availableOSes[7..8]
>> bsd: discard
>> linux: discard
>> macos: discard
>> windows: discard
match args.getOptionValue("profiler", default="none", short="p",
into=availableProfilers):
>> ["default"]: profilerConfig()
>> ["mem"]: memProfileConfig()
>> ["native"]: nativeProfileConfig()
>> ["profile"]: profileConfig()
match args.getOptionValue("who", default="", into= @["user", "dev"]):
>> ["user"]:
config.isDeveloper = false
userConfig()
>> ["dev"]:
config.isDeveloper = true
devConfig()
if args.hasFlag("bundle", "b"):
config.generateBundle = true
config.bundle = args.getPositionalArg(2)
if args.hasFlag("debug", "d"):
config.shouldCompress = false
debugConfig()
if args.hasFlag("install", "i"):
config.shouldInstall = true
if args.hasFlag("log", "l"):
config.shouldLog = true
if args.hasFlag("raw"):
config.shouldCompress = false
if args.hasFlag("release"):
releaseConfig()
config.buildArturo(targetDir/config.binary)
cmd package, "Package arturo app and build executable":
## package <pkg-name>:
## Compiles packages into executables.
##
## --arch: $hostCPU chooses the target CPU
## [amd64, arm, arm64, i386, x86]
## --debug -d enables debugging
## --help
const availableCPUs = @["amd-64", "x64", "x86-64", "arm-64", "i386", "x86",
"x86-32", "arm", "arm-32"]
var config = buildConfig()
config.binary = args.getPositionalArg(2)
match args.getOptionValue("arch", short="a",
default=hostCPU,
into=availableCPUs):
let
amd64 = availableCPUs[0..2]
arm64 = [availableCPUs[3]]
x86 = availableCPUs[4..6]
arm32 = availableCPUs[7..8]
>> amd64: amd64Config()
>> arm64: arm64Config()
>> x86: arm64Config()
>> arm32: arm32Config()
if args.hasFlag("debug", "d"):
config.shouldCompress = false
debugConfig()
config.buildPackage()
cmd docs, "Build the documentation":
## docs:
## Builds the developer documentation
##
## --help
--define:DOCGEN
buildDocs()
cmd test, "Run test suite":
## test:
## Runs test suite
##
## --using -u: arturo runs with the given binary
## --help
let
binary = args.getOptionValue("using", default="arturo", short="u").toExe
paths: tuple = (
local: "bin"/binary,
global: paths.targetBin/binary
)
unless paths.global.performTests():
quit paths.local.performTests().toErrorCode
cmd benchmark, "Run benchmark suite":
## benchmark:
## Runs benchmark suite
##
## --using -u: arturo runs with the given binary
## --help
let
binary = args.getOptionValue("using", default="arturo", short="u").toExe
paths: tuple = (
local: "bin"/binary,
global: paths.targetBin/binary
)
unless paths.global.performBenchmarks():
quit paths.local.performBenchmarks().toErrorCode
helpForMissingCommand()