Skip to content
62 changes: 62 additions & 0 deletions bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
set -eu

LLVM_URL=https://github.com/llvm/llvm-project.git

function update_llvm () {
pushd llvm-project
git fetch --depth 1
git reset --hard origin/main
popd
}

function fetch_llvm () {
git clone --depth=1 $LLVM_URL --branch main --single-branch
}

function get_or_fetch_llvm () {
if [[ -d llvm-project ]]; then
update_llvm
else
fetch_llvm
fi;
}

function bootstrap_compiler_rt () {
#rm -rf sysroot
mkdir -p sysroot
SYSROOT=$(readlink -f sysroot)
CC=$(which clang)
CXX=$(which clang++)

#rm -rf llvm-project/compiler-rt/build
mkdir -p llvm-project/compiler-rt/build
pushd llvm-project/compiler-rt/build
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_CXX_COMPILER=$CXX \
-D CMAKE_CXX_COMPILER_TARGET=x86_64-unknown-linux-musl \
-D CMAKE_C_COMPILER=$CC \
-D CMAKE_C_COMPILER_TARGET=x86_64-unknown-linux-musl \
-D COMPILER_RT_BUILD_LIBFUZZER=NO \
-D COMPILER_RT_BUILD_MEMPROF=NO \
-D COMPILER_RT_BUILD_ORC=NO \
-D COMPILER_RT_BUILD_PROFILE=NO \
-D COMPILER_RT_BUILD_SANITIZERS=NO \
-D COMPILER_RT_BUILD_XRAY=NO \
-D COMPILER_RT_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl \
-D LLVM_ENABLE_PROJECTS="compiler-rt;" \
-D LLVM_TARGETS_TO_BUILD="X86;" \
-G Ninja \
-S ..
#-D CMAKE_INSTALL_LIBDIR=lib \
#-D CMAKE_INSTALL_PREFIX=woof \
#-D COMPILER_RT_INSTALL_PATH=woof \
#-D COMPILER_RT_INSTALL_LIBRARY_DIR=woof \
ninja compiler-rt
DESTDIR=$SYSROOT ninja install
popd
}

get_or_fetch_llvm
bootstrap_compiler_rt
./kernel.sh
./musl.sh
35 changes: 35 additions & 0 deletions kernel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
set -eu

LINUX_URL=https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

function update_linux () {
pushd linux
git fetch --depth 1
git reset --hard origin/master
popd
}

function fetch_linux () {
git clone --depth=1 $LINUX_URL --branch master --single-branch
}

function get_or_fetch_linux_kernel () {
if [[ -d linux ]]; then
update_linux
else
fetch_linux
fi;
}

function build_kernel_headers () {
if [[ -d kernel-headers ]]; then
return
fi

pushd linux
make LLVM=1 INSTALL_HDR_PATH=../kernel-headers mrproper headers_install -j$(nproc)
popd
}

get_or_fetch_linux_kernel
build_kernel_headers
56 changes: 56 additions & 0 deletions musl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
set -eu

MUSL_URL=git://git.musl-libc.org/musl

function update_musl () {
pushd musl
git fetch --depth 1
git reset --hard origin/master
popd
}

function fetch_musl () {
git clone --depth=1 $MUSL_URL --branch master --single-branch
}

function get_or_fetch_musl () {
if [[ -d musl ]]; then
update_musl
else
fetch_musl
fi;
}

function build_musl () {
# TODO: something better than this...like if we updated musl.
if [[ -d sysroot/usr/local/include ]]; then
if [[ -n "$(find sysroot/usr/local/include -type f | wc -l)" ]]; then
return
fi;
fi;

BUILTINS=$(readlink -f sysroot/usr/local/lib/linux/libclang_rt.builtins-x86_64.a)
CC=$(which clang)
SYSROOT=$(readlink -f sysroot)
rm -rf musl/build
mkdir -p musl/build
pushd musl/build
LIBCC=$BUILTINS CC=$CC ../configure \
--prefix=/usr/local/ \
--host=x86_64-unknown-linux-musl \
--syslibdir=/usr/local/lib \
--disable-static

make -j$(nproc) AR=llvm-ar RANLIB=llvm-ranlib

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
make -j$(nproc) AR=llvm-ar RANLIB=llvm-ranlib
make -j$(nproc) AR=${AR:-ar} RANLIB=${RANLIB:-ranlib}

The user can specify AR=llvm-ar RANLIB=llvm-ranlib to select it.

make DESTDIR=$SYSROOT install-libs install-headers -j$(nproc)
Comment thread
nickdesaulniers marked this conversation as resolved.

# TODO: hack
pushd $SYSROOT/usr/local/lib
ln -sf libc.so ld-musl-x86_64.so.1
popd
popd
./test_lib.sh
}

get_or_fetch_musl
build_musl
24 changes: 24 additions & 0 deletions test_lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set -eu

cat << EOF > test.c
#include <stdio.h>
int main() { puts("hello world"); }
EOF

# -fuse-ld: use LLD rather than BFD.
# -rtlib: use compiler-rt rather than libgcc.
# -Xlinker --dynamic-linker: use musl's dynamic loader as the program
# interperter.
# --target: use musl rather than glibc.
# -resource-dir: use our compiler-rt builtins.
# --sysroot: use our libc headers
clang test.c \
-fuse-ld=lld \
-rtlib=compiler-rt \
-Xlinker --dynamic-linker=sysroot/usr/local/lib/ld-musl-x86_64.so.1 \
--target=x86_64-unknown-linux-musl \
-resource-dir=sysroot/usr/local \
--sysroot=sysroot/usr/local

./a.out
rm -f test.c a.out