-
Notifications
You must be signed in to change notification settings - Fork 8
/
build
executable file
·253 lines (214 loc) · 5.96 KB
/
build
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
#!/bin/bash
ARGUMENTS="$@"
CWD=$(pwd)
# https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
DIRNAME="$(dirname "$(readlink -f "$0")")"
# with help from https://stackoverflow.com/a/29754866,
# saner programming env: these switches turn some bugs into errors
set -o errexit -o pipefail -o noclobber -o nounset
DOCKER="docker"
set +e
if ! $DOCKER ps >/dev/null; then
echo "error connecting to docker:"
$DOCKER ps
exit 1
fi
set -e
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo "GNU's enhanced getopt is required to run this script"
echo "You can usually find this in the util-linux package"
echo "On MacOS/OS X see homebrew's package: http://brewformulas.org/Gnu-getopt"
echo "For anyone else, build from source: http://frodo.looijaard.name/project/getopt"
exit 1
fi
OPTIONS=hi:o:
LONGOPTS=help,input:,output:,arch:,arm-version:,platform:
# -use ! and PIPESTATUS to get exit code with errexit set
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
HELP=n
ARCH=${ARCH:-} ARM_VERSION=${ARM_VERSION:-} PLATFORM=${PLATFORM:-}
INPUT="${CWD}" OUTPUT=prebuilds
while true; do
case "$1" in
-h|--help)
HELP=y
shift
;;
-i|--input)
INPUT="$2"
shift 2
;;
-o|--output)
OUTPUT="$2"
shift 2
;;
--arch)
ARCH="$2"
shift 2
;;
--arm-version)
ARM_VERSION="$2"
shift 2
;;
--platform)
PLATFORM="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
if
[ "${HELP}" == "y" ] || \
[ "${ARCH}" == "" ] || \
!( [ "${ARCH}" != "x32" ] && [ "${ARCH}" != "x64" ] && [ "${ARCH}" != "arm" ] && [ "${ARCH}" != "arm64" ] ) || \
[ "${PLATFORM}" == "" ] || \
!( [ "${PLATFORM}" != "android" ] && [ "${PLATFORM}" != "linux" ] ) || \
[ "${ARCH}" == "arm" ] && [ "${ARM_VERSION}" == "" ]
then
cat >&2 <<EOF
Usage:
prebuildify-cross [options]
Arguments:
--arch <arch>: **required** architecture (supported: x32, x64, arm, arm64)
--arm-version <version>: if using arm architecture, **required* arm version (supported: 5, 6, 7, 8)
--platform: **required** platform (supported: linux, android)
--input <path>: _optional_ directory of input image spec (default: cwd)
--output <path>: _optional_ directory to output build results (default: prebuilds)
Flags:
-h, --help: show this usage
Examples:
prebuildify-cross --platform linux --arch x64
prebuildify-cross --platform linux --arch arm --arm-version 7
prebuildify-cross --platform linux --arch arm64
prebuildify-cross --platform android --arch arm --arm-version 7
prebuildify-cross --platform android --arch arm64
EOF
exit 1
fi
if [[ "${INPUT}" != /* ]]
then
INPUT="${CWD}/${INPUT}"
fi
if [[ "${OUTPUT}" != /* ]]
then
OUTPUT="${CWD}/${OUTPUT}"
fi
echo input $INPUT
echo output $OUTPUT
if [ "${ARM_VERSION}" == "8" ]
then
ARCH=arm64
fi
if [ "${ARCH}" == "arm64" ]
then
ARM_VERSION=8
fi
function not_supported () {
if [ "${ARM_VERSION}" == "" ]
then
echo "platform=${PLATFORM}, arch=${ARCH}: not supported!"
else
echo "platform=${PLATFORM}, arch=${ARCH}, arm_version=${ARM_VERSION}: not supported!"
fi
exit 1
}
case "${PLATFORM}" in
"android")
case "${ARM_VERSION}" in
7)
TARGET=android-arm
;;
8)
TARGET=android-arm64
;;
*)
not_supported
;;
esac
;;
"linux")
case "${ARM_VERSION}" in
5|6|7)
TARGET="linux-armv${ARM_VERSION}"
;;
8)
TARGET=linux-arm64
;;
"")
TARGET=linux-${ARCH}
;;
*)
not_supported
;;
esac
;;
*)
not_supported
;;
esac
IMAGE_NAME=${IMAGE_NAME:-prebuildify-cross}
CONTAINER_NAME=${CONTAINER_NAME:-prebuildify-cross-work-${TARGET}}
CONTAINER_RUNNING=$($DOCKER ps --filter name="$CONTAINER_NAME" -q)
if [ "$CONTAINER_RUNNING" != "" ]; then
$DOCKER stop ${CONTAINER_NAME} > /dev/null
fi
CONTAINER_EXISTS=$($DOCKER ps -a --filter name="$CONTAINER_NAME" -q)
if [ "$CONTAINER_EXISTS" != "" ]; then
$DOCKER rm -v "${CONTAINER_NAME}"
fi
function cleanup {
echo 'got EXIT signal... please wait'
CONTAINER_RUNNING=$($DOCKER ps --filter name="${CONTAINER_NAME}" -q)
if [ "${CONTAINER_RUNNING}" != "" ]; then
$DOCKER stop ${CONTAINER_NAME} > /dev/null
fi
CONTAINER_EXISTS=$($DOCKER ps -a --filter name="${CONTAINER_NAME}" -q)
if [ "${CONTAINER_EXISTS}" != "" ]; then
$DOCKER rm -v ${CONTAINER_NAME} > /dev/null
fi
}
trap cleanup EXIT
echo ARCH "${ARCH}"
echo ARM_VERSION "${ARM_VERSION}"
echo PLATFORM "${PLATFORM}"
echo TARGET "${TARGET}"
# build target image
$DOCKER build \
--build-arg TARGET="${TARGET}" \
-t ${IMAGE_NAME}:${TARGET} \
${DIRNAME}
$DOCKER run \
--name "${CONTAINER_NAME}" \
-d \
-v "${INPUT}:/app/input" \
--env ARCH="${ARCH}" \
--env ARM_VERSION="${ARM_VERSION}" \
--env PLATFORM="${PLATFORM}" \
${IMAGE_NAME}:${TARGET} \
bash -e -o errexit -o pipefail -o noclobber -o nounset -c "
cd /app; ./build-in-docker ${ARGUMENTS};
"
time $DOCKER logs "${CONTAINER_NAME}" --follow || true &
wait "$!"
echo "copying results from output/"
$DOCKER cp "${CONTAINER_NAME}":/app/output/. "${OUTPUT}"
ls -lah "${OUTPUT}"
echo "Done! Your prebuilds should be in ${OUTPUT}"