-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_sysroot.sh
executable file
·124 lines (100 loc) · 3.35 KB
/
build_sysroot.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
#!/bin/bash
# Abort on errors, as well as unset variables. Makes the script less error prone.
set -eu
# Find the location of this script, as some required things are stored next to it.
SRC_DIR="$(pwd)"
TOOLS_DIR="${SRC_DIR}/tools"
# Toolchain file location for cmake
TOOLCHAIN_FILE="${TOOLS_DIR}/arm-cross-compile.toolchain"
# Location of the sysroot, which is used during cross compiling
SYSROOT="${TOOLS_DIR}/sysroot"
TARGET_ARCH="arm"
# We use the 'type -P' command instead of 'which' since the first one is built-in, faster and has better defined exit values.
QEMU_BINARY="$(type -P qemu-${TARGET_ARCH}-static)"
build_sysroot()
{
echo "Going to build sysroot for cross compiling"
mkdir -p "${SYSROOT}/etc/apt/trusted.gpg.d"
curl https://ftp-master.debian.org/keys/archive-key-10.asc | fakeroot apt-key --keyring "${SYSROOT}/etc/apt/trusted.gpg.d/jessie.gpg" add -
multistrap -f "${TOOLS_DIR}/sysroot_multistrap.cfg" -d "${SYSROOT}"
# Fix up the symlinks in the sysroot, find all links that start with absolute paths
# and replace them with relative paths inside the sysroot.
cd "${SYSROOT}"
symlinks="$(find . -type l)"
for file in ${symlinks}
do
link="$(readlink "${file}" || echo '')"
if [ -n "${link}" ]
then
if [ "${link:0:1}" == "/" ]
then
if [ -e "${SYSROOT}/${link}" ]; then
rm "${file}"
ln --relative -sf "${SYSROOT}${link}" "${file}"
fi
fi
fi
done
cd "${SRC_DIR}"
mount --bind "/dev" "${SYSROOT}/dev"
mount --bind "/proc" "${SYSROOT}/proc"
mount --bind "/sys" "${SYSROOT}/sys"
mkdir -p "${SYSROOT}/ccache"
cp -f "${QEMU_BINARY}" "${SYSROOT}/usr/bin"
# Install the forked specially configured dependencies
# TODO: These should also come from cloudsmith and have a correct version number
cp "${TOOLS_DIR}/"*".deb" "${SYSROOT}"
umount -lR "${SYSROOT}/dev"
umount -lR "${SYSROOT}/proc"
umount -lR "${SYSROOT}/sys"
echo "Finished building sysroot in: ${SYSROOT}"
echo "You can now use cmake -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE} to build software"
}
trap umount -lR "${SYSROOT}/dev" || true; umount -lR "${SYSROOT}/proc" || true; umount -lR "${SYSROOT}/sys" || true exit
if [ ! "$(id -u)" -eq 0 ]; then
echo "This script should be run with root permissions."
exit 1
fi
usage()
{
echo ""
echo "This is a build script for generation of a Debian based sysroot that can be used for cross-compiling."
echo ""
echo " -c Clean the build output directory '_build'."
echo " -h Print this help text and exit"
echo ""
echo " The package release version can be passed by passing 'RELEASE_VERSION' through the run environment."
}
while getopts ":ch" options; do
case "${options}" in
c)
if [ -d "${SYSROOT}" ] && [ -z "${SYSROOT##*sysroot*}" ]; then
rm -rf "${SYSROOT}"
fi
exit 0
;;
h)
usage
exit 0
;;
:)
echo "Option -${OPTARG} requires an argument."
exit 1
;;
?)
echo "Invalid option: -${OPTARG}"
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
if [ "${#}" -gt 1 ]; then
echo "Too many arguments."
usage
exit 1
fi
if [ "${#}" -eq 0 ]; then
build_sysroot
exit 0
fi
exit 0