diff --git a/.gitignore b/.gitignore index f3ff765..8dbc205 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -./sources/ -./build/ +/sources/ +/build/ diff --git a/Makefile b/Makefile index 345b0fb..81418d2 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,15 @@ +IS_MAKE=1 + include config.mk -all: +all: build pull: @if [ ! -d ./sources ]; then \ echo "Sources Directory Not Found!"; \ mkdir -p ./sources; \ echo "Pulling Sources..."; \ - xargs -a ${SOURCES_FILE} -n1 -P4 -- curl -L -S -O --output-dir ./sources || { echo "curl failed"; exit 1; }; \ + xargs -a ${SOURCES_FILE} -n1 -P4 -- curl -f -L -S -O --output-dir ./sources || { echo "curl failed"; exit 1; }; \ echo "done"; \ else \ ./scripts/check.sh; \ @@ -15,15 +17,32 @@ pull: build: init ./scripts/musl-cross.sh + ./scripts/elftoolchain-cross.sh init: @if [ ! -d ./build/${FROOT} ]; then \ - echo "Making toolchain directory..."; \ + @echo "Making toolchain directory..."; \ mkdir -p ./build/${FROOT}; \ + @echo "Making cross directory"; \ + mkdir -p ./build/cross; \ fi + @echo "updating configuration based on config.mk..." + cp -r config.mk ./scripts/config.sh + sed -i 's/shell //g' ./scripts/config.sh clean: rm -rf ./sources rm -rf ./build -.PHONY: all pull build clean init +help: + @echo "musllvm" + @echo "-----------------------" + @echo "commands:" + @echo " all - make all (duh)" + @echo " pull - pull all sources (on first run), second run will check indivdually" + @echo " build - build will build to toolchain" + @echo " clean - clean up all sources and build artifacts" + @echo " init - sets up everything, run also every time you make changes to config.mk" + @echo " help - shows this menu" + +.PHONY: all pull build clean init help diff --git a/README.md b/README.md index f45ff67..f9c2e8e 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,10 @@ Here are some dependencies required to build this toolchain: - a C compiler (preferably clang) - curl - make +- bmake - POSIX Unix utilities (GNU utilities are acceptable) # Credits * [Chimera linux](https://chimera-linux.org) - Majority of patches are sourced from chimera linux. * [CMLFS](https://github.com/dslm4515/CMLFS) - Many of the build scripts were taken as reference for the musllvm toolchain itself +* [Mussel](https://github.com/firasuke/mussel) - Best, shortest, fastest script that builds musl cross-compiler diff --git a/config.mk b/config.mk index 76726e1..9439092 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,15 @@ # musllvm default flags +# -------------------------------- +# feel free to change these however you like +# -SOURCES_FILE=./sources.list +# +# these are shared between the Makefile and scripts +# ARCH=$(shell uname -m) +SOURCES_FILE=./sources.list FROOT="${ARCH}-musllvm" +CROSS="./build/cross" # cross directory + +# MAKEFLAGS to parse the amounto of cores used +MAKEFLAGS="-j$(shell nproc) -l$(shell nproc)" diff --git a/files/elftoolchain/elfdefinitions.h b/files/elftoolchain/elfdefinitions.h new file mode 100644 index 0000000..7644f27 --- /dev/null +++ b/files/elftoolchain/elfdefinitions.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 Daniel Kolesa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* This file is a part of Chimera Linux. It provides a version of the + * file that does not conflict with system-wide elf.h. + */ + +#ifndef _SYS_ELFDEFINITIONS_H_ +#define _SYS_ELFDEFINITIONS_H_ + +#include + +typedef Elf32_auxv_t Elf32_Cap; +typedef Elf64_auxv_t Elf64_Cap; + +#endif diff --git a/patches/elftoolchain/0001-disable-ld-build.patch b/patches/elftoolchain/0001-disable-ld-build.patch new file mode 100644 index 0000000..d6965a3 --- /dev/null +++ b/patches/elftoolchain/0001-disable-ld-build.patch @@ -0,0 +1,25 @@ +From b287a2c2b779758a31dc3902a7daf63cc0e6727f Mon Sep 17 00:00:00 2001 +From: Daniel Kolesa +Date: Wed, 23 Jun 2021 01:40:50 +0200 +Subject: [PATCH 1/2] disable ld build + +--- + Makefile | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/Makefile b/Makefile +index e8713982..cbfd9c59 100644 +--- a/Makefile ++++ b/Makefile +@@ -31,7 +31,7 @@ SUBDIR += cxxfilt + SUBDIR += elfcopy + SUBDIR += elfdump + SUBDIR += findtextrel +-SUBDIR += ld ++#SUBDIR += ld + SUBDIR += nm + SUBDIR += readelf + SUBDIR += size +-- +2.32.0 + diff --git a/patches/elftoolchain/allow-empty-elf-data.patch b/patches/elftoolchain/allow-empty-elf-data.patch new file mode 100644 index 0000000..f1aff7a --- /dev/null +++ b/patches/elftoolchain/allow-empty-elf-data.patch @@ -0,0 +1,26 @@ +From fe335f0f01aabe08df740092306ecc107ff5d96d Mon Sep 17 00:00:00 2001 +From: Michael Forney +Date: Mon, 28 Jun 2021 18:23:22 -0700 +Subject: [PATCH] Allow empty Elf_Data + +--- + libelf/elf_update.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/libelf/elf_update.c b/libelf/elf_update.c +index 3e19b78a..62f64fc1 100644 +--- a/libelf/elf_update.c ++++ b/libelf/elf_update.c +@@ -815,6 +815,9 @@ _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) + LIBELF_PRIVATE(fillchar), + (size_t) (sh_off + d->d_off - (uint64_t) rc)); + ++ if (d->d_size == 0) ++ continue; ++ + rc = (off_t) (sh_off + d->d_off); + + assert(d->d_buf != NULL); +-- +2.32.0 + diff --git a/patches/elftoolchain/cxxfilt.patch b/patches/elftoolchain/cxxfilt.patch new file mode 100644 index 0000000..742005d --- /dev/null +++ b/patches/elftoolchain/cxxfilt.patch @@ -0,0 +1,14 @@ +Using --as-needed here eliminates the libelf linkage, +which results in undefined symbols at runtime. + +--- a/cxxfilt/Makefile ++++ b/cxxfilt/Makefile +@@ -8,7 +8,7 @@ SRCS= cxxfilt.c + WARNS?= 6 + + DPADD= ${LIBELFTC} ${LIBELF} +-LDADD= -lelftc -lelf ++LDADD= -Wl,--no-as-needed -lelftc -lelf + + MAN1= c++filt.1 + diff --git a/patches/elftoolchain/fix-version.patch b/patches/elftoolchain/fix-version.patch new file mode 100644 index 0000000..a0db35c --- /dev/null +++ b/patches/elftoolchain/fix-version.patch @@ -0,0 +1,11 @@ +--- elftoolchain-f27fcce314b91b3dece6bee90949183f7a1e18b3/libelftc/make-toolchain-version 2022-05-06 06:22:42.000000000 -0500 ++++ elftoolchain-0.7.1/libelftc/make-toolchain-version 2023-10-07 19:26:54.758190500 -0500 +@@ -42,7 +42,7 @@ + # - Otherwise, we use `git --describe'. + get_revision_string() + { +- v="unknown:unknown" ++ v="0.7.1:svn-20230416" + if [ -d CVS ]; then # Look for CVS (NetBSD). + v="cvs:unknown" + elif [ -d .svn ]; then # An SVN checkout (SourceForge or FreeBSD). diff --git a/patches/elftoolchain/use-symlinks.patch b/patches/elftoolchain/use-symlinks.patch new file mode 100644 index 0000000..8ee9b68 --- /dev/null +++ b/patches/elftoolchain/use-symlinks.patch @@ -0,0 +1,39 @@ +From 974e9f2d745c4c20a6f68b54e7f9c1d38078434c Mon Sep 17 00:00:00 2001 +From: Daniel Kolesa +Date: Fri, 5 Nov 2021 22:21:54 +0100 +Subject: [PATCH] use symlinks for binaries + +--- + ar/Makefile | 2 +- + elfcopy/Makefile | 2 +- + 2 files changed, 2 insertions(+), 2 deletions(-) + +diff --git a/ar/Makefile b/ar/Makefile +index cfbaac3..ae49132 100644 +--- a/ar/Makefile ++++ b/ar/Makefile +@@ -14,7 +14,7 @@ LDADD= -larchive -lelftc -lelf -lz + + CFLAGS+=-I. -I${.CURDIR} + +-LINKS= ${BINDIR}/ar ${BINDIR}/ranlib ++SYMLINKS= ${BINDIR}/ar ${BINDIR}/ranlib + + EXTRA_TARGETS= ranlib + +diff --git a/elfcopy/Makefile b/elfcopy/Makefile +index a73515a..ca6e03c 100644 +--- a/elfcopy/Makefile ++++ b/elfcopy/Makefile +@@ -31,7 +31,7 @@ MLINKS= elfcopy.1 objcopy.1 + + NO_SHARED?= yes + +-LINKS= ${BINDIR}/elfcopy ${BINDIR}/mcs \ ++SYMLINKS= ${BINDIR}/elfcopy ${BINDIR}/mcs \ + ${BINDIR}/elfcopy ${BINDIR}/objcopy \ + ${BINDIR}/elfcopy ${BINDIR}/strip + +-- +2.33.1 + diff --git a/scripts/check.sh b/scripts/check.sh index 9b88d98..316ab33 100755 --- a/scripts/check.sh +++ b/scripts/check.sh @@ -1,7 +1,7 @@ #!/bin/sh set -eu -SOURCES_FILE=./sources.list +source ./scripts/config.sh while IFS= read -r url; do \ [ -z "$url" ] && continue; \ @@ -10,6 +10,6 @@ if [ -e "./sources/$fname" ]; then \ printf "SKIP: %s (already exists)\n" "$fname"; \ else \ printf "GET: %s -> %s\n" "$url" "$fname"; \ - curl -o "./sources/$fname" "$url" || { printf "ERROR: failed to download %s\n" "$url"; exit 1; }; \ + curl -f -L -O -S --output-dir "./sources/" "$url" || { printf "ERROR: failed to download %s\n" "$url"; exit 1; }; \ fi; \ done < "$SOURCES_FILE"; \ diff --git a/scripts/config.sh b/scripts/config.sh new file mode 100755 index 0000000..7b429ec --- /dev/null +++ b/scripts/config.sh @@ -0,0 +1,15 @@ +# musllvm default flags +# -------------------------------- +# feel free to change these however you like +# + +# +# these are shared between the Makefile and scripts +# +ARCH=$(uname -m) +SOURCES_FILE=./sources.list +FROOT="${ARCH}-musllvm" +CROSS="./build/cross" # cross directory + +# MAKEFLAGS to parse the amounto of cores used +MAKEFLAGS="-j$(nproc) -l$(nproc)" diff --git a/scripts/elftoolchain-cross.sh b/scripts/elftoolchain-cross.sh new file mode 100755 index 0000000..dc18989 --- /dev/null +++ b/scripts/elftoolchain-cross.sh @@ -0,0 +1,42 @@ +#!/bin/sh +set -eu + +source ./scripts/config.sh + +ELF_TAR=$(ls ./sources/elftoolchain-*.tar.* 2>/dev/null | head -n 1 | xargs basename) +if [ -z "$ELF_TAR" ]; then + echo "ERR: No Musl tarball found in sources directory" + exit 1 +fi +ELF_SOURCE="${ELF_TAR%%.tar.*}" + +if command -v "clang" >/dev/null 2>&1 && command -v "lld" >/dev/null 2>&1; then + export CC="clang" +else + export CC="cc" +fi + +if [ ! -d ./build/"$ELF_SOURCE" ]; then + echo "Extracting Elftoolchain tarball to ./build/" + tar -xpf ./sources/"$ELF_TAR" -C ./build +else + echo "Elftoolchain source already extracted." +fi + +echo "Buliding musl headers" +if [ -f "./build/$ELF_SOURCE" ]; then + echo "ELFTOOLCHAIN already built, skipping..." + exit +fi + +cd ./build/"$ELF_SOURCE" + +echo "patching elftoolchain-cross..." + +patch -Np1 < ../../patches/elftoolchain/* + +echo "Installing elftoolchain-cross..." + +bmake WITH_ADDITIONAL_DOCUMENTATION=no \ + WITH_TESTS=no \ + MANTARGET=man diff --git a/scripts/headers.sh b/scripts/headers.sh index 999f659..b315b37 100755 --- a/scripts/headers.sh +++ b/scripts/headers.sh @@ -1,8 +1,8 @@ #!/bin/sh set -eu -ARCH=$(uname -m) -FROOT="$ARCH-musllvm" +source ./scripts/config.sh + LINUX_TAR=$(ls ./sources/linux-*.tar.* 2>/dev/null | head -n 1 | xargs basename) if [ -z "$LINUX_TAR" ]; then echo "ERR: No Linux tarball found in sources directory" diff --git a/scripts/musl-cross.sh b/scripts/musl-cross.sh index cd7c7c0..76f34b3 100755 --- a/scripts/musl-cross.sh +++ b/scripts/musl-cross.sh @@ -1,8 +1,8 @@ #!/bin/sh set -eu -ARCH=$(uname -m) -FROOT="../$ARCH-musllvm" # make install in source dir +source ./scripts/config.sh + MUSL_TAR=$(ls ./sources/musl-*.tar.* 2>/dev/null | head -n 1 | xargs basename) if [ -z "$MUSL_TAR" ]; then echo "ERR: No Musl tarball found in sources directory" @@ -32,4 +32,7 @@ fi cd ./build/"$MUSL_SOURCE" echo "Installing musl headers..." -DESTDIR=$FROOT make ARCH=$(uname -m) install-headers +./configure \ + --prefix=/ + +DESTDIR=../../$CROSS make ARCH=$(uname -m) install-headers diff --git a/sources.list b/sources.list index 2983f20..14afac9 100644 --- a/sources.list +++ b/sources.list @@ -2,6 +2,7 @@ https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.17.7.tar.xz https://musl.libc.org/releases/musl-1.2.5.tar.gz https://pub.sortix.org/libz/libz-1.2.8.2025.03.07.tar.gz https://github.com/chimera-linux/libatomic-chimera/archive/refs/tags/v0.90.0.tar.gz +https://sourceforge.net/projects/elftoolchain/files/Sources/elftoolchain-0.7.1/elftoolchain-0.7.1.tar.bz2 https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.2/llvm-21.1.2.src.tar.xz https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.2/cmake-21.1.2.src.tar.xz https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.2/third-party-21.1.2.src.tar.xz