-
Notifications
You must be signed in to change notification settings - Fork 14
/
build-release.sh
executable file
·250 lines (206 loc) · 5.19 KB
/
build-release.sh
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
#!/usr/bin/env bash
# Build release tarball/zip from a source package
#
# Copyright (c) 2020 Leorize <[email protected]>
#
# This script is licensed under the MIT license.
usage() {
cat << EOF
Usage: $0 [-o folder] [-v version] <source>
Build a binary Nim release from the specified source folder. This folder is
assumed to be created from a standard Nim source archive.
Options:
-o folder Where to output the resulting artifacts. Defaults to $PWD/output.
-d folder Where dependencies are downloaded into. Defaults to $PWD/external.
-r revision Add "-revision" to the binary archive name.
-h This help message.
Environment Variables:
CC The compiler used to build csources.
CFLAGS Flags to pass to C compilers when building C code.
LDFLAGS Flags to pass to C compilers when linking C code.
EOF
}
set -e
set -o pipefail
basedir=$(cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P)
# shellcheck source=lib.sh
source "$basedir/lib.sh"
output=$PWD/output
outrel=$PWD
deps=$PWD/external
revision=
while getopts "o:d:r:h" curopt; do
case "$curopt" in
'o')
output=$(realpath "$OPTARG")
;;
'd')
deps=$(realpath "$OPTARG")
;;
'r')
revision=$OPTARG
;;
'h')
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [[ $# -lt 1 ]]; then
echo "$0: missing required argument -- <source>"
usage
exit 1
fi
if [[ -e $deps/environment ]]; then
echo "Sourcing dependencies environment"
# shellcheck source=/dev/null
source "$deps/environment"
fi
mkdir -p "$output"
cd "$1"
if [[ $(os) == darwin ]]; then
: "${CC:=clang}"
else
: "${CC:=gcc}"
fi
export PATH=$PWD/bin${PATH:+:$PATH}
TIMEFORMAT="Took %lR"
cpu=$(arch_from_triple "$($CC -dumpmachine)")
cat <<EOF
====== Environment Information ======
CPU: $cpu
C compiler:
$($CC -v 2>&1)
EOF
time {
fold "Build 1-stage csources compiler"
make "-j$(ncpu)" ucpu="$cpu" CC="$CC"
endfold
}
buildtmp=$PWD/build
mkdir -p -- "$buildtmp/nim"
export XDG_CONFIG_HOME=$buildtmp
export XDG_CACHE_HOME=$buildtmp/nimcache
rm -f "$buildtmp/nim/nim.cfg"
if [[ -n "$CFLAGS" ]]; then
echo "passC%=\"\$CFLAGS\"" >> "$buildtmp/nim/nim.cfg"
fi
if [[ -n "$LDFLAGS" ]]; then
echo "passL%=\"\$LDFLAGS\"" >> "$buildtmp/nim/nim.cfg"
fi
if [[ -e $deps/nim.cfg ]]; then
echo "Importing configuration from $deps/nim.cfg"
cat "$deps/nim.cfg" >> "$buildtmp/nim/nim.cfg"
fi
time {
fold "Build koch"
nim c koch
endfold
}
time {
fold "Build compiler"
./koch boot -d:release --skipUserCfg:off
endfold
}
versionRegex='^Nim Compiler Version ([0-9]+\.[0-9]+\.[0-9]+) \[([a-zA-Z0-9]+): [a-zA-Z0-9]+\]'
if [[ $(nim --version 2>&1) =~ $versionRegex ]]; then
version=${BASH_REMATCH[1]}
os=$(tolowercase "${BASH_REMATCH[2]}")
fi
# Fail if the variables are not declared (ie. nim couldn't run)
: "${version:?}" "${os:?}"
cpusuffix=
case "$cpu" in
i?86)
cpusuffix=_x32
;;
x86_64)
cpusuffix=_x64
;;
aarch64)
cpusuffix=_arm64
;;
*)
cpusuffix=_$cpu
;;
esac
suffix=-${os}$cpusuffix
case "$os" in
windows)
time {
fold "Generate release"
mkdir -p web/upload/download
# Package DLLs
cp -t bin "$deps/windeps/"*
nim c --outdir:. tools/winrelease
./winrelease
artifact=$output/nim-$version${revision:+-$revision}$suffix.zip
cp "web/upload/download/nim-$version$cpusuffix.zip" "$artifact"
echo "Generated release artifact at $artifact"
echo "$artifact" > "$output/nim.txt"
endfold
}
;;
*)
time {
fold "Build tools"
./koch tools -d:release
endfold
}
major=${version%%.*}
minor=${version#*.}
minor=${minor%.*}
patch=${version##*.}
doc=(docs -d:release)
if [[ $major -ge 1 && $minor -ge 3 && $patch -ge 5 ]]; then
# Skip runnable examples and web docs build when supported. This speeds
# up the build by a huge margin, esp. on non-native archs.
doc=(--localdocs "${doc[@]}" --doccmd:skip)
fi
time {
fold "Build docs"
# Build release docs
./koch "${doc[@]}"
endfold
}
time {
fold "Generate release"
# Cleanup build artifacts
# TODO: Rework niminst to be able to build binary archives for non-Windows
rm -rf "$buildtmp"
find . \
-mindepth 1 \
\( \
-name .git -prune -o \
-name c_code -prune -o \
-name nimcache -prune -o \
-name web -prune -o \
-name build.sh -o \
-name 'build*.bat' -o \
-name makefile -o \
-name '*.o' -o \
-path '*/compiler/nim' -o \
-path '*/compiler/nim?' \
\) \
-exec rm -rf '{}' +
cd ..
srcDir=$(basename "$1")
if [[ $srcDir != "nim-$version" ]]; then
# This is for people who build this locally...
ln -sf "$srcDir" "nim-$version"
fi
artifact=$output/nim-$version${revision:+-$revision}$suffix.tar
tar chf "$artifact" "nim-$version"
xz -9e "$artifact"
artifact=$artifact.xz
echo "Generated release artifact at $artifact"
echo "$artifact" > "$output/nim.txt"
endfold
}
;;
esac