forked from landley/aboriginal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·172 lines (127 loc) · 4.51 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
#!/bin/bash
# Run all the steps needed to build a system image from scratch.
# The default set of stages run by this script is (in order):
# download, host-tools, simple-cross-compiler, root-filesystem,
# native-compiler, system-image.
# That sanitizes the host build environment and builds a cross compiler,
# cross compiles a root filesystem and a native toolchain for the target,
# and finally packages the root filesystem up into a system image bootable
# by qemu.
# The optional cross-compiler stage (after simple-cross-compiler but before
# root-filesystem) creates a more powerful and portable cross compiler
# that can be used to cross compile more stuff (if you're into that sort of
# thing). To enable that:
# CROSS_COMPILER_HOST=i686 ./build.sh $TARGET
# Where "i686" is whichever target you want the new cross compiler to run on.
# The simplest set of stages (if you run them yourself) is:
# download, simple-cross-compiler, root-filesystem, system-image.
# If this script was run with no arguments, list available architectures
[ ! -z "$2" ] && REBUILD="$2" &&
[ ! -e "$2".sh ] && echo "no stage $2" && exit 1
if [ $# -lt 1 ] || [ $# -gt 2 ] || [ ! -e sources/targets/"$1" ]
then
echo
echo "Usage: $0 TARGET [REBUILD_FROM_STAGE]"
echo
echo "Supported architectures:"
ls sources/targets
echo
echo "Build stages:"
sed -n 's/#.*//;s@.*[.]/\([^.]*\)[.]sh.*@\1@p' "$0" | uniq | xargs echo
echo
exit 1
fi
ARCH="$1"
# Use environment variables persistently set in the config file.
[ -e config ] && source config
# Allow the output directory to be overridden. This hasn't been tested in
# forever and probably doesn't work.
[ -z "$BUILD" ] && BUILD="build"
# Very simple dependency tracking: skip stages that have already been done
# (because the tarball they create is already there).
# If you need to rebuild a stage (and everything after it), delete its
# tarball out of "build" and re-run build.sh.
not_already()
{
[ "$AGAIN" == "$1" ] && return 0
[ "$REBUILD" == "$1" ] && zap "$1"
if [ -f "$BUILD/$1-$ARCH.tar.gz" ]
then
echo "=== Skipping $1-$ARCH (already there)"
return 1
fi
return 0
}
zap()
{
for i in "$@"
do
rm -f "$BUILD/$i-$ARCH.tar.gz"
done
}
# If $AFTER set, skip stages until we match $AFTER to implement $2="start here"
do_stage()
{
STAGE="$1"
shift
if [ "$AFTER" == "$STAGE" ]
then
unset AFTER
else
time ./"$STAGE".sh "$@" || exit 1
fi
}
# The first two stages (download.sh and host-tools.sh) are architecture
# independent. In order to allow multiple builds in parallel, re-running
# them after they've already completed must be a safe NOP.
# Download source code. If tarballs already there, verify sha1sums and
# delete/redownload if they don't match (to handle interrupted partial
# download).
do_stage download
# Build host tools. This populates a single directory with every command the
# build needs, so we can ditch the host's $PATH afterwards.
if [ -z "$NO_HOST_TOOLS" ]
then
do_stage host-tools
fi
# Do we need to build the simple cross compiler?
if [ -z "$MY_CROSS_PATH" ] && not_already simple-cross-compiler
then
# If we need to build cross compiler, assume root filesystem is stale.
zap root-filesystem cross-compiler native-compiler
do_stage simple-cross-compiler "$ARCH"
fi
# Optionally, we can build a more capable statically linked compiler via
# canadian cross. (It's more powerful than we need here, but if you're going
# to use the cross compiler in other contexts this is probably what you want.)
if [ -z "$MY_CROSS_PATH" ] && [ ! -z "$CROSS_COMPILER_HOST" ] &&
not_already cross-compiler
then
zap root-filesystem native-compiler
# Build the host compiler if necessary
if ARCH="$CROSS_COMPILER_HOST" not_already simple-cross-compiler
then
do_stage simple-cross-compiler "$CROSS_COMPILER_HOST"
fi
do_stage cross-compiler "$ARCH"
fi
if ! grep -q KARCH= sources/targets/"$ARCH"
then
echo no KARCH in $1, stopping here
fi
# Build the basic root filesystem.
if not_already root-filesystem
then
do_stage root-filesystem "$ARCH"
fi
# Build a native compiler. It's statically linked by default so it can
# run on an arbitrary host system.
# Not trying to build nommu native compilers for the moment.
if [ -z "$MY_CROSS_PATH" ] && ! grep -q ELF2FLT sources/targets/"$ARCH" &&
not_already native-compiler
then
do_stage native-compiler "$ARCH"
fi
# Package it all up into something qemu can boot. Like host-tools.sh,
# this is always called and handles its own dependencies internally.
do_stage system-image "$ARCH"