summaryrefslogtreecommitdiff
path: root/scripts/newstrap.sh
blob: ba3974795f905e6546968a044811ff1ad4079f79 (plain) (blame)
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
#!/bin/sh -e

###
# Basic sanity.
###
set -e
[ -z $ZSH_VERSION ] || set -y
readonly APK=abuild-apk
readonly DEF_ARCH=$(uname -m)
readonly DEF_TD=${HOME}/builds
readonly DISTRO_NAME="Adélie Linux"
readonly PROGNAME=$(basename $0)
export PATH=/usr/bin:`$(command -v getconf || echo false) CS_PATH || echo /usr/local/bin:/usr/bin:/bin`:/usr/local/sbin:/usr/sbin:/sbin


###
# Messaging utility functions.
###

info() {
	printf '\033[01;32m>>>\033[00;39m %s\n' "$1"
}

warn() {
	printf '\033[01;33m>>> WARNING\033[00;39m: %s\n' "$1"
}

error() {
	printf '\033[01;31m>>> ERROR\033[00;39m: %s\n' "$1"
}


###
# Check for required utilities.
###

if ! command -v ${APK}>/dev/null 2>/dev/null; then
	printf "APK Tools must be present to bootstrap ${DISTRO_NAME}.\n"
	error "apk (as ${APK}): not found in $PATH"
	exit 127
fi

if ! command -v abuild>/dev/null 2>/dev/null; then
	printf "abuild must be present to bootstrap ${DISTRO_NAME}.\n"
	error "abuild: not found in $PATH"
	exit 127
fi


###
# Provide the user with help, if requested or needed.
###

usage() {
	printf 'usage: %s [-a ARCH] [-b PATH] [-h HOSTC] [-k KEYDIR] [-p PATH] [-t TARGC] [-v] [--help]\n\n' $PROGNAME
	printf 'Creates a local cross-compiler, and uses it to create a small\n'
	printf 'bootstrap environment, capable of building itself.\n\n'
	printf 'This environment can be used to seed autobuilder, to verify\n'
	printf 'correctness and reproducibility of builds, or to perform\n'
	printf 'any number of scientific experiments.\n\n'
	printf 'Default ARCH: %s\n' $DEF_ARCH
	printf 'Default root for TARGET_DIRs: %s\n' $DEF_TD
}


###
# Determine the desired output.
###

while [ -n "$1" ]; do
	case $1 in
		-a | --arch)
			shift
			readonly MY_ARCH=$1
			;;
		-b | --build-path)
			shift
			readonly MY_BUILDPATH=$1
			;;
		-h | --host-conf)
			shift
			readonly MY_HOSTCONF=$1
			;;
		--help)
			usage
			exit
			;;
		-k | --key-dir)
			shift
			readonly MY_KEYDIR=$1
			;;
		-p | --path)
			shift
			readonly MY_TD=$1
			;;
		-t | --target-conf)
			shift
			readonly MY_TARGCONF=$1
			;;
		-v | --verbose)
			readonly BE_VERBOSE=
			;;
		*)
			usage >&2
			exit 127
			;;
	esac
	shift
done


readonly ARCH=${MY_ARCH:-$DEF_ARCH}
readonly BUILD_PATH=${MY_BUILDPATH:-$PWD}
readonly KEY_DIR=${MY_KEYDIR:-/etc/apk/keys}
readonly TARGET_DIR=${MY_TD:-${DEF_TD}/${ARCH}}


###
# Here comes the pain!
###

[ -z ${BE_VERBOSE+v} ] || (
	info "Building the target environment:"
	info " "
	info "ARCH: $ARCH"
	info "TARGET_DIR: $TARGET_DIR"
	info "Using APKBUILDs from: $BUILD_PATH"
	info "Using keys from: $KEY_DIR"
	[ -z ${MY_HOSTCONF} ] || info "Host-side abuild.conf: $MY_HOSTCONF"
	[ -z ${MY_TARGCONF} ] || info "Target-side abuild.conf: $MY_TARGCONF"
	printf '\n'
)

info "Building stage1 bootstrap tools ($(uname -m) -> $ARCH)..."

[ -z ${BE_VERBOSE+v} ] || info 'Setting up APK...'
mkdir -p "$TARGET_DIR"/etc/apk/keys
cp -a "$KEY_DIR"/* "$TARGET_DIR"/etc/apk/keys/
${APK} add ${BE_VERBOSE---quiet} --usermode --initdb --arch $ARCH --root "$TARGET_DIR"


[ -z ${BE_VERBOSE+v} ] || info 'Setting up configuration...'
if [ -z ${MY_HOSTCONF} ]; then
	if [ -f "$TARGET_DIR"/etc/abuild.conf.host ]; then
		[ -z ${BE_VERBOSE+v} ] || info 'Host-side configuration reused.'
	else 
		cat >"$TARGET_DIR"/etc/abuild.conf.host <<HOST_CONF
# Just enough to get us by..
REPODEST="$TARGET_DIR"/repository
CLEANUP="deps srcdir bldroot pkgdir deps"
export CFLAGS="-O2 -fno-omit-frame-pointer"
export USE_COLORS=1
export DEFAULT_DBG=YesPlease
HOST_CONF
	fi
else
	cp "$MY_HOSTCONF" "$TARGET_DIR"/etc/abuild.conf.host
fi

if [ -z ${MY_TARGCONF} ]; then
	if [ -f "$TARGET_DIR"/etc/abuild.conf.target ]; then
		[ -z ${BE_VERBOSE+v} ] || info 'Target-side configuration reused.'
	else
		cat >"$TARGET_DIR"/etc/abuild.conf.target <<TARG_CONF
# Just enough to get us by..
REPODEST="$TARGET_DIR"/repository
CLEANUP="deps srcdir bldroot pkgdir deps"
export CFLAGS="-O2 -fno-omit-frame-pointer"
export USE_COLORS=1
export DEFAULT_DBG=YesPlease
TARG_CONF
	fi
else
	cp "$MY_TARGCONF" "$TARGET_DIR"/etc/abuild.conf.target
fi

abuild_host() {
	(
	export APKBUILD=${BUILD_PATH}/system/$1/APKBUILD ABUILD_CONF="$TARGET_DIR"/etc/abuild.conf.host CBUILDROOT="$TARGET_DIR";
	if [ -z ${BE_VERBOSE+v} ]; then
		abuild -r 2>"$TARGET_DIR"/$(date +%s)-$1.log
	else
		abuild -r
	fi
	) || (error "building $1. Stop."; false)
}

abuild_target() {
	EXTRADEPENDS_TARGET="musl musl-dev" CHOST=$ARCH BOOTSTRAP=bootimage APKBUILD=${BUILD_PATH}/system/$1/APKBUILD ABUILD_CONF="$TARGET_DIR"/etc/abuild.conf.target CBUILDROOT="$TARGET_DIR" abuild -r
}

CTARGET=$ARCH BOOTSTRAP=nobase abuild_host binutils
CHOST=$ARCH BOOTSTRAP=nocc abuild_host musl
EXTRADEPENDS_TARGET=musl-dev CTARGET=$ARCH BOOTSTRAP=nolibc abuild_host gcc
EXTRADEPENDS_BUILD=gcc-pass2-$ARCH CHOST=$ARCH BOOTSTRAP=nolibc abuild_host musl
EXTRADEPENDS_BUILD=gcc-pass2-$ARCH EXTRADEPENDS_TARGET="musl musl-dev" CTARGET=$ARCH BOOTSTRAP=nobase abuild_host gcc

for next_pkg in musl dash pkgconf zlib \
	gettext-tiny ncurses bash binutils make bison flex m4 \
	openssl apk-tools gmp mpfr3 mpc1 isl gcc; do
	abuild_target $next_pkg
done