forked from zeromq/zproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zproject_rpi.gsl
322 lines (261 loc) · 8.96 KB
/
zproject_rpi.gsl
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
# Generate cross compile environment for the Raspberry Pi
#
# This is a code generator built using the iMatix GSL code generation
# language. See https://github.com/imatix/gsl for details.
#
# Copyright (c) the Contributors as noted in the AUTHORS file.
# This file is part of zproject.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
register_target ("rpi", "Raspberry Pi cross build system")
.macro target_rpi
.directory.create('builds/rpi')
.output "builds/rpi/build.sh"
#!/usr/bin/env bash
$(project.GENERATED_WARNING_HEADER:)
# Download cross compiler and sysroot
if [ ! -d $PWD/tools ]; then
git clone --depth 1 https://github.com/raspberrypi/tools
else
( cd tools && git pull --rebase --quiet ) || exit 1
fi
# Cross build for the Raspberry Pi
mkdir -p $PWD/tmp
BUILD_PREFIX=$PWD/tmp
TOOLCHAIN_HOST="arm-linux-gnueabihf"
TOOLCHAIN_PATH="${PWD}/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin"
SYSROOT=$PWD/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/arm-linux-gnueabihf/sysroot
CFLAGS="--sysroot=${SYSROOT} -I${BUILD_PREFIX}/include"
CPPFLAGS="--sysroot=${SYSROOT} -I${BUILD_PREFIX}/include"
CXXFLAGS="--sysroot=${SYSROOT} -I${BUILD_PREFIX}/include"
LDFLAGS="-L${BUILD_PREFIX}/lib"
CONFIG_OPTS=()
CONFIG_OPTS+=("CFLAGS=${CFLAGS}")
CONFIG_OPTS+=("CPPFLAGS=${CPPFLAGS}")
CONFIG_OPTS+=("CXXFLAGS=${CXXFLAGS}")
CONFIG_OPTS+=("LDFLAGS=${LDFLAGS}")
CONFIG_OPTS+=("PKG_CONFIG_DIR=")
CONFIG_OPTS+=("PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/arm-linux-gnueabihf/pkgconfig:${SYSROOT}/usr/share/pkgconfig")
CONFIG_OPTS+=("PKG_CONFIG_SYSROOT=${SYSROOT}")
CONFIG_OPTS+=("PKG_CONFIG_PATH=${BUILD_PREFIX}/lib/pkgconfig")
CONFIG_OPTS+=("--prefix=${BUILD_PREFIX}")
CONFIG_OPTS+=("--host=${TOOLCHAIN_HOST}")
CONFIG_OPTS+=("--with-docs=no")
CPP="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-cpp"
CC="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-gcc"
CXX="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-g++"
LD="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ld"
AS="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-as"
AR="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ar"
RANLIB="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ranlib"
CONFIG_OPTS+=("CPP=${CPP}")
CONFIG_OPTS+=("CC=${CC}")
CONFIG_OPTS+=("CXX=${CXX}")
CONFIG_OPTS+=("LD=${LD}")
CONFIG_OPTS+=("AS=${AS}")
CONFIG_OPTS+=("AR=${AR}")
CONFIG_OPTS+=("RANLIB=${RANLIB}")
# Clone and build dependencies
. for use where defined (use.tarball)
if [ ! -e \$(basename "$(use.tarball)") ]; then
wget $(use.tarball)
tar -xzf \$(basename "$(use.tarball)")
fi
pushd \$(basename "$(use.tarball)" .tar.gz)
(
./configure "${CONFIG_OPTS[@]}"
make -j4
make install
) || exit 1
popd
. endfor
. for use where defined (use.repository) & ! defined (use.tarball)
if [ ! -e $(use.project) ]; then
. if defined (use.release)
git clone --quiet --depth 1 -b $(use.release) $(use.repository) $(use.project)
. else
git clone --quiet --depth 1 $(use.repository) $(use.project)
. endif
fi
pushd $(use.project)
(
git --no-pager log --oneline -n1
if [ -e autogen.sh ]; then
./autogen.sh 2> /dev/null
fi
if [ -e buildconf ]; then
./buildconf 2> /dev/null
fi
./configure "${CONFIG_OPTS[@]}"
make -j4
make install
) || exit 1
popd
. endfor
# Cross build this project
pushd ../..
(
./autogen.sh 2> /dev/null
./configure --enable-drafts=yes "${CONFIG_OPTS[@]}"
make -j4
make install
) || exit 1
popd
$(project.GENERATED_WARNING_HEADER:)
.close
.chmod_x ("builds/rpi/build.sh")
.output "builds/rpi/build_image.sh"
#!/usr/bin/env bash
$(project.GENERATED_WARNING_HEADER:)
# Cross compile this project and its dependencies
\./build.sh
# Get the latest Raspbian image and enlarge it so there is enough space to
# install to it.
sudo ./prepare_img.sh
# Sync with image
sudo rsync -avz --no-p --no-g --chmod=ugo=rwX tmp/ raspbian-root/usr
sudo proot -Q qemu-arm -B -r raspbian-root -b raspbian-boot:/boot sudo ldconfig
# Umount image
sync; sudo umount ./raspbian-boot ./raspbian-root
# Make sure all allocated loop devices are free'd
for i in {0..7};
do
sudo sudo dmsetup clear loop${i}p1 &&
sudo sudo dmsetup clear loop${i}p2 &&
sudo sudo dmsetup remove loop${i}p1 &&
sudo sudo dmsetup remove loop${i}p2 &&
sudo sudo losetup -d /dev/loop$i
done
$(project.GENERATED_WARNING_HEADER:)
.close
.chmod_x ("builds/rpi/build_image.sh")
.output("builds/rpi/prepare_img.sh")
#!/usr/bin/env bash
$(project.GENERATED_WARNING_HEADER:)
if [ ! -f $PWD/raspbian_lite_latest.zip ]; then
wget --connect-timeout=10 https://downloads.raspberrypi.org/raspbian_lite_latest -O raspbian_lite_latest.zip
fi
RASPBIAN_IMG=\$(unzip -Z1 raspbian_lite_latest.zip)
if [ ! -f $PWD/$RASPBIAN_IMG ]; then
unzip raspbian_lite_latest.zip
fi
if [ ! -f $PWD/raspbian_$(project.name:c).img ]; then
# Allocate disk space for extended image
truncate -s 1536M raspbian_$(project.name:c).img
# Begin partitioning disk image.
# Creating two partitions:
# BOOT = 100 MB
# ROOT = 1536 MB
fdisk raspbian_$(project.name).img <<EOF
o
n
p
1
+100M
t
c
n
p
2
w
EOF
# Create a loop device and mount the extended image's partitions
export LOOPDEV_EX="\$(losetup --show --find raspbian_$(project.name).img)"
# Use sudo kpartx to create partitions in /dev/mapper
kpartx -av $LOOPDEV_EX
dmsetup --noudevsync mknodes
# Create partition names to mount
export BOOTPARTION_EX=\$(echo $LOOPDEV_EX | sed 's|'/dev'/|'/dev/mapper/'|')p1
export ROOTPARTION_EX=\$(echo $LOOPDEV_EX | sed 's|'/dev'/|'/dev/mapper/'|')p2
# Create file systems for the partitions
mkfs.vfat $BOOTPARTION_EX
mkfs.ext4 $ROOTPARTION_EX
# Create a loop device and mount the original image's partitions
export LOOPDEV_ORIG="\$(losetup --show --find $RASPBIAN_IMG)"
# Use sudo kpartx to create partitions in /dev/mapper
kpartx -av $LOOPDEV_ORIG
dmsetup --noudevsync mknodes
# Create partition names to mount
export BOOTPARTION_ORIG=\$(echo $LOOPDEV_ORIG | sed 's|'/dev'/|'/dev/mapper/'|')p1
export ROOTPARTION_ORIG=\$(echo $LOOPDEV_ORIG | sed 's|'/dev'/|'/dev/mapper/'|')p2
# Copy data from original image to extended image
dd if=$BOOTPARTION_ORIG of=$BOOTPARTION_EX bs=64K conv=noerror,sync
dd if=$ROOTPARTION_ORIG of=$ROOTPARTION_EX bs=64K conv=noerror,sync
# Remove loop devices for original image
dmsetup clear $BOOTPARTION_ORIG
dmsetup clear $ROOTPARTION_ORIG
dmsetup remove $BOOTPARTION_ORIG
dmsetup remove $ROOTPARTION_ORIG
losetup -d $LOOPDEV_ORIG
fi
# Try to mount extended image
if [ -z "\$(mount|grep raspbian-boot)" ] && [ -z "\$(mount|grep raspbian-root)" ]; then
# Create a loop device and mount the disk image
export LOOPDEV_EX="\$(losetup --show --find ./raspbian_$(project.name).img)"
# Use sudo kpartx to create partitions in /dev/mapper
kpartx -av $LOOPDEV_EX
dmsetup --noudevsync mknodes
# Create partition names to mount
export BOOTPARTION_EX=\$(echo $LOOPDEV_EX | sed 's|'/dev'/|'/dev/mapper/'|')p1
export ROOTPARTION_EX=\$(echo $LOOPDEV_EX | sed 's|'/dev'/|'/dev/mapper/'|')p2
# Make sure mount points exist and are clean
mkdir -p raspbian-boot
mkdir -p raspbian-root
rm -rf raspbian-boot/*
rm -rf raspbian-root/*
# Mount partitions
mount $BOOTPARTION_EX -t vfat raspbian-boot
mount $ROOTPARTION_EX -t ext4 raspbian-root
else
echo "Image already mounted!"
fi
$(project.GENERATED_WARNING_HEADER:)
.close
.chmod_x ("builds/rpi/prepare_img.sh")
.output "builds/rpi/README.md"
# Raspberry Pi
Cross compile environment for the Raspberry Pi (all models)
## Cross compile for Raspbian
Simply run
./build.sh
Once the build script finishes the installation data is located at `tmp/`.
To copy those files to your Raspberry Pi you can use `rsync`. Therefore
you'll have to make sure rsync is installed on the Raspberry Pi
apt-get install rsync
Next make sure your Pi user has sufficient access rights to copy data to
`/usr/local`. To force this you can use the following command to change
the ownership of the directory accordingly.
chown -R root:users /usr/local/
And finally sync your libraries to the Pi
rsync -avz tmp/ <pi-user>@<pi-ip>:/usr/local
The first time you install a library onto the Raspberry Pi you'll need to run
sudo ldconfig
## Cross compile and install to Raspbian image
This requires sudo access rights on your local maschine. Also make sure
you have `kpartx`, `qemu-user` and `proot` installed.
To cross compile and install this project to an raspbian image run
./build_image.sh
This script will download the latest Raspbian image extend it so we can install
software into it and finally copy the cross compiled stuff to it. Once it's done
simple copy the image to a SD card and plug it into your Raspberry Pi.
```
$(project.GENERATED_WARNING_HEADER:)
```
.close
.output "builds/rpi/.gitignore"
. for use where defined (use.tarball)
$(use.project)*
. endfor
. for use where defined (use.repository) & ! defined (use.tarball)
$(use.project)
. endfor
raspbian-boot
raspbian-root
tmp/
tools/
*.img
*.zip
.close
.endmacro