diff options
author | davidovski <david@davidovski.xyz> | 2022-03-01 17:38:38 +0000 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2022-03-01 17:38:38 +0000 |
commit | e35245a4f72136aad1f0d5a54d005892ff2d929a (patch) | |
tree | c45b55c2bc517e484c31fef511947a799afdf78f /bootstrap | |
parent | fe55aea04e6fa6914e3df635d4771fb2297dd3bd (diff) |
fixed bootstrap
Diffstat (limited to 'bootstrap')
-rwxr-xr-x | bootstrap/bootstrap.sh | 163 | ||||
-rw-r--r-- | bootstrap/stage1.sh (renamed from bootstrap/cross_tools.sh) | 0 | ||||
-rw-r--r-- | bootstrap/stage2.sh (renamed from bootstrap/toolchain.sh) | 86 | ||||
-rw-r--r-- | bootstrap/stage3.sh | 135 |
4 files changed, 334 insertions, 50 deletions
diff --git a/bootstrap/bootstrap.sh b/bootstrap/bootstrap.sh index 191bebc..814f39a 100755 --- a/bootstrap/bootstrap.sh +++ b/bootstrap/bootstrap.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash ## VERSIONS ## @@ -48,12 +48,13 @@ CPU=x86-64 CROSS_TOOLS=/cross-tools TOOLS=/tools +chroot=$(pwd)/chroot PATH=${TOOLS}/bin:${CROSS_TOOLS}/bin:/usr/bin MAKEFLAGS="-j$(nproc)" -export CHROOT TARGET PATH WD CURL_OPTS CROSS_TOOLS TOOLS MAKEFLAGS +export chroot TARGET PATH WD CURL_OPTS CROSS_TOOLS TOOLS MAKEFLAGS unset CFLAGS CXXFLAGS @@ -63,6 +64,7 @@ die () { } extract () { + echo "extracting $1" FILE=$1 case "${FILE##*.}" in "gz" ) @@ -74,6 +76,9 @@ extract () { "zip" ) unzip $FILE ;; + "xz" ) + tar -xf $FILE + ;; * ) tar -xf $FILE ;; @@ -87,6 +92,7 @@ src () { printf "${LIGHT_BLUE}Fetching $filename...${RESET}\n" curl ${CURL_OPTS} $source > $filename + file $filename extract $filename cd ${filename%.t*} } @@ -105,6 +111,38 @@ clean () { mkdir -pv $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" @@ -145,41 +183,106 @@ patch_gcc () { #ptch $PATCH_SRC/0022-DP-Use-push-state-pop-state-for-gold-as-well-when-li.patch && } -package_chroot () { +create_chroot () { PATH=/usr/bin:/bin - local chroot=$(pwd)/chroot mkdir chroot - xi -r ${chroot} bootstrap - cp -r ${TOOLS} ${chroot}/tools - ln -s /tools/bin/bash ${chroot}/bin/sh + 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 -# TODO bad impl -if [ "$#" = "0" ]; then - clean - $0 stage1 - $0 stage2 -else - case "$1" in - stage1|cross|cross_tools) - . ./cross_tools.sh - ;; - stage2|tools|toolchain) - . ./toolchain.sh - ;; - package) - package_chroot - ;; - *) - clean - $0 stage1 - $0 stage2 - ;; - esac -fi +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/cross_tools.sh b/bootstrap/stage1.sh index 68fa048..68fa048 100644 --- a/bootstrap/cross_tools.sh +++ b/bootstrap/stage1.sh diff --git a/bootstrap/toolchain.sh b/bootstrap/stage2.sh index 5923bf3..161f799 100644 --- a/bootstrap/toolchain.sh +++ b/bootstrap/stage2.sh @@ -45,7 +45,7 @@ toolchain_musl () { unset barch } -toolchain_adjustments () { +toolchain_cross_adjustments () { export SPECFILE=`dirname $(${TARGET}-gcc -print-libgcc-file-name)`/specs ${TARGET}-gcc -dumpspecs > specs @@ -69,7 +69,7 @@ toolchain_adjustments () { esac # Install modified specs to the cross toolchain - mv -v specs $SPECFILE + mv specs $SPECFILE unset SPECFILE # Quick check the tool chain: @@ -89,7 +89,51 @@ toolchain_adjustments () { echo "Please confirm the above, and pres enter to continue:" read confirm - rm -v a.out dummy.c + 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 -rvf '{}' \; && + rm -vf `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 -v dummy.c a.out + } toolchain_binutils () { @@ -162,6 +206,7 @@ toolchain_gcc () { --prefix=$TOOLS \ --with-local-prefix=$TOOLS \ --with-native-system-header-dir=$TOOLS/include \ + --with-pic \ --enable-languages=c,c++ \ --disable-libstdcxx-pch \ --disable-multilib \ @@ -174,7 +219,7 @@ toolchain_gcc () { --disable-libitm \ --disable-libsanitizer && - PATH=/bin:/usr/bin:$CROSS_TOOLS/bin:$TOOLS/bin make && + PATH=$CROSS_TOOLS/bin:/bin:/usr/bin:$TOOLS/bin make && make install && ln -sv gcc $TOOLS/bin/cc } @@ -214,6 +259,7 @@ toolchain_libstdcxx () { --disable-nls \ --disable-libstdcxx-threads \ --disable-libstdcxx-pch \ + --with-pic \ --with-gxx-include-dir=$TOOLS/${TARGET}/include/c++/$GCC_VER && make && @@ -338,7 +384,7 @@ toolchain_bison () { toolchain_coreutils () { git clone git://git.suckless.org/sbase sbase cd sbase - make && make DESTDIR=$TOOLS install + make && make PREFIX=/ DESTDIR=$TOOLS install } toolchain_diffutils () { @@ -420,19 +466,19 @@ toolchain_sed () { 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 -vrf ../perl-cross-$PERL_CROSS_VER/* ./ -# -# ./configure --prefix=${TOOLS} \ -# --target=${TARGET} && -# make && -# cp -v perl cpan/podlators/scripts/pod2man ${TOOLS}/bin && -# mkdir -pv ${TOOLS}/lib/perl5/$PERL_VER && -# cp -Rv lib/* ${TOOLS}/lib/perl5/$PERL_VER -#} +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 -vrf ../perl-cross-$PERL_CROSS_VER/* ./ + + ./configure --prefix=${TOOLS} \ + --target=${TARGET} && + make && + cp -v perl cpan/podlators/scripts/pod2man ${TOOLS}/bin && + mkdir -pv ${TOOLS}/lib/perl5/$PERL_VER && + cp -Rv lib/* ${TOOLS}/lib/perl5/$PERL_VER +} toolchain_texinfo () { src "https://ftp.gnu.org/gnu/texinfo/texinfo-$TEXINFO_VER.tar.xz" @@ -471,12 +517,12 @@ toolchain_strip () { printf "${BLUE}building musl...\n${RESET}" toolchain_musl || die "Failed building musl" -toolchain_adjustments - #perl \ +toolchain_cross_adjustments for p in \ binutils \ gcc \ kernel_headers \ + adjustments \ libstdcxx \ tcl \ expect \ 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" |