#!/bin/sh # Amethyst bootstrap script that does the bs I don't want to do by hand every time I start from scratch. # Written by Kana Steimle # This script builds from source the minimum set of required packages to start using apb and bootstrap Amethyst Linux as a whole. # These consist of the following packages: gcc, binutils, make, grep, sed, tar, gzip, oksh, coreutils, and wget. # It will also build apm and apb for you, installing them in the cross/bin directory along with the cross compiler. # If you're using pre-packaged sources, you can technically remove the tar, gzip, and wget bootstrap packages. But I'd still recommend keeping them until you know you don't need them. # In addition to building packages for the local machine, this can also cross compile the packages for a different architecture. # Pass the TARGET environment variable to specify the target machine's (and optionally the HOST environment variable too) # Some of the generated packages aren't quite up to standard with my current packages... I'm working on it. # For the sake of debugging: To start building from a particular step, pass the STEP environment variable # To stop after finishing a particular step, pass the LASTSTEP environment variable BASE="$(dirname "$(realpath $0)")" SRC="${BASE}/src" EXTSRC="${BASE}/extracted-src" CROSS="${BASE}/cross" PKGBS="${BASE}/pkg" mkdir -p "${PKGBS}" PATH="${CROSS}/bin:/usr/bin:/bin" export MAKEFLAGS="-j$(nproc)" [ -z "$STEP" ] && STEP=1 if [ -z "${HOST}" ]; then HOST=$(cc -dumpmachine) fi if [ -z "${TARGET}" ]; then TARGET=$(echo "${HOST}" | cut -d- -f1)-pc-linux-musl # Make sure gcc actually builds a cross compiler instead of a native compiler if [ "x${TARGET}" = "x${HOST}" ]; then TARGET=$(echo "${HOST}" | cut -d- -f1)-amethyst-linux-musl fi fi buildingpkg() { PACKAGE="$1" BUILD="${BASE}/$1_build" PKGOUT="${BUILD}/pkgout" mkdir -p "${BUILD}" cd "${BUILD}" } configure() { "${EXTSRC}/${PACKAGE}/configure" "$@" } strippkg() { for f in $(find "${PKGOUT}/bin" "${PKGOUT}/libexec" -type f -executable -exec file '{}' + | grep 'not stripped' | cut -d: -f1); do "${TARGET}-strip" "$f" done } package() { PWD_bak="${PWD}" cd "${PKGOUT}" switches="$1" shift set -- "$@" "${PKGBS}/${PACKAGE}${switches}.pkg" mksquashfs "$@" -all-root -noappend -no-strip cd "${PWD_bak}" } rmbuild() { cd "${BASEDIR}" rm -rf "${BUILD}" } nextstep() { if [ "${STEP}" = "${LASTSTEP}" ]; then exit 0 fi STEP="$((STEP+1))" } set -ex # Extract sources mkdir -p "${EXTSRC}" cd "${EXTSRC}" for f in apm apb binutils gcc musl gmp mpfr mpc libressl make grep sed tar gzip oksh coreutils wget; do if ! [ -e $f ]; then tar -xf "${SRC}/$f-"*.tar.* mv $f-* $f case $f in gcc) [ "$(echo "${TARGET}" | cut -d- -f1)" = x86_64 ] && sed -i '/m64=/s/lib64/lib/' "${EXTSRC}/$f/gcc/config/i386/t-linux64" ;; esac for p in "${SRC}/$f-"*.patch; do [ -r "$p" ] && patch -p1 -d "${EXTSRC}/$f" -i "$p" done fi done # -- Phase 1: Cross tools -- # Step 1: apm if [ 1 -eq "$STEP" ]; then buildingpkg apm configure --prefix="${CROSS}" --bindir="${CROSS}/bin" --disable-vim --disable-man --disable-doc make make install rmbuild nextstep fi # Step 2: apb if [ 2 -eq "$STEP" ]; then buildingpkg apb configure --prefix="${CROSS}" --bindir="${CROSS}/bin" --disable-vim --disable-man --sysconfdir="${CROSS}/etc" --pkgdir="${PKGBS}" make make install rmbuild nextstep fi # Step 3: cross binutils if [ 3 -eq "$STEP" ]; then buildingpkg binutils configure --prefix="${CROSS}" --host="${HOST}" --target="${TARGET}" --disable-nls --disable-gprofng --disable-gold make make install rmbuild nextstep fi # Step 4: cross gcc if [ 4 -eq "$STEP" ]; then buildingpkg gcc configure --prefix="${CROSS}" --host="${HOST}" --target="${TARGET}" --disable-nls --enable-languages=c,c++ --disable-multilib --disable-shared make all-gcc make install-gcc # Don't rmbuild because we're going to keep building from where we already are. nextstep fi # All the cross tools are built; strip them. for f in $(find "${CROSS}"/bin "${CROSS}"/libexec -type f -executable -exec file '{}' + | grep 'not stripped' | cut -d: -f1); do strip $f done # -- Phase 2: Libraries -- # Step 5: musl if [ 5 -eq "$STEP" ]; then buildingpkg musl configure --prefix="${CROSS}/${TARGET}" --build="${HOST}" --host="${TARGET}" --disable-shared make make install rmbuild nextstep fi # Step 6: libgcc and libstdc++-v3 if [ 6 -eq "$STEP" ]; then buildingpkg gcc # Use last configuration make all-target-libgcc all-target-libstdc++-v3 make install-target-libgcc install-target-libstdc++-v3 rmbuild nextstep fi # Steps 7-9: gmp, mpfr, mpc RSTEP=7 for pkg in gmp mpfr mpc; do if [ $RSTEP -eq "$STEP" ]; then buildingpkg $pkg configure --prefix="${CROSS}/${TARGET}" --build="${HOST}" --host="${TARGET}" --disable-shared make make install rmbuild nextstep fi RSTEP=$((RSTEP+1)) done # Step 10: libressl if [ 10 -eq "$STEP" ]; then buildingpkg libressl configure --prefix="${CROSS}/${TARGET}" --sysconfdir=/etc --build="${HOST}" --host="${TARGET}" --disable-shared # We only want to build the libraries libcrypto and libssl make -C crypto make -C ssl # Install the libraries make -C crypto install make -C ssl install # Install the headers make -C include/openssl install # Install the configuration files and certificates as a package; wget can't use ssl without them make DESTDIR="${PKGOUT}" install-exec-hook package -certs-bootstrap etc/ssl/cert.pem rmbuild nextstep fi # -- Phase 3: Native tools -- # Step 11: binutils if [ 11 -eq "$STEP" ]; then buildingpkg binutils configure --prefix='' --build="${HOST}" --host="${TARGET}" --disable-nls --disable-gprofng --disable-gold --disable-shared make make install DESTDIR="${PKGOUT}" tooldir= strippkg package -bin-bootstrap-musl-static bin lib/ldscripts rmbuild nextstep fi # Step 12: gcc if [ 12 -eq "$STEP" ]; then buildingpkg gcc configure --prefix='' --build="${HOST}" --host="${TARGET}" --target="${TARGET}" --disable-nls --enable-languages=c,c++ --disable-multilib --disable-shared --disable-lto CFLAGS_FOR_TARGET="-fPIE" CXXFLAGS_FOR_TARGET="-fPIE" LDFLAGS_FOR_TARGET="-fPIE" make all-gcc all-target-libgcc all-target-libstdc++-v3 make install-gcc install-target-libgcc install-target-libstdc++-v3 DESTDIR="${PKGOUT}" ln -sf gcc "${BUILD}/pkgout/bin/cc" ln -sf gcc "${BUILD}/pkgout/bin/${TARGET}-cc" strippkg package -bin-bootstrap-musl-static bin libexec package -lib-bootstrap-musl-static lib package -headers-bootstrap-musl-static include rmbuild nextstep fi # Step 13: oksh if [ 13 -eq "$STEP" ]; then buildingpkg oksh # Bug in oksh Makefile; it won't be able to install manpages unless they're in the build directory cp "${EXTSRC}/${PACKAGE}/"*.1 "${BUILD}/" configure --prefix='' --cc="${TARGET}-gcc" make make install DESTDIR="${PKGOUT}" cp sh.1 "${BUILD}/pkgout/share/man/man1/" ln -sf oksh "${BUILD}/pkgout/bin/sh" strippkg package -bin-bootstrap-musl-static bin rmbuild nextstep fi # Step 14: coreutils if [ 14 -eq "$STEP" ]; then buildingpkg coreutils configure --prefix='' --host="${TARGET}" --disable-nls --disable-shared --enable-single-binary=symlinks --enable-no-install-program=stdbuf make make install DESTDIR="${PKGOUT}" strippkg package -bin-bootstrap-musl-static bin rmbuild nextstep fi # Step 15-19: make, grep, sed, tar, and gzip RSTEP=15 for pkg in make grep sed tar gzip; do if [ $RSTEP -eq "$STEP" ]; then buildingpkg $pkg configure --prefix='' --host="${TARGET}" --disable-nls make make install DESTDIR="${PKGOUT}" strippkg package -bin-bootstrap-musl-static bin rmbuild nextstep fi RSTEP=$((RSTEP+1)) done # Step 20: wget if [ 20 -eq "$STEP" ]; then buildingpkg wget # There's a bunch of host libraries it'll try to use if we don't explicitly tell it not to configure --prefix='' --host="${TARGET}" --with-ssl=openssl --disable-pcre --disable-pcre2 --without-zlib --without-libpsl --disable-iri --disable-nls make make install DESTDIR="${PKGOUT}" strippkg package -bin-bootstrap-musl-static bin rmbuild nextstep fi