-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·332 lines (303 loc) · 10.4 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
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
#!/bin/sh
# example: ./build.sh compile amd64; ./build.sh package amd64 apollolake 7.0-4000
set -eu
# https://originhelp.synology.com/developer-guide/appendix/index.html
# https://github.com/SynoCommunity/spksrc/wiki/Architecture-per-Synology-model
# https://github.com/SynoCommunity/spksrc/blob/master/mk/spksrc.common.mk
ARM5_ARCHES="88f6281"
ARM7_ARCHES="alpine armada370 armada375 armada38x armadaxp comcerto2k monaco hi3535 ipq806x northstarplus"
ARM8_ARCHES="rtd1296 armada37xx aarch64"
ARM_ARCHES="$ARM5_ARCHES $ARM7_ARCHES $ARM8_ARCHES"
PPC32_ARCHES="powerpc ppc824x ppc853x ppc854x qoriq"
x86_ARCHES="evansport"
x64_ARCHES="apollolake avoton braswell broadwell broadwellnk bromolow cedarview denverton dockerx64 grantley kvmx64 x86 x64 x86_64"
# x64_ARCHES="x86 cedarview bromolow"
gsha256sum() {
if command -v sha256sum > /dev/null; then
command sha256sum "$@"
elif command -v gsha256sum > /dev/null; then
command gsha256sum "$@"
fi
}
gmd5sum() {
if command -v md5sum > /dev/null; then
command md5sum "$@"
elif command -v gmd5sum > /dev/null; then
command gmd5sum "$@"
fi
}
sedi() {
if [ "$(uname)" = 'Darwin' ]; then
command sed -i '' "$@"
else
command sed -i "$@"
fi
}
usage() {
echo "Usage: $0 command"
echo
echo "Commands:"
echo " compress compresses compiled binary with upx"
echo " update update dependencies with yarn or npm"
echo " dependencies installs npm and go dependencies (yarn/npm and dep)"
echo " all compile and package for all architectures and DSM6/7 versions"
echo " compile [arch] compile go project: e.g. compile amd64"
echo " package [arch] [syno_arch] [min_dsm_version] create spk e.g. package amd64 broadwell 6.1-14715"
echo " dev runs '_cp', 'compile' and 'package' commands using the native platform"
echo " clean|clear remove all *spk files"
echo " lint lint code"
echo " test run a simple test"
echo " amd64 alias to compile and package for amd64 only, good for quick development"
echo ""
}
_cp() {
mkdir -p package/ui/codemirror/theme/
cp -r node_modules/codemirror/addon package/ui/codemirror/
cp -r node_modules/codemirror/keymap package/ui/codemirror/
cp -r node_modules/codemirror/lib package/ui/codemirror/
cp -r node_modules/codemirror/mode package/ui/codemirror/
# cp -r node_modules/codemirror/theme package/ui/codemirror/
# cp -r node_modules/codemirror/theme/monokai.css package/ui/codemirror/theme/
}
clean() {
rm -v ./*.spk
}
## Update node_modules
update() {
if command -v yarn > /dev/null; then
yarn upgrade --latest
elif command -v npm > /dev/null; then
npm update
fi
_cp
if command -v go > /dev/null; then
go list -u -m all
go get -u
go mod vendor
# go get -u=patch
fi
}
## Step 1 Install dependencies
dependencies() {
if [ ! -d node_modules ]; then
if command -v yarn > /dev/null; then
yarn
elif command -v npm > /dev/null; then
npm install
else
echo "JavaScript libraries are NOT updated! Requires Yarn or NPM to be installed on the system." >&2
fi
_cp
fi
if [ ! -d vendor ]; then
if command -v go > /dev/null; then
# go get github.com/BurntSushi/toml
go mod download
go mod vendor
else
echo "go not found! go is needed for this project."
fi
fi
}
checksum_database(){
sha256sum="$(shell command -v sha256sum 2>/dev/null || command -v gsha256sum 2>/dev/null)"
checksum=$($sha256sum package/ui/database.toml | awk '{print $1}')
if ! grep -q "$checksum" package/src/synoedit/main.go; then
echo "Checksum mismatch!"
$sha256sum package/ui/database.toml
grep 'DefaultDatabaseSHA256Checksum =' package/src/synoedit/main.go
exit 1
fi
}
checksum_database_fix() {
sha256sum="$(shell command -v sha256sum 2>/dev/null || command -v gsha256sum 2>/dev/null)"
checksum=$($sha256sum package/ui/database.toml | awk '{print $1}')
if ! grep -q "$checksum" package/src/synoedit/main.go; then
echo "Checksum mismatch! Fixing..."
sedi -e "s/DefaultDatabaseSHA256Checksum\s*=.*/DefaultDatabaseSHA256Checksum = \"${checksum}\"/" package/src/synoedit/main.go
fi
}
compileAll() {
checksum_database
lint
dependencies
## match arches to go build arches:
arches="arm arm64 386 amd64 ppc64"
os_min_ver="${1:-"7.0-4000"}"
for arch in ${arches}; do
supported_arches=""
case "$arch" in
"arm")
compile "$arch" "5"
package "$arch"_v5 "$ARM5_ARCHES" "$os_min_ver"
compile "$arch" "7"
ARM7_ARCHES=$(echo "$ARM7_ARCHES" | sed 's/ ipq806x//' | sed 's/ northstarplus//')
package "$arch"_v7 "$ARM7_ARCHES" "$os_min_ver"
# SRM
package "$arch"_v7 "ipq806x northstarplus" "1.1.6-6931"
;;
"arm64") # ARMv8
compile "$arch"
supported_arches="$ARM8_ARCHES"
;;
"386")
compile "$arch"
supported_arches="$x86_ARCHES"
;;
"amd64")
compile "$arch"
supported_arches="$x64_ARCHES"
;;
"ppc64")
supported_arches=""
;;
*)
echo "Unsupported Architecture!"
exit 1
;;
esac
package "$arch" "$supported_arches" "$os_min_ver"
done
}
## Step 2 compile
compile() {
_ARCH="${1:-""}"
_GOARM="${2:-""}"
checksum_database
if command -v go > /dev/null; then
cd package/src/synoedit || exit
echo "go compiling ..."
if [ -z "$_ARCH" ]; then
go build -ldflags="-s -w" -o ../../ui/index.cgi
# go build -ldflags "-s -w" -o package/ui/index.cgi -- package/src/synoedit/*.go
else
# env GOOS=linux GOARCH="$ARCH" go build -ldflags "-s -w" -o package/ui/index.cgi -- package/src/*.go
echo "GOARCH=$_ARCH GOARM=$_GOARM"
env CGO_ENABLED=0 GOOS=linux GOARCH="$_ARCH" GOARM="$_GOARM" go build -ldflags "-s -w" -o ../../ui/index.cgi
chmod +x ../../ui/index.cgi
fi
cd ../../.. || edit
else
echo "go is missing. Install golang before trying again. This software doesn't compile itself!" >&2
echo "https://golang.org/"
exit 1
fi
}
compress() { # not recommended, slows down launch time ~0.8s
if command -v upx > /dev/null; then
upx package/ui/index.cgi
# upx --brute package/ui/index.cgi # slow
else
echo "upx not found. This option requires upx. 'brew install upx'" >&2
exit 1
fi
}
## Step 3 Compress package and create spk
package() {
_arch=${1:-native}
_supported_arches=${2:-noarch}
_os_min_ver=${3:-7.0-4000}
# sha1sum="$(shell command -v sha1sum 2>/dev/null || command -v gsha1sum 2>/dev/null)"
# sha256sum="$(shell command -v sha256sum 2>/dev/null || command -v gsha256sum 2>/dev/null)"
md5sum="$(shell command -v md5sum 2>/dev/null || command -v gmd5sum 2>/dev/null)"
## Create package.tgz
echo "tar cpf package.tgz"
tar cfz package.tgz --exclude='src' \
--exclude="pkg" \
--exclude='ui/test' \
--exclude='ui/test.sh' \
--exclude='.DS_Store' \
-C package .
# arch="arm arm64 386 amd64 ppc64" -> arm, x86, x86_64
## Create checksum
checksum=$($md5sum package.tgz | awk '{print $1}')
sedi -e "s/checksum=.*/checksum=\"${checksum}\"/" INFO
sedi -e "s/arch=.*/arch=\"${_supported_arches}\"/" INFO
sedi -e "s/os_min_ver=.*/os_min_ver=\"${_os_min_ver}\"/" INFO
# pkg_get_spk_platform
# pkg_get_spk_family
## Create spk
echo "tar cpf $_arch-$_os_min_ver.spk"
tar cpf synoedit-"$_arch"-"$_os_min_ver".spk \
--exclude='node_modules' \
--exclude='*.afdesign' \
--exclude='*.afphoto' \
--exclude='.DS_Store' \
--exclude='yarn.lock' \
--exclude='package.json' \
--exclude='ui/js/main.js' \
--exclude='package' \
--exclude='*.sh' \
--exclude='*.spk' \
--exclude='synoedit-*' \
--exclude='*.log' \
--exclude='.git' \
--exclude='.github' \
--exclude='go.mod' \
--exclude='go.sum' \
--exclude='vendor' \
-- *
}
lint () {
gofmt -s -w -- package/src/synoedit/*.go
if ! command -v golint > /dev/null || ! command -v revive > /dev/null; then
go get -u golang.org/x/lint/golint
fi
go mod tidy
if command -v golint > /dev/null; then
golint package/src/synoedit/*.go
elif command -v revive > /dev/null; then
revive package/src/synoedit/*.go
fi
if command -v yarn > /dev/null; then
yarn audit
elif command -v npm > /dev/null; then
npm audit
fi
}
test () {
lint
compile
env SERVER_PROTOCOL=HTTP/1.1 REQUEST_METHOD=GET package/ui/index.cgi --dev > package/ui/test.html
# godoc ./package/src/synoedit/
}
CMD="${1:-""}"
BUILD_ARCH="${2:-""}"
if [ "$CMD" = "compress" ]; then
compress
elif [ "$CMD" = "update" ]; then
update
elif [ "$CMD" = "dependencies" ] || [ "$CMD" = "javascript" ] || [ "$CMD" = "yarn" ] || [ "$CMD" = "npm" ]; then
dependencies
elif [ "$CMD" = "all" ]; then
_cp
compileAll 7.0-4000
compileAll 6.1-14715
compile
elif [ "$CMD" = "compile" ]; then
_cp
compile "$BUILD_ARCH"
elif [ "$CMD" = "package" ]; then
shift
package "$@"
elif [ "$CMD" = "dev" ]; then
_cp
checksum_database_fix
compile
package
elif [ "$CMD" = "clean" ] || [ "$CMD" = "clear" ]; then
clean
elif [ "$CMD" = "lint" ]; then
lint
elif [ "$CMD" = "test" ]; then
test
elif [ "$CMD" = "amd64" ]; then
dependencies
_cp
checksum_database_fix
compile amd64
package amd64 "${x64_ARCHES}" 6.1-14715
package amd64 "${x64_ARCHES}" 7.0-4000
else
usage
fi