forked from riscvarchive/riscv-gentoo-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·153 lines (122 loc) · 5.1 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
#!/bin/bash
# Here are some configuration variables
RISCV_GENTOO="git://github.com/palmer-dabbelt/riscv-gentoo.git"
RISCV_LINUX="git://github.com/riscv/riscv-linux.git"
LINUX_TARBALL="https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.14.29.tar.xz"
set -ex
# This is going to use crossdev to bring up the cross compiler, which
# is only going to work if we're on a native Gentoo machine.
which crossdev >& /dev/null
if [[ "$?" != "0" ]]
then
echo "crossdev not installed"
exit 1
fi
# GCC fails to cross compile when systemtap is installed
if [[ "$(equery list systemtap | wc -l)" != "0" ]]
then
echo "systemtap is installed"
exit 1
fi
# Cleans out the existing crossdev toolchain
yes | crossdev -C riscv64-unknown-linux-gnu
rm -rf /usr/portage/local/cross-riscv64-unknown-linux-gnu/
# Cleans out the existing set of generated files
rm -rf generated
mkdir -p generated
# Creates a new temporary directory where we're going to do all our
# work. Note that this has to be readable by everyone because portage
# is going to use some other user to bring this up.
mkdir -p workdir
tempdir="$(pwd)"/workdir
export tempdir
chmod og+rX -R $tempdir
# Clones the RISC-V overlay to get a copy of the relevant toolchain
# files so they can be built.
git clone $RISCV_GENTOO $tempdir/riscv
chmod og+rX -R $tempdir/riscv
# Builds a cross compiler,
crossdev -t riscv64-unknown-linux-gnu \
--ov-output /usr/portage/local \
--env 'USE="-sanitize"' \
--binutils 2.29.1-r1 --ov-binutils $tempdir/riscv \
--kernel 4.15_rc4 --ov-kernel $tempdir/riscv \
--gcc 7.2.0 --ov-gcc $tempdir/riscv \
--libc 2.27_pre3 --ov-libc $tempdir/riscv
# Adds some stuff the the cross make.conf in order to actually allow
# my code to build.
cat >> /usr/riscv64-unknown-linux-gnu/etc/portage/make.conf <<EOF
PORTDIR_OVERLAY="\$PORTDIR_OVERLAY $tempdir/riscv"
PORTAGE_TEMPDIR="/var/tmp/riscv64-unknown-linux-gnu-portage/"
PYTHON_TARGETS="python2_7"
MAKEOPTS="-j2"
EOF
cp -r /etc/portage/repos.conf /usr/riscv64-unknown-linux-gnu/etc/portage/
# Set up the profile to point into this new cross toolchain
rm /usr/riscv64-unknown-linux-gnu/etc/portage/make.profile
ln -s $tempdir/riscv/profiles/default/linux/riscv/bootstrap-0 \
/usr/riscv64-unknown-linux-gnu/etc/portage/make.profile
cp -r /etc/portage/repos.conf /usr/riscv64-unknown-linux-gnu/etc/portage
rm -rf /usr/riscv64-unknown-linux-gnu/etc/portage/riscv
# Build a "cross stage3", which isn't actually a proper stage3 but
# does have roughly enough stuff to bring up portage on the RISC-V
# QEMU target
riscv64-unknown-linux-gnu-emerge -vu @system
# More tools are required for the image to be build
USE="static" riscv64-unknown-linux-gnu-emerge -v1 busybox
riscv64-unknown-linux-gnu-emerge -vu riscv-pk
# Generates a temporary disk image used for building a native RISC-V
# stage3.
fallocate -l 40G generated/scratch.img
/sbin/mkfs.ext4 -F generated/scratch.img
mkdir $tempdir/scratch
mount -o loop -t ext4 generated/scratch.img $tempdir/scratch
trap "umount $tempdir/scratch" EXIT
# Copies over the cross-compiled stage3 to give us something to
# bootstrap into.
tar -C /usr/riscv64-unknown-linux-gnu -c . | tar -C $tempdir/scratch -xv
# Copies over the relevant portage files so we can actually go ahead
# and start building things when we end up in the native QEMU boot
tar -C / -c /usr/portage | tar -C $tempdir/scratch -xv
mkdir -p $tempdir/scratch/usr/portage/local
tar -C $tempdir/riscv -c . | tar -C $tempdir/scratch/usr/portage/local -xv
# Since we've been coping a whole bunch of stuff from the local
# machine, we need to go ahead and make sure all the group information
# copies over properly.
cat >$tempdir/root-files <<EOF
/etc/passwd
/etc/shadow
/etc/group
EOF
tar -C / -T $tempdir/root-files -c | tar -C $tempdir/scratch -xv
# There are some configuration files that really need to have been
# manually written.
tar -C rootfs-overlays/generate-stage3 -c . | tar -C $tempdir/scratch -xv
umount $tempdir/scratch
trap "" EXIT
# Demonstrate to Linux how we're going to be building the initramfs,
# which really just consists of sticking an absolute path in there
# somewhere...
mkdir -p $tempdir/initramfs/initrd
cat rootfs-overlays/initramfs/initrd.conf | \
./scripts/sed_vars > $tempdir/initramfs/initrd.conf
# Some initramfs files need to be modified
cat rootfs-overlays/initramfs/initrd/init.sh | \
./scripts/sed_vars > $tempdir/initramfs/initrd/init.sh
# Now that we've got the whole cross system up, it's time to go and
# build a kernel and initrammfs that we can use to boot into Gentoo
# and rebuild the stage3 manually.
git clone $RISCV_LINUX $tempdir/linux -b linux-4.1.y-riscv
# Apply some extra Linux patches that aren't upstream yet
find patches/linux/ -iname "*.patch" | while read patch
do
patch -p1 -d $tempdir/linux < $patch
done
# Obtain a Linux configuration, which I store as a diff from the
# default configuration.
make -C $tempdir/linux ARCH=riscv64 defconfig
cat patches/linux/config | ./scripts/sed_vars >> $tempdir/linux/.config
make -C $tempdir/linux ARCH=riscv64 olddefconfig
# Actually build Linux
make -C $tempdir/linux ARCH=riscv64 -j8
cp $tempdir/linux/vmlinux generated/vmlinux