From 7b365e3bef46ef5d102b38b04f51434260bd4957 Mon Sep 17 00:00:00 2001 From: davidovski Date: Thu, 19 May 2022 00:20:25 +0100 Subject: restored previous bootstrap --- bootstrap.sh | 63 ------ bootstrap/bootstrap.sh | 308 +++++++++++++++++++++++++++ bootstrap/stage1.sh | 166 +++++++++++++++ bootstrap/stage2.sh | 553 +++++++++++++++++++++++++++++++++++++++++++++++++ bootstrap/stage3.sh | 135 ++++++++++++ xib.sh | 6 +- 6 files changed, 1163 insertions(+), 68 deletions(-) delete mode 100755 bootstrap.sh create mode 100755 bootstrap/bootstrap.sh create mode 100644 bootstrap/stage1.sh create mode 100644 bootstrap/stage2.sh create mode 100644 bootstrap/stage3.sh diff --git a/bootstrap.sh b/bootstrap.sh deleted file mode 100755 index e4a041b..0000000 --- a/bootstrap.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/sh - -toolchaindest="$xib_dir/chroot-toolchain" - -toolchainpackages=" -musl -linux-headers -llvm -clang -clang-tools-extra -libcxx -libcxxabi -libunwind -lld -libexecinfo -ncurses -tcl -expect -dejagnu -m4 -dash -bison -bzip2 -sbase -sort -findutils -diffutils -gettext -gzip -grep -make -patch -perl -sed -tar -texinfo -cmake-toolchain -ninja -" - -bootstrap () { - mkdir -p $toolchaindest - rm -rf $stage - mkdir -p $stage - xi -nyl -r $toolchaindest bootstrap - - for pkg in $toolchainpackages; do - pkg_build=$(get_package_build $pkg) - parent=$(basename $pkg_build) - [ ! -d "$stage/$parent" ] && mkdir -p $stage/$parent - [ ! -f $stage/$parent/$pkg.xipkg ] && { - xibuild -nv -k $keychain/$priv_key -C $pkg_build -d $stage/$parent -r $chroot || return 1 - } - echo "Installing $pkg" - xi -nyl -r $toolchaindest install $stage/$parent/$pkg.xipkg - done - - printf "creating tarball..." - output="xib-chroot-tools-$(date +%y%m%d).tar.xz" - tar -C $toolchaindest -cJf $output ./ - printf "Complete!\n" -} - diff --git a/bootstrap/bootstrap.sh b/bootstrap/bootstrap.sh new file mode 100755 index 0000000..2c244f1 --- /dev/null +++ b/bootstrap/bootstrap.sh @@ -0,0 +1,308 @@ +#!/bin/bash + + +## VERSIONS ## +# TODO move to a different package + +WD=$(pwd)/working +BUILDFILES=$WD/buildfiles + +getbuildfiles () { + [ -d $BUILDFILES ] && { + cd $BUILDFILES + git pull + cd $OLDPWD + } || { + mkdir $BUILDFILES + git clone https://xi.davidovski.xyz/git/buildfiles.git $BUILDFILES + } +} + +getversion() { + find $BUILDFILES/ -name "$1.xibuild" | head -1 | xargs grep "PKG_VER=" | cut -d"=" -f2 +} + +getbuildfiles + +LINUX_VER=$(getversion linux) +BINUTILS_VER=$(getversion binutils) +MPFR_VER=$(getversion mpfr) +MPC_VER=$(getversion mpc) +GMP_VER=$(getversion gmp) +GCC_VER=$(getversion gcc) +MUSL_VER=$(getversion musl) +FILE_VER=$(getversion file) +TCL_VER=$(getversion tcl) +M4_VER=$(getversion m4) +EXPECT_VER=$(getversion expect) +DEJAGNU_VER=$(getversion dejagnu) +NCURSES_VER=$(getversion ncurses) +BASH_VER=$(getversion bash) +BISON_VER=$(getversion bison) +BZIP2_VER=$(getversion bzip2) +COREUTILS_VER=$(getversion sbase) +DIFFUTILS_VER=$(getversion diffutils) +GAWK_VER=$(getversion gawk) +GETTEXT_VER=$(getversion gettext) +GREP_VER=$(getversion grep) +GZIP_VER=$(getversion gzip) +MAKE_VER=$(getversion make) +PATCH_VER=$(getversion patch) +SED_VER=$(getversion sed) +PERL_VER=$(getversion perl) +TEXINFO_VER=$(getversion texinfo) +FLEX_VER=$(getversion fle) +PERL_CROSS_VER=1.3.6 +GETTEXT_TINY_VER=$(getversion gettext) +FINDUTILS_VER=$(getversion findutils) + +#### + +CURL_OPTS="-SsL" + + +HOST=x86_64-linux-gnu +TARGET=x86_64-linux-musl +ARCH=x86 +CPU=x86-64 + +CROSS_TOOLS=/cross-tools +TOOLS=/tools +chroot=$(pwd)/chroot + +PATH=${TOOLS}/bin:${CROSS_TOOLS}/bin:/usr/bin + +MAKEFLAGS="-j$(grep "processor" /proc/cpuinfo | wc -l)" + +export chroot TARGET PATH WD CURL_OPTS CROSS_TOOLS TOOLS MAKEFLAGS + +unset CFLAGS CXXFLAGS + +die () { + printf "${RED}$@${RESET}\n" + exit 1 +} + +extract () { + echo "extracting $1" + FILE=$1 + case "${FILE##*.}" in + "gz" ) + tar -zxf $FILE + ;; + "lz" ) + tar --lzip -xf "$FILE" + ;; + "zip" ) + unzip $FILE + ;; + "xz" ) + tar -xf $FILE + ;; + * ) + tar -xf $FILE + ;; + esac +} + +src () { + cd ${WD} + local source=$1 + local filename=$(basename $source) + printf "${LIGHT_BLUE}Fetching $filename...${RESET}\n" + + curl ${CURL_OPTS} $source > $filename + file $filename + extract $filename + cd ${filename%.t*} +} + +ptch () { + local source=$1 + local filename=$(basename $source) + + printf "${LIGHT_BLUE}Patching $filename...${RESET}\n" + curl ${CURL_OPTS} $source > $filename + patch -Np1 -i $filename +} + +clean () { + rm -rf $WD $CROSS_TOOLS $TOOLS + mkdir -p $WD $CROSS_TOOLS $TOOL +} + +mount_chroot () { + mkdir -p $chroot/{dev,proc,sys,run} + mknod -m 600 $chroot/dev/console c 5 1 + mknod -m 666 $chroot/dev/null c 1 3 + + mount --bind /dev $chroot/dev + mount -t devpts devpts $chroot/dev/pts -o gid=5,mode=620 + mount -t proc proc $chroot/proc + mount -t sysfs sysfs $chroot/sys + mount -t tmpfs tmpfs $chroot/run + if [ -h $chroot/dev/shm ]; then + mkdir -p $chroot/$(readlink $chroot/dev/shm) + fi +} + +umount_chroot () { + umount $chroot/dev/pts + umount $chroot/dev + umount $chroot/run + umount $chroot/proc + umount $chroot/sys +} + +tchroot () { + chroot "$chroot" /tools/bin/env -i \ + HOME=/root \ + TERM="$TERM" \ + PS1='(chroot) \u:\w\$ ' \ + PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \ + $@ +} + +patch_gcc () { + local PATCH_SRC="https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/gcc-alpine" + + ptch $PATCH_SRC/0001-posix_memalign.patch && + ptch $PATCH_SRC/0003-Turn-on-Wl-z-relro-z-now-by-default.patch && + ptch $PATCH_SRC/0004-Turn-on-D_FORTIFY_SOURCE-2-by-default-for-C-C-ObjC-O.patch && + ptch $PATCH_SRC/0006-Enable-Wformat-and-Wformat-security-by-default.patch && + ptch $PATCH_SRC/0007-Enable-Wtrampolines-by-default.patch && + ptch $PATCH_SRC/0009-Ensure-that-msgfmt-doesn-t-encounter-problems-during.patch && + ptch $PATCH_SRC/0010-Don-t-declare-asprintf-if-defined-as-a-macro.patch && + ptch $PATCH_SRC/0011-libiberty-copy-PIC-objects-during-build-process.patch && + ptch $PATCH_SRC/0012-libitm-disable-FORTIFY.patch && + ptch $PATCH_SRC/0013-libgcc_s.patch && + ptch $PATCH_SRC/0014-nopie.patch && + ptch $PATCH_SRC/0015-libffi-use-__linux__-instead-of-__gnu_linux__-for-mu.patch && + ptch $PATCH_SRC/0016-dlang-update-zlib-binding.patch && + ptch $PATCH_SRC/0017-dlang-fix-fcntl-on-mips-add-libucontext-dep.patch && + ptch $PATCH_SRC/0018-ada-fix-shared-linking.patch && + ptch $PATCH_SRC/0019-build-fix-CXXFLAGS_FOR_BUILD-passing.patch && + ptch $PATCH_SRC/0020-add-fortify-headers-paths.patch && + ptch $PATCH_SRC/0023-Pure-64-bit-MIPS.patch && + ptch $PATCH_SRC/0024-use-pure-64-bit-configuration-where-appropriate.patch && + ptch $PATCH_SRC/0025-always-build-libgcc_eh.a.patch && + ptch $PATCH_SRC/0027-ada-musl-support-fixes.patch && + ptch $PATCH_SRC/0028-gcc-go-Use-_off_t-type-instead-of-_loff_t.patch && + ptch $PATCH_SRC/0029-gcc-go-Don-t-include-sys-user.h.patch && + ptch $PATCH_SRC/0030-gcc-go-Fix-ucontext_t-on-PPC64.patch && + ptch $PATCH_SRC/0031-gcc-go-Fix-handling-of-signal-34-on-musl.patch && + ptch $PATCH_SRC/0032-gcc-go-Use-int64-type-as-offset-argument-for-mmap.patch && + #ptch $PATCH_SRC/0034-gcc-go-signal-34-is-special-on-musl-libc && + ptch $PATCH_SRC/0035-gcc-go-Prefer-_off_t-over-_off64_t.patch && + ptch $PATCH_SRC/0036-gcc-go-undef-SETCONTEXT_CLOBBERS_TLS-in-proc.c.patch && + ptch $PATCH_SRC/0037-gcc-go-link-to-libucontext.patch && + ptch $PATCH_SRC/0038-gcc-go-Disable-printing-of-unaccessible-ppc64-struct.patch && + ptch $PATCH_SRC/0041-Use-generic-errstr.go-implementation-on-musl.patch && + ptch $PATCH_SRC/0042-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch && + ptch $PATCH_SRC/0043-configure-Add-enable-autolink-libatomic-use-in-LINK_.patch + #ptch $PATCH_SRC/0022-DP-Use-push-state-pop-state-for-gold-as-well-when-li.patch && +} + +create_chroot () { + PATH=/usr/bin:/bin + mkdir chroot + xi -l -r ${chroot} bootstrap + echo "copying tools..." + mkdir ${chroot}/tools + cp -r ${TOOLS} ${chroot}/ + + echo "making essential links" + + ln -s /tools/bin/bash ${chroot}/bin + ln -s /tools/bin/cat ${chroot}/bin + ln -s /tools/bin/dd ${chroot}/bin + ln -s /tools/bin/echo ${chroot}/bin + ln -s /tools/bin/ln ${chroot}/bin + ln -s /tools/bin/pwd ${chroot}/bin + ln -s /tools/bin/rm ${chroot}/bin + ln -s /tools/bin/stty ${chroot}/bin + ln -s /tools/bin/install ${chroot}/bin + ln -s /tools/bin/env ${chroot}/bin + #ln -s /tools/bin/perl ${chroot}/bin + + ln -s /tools/lib/libgcc_s.so.1 ${chroot}/usr/lib + ln -s /tools/lib/libgcc_s.so ${chroot}/usr/lib + + ln -s /tools/lib/libstdc++.a ${chroot}/usr/lib + ln -s /tools/lib/libstdc++.so ${chroot}/usr/lib + ln -s /tools/lib/libstdc++.so.6 ${chroot}/usr/lib + ln -s bash ${chroot}/bin/sh + + mkdir -p ${chroot}/etc + ln -s /proc/self/mounts ${chroot}/etc/mtab + + cat > ${chroot}/etc/passwd << "EOF" +root:x:0:0:root:/root:/bin/bash +daemon:x:6:6:Daemon User:/dev/null:/bin/false +messagebus:x:18:18:D-Bus Message Daemon User:/var/run/dbus:/bin/false +nobody:x:99:99:Unprivileged User:/dev/null:/bin/false +EOF + + cat > ${chroot}/etc/group << "EOF" +root:x:0: +sys:x:2: +kmem:x:3: +tape:x:4: +tty:x:5: +daemon:x:6: +floppy:x:7: +disk:x:8: +lp:x:9: +dialout:x:10: +audio:x:11: +video:x:12: +utmp:x:13: +usb:x:14: +cdrom:x:15: +adm:x:16: +messagebus:x:18: +input:x:24: +mail:x:34: +nogroup:x:99: +users:x:999: +EOF + + echo "Created chroot" +} + +package_chroot () { + PATH=/usr/bin:/bin + echo "compressing..." + echo ${chroot} + tar -C ${chroot} -czf chroot-tools.tar.gz ./ + echo "created chroot-tools.tar.gz..." +} + +[ -f /usr/lib/colors.sh ] && . /usr/lib/colors.sh + +rm -rf $WD ; mkdir $WD + +case "$1" in + stage1|cross|cross_tools) + . ./stage1.sh + ;; + stage2|tools|toolchain) + . ./stage2.sh + ;; + stage3) + . ./stage3.sh + ;; + package) + umount_chroot + package_chroot + ;; + *) + clean + $0 stage1 + $0 stage2 + $0 stage3 + umount_chroot + package_chroot + ;; +esac + diff --git a/bootstrap/stage1.sh b/bootstrap/stage1.sh new file mode 100644 index 0000000..17076b5 --- /dev/null +++ b/bootstrap/stage1.sh @@ -0,0 +1,166 @@ +#!/bin/sh + +cross_tools_kernel_headers () { + src "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-$LINUX_VER.tar.xz" + + make mrproper + + make ARCH=${ARCH} headers + mkdir -p $CROSS_TOOLS/$TARGET/include + + cp -r usr/include/* $CROSS_TOOLS/$TARGET/include + find $CROSS_TOOLS/$TARGET/include -name '.*.cmd' -exec rm -f {} \; + rm $CROSS_TOOLS/$TARGET/include/Makefile +} + +cross_tools_binutils () { + src "https://ftp.gnu.org/gnu/binutils/binutils-$BINUTILS_VER.tar.xz" + + mkdir build && + cd build && + + ../configure \ + --prefix=${CROSS_TOOLS} \ + --target=${TARGET} \ + --host=${HOST} \ + --with-sysroot=${CROSS_TOOLS}/${TARGET} \ + --disable-nls \ + --disable-multilib \ + --disable-werror \ + --enable-deterministic-archives \ + --disable-compressed-debug-sections && + + make configure-host && + make && make install +} + +cross_tools_gcc_static () { + cd ${WD} + src "https://ftp.gnu.org/gnu/mpfr/mpfr-$MPFR_VER.tar.xz" + src "https://ftp.gnu.org/gnu/gmp/gmp-$GMP_VER.tar.xz" + src "https://ftp.gnu.org/gnu/mpc/mpc-$MPC_VER.tar.gz" + src "https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VER/gcc-$GCC_VER.tar.xz" + + mv ../mpfr-$MPFR_VER mpfr && + mv ../gmp-$GMP_VER gmp && + mv ../mpc-$MPC_VER mpc && + + mkdir build && cd build && + + CFLAGS='-g0 -O0' \ + CXXFLAGS=$CFLAGS \ + ../configure \ + --prefix=${CROSS_TOOLS} --build=${HOST} \ + --host=${HOST} --target=${TARGET} \ + --with-sysroot=${CROSS_TOOLS}/${TARGET} \ + --disable-nls --with-newlib \ + --disable-libitm --disable-libvtv \ + --disable-libssp --disable-shared \ + --disable-libgomp --without-headers \ + --disable-threads --disable-multilib \ + --disable-libatomic --disable-libstdcxx \ + --enable-languages=c --disable-libquadmath \ + --disable-libsanitizer --with-arch=${CPU} \ + --disable-decimal-float --enable-clocale=generic && + + make all-gcc all-target-libgcc && + make install-gcc install-target-libgcc +} + +cross_tools_musl () { + src https://musl.libc.org/releases/musl-$MUSL_VER.tar.gz + + ./configure \ + CROSS_COMPILE=${TARGET}- \ + --prefix=/ \ + --target=${TARGET} && + + make && DESTDIR=${CROSS_TOOLS} make install && + + # Add missing directory and link + mkdir ${CROSS_TOOLS}/usr && + ln -s ../include ${CROSS_TOOLS}/usr/include && + + case $(uname -m) in + x86_64) export ARCH="x86_64" + ;; + i686) export ARCH="i386" + ;; + arm*) export ARCH="arm" + ;; + aarch64) export ARCH="aarch64" + ;; + esac + + # Fix link + rm -f ${CROSS_TOOLS}/lib/ld-musl-${ARCH}.so.1 && + ln -s libc.so ${CROSS_TOOLS}/lib/ld-musl-${ARCH}.so.1 + + # Create link for ldd: + ln -s ../lib/ld-musl-$ARCH.so.1 ${CROSS_TOOLS}/bin/ldd + + # Create config for dynamic library loading: + mkdir ${CROSS_TOOLS}/etc + + echo $CROSS_TOOLS/lib >> ${CROSS_TOOLS}/etc/ld-musl-$ARCH.path + echo $TOOLS/lib >> ${CROSS_TOOLS}/etc/ld-musl-$ARCH.path + + unset ARCH ARCH2 +} + +cross_tools_gcc_final () { + cd ${WD} + rm -rf * + + src "https://ftp.gnu.org/gnu/mpfr/mpfr-$MPFR_VER.tar.xz" + src "https://ftp.gnu.org/gnu/gmp/gmp-$GMP_VER.tar.xz" + src "https://ftp.gnu.org/gnu/mpc/mpc-$MPC_VER.tar.gz" + src "https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VER/gcc-$GCC_VER.tar.xz" + + mv ../mpfr-$MPFR_VER mpfr + mv ../gmp-$GMP_VER gmp + mv ../mpc-$MPC_VER mpc + + patch_gcc + +# Configure in a dedicated build directory +mkdir build && cd build && +AR=ar LDFLAGS="-Wl,-rpath,${CROSS_TOOLS}/lib" \ +../configure \ + --prefix=${CROSS_TOOLS} \ + --build=${HOST} \ + --host=${HOST} \ + --target=${TARGET} \ + --disable-multilib \ + --with-sysroot=${CROSS_TOOLS} \ + --disable-nls \ + --enable-shared \ + --enable-languages=c,c++ \ + --enable-threads=posix \ + --enable-clocale=generic \ + --enable-libstdcxx-time \ + --enable-fully-dynamic-string \ + --disable-symvers \ + --disable-libsanitizer \ + --disable-lto-plugin \ + --disable-libssp + + # Build + make AS_FOR_TARGET="${TARGET}-as" \ + LD_FOR_TARGET="${TARGET}-ld" && + + # Install + make install +} + +cross_tools_file () { + src "https://astron.com/pub/file/file-$FILE_VER.tar.gz" + ./configure --prefix=${CROSS_TOOLS} --disable-libseccomp + make && make install +} + +for p in kernel_headers binutils gcc_static musl gcc_final file; do + printf "${BLUE}building $p...\n${RESET}" + cross_tools_$p || die "Failed building $p" +done +printf "${GREEN}finished building cross-tools${RESET}\n" diff --git a/bootstrap/stage2.sh b/bootstrap/stage2.sh new file mode 100644 index 0000000..245927b --- /dev/null +++ b/bootstrap/stage2.sh @@ -0,0 +1,553 @@ +#!/bin/sh + +set_env () { + CC="${TARGET}-gcc" + CXX="${TARGET}-g++" + AR="${TARGET}-ar" + AS="${TARGET}-as" + RANLIB="${TARGET}-ranlib" + LD="${TARGET}-ld" + STRIP="${TARGET}-strip" + export CC CXX AR AS RANLIB LD STRIP +} + +toolchain_musl () { + src "https://musl.libc.org/releases/musl-$MUSL_VER.tar.gz" + ./configure \ + CROSS_COMPILE=${TARGET}- \ + --prefix=/ \ + --target=${TARGET} && + + make && make DESTDIR=$TOOLS install && + + case $(uname -m) in + x86_64) rm $TOOLS/lib/ld-musl-x86_64.so.1 + ln -s libc.so $TOOLS/lib/ld-musl-x86_64.so.1 + export barch=$(uname -m) + ;; + i686) rm $TOOLS/lib/ld-musl-i386.so.1 + ln -s libc.so $TOOLS/lib/ld-musl-i386.so.1 + export barch=i386 + ;; + arm*) rm $TOOLS/lib/ld-musl-arm.so.1 + ln -s libc.so $TOOLS/lib/ld-musl-arm.so.1 + export barch=arm + ;; + aarch64) rm $TOOLS/lib/ld-musl-aarch64.so.1 + ln -s libc.so $TOOLS/lib/ld-musl-aarch64.so.1 + export barch=$(uname -m) + ;; + esac && + + # Create dynamic linker config + mkdir -p $TOOLS/etc && + echo "$TOOLS/lib" > $TOOLS/etc/ld-musl-${barch}.path + unset barch +} + +toolchain_cross_adjustments () { + export SPECFILE=`dirname $(${TARGET}-gcc -print-libgcc-file-name)`/specs + ${TARGET}-gcc -dumpspecs > specs + + case $(uname -m) in + x86_64) sed -i 's/\/lib\/ld-musl-x86_64.so.1/\/tools\/lib\/ld-musl-x86_64.so.1/g' specs + # check with + grep "/tools/lib/ld-musl-x86_64.so.1" specs --color=auto + ;; + i686) sed -i 's/\/lib\/ld-musl-i386.so.1/\/tools\/lib\/ld-musl-i386.so.1/g' specs + # check with + grep "/tools/lib/ld-musl-i386.so.1" specs --color=auto + ;; + arm*) sed -i 's/\/lib\/ld-musl-arm/\/tools\/lib\/ld-musl-arm/g' specs + # check with + grep "/tools/lib/ld-musl-arm" specs --color=auto + ;; + aarch64) sed -i 's/\/lib\/ld-musl-aarch64/\/tools\/lib\/ld-musl-aarch64/g' specs + # check with + grep "/tools/lib/ld-musl-aarch64" specs --color=auto + ;; + esac + + # Install modified specs to the cross toolchain + mv specs $SPECFILE + unset SPECFILE + + # Quick check the tool chain: + echo 'int main(){}' > dummy.c + ${TARGET}-gcc dummy.c + ${TARGET}-readelf -l a.out | grep Requesting + + echo + echo "Output should be:" + echo "[Requesting program interpreter: /tools/lib/ld-musl-x86_64.so.1]" + echo "or" + echo "[Requesting program interpreter: /tools/lib/ld-musl-i386.so.1]" + echo "or" + echo "[Requesting program interpreter: /tools/lib/ld-musl-arm.so.1]" + echo "or" + echo "[Requesting program interpreter: /tools/lib/ld-musl-aarch64.so.1]" + echo "Please confirm the above, and pres enter to continue:" + read confirm + + rm a.out dummy.c +} + +toolchain_adjustments () { + SPECFILE=`dirname $(${TARGET}-gcc -print-libgcc-file-name)`/specs && + ${TARGET}-gcc -dumpspecs > tempspecfile + + #case $(uname -m) in + # i686) # for i386 + # sed -i 's/\/lib\/ld-musl-i386.so.1/\/tools\/lib\/ld-musl-i386.so.1/g' tempspecfile + # # check with sed + # grep "/tools/lib/ld-musl-i386.so.1" tempspecfile --color=auto + # ;; + # x86_64) # for x86_64 + # sed -i 's/\/lib\/ld-musl-x86_64.so.1/\/tools\/lib\/ld-musl-x86_64.so.1/g' tempspecfile + # # check with + # grep "/tools/lib/ld-musl-x86_64.so.1" tempspecfile --color=auto + # ;; + # arm*) # for arm + # sed -i 's/\/lib\/ld-musl-arm/\/tools\/lib\/ld-musl-arm/g' tempspecfile + # # check with + # grep "/tools/lib/ld-musl-arm" tempspecfile --color=auto + # ;; + # aarch64) # for 64-bit arm64 + # sed -i 's/\/lib\/ld-musl-aarch64/\/tools\/lib\/ld-musl-aarch64/g' tempspecfile + # # check with + # grep "/tools/lib/ld-musl-aarch64" tempspecfile --color=auto + # ;; + #esac + + mv -f tempspecfile $SPECFILE && + unset SPECFILE + + GCC_INCLUDEDIR=`dirname $(${TARGET}-gcc -print-libgcc-file-name)`/include && + find ${GCC_INCLUDEDIR}/* -maxdepth 0 -xtype d -exec rm -rf '{}' \; && + rm -f `grep -l "DO NOT EDIT THIS FILE" ${GCC_INCLUDEDIR}/*` && + unset GCC_INCLUDEDIR + + echo 'int main(){}' > dummy.c + ${TARGET}-gcc -B/tools/lib dummy.c + readelf -l a.out | grep ': /tools' + read confirm + + rm dummy.c a.out + +} + +toolchain_binutils () { + src "https://ftp.gnu.org/gnu/binutils/binutils-$BINUTILS_VER.tar.xz" + + # Link directories so libraries can be found in both lib & lib64 + case $(uname -m) in + x86_64) ln -s lib $TOOLS/lib64 ;; + esac && + + # Configure in dedicated build directory + mkdir build && cd build && + ../configure --prefix=$TOOLS \ + --with-lib-path=$TOOLS/lib \ + --build=${HOST} \ + --host=${TARGET} \ + --target=${TARGET} \ + --disable-nls \ + --disable-werror \ + --with-sysroot && + + make && + make install && + + make -C ld clean && + make -C ld LIB_PATH=/usr/lib:/lib && + cp ld/ld-new $TOOLS/bin +} + +toolchain_gcc () { + src "https://ftp.gnu.org/gnu/mpfr/mpfr-$MPFR_VER.tar.xz" + src "https://ftp.gnu.org/gnu/gmp/gmp-$GMP_VER.tar.xz" + src "https://ftp.gnu.org/gnu/mpc/mpc-$MPC_VER.tar.gz" + src "https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VER/gcc-$GCC_VER.tar.xz" + + mv ../mpfr-$MPFR_VER mpfr && + mv ../gmp-$GMP_VER gmp && + mv ../mpc-$MPC_VER mpc && + + patch_gcc && + + # Re-create internal header + cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \ + $(dirname $($TARGET-gcc -print-libgcc-file-name))/include-fixed/limits.h && + + ## Change the location of GCC's default dynamic linker to use the one installed in /tools + # + # For i686/x86_64: + for file in gcc/config/linux/linux.h gcc/config/linux/linux64.h gcc/config/i386/linux.h gcc/config/i386/linux64.h + do + cp $file $file.orig + sed -e "s,/lib\(64\)\?\(32\)\?/ld,$TOOLS&,g" \ + -e "s,/usr,$TOOLS,g" ${file}.orig > ${file} + echo " +#undef STANDARD_STARTFILE_PREFIX_1 +#undef STANDARD_STARTFILE_PREFIX_2 +#define STANDARD_STARTFILE_PREFIX_1 \"$TOOLS/lib/\" +#define STANDARD_STARTFILE_PREFIX_2 \"\"" >> ${file} + touch ${file}.orig + done && + + # Configure in dedicated build directory + mkdir build && cd build && + CFLAGS='-g0 -O0' \ + CXXFLAGS=$CFLAGS \ + ../configure \ + --target=${TARGET} \ + --build=${HOST} \ + --host=${TARGET} \ + --prefix=$TOOLS \ + --with-local-prefix=$TOOLS \ + --with-native-system-header-dir=$TOOLS/include \ + --with-pic \ + --enable-languages=c,c++ \ + --disable-libstdcxx-pch \ + --disable-multilib \ + --disable-bootstrap \ + --disable-libgomp \ + --disable-libquadmath \ + --disable-libssp \ + --disable-libvtv \ + --disable-symvers \ + --disable-libitm \ + --disable-libsanitizer && + + PATH=$CROSS_TOOLS/bin:/bin:/usr/bin:$TOOLS/bin make && + make install && + ln -s gcc $TOOLS/bin/cc +} + +toolchain_kernel_headers () { + src "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-$LINUX_VER.tar.xz" + + make mrproper && + ARCH=${ARCH} make headers && + cp -r usr/include/* $TOOLS/include + + find $TOOLS/include -name '.*.cmd' -exec rm -f {} \; + rm $TOOLS/include/Makefile +} + +toolchain_libstdcxx () { + rm -rf ${WD} ; mkdir ${WD} + src "https://ftp.gnu.org/gnu/mpfr/mpfr-$MPFR_VER.tar.xz" + src "https://ftp.gnu.org/gnu/gmp/gmp-$GMP_VER.tar.xz" + src "https://ftp.gnu.org/gnu/mpc/mpc-$MPC_VER.tar.gz" + src "https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VER/gcc-$GCC_VER.tar.xz" + + mv ../mpfr-$MPFR_VER mpfr && + mv ../gmp-$GMP_VER gmp && + mv ../mpc-$MPC_VER mpc && + + ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/gcc-mlfs-$GCC_VER/fix_fenv_header.patch" && + patch_gcc && + + mkdir build && cd build && + ../libstdc++-v3/configure \ + --target=${TARGET} \ + --build=${HOST} \ + --host=${TARGET} \ + --prefix=$TOOLS \ + --disable-multilib \ + --disable-nls \ + --disable-libstdcxx-threads \ + --disable-libstdcxx-pch \ + --with-pic \ + --with-gxx-include-dir=$TOOLS/${TARGET}/include/c++/$GCC_VER && + + make && + make install +} + +toolchain_tcl () { + local source="https://downloads.sourceforge.net/tcl/tcl$TCL_VER-src.tar.gz" + local filename=$(basename $source) + printf "${LIGHT_BLUE}Fetching $filename...${RESET}\n" + curl ${CURL_OPTS} $source > $filename + extract $filename + cd tcl$TCL_VER + + cd unix + ac_cv_func_strtod=yes \ + tcl_cv_strtod_buggy=1 \ + ./configure --build=${HOST} \ + --host=${TARGET} \ + --prefix=${TOOLS} && + + make && make install + + chmod u+w $TOOLS/lib/libtcl${TCL_VER%.*}.so + make install-private-headers + ln -s tclsh${TCL_VER%.*} $TOOLS/bin/tclsh +} + +toolchain_expect () { + src "https://prdownloads.sourceforge.net/expect/expect$EXPECT_VER.tar.gz" + + PATCH_URL="https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/expect-5.45.3" + ptch $PATCH_URL/dont-put-toolchain-in-libpath.patch + ptch $PATCH_URL/dont-configure-testsuites.patch + ptch $PATCH_URL/allow-cross-compile.patch + + curl ${CURL_OPTS} "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/files/config.guess-musl" > tclconfig/config.guess + curl ${CURL_OPTS} "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/files/config.sub-musl" > tclconfig/config.sub + cp configure configure.orig + sed 's:/usr/local/bin:/bin:' configure.orig > configure + + ./configure --build=${HOST} \ + --host=${TARGET} \ + --prefix=$TOOLS \ + --with-tcl=$TOOLS/lib \ + --with-tclinclude=$TOOLS/include && + + make && make SCRIPTS="" install +} + +toolchain_dejagnu () { + src "https://ftp.gnu.org/gnu/dejagnu/dejagnu-$DEJAGNU_VER.tar.gz" + ./configure --build=${HOST} \ + --host=${TARGET} \ + --prefix=$TOOLS && + + make && make install +} + +toolchain_m4 () { + src "https://ftp.gnu.org/gnu/m4/m4-$M4_VER.tar.xz" + + ./configure --prefix=$TOOLS \ + --build=${HOST} \ + --host=${TARGET} && + + make && make install +} + +toolchain_ncurses () { + src "https://invisible-mirror.net/archives/ncurses/ncurses-$NCURSES_VER.tar.gz" + sed -i s/mawk// configure + + # Configure source + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} \ + --with-shared \ + --without-debug \ + --without-ada \ + --enable-widec \ + --enable-overwrite + make && make install + echo "INPUT(-lncursesw)" > ${TOOLS}/lib/libncurses.so + ln -s libncurses.so ${TOOLS}/lib/libcurses.so +} + +toolchain_bash () { + src "https://ftp.gnu.org/gnu/bash/bash-$BASH_VER.tar.gz" + + cat > config.cache << "EOF" +ac_cv_func_mmap_fixed_mapped=yes +ac_cv_func_strcoll_works=yes +ac_cv_func_working_mktime=yes +bash_cv_func_sigsetjmp=present +bash_cv_getcwd_malloc=yes +bash_cv_job_control_missing=present +bash_cv_printf_a_format=yes +bash_cv_sys_named_pipes=present +bash_cv_ulimit_maxfds=yes +bash_cv_under_sys_siglist=yes +bash_cv_unusable_rtsigs=no +gt_cv_int_divbyzero_sigfpe=yes +EOF + ./configure --prefix=${TOOLS} \ + --without-bash-malloc \ + --build=${HOST} \ + --host=${TARGET} \ + --cache-file=config.cache && + + make && make install +} + +toolchain_bison () { + src "https://ftp.gnu.org/gnu/bison/bison-$BISON_VER.tar.xz" + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} && + make && make install +} + +toolchain_coreutils () { + git clone git://git.suckless.org/sbase sbase + cd sbase + make && make PREFIX=/ DESTDIR=$TOOLS install +} + +toolchain_diffutils () { + src "https://ftp.gnu.org/gnu/diffutils/diffutils-$DIFFUTILS_VER.tar.xz" + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} + + make && make install +} + +toolchain_file () { + src "https://astron.com/pub/file/file-$FILE_VER.tar.gz" + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} + make && make install +} + +toolchain_findutils () { + src "https://ftp.gnu.org/gnu/findutils/findutils-$FINDUTILS_VER.tar.xz" + sed -i 's/IO_ftrylockfile/IO_EOF_SEEN/' gl/lib/*.c && + sed -i '/unistd/a #include ' gl/lib/mountlist.c && + echo "#define _IO_IN_BACKUP 0x100" >> gl/lib/stdio-impl.h + + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} + + make && make install +} + +toolchain_gawk () { + src "https://ftp.gnu.org/gnu/gawk/gawk-$GAWK_VER.tar.xz" + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} + make && make install +} + +toolchain_gettext () { + src "https://ftp.barfooze.de/pub/sabotage/tarballs/gettext-tiny-$GETTEXT_TINY_VER.tar.xz" + make ${MJ} LIBINTL=MUSL prefix=$TOOLS + cp msgfmt msgmerge xgettext $TOOLS/bin +} + +toolchain_grep () { + src "https://ftp.gnu.org/gnu/grep/grep-$GREP_VER.tar.xz" + + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} && + make && make install +} + +toolchain_make () { + src "https://ftp.gnu.org/gnu/make/make-$MAKE_VER.tar.gz" + + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} \ + --without-guile && + make && make install +} + +toolchain_patch () { + src "https://ftp.gnu.org/gnu/patch/patch-$PATCH_VER.tar.xz" + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} && + make && make install +} + +toolchain_sed () { + src "https://ftp.gnu.org/gnu/sed/sed-$SED_VER.tar.xz" + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} && + make && make install +} + +toolchain_perl () { + src "https://github.com/arsv/perl-cross/releases/download/$PERL_CROSS_VER/perl-cross-$PERL_CROSS_VER.tar.gz" + src "https://www.cpan.org/src/5.0/perl-$PERL_VER.tar.xz" + + cp -rf ../perl-cross-$PERL_CROSS_VER/* ./ + + ./configure --prefix=${TOOLS} \ + --target=${TARGET} && + make && + cp perl cpan/podlators/scripts/pod2man ${TOOLS}/bin && + mkdir -p ${TOOLS}/lib/perl5/$PERL_VER && + cp -r lib/* ${TOOLS}/lib/perl5/$PERL_VER +} + +toolchain_texinfo () { + src "https://ftp.gnu.org/gnu/texinfo/texinfo-$TEXINFO_VER.tar.xz" + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} && + make && make install +} + +toolchain_flex () { + src "https://github.com/westes/flex/releases/download/v$FLEX_VER/flex-$FLEX_VER.tar.gz" + + ac_cv_func_malloc_0_nonnull=yes \ + ac_cv_func_realloc_0_nonnull=yes \ + HELP2MAN=${TOOLS}/bin/true \ + ./configure --prefix=${TOOLS} \ + --build=${HOST} \ + --host=${TARGET} + make && make install +} + +toolchain_strip () { + find ${TOOLS}/lib -type f -exec strip --strip-unneeded {} \; + /usr/bin/strip --strip-unneeded ${TOOLS}/bin/* ${TOOLS}/sbin/* + + # Remove the documentation: + rm -rf ${TOOLS}/share/info \ + ${TOOLS}/share/man \ + ${TOOLS}/share/doc \ + ${TOOLS}/info \ + ${TOOLS}/man \ + ${TOOLS}/doc + + find ${TOOLS}/lib ${TOOLS}/libexec -name \*.la -exec rm -rf {} \; +} + +printf "${BLUE}building musl...\n${RESET}" +toolchain_musl || die "Failed building musl" +toolchain_cross_adjustments +for p in \ + binutils \ + gcc \ + kernel_headers \ + adjustments \ + libstdcxx \ + tcl \ + expect \ + dejagnu \ + m4 \ + ncurses \ + bash \ + bison \ + coreutils \ + diffutils \ + file \ + findutils \ + gawk \ + gettext \ + grep \ + make\ + patch \ + sed \ + texinfo \ + flex \ + ; do + + printf "${BLUE}building $p...\n${RESET}" + set_env + toolchain_$p || die "Failed building $p" +done + +printf "${GREEN}finished building toolchain${RESET}\n" diff --git a/bootstrap/stage3.sh b/bootstrap/stage3.sh new file mode 100644 index 0000000..567a4ad --- /dev/null +++ b/bootstrap/stage3.sh @@ -0,0 +1,135 @@ +#!/bin/bash + +clean_build () { + rm -rf ${chroot}/build + mkdir ${chroot}/build +} + +build_headers () { + src "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-$LINUX_VER.tar.xz" + make mrproper + + ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/kernel/include-uapi-linux-swab-Fix-potentially-missing-__always_inline.patch" + + cat > ${chroot}/build/build.sh << "EOF" +#!/bin/bash +cd /build/ +cp -r */* . + +make headers +mkdir /usr/include +cp -r usr/include/* /usr/include +find /usr/include -name '.*' -exec rm -f {} \; +rm /usr/include/Makefile +EOF + chmod +x ${chroot}/build/build.sh + tchroot /build/build.sh +} + +build_musl () { + src "https://musl.libc.org/releases/musl-$MUSL_VER.tar.gz" + ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-mlfs/fix-utmp-wtmp-paths.patch" + ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-mlfs/change-scheduler-functions-Linux-compatib.patch" + ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-alpine/0001-riscv64-define-ELF_NFPREG.patch" + ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-alpine/handle-aux-at_base.patch +" + ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-alpine/syscall-cp-epoll.patch" + curl "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/files/__stack_chk_fail_local.c" > __stack_chk_fail_local.c + + cat > ${chroot}/build/build.sh << "EOF" +#!/bin/bash +cd /build/ +cp -r */* . +LDFLAGS="$LDFLAGS -Wl,-soname,libc.musl-${CARCH}.so.1" \ +./configure --prefix=/usr \ + --sysconfdir=/etc \ + --localstatedir=/var \ + --disable-gcc-wrapper + +make && make install + + +/tools/bin/x86_64-linux-musl-gcc -fpie -c __stack_chk_fail_local.c -o __stack_chk_fail_local.o +/tools/bin/x86_64-linux-musl-gcc-ar r libssp_nonshared.a __stack_chk_fail_local.o + +cp libssp_nonshared.a /usr/lib/ + +export ARCH="x86_64" + +ln -s /lib/ld-musl-$ARCH.so.1 /bin/ldd +EOF + chmod +x ${chroot}/build/build.sh + tchroot /build/build.sh +} + +adjust_tools() { + + cat > ${chroot}/build/build.sh << "EOF" +#!/bin/bash +export TARGET="x86_64-linux-musl" +mv /tools/bin/{ld,ld-old} +mv /tools/${TARGET}/bin/{ld,ld-old} +mv /tools/bin/{ld-new,ld} +ln -s /tools/bin/ld /tools/${TARGET}/bin/ld + +export SPECFILE=`dirname $(gcc -print-libgcc-file-name)`/specs +gcc -dumpspecs | sed -e 's@/tools@@g' \ + -e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \ + -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' > tempspecfile +mv -f tempspecfile $SPECFILE && +unset SPECFILE TARGET + +echo 'int main(){}' > dummy.c +cc dummy.c -v -Wl,--verbose > dummy.log 2>&1 +readelf -l a.out | grep ': /lib' +printf "above should be:\n\033[0;33m[Requesting program interpreter: /lib/ld-musl-x86_64.so.1]\033[0m\n" +printf "######################\n" +read wait + +grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log +printf "above should be:\033[0;33m\n" +printf "/usr/lib/crt1.o succeeded\n" +printf "/usr/lib/crti.o succeeded\n" +printf "/usr/lib/crtn.o succeeded\n" +printf "\033[0m\n" +printf "######################\n" +read wait + + +grep -B1 '^ /usr/include' dummy.log +printf "above should be:\033[0;33m\n" +printf "#include <...> search starts here:\n" +printf "/usr/include\n" +printf "\033[0m\n" +printf "######################\n" +read wait + +grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g' +printf "above should be:\033[0;33m\n" +printf "SEARCH_DIR(\"=/tools/x86_64-mlfs-linux-musl/lib64\")\n" +printf "SEARCH_DIR(\"/usr/lib\")\n" +printf "SEARCH_DIR(\"/lib\")\n" +printf "SEARCH_DIR(\"=/tools/x86_64-mlfs-linux-musl/lib\")\n" +printf "\033[0m\n" +printf "######################\n" +read wait +rm dummy.c a.out dummy.log +EOF + chmod +x ${chroot}/build/build.sh + tchroot /build/build.sh +} + +umount_chroot +rm -r ${chroot} +create_chroot +mount_chroot +export WD=${chroot}/build +clean_build +build_headers +clean_build +build_musl +clean_build +adjust_tools +clean_build + +printf "${GREEN}Completed stage3\n" diff --git a/xib.sh b/xib.sh index 0d529a7..5129bef 100755 --- a/xib.sh +++ b/xib.sh @@ -201,11 +201,7 @@ xibd () { [ "$#" = 0 ] && { xib_all } || { - [ "$1" = "bootstrap" ] && { - . ./bootstrap.sh - bootstrap - exit 0 - } || for x in $@; do + for x in $@; do xib_single $x done } -- cgit v1.2.1