-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·329 lines (276 loc) · 7.66 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
#!/bin/bash
#
# Build Script
#
# ./build.sh <options>
# stable - Get latest tag
# latest - Get latest source
# package - Build Package
# install - Install Locally
#
function usage()
{
echo "usage: build.sh [bootstrap] [stable | latest] [package] [install]"
echo
echo " bootstrap - Fetch latest Swift & Packages (like bootstrap.sh)"
echo " stable | latest - Stable fetches latest release, latest contains newer changes"
echo " install - Install to /opt/rpilight"
echo
echo "WARNING: package option is currently unsupported"
}
#
# Ensures Swift is in the PATH
#
# Used as a catch-all if this is run in the same shell as
#
function ensure_swift_in_path() {
which swift
swift_profile="/etc/profile.d/swiftlang.sh"
if [ $? -ne 0 ] && [ -e $swift_profile ]; then
source $swift_profile
fi
which swift
if [ $? -ne 0 ]; then
echo "Error: Swift doesn't appear to be installed, or isn't in the PATH."
echo
exit 1
fi
}
#
# Run Bootstrap Again for Swift/Packages
#
function run_bootstrap() {
echo "Running Bootstrap"
bash ./bootstrap.sh nobuild
}
#
# Grab Latest Tag
#
function update_latest_source() {
echo "Fetching Latest Source"
git checkout master
git pull --rebase
}
#
# Grab Latest Git State
#
function update_stable_source() {
echo "Fetching Tags"
git fetch
LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1`)
echo "Updating to $LATEST_TAG..."
git checkout $LATEST_TAG
}
#
# Build It
#
function build_rpilight() {
echo "Building RPiLight (Release)..."
# Need this to avoid causing real bad throttling on Pi.
swift build -c release -j 2
}
#
# Copy Binaries to Output
#
function copy_libraries() {
NEED_SUDO="$1"
RPILIGHT_BINARY="$2"
SWIFT_LIBS="$3"
LIBRARY_DIR="$4"
# Start from Clean Folder
if [ -e "$LIBRARY_DIR" ]; then
$NEED_SUDO rm -rf "$LIBRARY_DIR"
fi
$NEED_SUDO mkdir -p "$LIBRARY_PATH"
ldd "$RPILIGHT_BINARY" | grep "not found" | while read -r line ; do
library="$(cut -d' ' -f1 <<<"$line")"
echo "Copying $library"
$NEED_SUDO cp "$SWIFT_LIBS/$library" "$LIBRARY_DIR"
done
}
function copy_binaries() {
ROOT_DIR="$1"
NEED_SUDO="$2"
SWIFT_BINARY=`which swift`
SWIFT_LIB_DIR="$(dirname $(dirname $SWIFT_BINARY))/lib/swift/linux"
BINARY_PATH="$ROOT_DIR/opt/rpilight"
LIBRARY_PATH="$ROOT_DIR/opt/rpilight/lib"
CONFIG_PATH="$BINARY_PATH/config"
SERVICE_PATH="$ROOT_DIR/lib/systemd/system"
RPILIGHT_BINARY=$(realpath .build/release/RPiLight)
RPILIGHT_EXAMPLES=$(realpath examples)
RPILIGHT_SERVICE=$(realpath rpilight.service)
if [ ! -e "$BINARY_PATH" ]; then
$NEED_SUDO mkdir -p "$BINARY_PATH"
fi
if [ ! -e "$CONFIG_PATH" ]; then
$NEED_SUDO mkdir -p "$CONFIG_PATH"
fi
if [ ! -e "$SERVICE_PATH" ]; then
$NEED_SUDO mkdir -p "$SERVICE_PATH"
fi
echo "Copying Binaries to $BINARY_PATH"
$NEED_SUDO cp "$RPILIGHT_BINARY" "$BINARY_PATH/RPiLight"
echo "Making Self-Sufficient..."
$NEED_SUDO chrpath -r "\$ORIGIN/lib" "$BINARY_PATH/RPiLight"
copy_libraries "$NEED_SUDO" "$BINARY_PATH/RPiLight" "$SWIFT_LIB_DIR" "$LIBRARY_PATH"
echo "Copying Examples"
$NEED_SUDO rsync --delete -r "$RPILIGHT_EXAMPLES/" "$BINARY_PATH/examples/"
echo "Copying Service Configuration to $SERVICE_PATH"
sudo cp "$RPILIGHT_SERVICE" "$SERVICE_PATH"
}
#
# Calculate the Package Version
#
# Takes variable to set with version
function get_package_version() {
local VERSION=$(git describe --tags)
local VERSION_ARRAY=(${VERSION//-/ })
if [ "${VERSION_ARRAY[1]}" == "0" ]; then
eval "$1=${VERSION_ARRAY[0]}"
fi
eval "$1=$VERSION"
}
function render_template() {
SOURCE_FILE="$1"
SUBST_FILE="$2"
DEST_FILE="$3"
cp "$SOURCE_FILE" "$DEST_FILE"
cat "$SUBST_FILE" | while read line; do
if [[ -n $line ]]; then
variable="${line%%=*}"
value="${line#*=}"
sed -i "s/\${$variable}/$value/g" "$DEST_FILE"
fi
done
}
#
# Build Binary Package
#
function build_package() {
PACKAGE_PATH=$(realpath .package)
PACKAGE_DEBIAN="$PACKAGE_PATH/DEBIAN"
PACKAGE_CONTROL="$PACKAGE_PATH/debian"
PACKAGE_INSTALL="$PACKAGE_PATH/opt/rpilight"
PACKAGE_SYSTEMD="$PACKAGE_PATH/lib/systemd/system"
PACKAGE_ASSETS="$(realpath Assets/Package)"
echo "Packaging..."
rm -rf "$PACKAGE_PATH"
mkdir "$PACKAGE_PATH"
copy_binaries "$PACKAGE_PATH" ""
version=""
get_package_version version
SYSTEM_ARCH=$(uname -m)
case $SYSTEM_ARCH in
aarch64 ) arch=arm64
filename_arch=$arch
;;
armv6l | armv7l ) arch=armhf
filename_arch=$arch
;;
* ) echo "Unknown Platform Architecture"
exit 1
;;
esac
mkdir -p "$PACKAGE_CONTROL"
cp "$PACKAGE_ASSETS/control.source" "$PACKAGE_CONTROL/control"
cat <<EOT > "$PACKAGE_CONTROL/substvars"
dist:Architecture=${arch}
dist:Version=${version}
EOT
# Package Content
mkdir -p "$PACKAGE_DEBIAN"
cp "$PACKAGE_ASSETS/preinst" "$PACKAGE_DEBIAN/"
cp "$PACKAGE_ASSETS/postinst" "$PACKAGE_DEBIAN/"
pushd "$PACKAGE_PATH" >> /dev/null
PACKAGE_NAME="rpilight_${version}_${filename_arch}.deb"
libraries=`find . -iname \*.so`
dpkg-shlibdeps -S"$PACKAGE_PATH" opt/rpilight/RPiLight $libraries
render_template "$PACKAGE_ASSETS/control" "$PACKAGE_CONTROL/substvars" "$PACKAGE_DEBIAN/control"
fakeroot dpkg-deb --build "$PACKAGE_PATH" "$PACKAGE_NAME"
if [ -f "$PACKAGE_NAME" ]; then
mv "$PACKAGE_NAME" ../
fi
popd >> /dev/null
}
#
# Install Binaries
#
function install_rpilight() {
echo "Installing..."
copy_binaries "" "sudo"
}
#
# Service Utilities
#
function shutdown_service() {
PACKAGE_ASSETS="$(realpath Assets/DEBIAN)"
sudo "$PACKAGE_ASSETS/preinst"
}
function configure_service() {
PACKAGE_ASSETS="$(realpath Assets/DEBIAN)"
sudo "$PACKAGE_ASSETS/postinst"
}
#
#
#
#
# Install Script
#
FETCH=0
PACKAGE=0
INSTALL=0
BOOTSTRAP=0
while [ "$1" != "" ]; do
case $1 in
bootstrap) BOOTSTRAP=1
;;
stable | latest ) FETCH=$1
;;
package ) PACKAGE=1
;;
install ) INSTALL=1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
SCRIPT_PATH=$(realpath $0)
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
# This is called just to make sure we can install swift
ensure_swift_in_path || exit $?
echo "RPiLight Directory: $SCRIPT_DIR"
if [ "$FETCH" != "0" ]; then
echo "Fetch Source: $FETCH"
fi
echo "Build Type: release"
if [ "$PACKAGE" == "1" ]; then
echo "Build Package: yes"
fi
if [ "$INSTALL" == "1" ]; then
echo "Install to /opt: yes"
fi
pushd "$SCRIPT_DIR" > /dev/null
if [ "$FETCH" == "stable" ]; then
update_stable_source
elif [ "$FETCH" == "latest" ]; then
update_latest_source
fi
if [ "$BOOTSTRAP" == "1" ]; then
run_bootstrap
fi
build_rpilight
if [ "$PACKAGE" == "1" ]; then
build_package
fi
if [ "$INSTALL" == "1" ]; then
shutdown_service
install_rpilight
configure_service
echo "To start service, restart or run 'sudo systemctl start rpilight'."
fi
popd > /dev/null