-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.nims
50 lines (42 loc) · 1.46 KB
/
config.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
from macros import error
from os import splitFile, `/`
# -d:musl
when defined(musl):
var muslGccPath: string
echo " [-d:musl] Building a static binary using musl .."
muslGccPath = findExe("musl-gcc")
if muslGccPath == "":
error("'musl-gcc' binary was not found in PATH.")
switch("gcc.exe", muslGccPath)
switch("gcc.linkerexe", muslGccPath)
switch("passL", "-static")
proc binOptimize(binFile: string) =
## Optimize size of the binFile binary.
echo ""
if findExe("strip") != "":
echo "Running 'strip -s' .."
exec "strip -s " & binFile
if findExe("upx") != "":
# https://github.com/upx/upx/releases/
echo "Running 'upx --best' .."
exec "upx --best " & binFile
# nim musl foo.nim
task musl, "Builds an optimized static binary using musl":
## Usage: nim musl <.nim file path>
let
numParams = paramCount()
if numParams != 2:
error("The 'musl' sub-command needs exactly 1 argument, the Nim file (but " &
$(numParams-1) & " were detected)." &
"\n Usage Example: nim musl FILE.nim.")
let
nimFile = paramStr(numParams) ## The nim file name *must* be the last
(dirName, baseName, _) = splitFile(nimFile)
binFile = dirName / baseName # Save the binary in the same dir as the nim file
nimArgs = "c -d:musl -d:release --opt:size --gc:orc" & nimFile
# Build binary
echo "\nRunning 'nim " & nimArgs & "' .."
selfExec nimArgs
# Optimize binary
binOptimize(binFile)
echo "\nCreated binary: " & binFile