| git.druid.rocks | index | druid520 | ports | ports/ | compilers/ | chibicc/ | add.sh |
ports/compilers/chibicc/add.sh
set -e
git clone --depth 1 https://github.com/rui314/chibicc.git
cd chibicc
# musl's own <bits/alltypes.h> (reached from <stdio.h> and friends, not
# just from <stdarg.h> itself) defines va_list via the gcc/clang compiler
# builtin "__builtin_va_list", a type chibicc's parser has no idea about --
# chibicc's own bundled include/stdarg.h sidesteps this with its own
# struct-based va_list, but that only helps code that includes <stdarg.h>
# directly; musl's <stdio.h> reaches the same problematic typedef through
# <bits/alltypes.h>, bypassing chibicc's compat header entirely. per the
# still-open upstream issue (rui314/chibicc#64): add a
# "typedef va_list __builtin_va_list;" to chibicc's own stdarg.h so
# __builtin_va_list becomes a known alias once that header has been
# processed, then force it to always be processed first via "-include" on
# every invocation (chibicc has no built-in way to inject this itself,
# hence the wrapper script below instead of installing the real binary
# straight to $MP_PREFIX/bin).
printf '\ntypedef va_list __builtin_va_list;\n' >> include/stdarg.h
# main.c's own linker driver hardcodes glibc/Debian-multiarch-only crt
# search paths (find_libpath/find_gcc_libpath) and a glibc dynamic linker
# path -- none of which exist on a musl host, so linking fails outright
# ("library path is not found") even once the parse error above is fixed.
# chibicc has no configure step or command-line override for any of this
# (unlike tcc's --config-musl), so patch the three exact spots directly:
# add musl's real crti.o location as a fallback, add a glob for any *-musl
# gcc target dir to the crtbegin.o search list, and swap the hardcoded
# ELF interpreter for musl's.
perl -0777 -pi -e '
s/(if \(file_exists\("\/usr\/lib64\/crti\.o"\)\)\n return "\/usr\/lib64";\n)/ if (file_exists("\/usr\/lib\/crti.o"))\n return "\/usr\/lib";\n$1/
or die "chibicc musl patch: libpath site not found -- upstream main.c changed?\n";
s/(char \*paths\[\] = \{\n)/$1 "\/usr\/lib\/gcc\/*-linux-musl\/*\/crtbegin.o",\n/
or die "chibicc musl patch: gcc libpath site not found -- upstream main.c changed?\n";
my $n = (s/\/lib64\/ld-linux-x86-64\.so\.2/\/lib\/ld-musl-x86_64.so.1/g);
$n > 0 or die "chibicc musl patch: dynamic-linker site not found -- upstream main.c changed?\n";
' main.c
make CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS"
mkdir -p $MP_PREFIX/libexec/chibicc/include
cp chibicc $MP_PREFIX/libexec/chibicc/chibicc
chmod +x $MP_PREFIX/libexec/chibicc/chibicc
cp include/*.h $MP_PREFIX/libexec/chibicc/include/
cat > $MP_PREFIX/bin/chibicc <<WRAP
#!/bin/sh
exec "$MP_PREFIX/libexec/chibicc/chibicc" -include "$MP_PREFIX/libexec/chibicc/include/stdarg.h" "\$@"
WRAP
chmod +x $MP_PREFIX/bin/chibicc
make clean || true