Cleaned the branch up

This commit is contained in:
Alexander Hill
2026-01-24 15:10:18 -05:00
parent 021c1f2cb5
commit 14e50055c0
10 changed files with 15 additions and 13 deletions

294
scripts/bootstrap.sh Executable file
View File

@@ -0,0 +1,294 @@
#!/bin/zsh -e
export MICROARCH=skylake
export TARGET=x86_64-maple-linux-musl
# Set the environment up
export ARCH=$(echo $TARGET | cut -d"-" -f1)
export BOOTSTRAP=$(pwd)/.bootstrap
export PROCS=$(nproc)
export SOURCES=$(pwd)/.treetap/sources
export SPEC=$(pwd)/sources
export AR=llvm-ar
export AS=llvm-as
if [ ! -z "$CCACHE" ]; then
export CC="$CCACHE clang"
export CXX="$CCACHE clang++"
else
export CC=clang
export CXX=clang++
fi
export CFLAGS="-fuse-ld=lld -O3 -march=$MICROARCH -pipe --sysroot=$BOOTSTRAP/root -Wno-unused-command-line-argument"
export CXXFLAGS=$CFLAGS
export RANLIB=llvm-ranlib
export LD=ld.lld
export LDFLAGS="--sysroot=$BOOTSTRAP/root"
export TREETAP=$(pwd)/scripts/treetap
export TT_DIR=$(pwd)/.treetap
export TT_MICROARCH=$MICROARCH
export TT_SYSROOT=$BOOTSTRAP/root
export TT_TARGET=$TARGET
# Fetch sources required for a bootstrap
$TREETAP fetch sources/linux/linux.spec
$TREETAP fetch sources/llvm/llvm.spec
$TREETAP fetch sources/musl/musl.spec
# Make sure both clang-tblgen and llvm-tblgen are in the PATH. ~ahill
echo -n "Verifying that clang-tblgen and llvm-tblgen are in the PATH... "
! which clang-tblgen && exit 1
! which llvm-tblgen && exit 1
echo "Done!"
# Simplified filesystem heirarchy with symlinks for compatibility
mkdir -p $BOOTSTRAP/root/{bin,boot/EFI/BOOT,dev,etc,home,lib,proc,run,sys,tmp,usr/{include,share},var/{cache,lib,log,spool,tmp}}
ln -sf bin $BOOTSTRAP/root/sbin
ln -sf ../bin $BOOTSTRAP/root/usr/bin
ln -sf ../bin $BOOTSTRAP/root/usr/sbin
ln -sf ../lib $BOOTSTRAP/root/usr/lib
ln -sf ../lib $BOOTSTRAP/root/usr/libexec
ln -sf ../run/lock $BOOTSTRAP/root/var/lock
ln -sf ../run $BOOTSTRAP/root/var/run
# Prepare for the build
mkdir -p $BOOTSTRAP/build
cd $BOOTSTRAP/build
# Define the target for Maple Linux
# NOTE: We run cut on CC and CXX just in case ccache is in use. ~ahill
cat << EOF > $BOOTSTRAP/$TARGET.cmake
set(CMAKE_ASM_COMPILER_TARGET $TARGET)
set(CMAKE_C_COMPILER $(echo $CC | cut -d" " -f2))
set(CMAKE_C_COMPILER_TARGET $TARGET)
set(CMAKE_C_FLAGS_INIT "$CFLAGS")
set(CMAKE_CXX_COMPILER $(echo $CXX | cut -d" " -f2))
set(CMAKE_CXX_COMPILER_TARGET $TARGET)
set(CMAKE_CXX_FLAGS_INIT "$CXXFLAGS")
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_LINKER_TYPE LLD)
set(CMAKE_SYSROOT "$BOOTSTRAP/root")
set(CMAKE_SYSTEM_NAME Linux)
EOF
# NOTE: CMake doesn't like dealing with ccache inside of CC/CXX, so we do this
# instead. ~ahill
if [ ! -z "$CCACHE" ]; then
cat << EOF >> $BOOTSTRAP/$TARGET.cmake
set(CMAKE_C_COMPILER_LAUNCHER $CCACHE)
set(CMAKE_CXX_COMPILER_LAUNCHER $CCACHE)
EOF
fi
# Install headers for Linux
LINUX_VERSION=$($TREETAP variable $SPEC/linux/linux.spec SRC_VERSION)
tar xf $SOURCES/linux/$LINUX_VERSION/linux-*.tar*
cd linux-*/
# NOTE: LLVM=1 is required here because GCC and other GNU tools are required in
# some places still. This allows us to use LLVM and bypass the parts that
# haven't become portable yet. ~ahill
LLVM=1 make -j $PROCS mrproper
# NOTE: We don't use the built-in headers_install target because it requires
# rsync for some reason. ~ahill
LLVM=1 make -j $PROCS headers ARCH=$ARCH
find usr/include -type f ! -name "*.h" -delete
cp -r usr/include $BOOTSTRAP/root/usr
cd ..
# Install headers for musl
MUSL_VERSION=$($TREETAP variable $SPEC/musl/musl.spec SRC_VERSION)
tar xf $SOURCES/musl/$MUSL_VERSION/musl-*.tar*
cd musl-*/
# NOTE: Patch for musl 1.2.5 to prevent a character encoding vulnerability. This
# should be safe to remove after the next release. ~ahill
patch -p1 < $BOOTSTRAP/../sources/musl/CVE-2025-26519.patch
# NOTE: We are intentionally not passing --target here because musl follows the
# GNU approach when it comes to cross-compiling. This means the build
# script prefaces the name of every build tool with the target triple
# we're compiling for. This is unnecessary for LLVM, because we can simply
# pass -target <triple> to the compiler and have it generate machine code
# for that target. ~ahill
./configure \
--bindir=/bin \
--includedir=/usr/include \
--libdir=/lib \
--prefix=/
# NOTE: The build is skipped here because we only care about the header files at
# this point. ~ahill
make -O -j $PROCS install-headers DESTDIR=$BOOTSTRAP/root
cd ..
# Build and install compiler-rt builtins
LLVM_VERSION=$($TREETAP variable $SPEC/llvm/llvm.spec SRC_VERSION)
LLVM_MAJOR_VERSION=$(echo $LLVM_VERSION | cut -d"." -f1)
tar xf $SOURCES/llvm/$LLVM_VERSION/llvm-project-*.tar*
cd llvm-project-*/
cmake -S compiler-rt/lib/builtins -B build-builtins \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$BOOTSTRAP/root/lib/clang/$LLVM_MAJOR_VERSION \
-DCMAKE_TOOLCHAIN_FILE=$BOOTSTRAP/$TARGET.cmake \
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
cmake --build build-builtins --parallel $PROCS
cmake --install build-builtins --parallel $PROCS
cd ..
# Build musl for real this time
# NOTE: LIBCC is required here because it will attempt to link with the build
# system's runtime if this is not specified. ~ahill
LIBCC="$BOOTSTRAP/root/lib/clang/$LLVM_MAJOR_VERSION/lib/linux/libclang_rt.builtins-x86_64.a" \
$TREETAP build $SPEC/musl/musl.spec
$TREETAP install $($TREETAP variable $SPEC/musl/musl.spec TT_PACKAGE) $BOOTSTRAP/root
# Include compiler-rt and musl in our environment
export CFLAGS="$CFLAGS -Qunused-arguments -rtlib=compiler-rt -Wl,--dynamic-linker=/lib/ld-musl-$ARCH.so.1"
export CXXFLAGS="$CXXFLAGS -Qunused-arguments -rtlib=compiler-rt -Wl,--dynamic-linker=/lib/ld-musl-$ARCH.so.1"
# Build the LLVM runtimes
# NOTE: When CMake tests the C++ compiler, it attempts to link libstdc++/libc++
# before it even exists, causing an error. We can bypass this by simply
# setting CMAKE_CXX_COMPILER_WORKS, therefore tricking CMake into
# performing a successful build. Yet another instance where the genie in
# the bottle does exactly what we asked, and not what we wanted. ~ahill
# NOTE: Not sure why this didn't show up before, but CMAKE_C_COMPILER_WORKS is
# is manually set because the C compiler attempts to link with libgcc_s or
# libunwind, even though it doesn't exist yet. ~ahill
cd llvm-project-*/
cmake -S runtimes -B build-runtimes \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER_WORKS=ON \
-DCMAKE_CXX_COMPILER_WORKS=ON \
-DCMAKE_INSTALL_INCLUDEDIR=$BOOTSTRAP/root/usr/include \
-DCMAKE_INSTALL_PREFIX=$BOOTSTRAP/root \
-DCMAKE_TOOLCHAIN_FILE=$BOOTSTRAP/$TARGET.cmake \
-DLIBCXX_CXX_ABI=libcxxabi \
-DLIBCXX_HAS_MUSL_LIBC=ON \
-DLIBCXX_USE_COMPILER_RT=ON \
-DLIBCXXABI_USE_COMPILER_RT=ON \
-DLIBCXXABI_USE_LLVM_UNWINDER=ON \
-DLIBUNWIND_USE_COMPILER_RT=ON \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind"
cmake --build build-runtimes --parallel $PROCS
cmake --install build-runtimes --parallel $PROCS
cd ..
# Now we can introduce libunwind and libc++ into the environment
# NOTE: clang++ attempts to build with headers from the build system rather than
# exclusively relying on the sysroot. Because of this, we must manually
# define the proper include path. ~ahill
export CFLAGS="$CFLAGS -unwindlib=libunwind"
export CXXFLAGS="$CXXFLAGS -isystem $BOOTSTRAP/root/usr/include/c++/v1 -nostdinc++ -stdlib=libc++ -unwindlib=libunwind"
# Build clang/LLVM
# NOTE: LLVM_ENABLE_ZSTD is disabled because we don't have zstd in the sysroot,
# and because I don't believe that a library created by Facebook should
# be required for an operating system to function. ~ahill
# NOTE: LLVM attempts to build its own copy of tblgen, which will cause the
# build to fail since we're cross-compiling and can't run programs built
# for another platform. LLVM_NATIVE_TOOL_DIR, LLVM_TABLEGEN, and
# CLANG_TABLEGEN are set to remedy this issue. ~ahill
# NOTE: Without CLANG_DEFAULT_LINKER, clang attempts to invoke "ld" to link
# programs, which doesn't exist on Maple Linux (at least not yet). ~ahill
# NOTE: We're using sed to remove newlines instead of dirname's own -z switch
# because -z adds a null byte, which messes with the files generated by
# LLVM's build process. ~ahill
# NOTE: Many build scripts still rely on the old Unix names for tools such as
# cc and ld to function. Because of this, we enable
# LLVM_INSTALL_BINUTILS_SYMLINKS and LLVM_INSTALL_CCTOOLS_SYMLINKS for
# compatibility's sake. ~ahill
NATIVE_TOOL_DIR=$(dirname $(which llvm-tblgen) | sed -z "s/\n//g")
cd llvm-project-*/
cmake -S llvm -B build-llvm \
-DCLANG_DEFAULT_CXX_STDLIB=libc++ \
-DCLANG_DEFAULT_LINKER=lld \
-DCLANG_DEFAULT_RTLIB=compiler-rt \
-DCLANG_DEFAULT_UNWINDLIB=libunwind \
-DCLANG_TABLEGEN=$NATIVE_TOOL_DIR/clang-tblgen \
-DCLANG_VENDOR=Maple \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$BOOTSTRAP/root \
-DCMAKE_TOOLCHAIN_FILE=$BOOTSTRAP/$TARGET.cmake \
-DLLVM_ENABLE_LIBCXX=ON \
-DLLVM_ENABLE_PROJECTS="clang;lld;llvm" \
-DLLVM_ENABLE_ZSTD=OFF \
-DLLVM_HOST_TRIPLE=$TARGET \
-DLLVM_INSTALL_BINUTILS_SYMLINKS=ON \
-DLLVM_INSTALL_CCTOOLS_SYMLINKS=ON \
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_TOOL_DIR \
-DLLVM_TABLEGEN=$NATIVE_TOOL_DIR/llvm-tblgen
cmake --build build-llvm --parallel $PROCS
cmake --install build-llvm --parallel $PROCS
# NOTE: LLVM doesn't add symlinks for clang, so we'll make them ourselves.
# ~ahill
ln -sf clang $BOOTSTRAP/root/bin/cc
ln -sf clang++ $BOOTSTRAP/root/bin/c++
cd ..
# Build remaining software with treetap
SOURCES=(coreutils dash diffutils findutils grep gzip make mawk patch sed tar)
for name in $SOURCES; do
$TREETAP fetch $SPEC/$name/$name.spec
$TREETAP build $SPEC/$name/$name.spec
$TREETAP install $($TREETAP variable $SPEC/$name/$name.spec TT_PACKAGE) $BOOTSTRAP/root
done
# Install Treetap
cp $TREETAP $BOOTSTRAP/root/bin/
# Prepare for chroot build
mkdir -p $BOOTSTRAP/root/maple/
cp $BOOTSTRAP/../scripts/rootbuild.sh $BOOTSTRAP/root/maple/
export TT_DIR=$BOOTSTRAP/root/maple/.treetap
SOURCES=(
autoconf
automake
bc
byacc
bzip2
cmake
coreutils
curl
dash
diffutils
expat
findutils
flex
fortune-mod
gettext
git
grep
groff
gzip
initramfs-tools
kmod
libarchive
libcap2
libelf
libressl
libtool
limine
linux
llvm
m4
make
mawk
muon
musl
nano
nasm
ncurses
openrc
patch
perl
pkgconf
python
sed
tar
xz
zlib
zsh
)
for name in $SOURCES; do
$TREETAP fetch $SPEC/$name/$name.spec
done
cp -r $SPEC $BOOTSTRAP/root/maple/

36
scripts/licensebar.sh Executable file
View File

@@ -0,0 +1,36 @@
#!/bin/sh -e
# Usage: ./licensebar.sh docs/SOFTWARE.md > licensebar.svg
# Yes, this is cursed, but it was the simplest way I could think of automating this. ~ahill
COPYLEFT_COUNT=$(grep "| Copyleft " $1 | wc -l)
SLIGHTLY_COPYLEFT_COUNT=$(grep "| Slightly Copyleft " $1 | wc -l)
FREE_COUNT=$(grep "| Free " $1 | wc -l)
MIXED_COUNT=$(grep "| Mixed " $1 | wc -l)
SLIGHTLY_COPYRIGHT_COUNT=$(grep "| Slightly Copyright |" $1 | wc -l)
COPYRIGHT_COUNT=$(grep "| Copyright " $1 | wc -l)
BAR_BORDER=3
BAR_HEIGHT=16
BAR_TOTAL=$(expr $COPYLEFT_COUNT + $SLIGHTLY_COPYLEFT_COUNT + $FREE_COUNT + $MIXED_COUNT + $SLIGHTLY_COPYRIGHT_COUNT + $COPYRIGHT_COUNT)
BAR_WIDTH=1024
BAR_END=$(expr $BAR_WIDTH - $BAR_BORDER)
BAR_INNER=$(expr $BAR_WIDTH - \( 2 \* $BAR_BORDER \))
BAR_OFFSET=$BAR_BORDER
render_segment() {
size=$(printf %.0f $(echo "($BAR_INNER / $BAR_TOTAL) * $2" | bc -l))
echo "<rect fill=\"$1\" height=\"$(expr $BAR_HEIGHT - \( 2 \* $BAR_BORDER \))\" width=\"$size\" x=\"$BAR_OFFSET\" y=\"$BAR_BORDER\" />"
BAR_OFFSET=$(expr $BAR_OFFSET + $size)
}
echo "<svg height=\"$BAR_HEIGHT\" version=\"1.1\" width=\"$BAR_WIDTH\" xmlns=\"http://www.w3.org/2000/svg\">"
echo "<rect fill=\"black\" height=\"100%\" width=\"100%\" />"
render_segment blue $COPYLEFT_COUNT
render_segment cornflowerblue $SLIGHTLY_COPYLEFT_COUNT
render_segment white $FREE_COUNT
render_segment mediumpurple $MIXED_COUNT
render_segment indianred $SLIGHTLY_COPYRIGHT_COUNT
render_segment crimson $COPYRIGHT_COUNT
echo "</svg>"

25
scripts/maple-chroot Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/sh
if [ -z "$1" ] && [ ! -d "$1" ]; then
echo "Usage: $0 <root>"
exit 1
fi
if mount --rbind /dev $1/dev && mount --make-rslave $1/dev; then
if mount -t proc /proc $1/proc; then
if mount --bind /run $1/run; then
if mount --rbind /sys $1/sys && mount --make-rslave $1/sys; then
if mount --rbind /tmp $1/tmp; then
SHELL=$2
[ -z "$SHELL" ] && SHELL=/bin/sh
chroot $1 $SHELL
umount -R $1/tmp
fi
umount -R $1/sys
fi
umount -R $1/run
fi
umount -R $1/proc
fi
umount -R $1/dev
fi

36
scripts/rescue.sh Executable file
View File

@@ -0,0 +1,36 @@
#!/bin/sh
# This file is made with lines from bootstrap.sh to create a shell in case the
# bootstrap fails to build for whatever reason. Building a sysroot, especially
# with LLVM, takes a stupid amount of time and it makes it unreasonable to
# rebuild after something like diffutils fails to build. ~ahill
# The following script was created with:
# sh -c "grep export scripts/bootstrap.sh | sed /CCACHE/d; echo zsh" >> scripts/rescue.sh
export MICROARCH=skylake
export TARGET=x86_64-maple-linux-musl
export ARCH=$(echo $TARGET | cut -d"-" -f1)
export BOOTSTRAP=$(pwd)/.bootstrap
export PROCS=$(nproc)
export SOURCES=$(pwd)/.treetap/sources
export SPEC=$(pwd)/sources
export AR=llvm-ar
export AS=llvm-as
export CC=clang
export CXX=clang++
export CFLAGS="-fuse-ld=lld -O3 -march=$MICROARCH -pipe --sysroot=$BOOTSTRAP/root -Wno-unused-command-line-argument"
export CXXFLAGS=$CFLAGS
export RANLIB=llvm-ranlib
export LD=ld.lld
export LDFLAGS="--sysroot=$BOOTSTRAP/root"
export TREETAP=$(pwd)/scripts/treetap
export TT_DIR=$(pwd)/.treetap
export TT_MICROARCH=$MICROARCH
export TT_SYSROOT=$BOOTSTRAP/root
export TT_TARGET=$TARGET
export CFLAGS="$CFLAGS -Qunused-arguments -rtlib=compiler-rt -Wl,--dynamic-linker=/lib/ld-musl-$ARCH.so.1"
export CXXFLAGS="$CXXFLAGS -Qunused-arguments -rtlib=compiler-rt -Wl,--dynamic-linker=/lib/ld-musl-$ARCH.so.1"
export CFLAGS="$CFLAGS -unwindlib=libunwind"
export CXXFLAGS="$CXXFLAGS -isystem $BOOTSTRAP/root/usr/include/c++/v1 -nostdinc++ -stdlib=libc++ -unwindlib=libunwind"
export TT_DIR=$BOOTSTRAP/root/maple/.treetap
zsh

79
scripts/rootbuild.sh Executable file
View File

@@ -0,0 +1,79 @@
#!/bin/sh -e
export CC=clang
export CFLAGS="-O3 -pipe"
export CXX=clang++
export CXXFLAGS=$CFLAGS
export LD=ld.lld
export TT_MICROARCH=skylake
# Temporary workaround since root is the only user. ~ahill
export FORCE_UNSAFE_CONFIGURE=1
# xz Build
# NOTE: xz is needed to run "treetap build", so we manually build. ~ahill
cd /maple
XZ_VERSION=$(treetap variable /maple/sources/xz/xz.spec SRC_VERSION)
echo -n "Bootstrapping xz... "
cd .treetap/sources/xz/$XZ_VERSION
mkdir -p $TT_MICROARCH
cd $TT_MICROARCH
tar xf ../xz-*.tar*
cd xz-*/
./configure $(treetap variable /maple/sources/xz/xz.spec TT_AUTOCONF_COMMON) --disable-static --enable-year2038 > /maple/xz.log 2>&1
make -j $(nproc) >> /maple/xz.log 2>&1
make -j $(nproc) install DESTDIR=/ > /maple/xz.log 2>&1
echo "Done!"
# libarchive Build
# NOTE: bsdcpio is needed to run "treetap build", so we manually build.
# ~ahill
cd /maple
LIBARCHIVE_VERSION=$(treetap variable /maple/sources/libarchive/libarchive.spec SRC_VERSION)
echo -n "Bootstrapping libarchive... "
cd .treetap/sources/libarchive/$LIBARCHIVE_VERSION
mkdir -p $TT_MICROARCH
cd $TT_MICROARCH
tar xf ../libarchive-*.tar*
cd libarchive-*/
./configure $(treetap variable /maple/sources/libarchive/libarchive.spec TT_AUTOCONF_COMMON) --disable-static --enable-year2038 > /maple/libarchive.log 2>&1
make -j $(nproc) > /maple/libarchive.log 2>&1
make -j $(nproc) install DESTDIR=/ > /maple/libarchive.log 2>&1
echo "Done!"
# Now we can build stuff exclusively with treetap
# NOTE: bzip2, xz, and zlib need to be built before libarchive or we will be
# missing functionality! ~ahill
# NOTE: CMake requires LibreSSL and libarchive to function properly so it is
# built after them. ~ahill
# NOTE: flex requires byacc and m4 to build. ~ahill
# NOTE: autoconf requires GNU m4 and perl to build. ~ahill
# NOTE: automake requires m4 to build. ~ahill
# NOTE: groff requires Perl to build. ~ahill
# NOTE: nasm requires autoconf and automake to build. ~ahill
# NOTE: dash requires flex and mawk to build. ~ahill
# NOTE: libelf requires zlib to build. ~ahill
# NOTE: fortune-mod requires cmake to build. ~ahill
# NOTE: nano requires ncurses to build. ~ahill
# NOTE: kmod requires autoconf, automake, libtool to build. ~ahill
# NOTE: libcap2 requires zsh to build. ~ahill
# NOTE: Linux requires bc, byacc, flex, kmod, ... ~ahill
# NOTE: Limine requires nasm to build. ~ahill
# NOTE: OpenRC requires libcap2 and muon to build. ~ahill
# NOTE: curl requires LibreSSL and zlib to build. ~ahill
# NOTE: gettext requires ncurses to build. ~ahill
# NOTE: git requires curl, expat, and gettext to build. ~ahill
# NOTE: Python requires bzip2, expat, LibreSSL, ncurses, xz, and zlib to build. ~ahill
# NOTE: LLVM requires CMake and Python to build. ~ahill
# NOTE: Ruby requires autoconf, libffi, and libyaml to build. ~ahill
# NOTE: Liquid requires Ruby to build. ~ahill
cd /maple
LAYER0="bc byacc bzip2 coreutils diffutils expat findutils grep gzip initramfs-tools libressl m4 make mawk muon musl ncurses patch perl pkgconf sed tar xz zlib zsh"
LAYER1="autoconf automake curl flex gettext groff libarchive libcap2 libelf libtool nano openrc python"
LAYER2="cmake dash fortune-mod git kmod nasm"
LAYER3="limine linux"
PACKAGES="$LAYER0 $LAYER1 $LAYER2 $LAYER3"
for pkg in $PACKAGES; do
treetap fetch /maple/sources/$pkg/$pkg.spec
treetap build /maple/sources/$pkg/$pkg.spec
treetap install $(treetap variable /maple/sources/$pkg/$pkg.spec TT_PACKAGE)
done

466
scripts/treetap Executable file
View File

@@ -0,0 +1,466 @@
#!/bin/sh -e
# Copyright (c) 2025-2026 Alexander Hill
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#############
# Changelog #
#############
# January 16, 2026 (1.5.0)
# + Added TT_PACKAGE [ahill]
# + Added variable subcommand [ahill]
# * Converted the build log path to an absolute path [ahill]
# * Merged the functionality of the package verb into the build command [ahill]
# * Packages are no longer built if the package file exists already [ahill]
# - Removed the purge subcommand in favor of clean [ahill]
# January 4, 2026 (1.4.1)
# * Set LD to ld.lld after mold was removed from Maple Linux. [ahill]
# - Replaced clean with purge, since the current implementation of clean isn't
# effective. [ahill]
# December 6, 2025 (1.4.0)
# + Added sane defaults for the environment if they are not defined [ahill]
# + Added SRC_REVISION and SRC_FULLVERSION to prevent issues related to
# SRC_VERSION [ahill]
# * Fixed an issue where uninstalls fail due to spaces in the filename [ahill]
# November 29, 2025 (1.3.1)
# + Added GNUInstallDirs to TT_CMAKE_COMMON [ahill]
# * Tweaked the messages to make it easier to see that treetap exited
# unexpectedly [ahill]
# November 24, 2025 (1.3.0)
# + Added TT_DATADIR [ahill]
# + Added TT_MESON_COMMON for easy Meson integration [ahill]
# + Added TT_RUNDIR [ahill]
# + Added TT_STATEDIR [ahill]
# * Fixed a bug where applying TT_AUTOCONF_COMMON with some versions of autoconf
# cause it to think we're cross-compiling when we're not. [ahill]
# * Switched from GNU cpio to bsdcpio to function on Maple Linux [ahill]
# November 22, 2025 (1.2.0)
# + Added support for the CCACHE environment variable [ahill]
# + Added TT_ARCH [ahill]
# + Added TT_AUTOCONF_COMMON for easy autoconf integration [ahill]
# + Added TT_CMAKE_COMMON for easy CMake integration [ahill]
# + Added TT_MICROARCH for micro-optimization support [ahill]
# * Changed the build path to use TT_MICROARCH instead of TT_TARGET [ahill]
# * Fixed "missing package" error when passing absolute paths to the install
# subcommand [ahill]
# * Prevented xz from deleting package files [ahill]
# + Started printing the version number in the build logs [ahill]
# November 15, 2025 (1.1.0)
# + Added the ability to incorporate patches into the build [ahill]
# + Added TT_CONFDIR [ahill]
# * Replaced curl with wget to rely purely on Busybox [ahill]
# November 13, 2025 (1.0.2)
# + Added the target triple to the package path [ahill]
# * Prevented fetch from re-downloading packages given a valid hash [ahill]
# * Renamed all TREETAP_* variables to TT_* [ahill]
# November 11, 2025 (1.0.1)
# - Removed bashisms to become POSIX compliant [ahill]
# November 9, 2025 (1.0.0)
# * Initial release [ahill]
########################
# .spec File Reference #
########################
# For everyone trying to build .spec files (including myself), this is the list
# of useful variables to take advantage of. ~ahill
# Specification Functions:
# build - Builds the package and installs it to $TT_INSTALLDIR
# Specification Variables:
# SRC_FILENAME - The name of the tarball to extract (optional)
# SRC_FULLVERSION - The full version of the package, which is automatically
# set to SRC_VERSION, or "(SRC_VERSION)r(SRC_REVISION)" if
# SRC_REVISION is set.
# SRC_HASH - The hash of the tarball for verification purposes
# (required)
# SRC_NAME - The name of the package being built (required)
# SRC_PATCHES - A list of SHA256 hashes followed by their filenames to be
# copied to the build directory (optional)
# SRC_REVISION - The package revision number to denote changes specifically
# made for Maple Linux (optional)
# SRC_URL - The URL of the tarball to be downloaded (required)
# SRC_VERSION - The version of the package being built (required)
# Treetap Variables:
# TT_ARCH - The architecture portion of TT_TARGET
# [scope: source]
# TT_AUTOCONF_COMMON - The default autoconf arguments based on the environment
# [scope: source]
# TT_BINDIR - The desired path for binaries
# [scope: source] [default: /bin]
# TT_BUILD - The target triple of the build system
# [scope: source]
# TT_BUILDDIR - The path to the build directory
# [scope: source]
# TT_CMAKE_COMMON - The default CMake arguments based on the environment
# [scope: source]
# TT_CONFDIR - The desired path for configuration files
# [scope: source] [default: /etc]
# TT_DATADIR - The desired path for data files
# [scope: source] [default: /usr/share]
# TT_DIR - The path to the treetap directory
# [scope: global]
# TT_INCLUDEDIR - The desired path for header files
# [scope: source] [default: /usr/include]
# TT_INSTALLDIR - The path to the install directory
# [scope: source]
# TT_LIBDIR - The desired path for libraries
# [scope: source] [default: /lib]
# TT_MESON_COMMON - The default meson arguments based on the environment
# [scope: source]
# TT_MICROARCH - The microarchitecture to optimize for
# [scope: source]
# TT_PACKAGE - The path to the package being built
# [scope: source]
# TT_PKGDIR - The path to the package directory
# [scope: global]
# TT_PREFIX - The desired prefix for the package
# [scope: source] [default: /]
# TT_PROCS - The number of processors on the build system
# [scope: source]
# TT_RUNDIR - The desired path for temporary runtime information
# [scope: source] [default: /run]
# TT_STATEDIR - The desired path for persistent runtime information
# [scope: source] [default: /var]
# TT_SYSROOT - The sysroot of the target system
# [scope: global]
# TT_TARGET - The target triple of the target system
# [scope: source]
# TT_VERSION - The version of treetap being used
# [scope: global]
####################
# Global Variables #
####################
[ -z "$TT_DIR" ] && TT_DIR="$(pwd)/.treetap"
[ -z "$TT_PKGDIR" ] && TT_PKGDIR="$TT_DIR/packages"
[ -z "$TT_SYSROOT" ] && TT_SYSROOT=/
TT_VERSION="1.5.0"
#########################
# Environment Variables #
#########################
[ -z "$AR" ] && export AR=ar
[ -z "$AS" ] && export AS=nasm
[ -z "$CC" ] && export CC=clang
[ -z "$CXX" ] && export CXX=clang++
[ -z "$LD" ] && export LD=ld.lld
[ -z "$NM" ] && export NM=nm
[ -z "$OBJCOPY" ] && export OBJCOPY=objcopy
[ -z "$OBJDUMP" ] && export OBJDUMP=objdump
[ -z "$PKG_CONFIG" ] && export PKG_CONFIG=pkgconf
[ -z "$STRIP" ] && export STRIP=strip
#####################
# Utility Functions #
#####################
# Displays the usage information for treetap
help_message() {
echo "treetap $TT_VERSION"
echo
echo "Package Commands:"
echo " $0 install <package> [sysroot]"
echo " $0 uninstall <package> [sysroot]"
echo
echo "Source Commands:"
echo " $0 build <spec>"
echo " $0 clean <spec>"
echo " $0 fetch <spec>"
echo " $0 variable <spec> <name>"
exit 1
}
# Confirms that the given strings are valid package and sysroot paths
package_check() {
[ -z "$1" ] && (echo "package_check: Missing package file"; exit 1)
[ -z "$2" ] && (echo "package_check: Missing sysroot"; exit 1)
[ ! -f "$1" ] && (echo "package_check: Package file \"$1\" not found"; exit 1)
[ ! -d "$2" ] && (echo "package_check: Sysroot \"$2\" not found"; exit 1)
case "$1" in
"/"*) PKG_FULLPATH=$1 ;;
*) PKG_FULLPATH=$(pwd)/$1 ;;
esac
true
}
# Reads the source specification and ensures it includes the required data
source_spec() {
# Sanity Check
[ -z "$1" ] && (echo "source_spec: Missing specification file"; exit 1)
[ ! -f "$1" ] && (echo "source_spec: Specification file \"$1\" not found"; exit 1)
# Zhu Li, do the thing!
. $1
# Required Fields
[ -z "$SRC_HASH" ] && (echo "source_spec: SRC_HASH is required but not defined"; exit 1)
[ -z "$SRC_NAME" ] && (echo "source_spec: SRC_NAME is required but not defined"; exit 1)
[ -z "$SRC_URL" ] && (echo "source_spec: SRC_URL is required but not defined"; exit 1)
[ -z "$SRC_VERSION" ] && (echo "source_spec: SRC_VERSION is required but not defined"; exit 1)
if [ -z "$SRC_REVISION" ]; then
SRC_FULLVERSION=$SRC_VERSION
else
SRC_FULLVERSION="${SRC_VERSION}r${SRC_REVISION}"
fi
# Optional Fields
[ -z "$SRC_FILENAME" ] && SRC_FILENAME=$(basename $SRC_URL)
# Environmental Variables
[ -z "$TT_BINDIR" ] && TT_BINDIR=/bin
TT_BUILD=$(clang -dumpmachine)
[ -z "$TT_TARGET" ] && TT_TARGET=$TT_BUILD
[ -z "$TT_CONFDIR" ] && TT_CONFDIR=/etc
[ -z "$TT_DATADIR" ] && TT_DATADIR=/usr/share
[ -z "$TT_INCLUDEDIR" ] && TT_INCLUDEDIR=/usr/include
[ -z "$TT_LIBDIR" ] && TT_LIBDIR=/lib
[ -z "$TT_PREFIX" ] && TT_PREFIX=/
[ -z "$TT_PROCS" ] && TT_PROCS=$(nproc)
[ -z "$TT_RUNDIR" ] && TT_RUNDIR=/run
[ -z "$TT_STATEDIR" ] && TT_STATEDIR=/var
# Attempt to guess TT_MICROARCH if it isn't defined
TT_ARCH=$(echo $TT_TARGET | cut -d"-" -f1)
if [ -z "$TT_MICROARCH" ]; then
case "$TT_ARCH" in
"x86_64") TT_MICROARCH="skylake";;
*)
echo "TT_MICROARCH not defined for $TT_ARCH"
exit 1
;;
esac
fi
# Apply TT_MICROARCH to CFLAGS/CXXFLAGS
CFLAGS="-march=$TT_MICROARCH $CFLAGS"
CXXFLAGS="-march=$TT_MICROARCH $CXXFLAGS"
# Last, but certainly not least, let's define where we want the build to
# occur and where to put the artifacts. ~ahill
TT_BUILDDIR="$TT_DIR/sources/$SRC_NAME/$SRC_VERSION/$TT_MICROARCH"
TT_INSTALLDIR="$TT_BUILDDIR/install"
TT_PACKAGE="$TT_PKGDIR/$TT_MICROARCH/$SRC_NAME-$SRC_FULLVERSION-$TT_MICROARCH.cpio.xz"
# Create convenience variables
TT_AUTOCONF_COMMON=$(echo "--bindir=$TT_BINDIR \
--build=$TT_BUILD \
--datarootdir=$TT_DATADIR \
--includedir=$TT_INCLUDEDIR \
--libdir=$TT_LIBDIR \
--libexecdir=$TT_LIBDIR \
--localstatedir=$TT_STATEDIR \
--prefix=$TT_PREFIX \
--runstatedir=$TT_RUNDIR \
--sbindir=$TT_BINDIR \
--sysconfdir=$TT_CONFDIR \
--with-sysroot=$TT_SYSROOT" | xargs)
# NOTE: Some versions of autoconf does not like having host/target set
# unless we're actually cross-compiling.
if [ ! "$TT_BUILD" = "$TT_TARGET" ]; then
TT_AUTOCONF_COMMON=$(echo "$TT_AUTOCONF_COMMON \
--host=$TT_TARGET \
--target=$TT_TARGET" | xargs)
fi
# TODO: What should CMAKE_INSTALL_SHAREDSTATEDIR be? ~ahill
# NOTE: TT_INSTALLDIR is used here because CMake treats / as a special case
# and sets the prefix to /usr anyways, completely defeating the point
# of setting it to / to begin with. ~ahill
# See also: https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html#special-cases
TT_CMAKE_COMMON=$(echo "-DCMAKE_ASM_COMPILER_TARGET=$TT_TARGET \
-DCMAKE_C_COMPILER_TARGET=$TT_TARGET \
-DCMAKE_CXX_COMPILER_TARGET=$TT_TARGET \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_BINDIR=$TT_INSTALLDIR$TT_BINDIR \
-DCMAKE_INSTALL_DATAROOTDIR=$TT_INSTALLDIR$TT_DATADIR \
-DCMAKE_INSTALL_INCLUDEDIR=$TT_INSTALLDIR$TT_INCLUDEDIR \
-DCMAKE_INSTALL_LIBDIR=$TT_INSTALLDIR$TT_LIBDIR \
-DCMAKE_INSTALL_LIBEXECDIR=$TT_INSTALLDIR$TT_LIBDIR \
-DCMAKE_INSTALL_LOCALSTATEDIR=$TT_INSTALLDIR$TT_STATEDIR \
-DCMAKE_INSTALL_PREFIX=$TT_INSTALLDIR$TT_PREFIX \
-DCMAKE_INSTALL_RUNSTATEDIR=$TT_INSTALLDIR$TT_RUNDIR \
-DCMAKE_INSTALL_SBINDIR=$TT_INSTALLDIR$TT_BINDIR \
-DCMAKE_INSTALL_SYSCONFDIR=$TT_INSTALLDIR$TT_CONFDIR" | xargs)
# NOTE: CMake doesn't like having a space in CC and CXX, so we manually
# define a few things if CCACHE is set. ~ahill
if [ ! -z "$CCACHE" ]; then
TT_CMAKE_COMMON=$(echo "$TT_CMAKE_COMMON \
-DCMAKE_C_COMPILER=$(echo $CC | cut -d" " -f2) \
-DCMAKE_C_COMPILER_LAUNCHER=$CCACHE \
-DCMAKE_CXX_COMPILER=$(echo $CXX | cut -d" " -f2)
-DCMAKE_CXX_COMPILER_LAUNCHER=$CCACHE" | xargs)
fi
TT_MESON_COMMON=$(echo "-Dbindir=$TT_BINDIR \
-Ddatadir=$TT_DATADIR \
-Dincludedir=$TT_INCLUDEDIR \
-Dinfodir=$TT_DATADIR/info \
-Dlibdir=$TT_LIBDIR \
-Dlibexecdir=$TT_LIBDIR \
-Dlocaledir=$TT_DATADIR/locale \
-Dlocalstatedir=$TT_STATEDIR \
-Dmandir=$TT_DATADIR/man \
-Dprefix=$TT_PREFIX \
-Dsbindir=$TT_BINDIR \
-Dsysconfdir=$TT_CONFDIR" | xargs)
true
}
###############
# Subcommands #
###############
# Installs a package to the sysroot
package_install() {
[ ! -z "$2" ] && TT_SYSROOT=$2
package_check $1 $TT_SYSROOT
echo -n "Installing $(basename $1)... "
PUSHD=$(pwd)
cd $TT_SYSROOT
bsdcpio -iJ < $PKG_FULLPATH
cd $PUSHD
exit 0
}
# Uninstalls a package from the sysroot
package_uninstall() {
[ ! -z "$2" ] && TT_SYSROOT=$2
package_check $1 $TT_SYSROOT
echo -n "Uninstalling $(basename $1)... "
PUSHD=$(pwd)
cd $TT_SYSROOT
bsdcpio -iJt < $PKG_FULLPATH | tail -n +2 | sort -r | while read path; do
if [ -d "$path" ]; then
rmdir --ignore-fail-on-non-empty "$path"
else
rm -f "$path"
fi
done
cd $PUSHD
exit 0
}
# Builds the source from the previously fetched tarball
source_build() {
source_spec $1
if [ -f "$TT_PACKAGE" ]; then
echo "Skipping build for $SRC_NAME $SRC_FULLVERSION"
exit 0
fi
mkdir -p $TT_BUILDDIR
mkdir -p $TT_INSTALLDIR
if [ ! -z "$SRC_PATCHES" ]; then
echo -n "Validating patches for $SRC_NAME $SRC_FULLVERSION... "
cd $(dirname $1)
echo $SRC_PATCHES | sha256sum -c - > /dev/null
# Is this even the right way to check a return value? ~ahill
if [ ! "$?" = "0" ]; then
echo "Failed to validate patches for $SRC_NAME $SRC_FULLVERSION for $TT_MICROARCH ($TT_TARGET)"
exit 1
fi
echo $SRC_PATCHES | while read line; do
cp $(echo $line | cut -d" " -f2) $TT_BUILDDIR/
done
echo "Done!"
fi
echo -n "Building $SRC_NAME $SRC_FULLVERSION for $TT_MICROARCH... "
PUSHD=$(pwd)
cd $TT_BUILDDIR
# Please don't use this in your build script. This is meant for
# troubleshooting purposes. ~ahill
TT_BUILD_LOG=$TT_BUILDDIR/build-$(date +%Y%m%d%H%M%S).log
echo "Build started with treetap $TT_VERSION at $(date)" > $TT_BUILD_LOG
build >> $TT_BUILD_LOG 2>&1
echo "Build finished at $(date)" >> $TT_BUILD_LOG
echo "Done!"
cd $TT_INSTALLDIR
echo -n "Archiving $SRC_NAME $SRC_FULLVERSION for $TT_MICROARCH... "
mkdir -p $TT_PKGDIR/$TT_MICROARCH
find | bsdcpio -Jo > $TT_PACKAGE
rm -rf $TT_INSTALLDIR
echo "Done!"
cd $PUSHD
exit 0
}
# Fetches and verifies the integrity of the source tarball
source_fetch() {
source_spec $1
mkdir -p $TT_BUILDDIR
PUSHD=$(pwd)
cd $TT_BUILDDIR/..
if [ -f $SRC_FILENAME ]; then
if (echo "$SRC_HASH $SRC_FILENAME" | sha256sum -c - > /dev/null); then
echo "Skipping $SRC_FILENAME"
exit 0
else
rm -f $SRC_FILENAME
fi
fi
echo -n "Fetching $SRC_FILENAME... "
wget -O $SRC_FILENAME $SRC_URL
echo "Done!"
echo -n "Verifying $SRC_FILENAME... "
echo "$SRC_HASH $SRC_FILENAME" | sha256sum -c - > /dev/null
cd $PUSHD
echo "Done!"
exit 0
}
# Purges the entire build directory for a source
source_clean() {
source_spec $1
rm -rf $TT_BUILDDIR
exit 0
}
# Prints a variable defined by treetap
source_variable() {
source_spec $1
[ -z "$2" ] && (echo "source_variable: Variable name not given"; exit 1)
eval "echo \${$2}"
exit 0
}
###############
# Entry Point #
###############
case "$1" in
"build") source_build $2 ;;
"clean") source_clean $2 ;;
"fetch") source_fetch $2 ;;
"install") package_install $2 $3 ;;
"uninstall") package_uninstall $2 $3 ;;
"variable") source_variable $2 $3 ;;
*) help_message ;;
esac