-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·301 lines (241 loc) · 6.23 KB
/
build.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
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
#!/usr/bin/env bash
# =================
# FUNCTIONS
# =================
Usage () {
echo "Usage: ./build.sh [options]"
echo
echo "If no options are given, it will build for your machine's OS and arch"
echo
echo "Options:"
echo " --os <value> Comma delimited list of: macos, linux. No spaces. Required if --arch is given"
echo " --arch <value> Comma delimited list of: x64, arm64. No spaces. Required if --os is given"
echo " --all Build all possible OS-arch combinations"
echo " --bytecode Binaries will only have bytecode (no javascript). This will produce non-deterministic builds"
echo " --checksum Output the checksums after the builds"
echo " -h, --help Output this help message"
echo
echo "Example:"
echo " ./build.sh --os macos,linux --arch x64"
echo
exit 0
}
transformOS () {
#make input lower case
PARAM=$(echo "$1" | tr '[:upper:]' '[:lower:]')
if [ "$PARAM" == "macos" ]; then
OS_T="macos"
elif [ "$PARAM" == "linux" ]; then
OS_T="linux"
else
OS_T=
fi
}
transformArch () {
#make input lower case
PARAM=$(echo "$1" | tr '[:upper:]' '[:lower:]')
if
[ "$PARAM" == "x64" ] ||
[ "$PARAM" == "amd64" ] ||
[ "$PARAM" == "x86_64" ] ||
[ "$PARAM" == "x86" ];
then
ARCH_T="x64"
elif
[ "$PARAM" == "arm64" ] ||
[ "$PARAM" == "arm" ] ||
[ "$PARAM" == "armv8" ];
then
ARCH_T="arm64"
else
ARCH_T=
fi
}
# Darwin mbp-0223 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64 x86_64
# Darwin mbp-0418 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:35 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T8101 arm64
# Linux bitpay-kenny-xps 5.15.0-47-generic #51-Ubuntu SMP Thu Aug 11 07:51:15 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
getLocalInfo() {
echo Determining your system specs
SYSTEM_INFO=$(uname -a)
echo $SYSTEM_INFO
SI_ARR=(${SYSTEM_INFO// / })
if [ "${SI_ARR[0]}" == "Darwin" ]; then
MY_OS=macos
MY_ARCH=${SI_ARR[-1]}
elif [ "${SI_ARR[0]}" == "Linux" ]; then
MY_OS=linux
MY_ARCH=${SI_ARR[-2]}
fi
transformArch $MY_ARCH
MY_ARCH=$ARCH_T # transformed
}
buildForSelf () {
echo Building for your system
if [ $MY_OS != "macos" ] && [ $MY_OS != "linux" ]; then
echo Unknown OS: ${SI_ARR[0]}
exit 0
fi
if [ $MY_ARCH != "x64" ] && [ $MY_ARCH != "arm65" ]; then
echo Unknown architecture: $ARCH
exit 0
fi
OS_ARR=($MY_OS)
ARCH_ARR=($MY_ARCH)
}
# ===============
# /FUNCTIONS
# ===============
VERSION=v$(cat package.json | grep '"version":' | sed -r 's/[[:space:]]*["a-z:]*//gi' | sed -r 's/,//gi' )
echo Building $VERSION. Is that correct? \(y/n\)
read ANS
if [ "$ANS" != "y" ]; then
echo If this is a new version, be sure to update package.json.
echo Exiting.
exit 0
fi
if [ "$ANS" == "n" ]; then
echo Please select the version you wish to build with \`git tag\`
exit 0
fi
CWD=$(dirname $(readlink -f "$0"))
cd $CWD
ENGINE_VERSION=$(cat package.json | grep '"node":' | sed -r 's/[[:space:]]*["a-z:]*//gi')
EXISTS=$(which node)
if [ -z $EXISTS ]; then
echo Node is not installed. Node $ENGINE_VERSION required.
exit;
fi
NODE_VERSION=$(node --version)
if [ $(echo $NODE_VERSION | sed -r "s/v$ENGINE_VERSION.*/v$ENGINE_VERSION/") != "v$ENGINE_VERSION" ]; then
echo Node v$ENGINE_VERSION required.
exit;
fi
echo Using Node $NODE_VERSION
getLocalInfo
# ===============
# Parse input
BYTECODE=0
CHECKSUM=0
for i in `seq 1 $#`; do
PLUS1=$(( $i + 1 ))
if [ "${!i}" == "--os" ]; then
OS_INPUT=${!PLUS1}
OS_ARR=(${OS_INPUT//,/ })
elif [ "${!i}" == "--arch" ]; then
ARCH_INPUT=${!PLUS1}
ARCH_ARR=(${ARCH_INPUT//,/ })
elif [ "${!i}" == "--all" ]; then
OS_ARR=("linux", "macos")
ARCH_ARR=("x64", "arm64")
elif [ "${!i}" == "--bytecode" ]; then
BYTECODE=1
elif [ "${!i}" == "--checksum" ]; then
CHECKSUM=1
elif [ "${!i}" == "--help" ] || [ "${!i}" == "-h" ]; then
Usage
fi
done
HAS_OS=1
HAS_ARCH=1
if [[ -z ${OS_ARR[@]} ]]; then
HAS_OS=0
fi
if [[ -z ${ARCH_ARR[@]} ]]; then
HAS_ARCH=0
fi
# If neither --os nor --arch was given...
if [ $HAS_OS == 0 ] && [ $HAS_ARCH == 0 ]; then
# ...default to building for self
buildForSelf
elif [ $HAS_OS == 0 ] && [ $HAS_ARCH == 1 ]; then
# --os was not given but --arch was
Usage
elif [ $HAS_OS == 1 ] && [ $HAS_ARCH == 0 ]; then
# --os was given but --arch was not
Usage
fi
for os in ${OS_ARR[@]}; do
transformOS $os
if [ -z $OS_T ]; then
echo Unknown OS: $os
exit 1
fi
done
for arch in ${ARCH_ARR[@]}; do
transformArch $arch
if [ -z $ARCH_T ]; then
echo Unknown architecture: $arch
exit 1
fi
done
for os in ${OS_ARR[@]}; do
transformOS $os
for arch in ${ARCH_ARR[@]}; do
transformArch $arch
TARGET=node$ENGINE_VERSION-$OS_T-$ARCH_T
OUTPUT=lnd-tools-$OS_T-$ARCH_T-$VERSION
echo
echo Building $OUTPUT
# =========================
# Create .temp dir with dependencies
if [ -e .temp ]; then
echo Removing existing .temp dir
rm -rf .temp
fi
echo Making .temp
mkdir .temp
cp package.json .temp
cp package-lock.json .temp
cp *.js .temp
cp -r server .temp
cp -r client .temp
cd .temp
echo Installing dependencies
npm i --omit=dev
cd $CWD
# =========================
# Build the binaries
if [ -e build ]; then
cd build
else
mkdir build
cd build
fi
# reminder: this is being run in the $CWD/build folder
CMD="../node_modules/.bin/pkg ../.temp/lnd-tools.js \
--config ../package.json \
--target $TARGET \
--output $OUTPUT \
--compress GZip"
if [ $BYTECODE == 0 ]; then
CMD="$CMD \
--no-bytecode \
--public-packages \"*\" \
--public"
fi
$CMD
# cd back to cwd
cd $CWD
echo Cleaning up
rm -rf .temp
done
done
# cd back to cwd
cd $CWD
cp ./tls.sh ./build
if [ $CHECKSUM == 1 ]; then
cd build
echo
echo Calculating checksums
for os in ${OS_ARR[@]}; do
transformOS $os
for arch in ${ARCH_ARR[@]}; do
transformArch $arch
shasum -a 256 lnd-tools-$OS_T-$ARCH_T-$VERSION
done
done
cd $CWD
fi
echo
echo Build complete.
echo Output files can be found in $CWD/build