#!/bin/sh set -e # Main settings export EPREFIX=/data/data/com.termux/files/gentoo PROFILE=default/linux/arm64/17.0/prefix/kernel-3.2+ ldso=/lib/ld-linux-aarch64.so.1 virtual_os_headers=sys-kernel/linux-headers virtual_libc=sys-libs/glibc # Output paths export PORTDIR="$PWD/gentoo" export ROOT="$PWD/sysroot" PKGDIR="$PWD/binpkgs" PORTAGE_LOGDIR="$PWD/portlog" # Cleanup (optional) #rm -rf "$ROOT" # Configure portage profile and gather variables EROOT="$ROOT$EPREFIX" export SYSROOT="$ROOT" export PORTAGE_CONFIGROOT="$EROOT" rm -rf "$PORTAGE_CONFIGROOT/etc/portage" mkdir -p "$PORTAGE_CONFIGROOT/etc/portage" ln -srfT "$PORTDIR/profiles/$PROFILE" "$PORTAGE_CONFIGROOT/etc/portage/make.profile" BROOT="$(portageq envvar BROOT)" CHOST="$(portageq envvar CHOST)" ARCH="$(portageq envvar ARCH)" LIBDIR="$(portageq envvar LIBDIR_$ARCH)" # Portage settings export EMERGE_LOG_DIR="${EMERGE_LOG_DIR:-$EROOT/var/log}" export PORTAGE_LOGDIR="${PORTAGE_LOGDIR:-$EROOT/var/log/portage}" export PORTAGE_TMPDIR="${PORTAGE_TMPDIR:-$EROOT/var/tmp}" export PKGDIR="${PKGDIR:-$EROOT/var/cache/distfiles}" export PORTAGE_REPO_DUPLICATE_WARN=0 cat > "$PORTAGE_CONFIGROOT/etc/portage/make.conf" << EOF ACCEPT_KEYWORDS="\$ARCH -~\$ARCH" EOF # Install patches cp -a "$PWD/patches" "$PORTAGE_CONFIGROOT/etc/portage/patches" # Configure the toolchain rm -rf "$ROOT/tmp-bin" mkdir -p "$ROOT/tmp-bin" cat > "$ROOT/tmp-bin/$CHOST-gcc" << EOF #!/bin/sh exec '$(command -v $CHOST-gcc)' --sysroot='$EROOT' -specs='$ROOT/tmp-bin/$CHOST.specs' "\$@" EOF cat > "$ROOT/tmp-bin/$CHOST-g++" << EOF #!/bin/sh exec '$(command -v $CHOST-g++)' --sysroot='$EROOT' -specs='$ROOT/tmp-bin/$CHOST.specs' "\$@" EOF cat > "$ROOT/tmp-bin/$CHOST.specs" << EOF %rename link old_link *link: %(old_link) %{!static:%{!static-pie:%{!shared:--dynamic-linker=$EPREFIX$ldso}}} EOF chmod +x "$ROOT/tmp-bin/$CHOST-gcc" "$ROOT/tmp-bin/$CHOST-g++" #export PREROOTPATH="$ROOT/tmp-bin" # doesn't work anymore cat > "$BROOT/etc/env.d/000tmp" << EOF PATH='$ROOT/tmp-bin' EOF ( unset EPREFIX ROOT SYSROOT; env-update ) export PKG_CONFIG_PATH="$EROOT/usr/$LIBDIR/pkgconfig" # Sanity check gcc_host="$(portageq best_visible "$EROOT" sys-devel/gcc | cut -d/ -f2)" gcc_build="$(unset EPREFIX ROOT SYSROOT; portageq best_version "$BROOT" "cross-$CHOST/gcc" | cut -d/ -f2)" if [ "$gcc_host" != "$gcc_build" ]; then echo "Host and build gcc mismatch!" echo "Please update your build's gcc to match" echo "host: sys-devel/$gcc_host" echo "build: cross-$CHOST/$gcc_build" exit 1 fi # Autoconf defines... export ac_cv_func_malloc_0_nonnull=yes # required by sys-process/procps (old autoconf) export ac_cv_func_realloc_0_nonnull=yes # required by sys-process/procps (old autoconf) export ac_cv_file__dev_ptmx=yes # required by dev-lang/python export ac_cv_file__dev_ptc=no # required by dev-lang/python export CHOST # required by cross-emerge if [ -n "$*" ]; then cross-emerge -vnbk -1 "$@" else cross-emerge -vnbk -O1 sys-apps/baselayout cross-emerge -vnbk -o --onlydeps-with-rdeps=n $virtual_os_headers cross-emerge -vnbk -O1 $virtual_os_headers virtual/os-headers cross-emerge -vnbk -o --onlydeps-with-rdeps=n $virtual_libc cross-emerge -vnbk -O1 $virtual_libc virtual/libc cross-emerge -vnbk -DN @system cross-emerge -c --with-bdeps=n fi # NOTE: Modified portage/package/ebuild/doebuild.py to hardcode eprefix and get # ccache to work... #unset CCACHE_DISABLE #export CCACHE_DIR="$PWD/ccache" #export FEATURES="ccache" #cross-emerge -vnbk -O $virtual_os_headers #FEATURES="ccache nostrip" CFLAGS="-O2 -ggdb" cross-emerge -v -O $virtual_libc # Cleanup rm -rf "$EROOT/var/log" "$EROOT/var/tmp" "$EROOT/var/cache" rm -rf "$ROOT/tmp-bin" rm -f "$BROOT/etc/env.d/000tmp" ( unset EPREFIX ROOT SYSROOT; env-update ) # Finalize the portage config ln -sfT "../../var/db/repos/gentoo/profiles/$PROFILE" "$PORTAGE_CONFIGROOT/etc/portage/make.profile" # Install additional files mkdir -p "$EROOT/etc" cat > "$EROOT/etc/resolv.conf" << EOF # Termux defaults ¯\\_(ツ)_/¯ nameserver 8.8.8.8 nameserver 8.8.4.4 EOF # Package up rm -f sysroot.tar sysroot.tar sysroot.tar.xz tar cf sysroot.tar -C "$ROOT" --hard-dereference . xz -9v sysroot.tar # Termux setup: # - pkg up / termux-change-repo # - pkg in openssh # - passwd # - sshd # - ifconfig # Copying the files: # - scp -P 8022 sysroot.tar.xz :~ # - ssh -p 8022 # - cd /; tar xf ~/sysroot.tar.xz # Running the environment: # - LD_PRELOAD= /data/data/com.termux/files/gentoo/bin/bash -l # First-time initialization: # - /data/data/com.termux/files/gentoo/sbin/ldconfig # - /data/data/com.termux/files/gentoo/usr/sbin/env-update # - echo "termux:x:$(id -u):$(id -g):termux:/data/data/com.termux/files/home:/data/data/com.termux/gentoo/bin/bash" >> /data/data/com.termux/files/gentoo/etc/passwd # - echo "termux::$(id -g):" >> /data/data/com.termux/files/gentoo/etc/group # - exec /data/data/com.termux/files/gentoo/bin/bash -l # - emerge-webrsync