summaryrefslogtreecommitdiff
path: root/build_cross.sh
blob: e573e3ebdb79a554f05faa3c41d9a09f12ea34d2 (plain)
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
#!/bin/sh

# shellcheck source=./default.conf
. "./default.conf"

# Prepare the cross-compiler for the destination platform, in our
# case i486.

if test ! "$(getent group cross)"; then
	groupadd cross
fi

if test ! "$(getent passwd cross)"; then
	useradd -m -g cross cross
fi

# add 'cross' user as sudoer without password
if test ! -f /etc/sudoers.d/cross; then
	echo "cross ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/cross
fi

# we need the github keys
if test ! -d "$CROSS_HOME/.ssh"; then
	mkdir "$CROSS_HOME/.ssh"
	chown cross:cross "$CROSS_HOME/.ssh"
fi
if test ! -f "$CROSS_HOME/.ssh/id_rsa.pub"; then
	cp "$HOME/.ssh/id_rsa.pub" "$CROSS_HOME/.ssh/."
	cp "$HOME/.ssh/id_rsa" "$CROSS_HOME/.ssh/."
	chown cross:cross $CROSS_HOME/.ssh/id_rsa*
fi

if test ! -x /usr/local/bin/ct-ng; then
	echo "Installing crosstool-ng:"
	su - cross <<EOF
mkdir cross
cd cross
git clone https://github.com/crosstool-ng/crosstool-ng.git
cd crosstool-ng
./bootstrap
./configure --prefix=/usr/local
make
sudo make install
cd ..
EOF
	echo "Done crosstool-ng."
fi

if test ! -x $CROSS_HOME/x-tools/${TARGET_ARCH}/bin/${TARGET_ARCH}-gcc; then
	echo "Building cross compiler for ${TARGET_ARCH}=gcc:"
	rm -rf $XTOOLS_HOME
	cp ct-ng.config $CROSS_HOME/.config
	CPUS=$(nproc)
	sed -i "s/^CT_PARALLEL_JOBS=.*/CT_PARALLEL_JOBS=$CPUS/" $CROSS_HOME/.config
	chown cross:cross $CROSS_HOME/.config
	su - cross <<EOF
ct-ng build
EOF
	echo "Done creating the cross compiler."
fi

CROSS_MSG="$($CROSS_HOME/x-tools/${TARGET_ARCH}/bin/${TARGET_ARCH}-gcc --version | head -n 1)"
echo "Cross-compiler ready: $CROSS_MSG"