stargazing/notwa-util/cosmocc

88 lines
2.5 KiB
Bash
Executable file

#!/usr/bin/env sh
cosmo="${COSMO:-"/cosmopolitan"}"
dist="${COSMO_DIST:-"$cosmo/dist/def"}"
_CFLAGS="-fno-omit-frame-pointer -fdata-sections -ffunction-sections -fno-pie -pg -mnop-mcount -mno-tls-direct-seg-refs"
_CPPFLAGS="-DNDEBUG -nostdinc -iquote $cosmo -isystem $cosmo/libc/isystem -include $cosmo/libc/integral/normalize.inc"
_LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd -Wl,-melf_x86_64 -Wl,--gc-sections -Wl,-z,max-page-size=0x1000"
_LDFLAGS="$_LDFLAGS -Wl,-T,$dist/public/ape.lds $dist/ape-no-modify-self.o $dist/crt.o"
_LDLIBS="$dist/cosmopolitan.a"
is_in() {
x="$1"
shift || exit
for y in "$@"; do
if [ "$x" = "$y" ]; then
return 0
fi
done
return 1
}
any=0
flag_c=0
flag_E=0
flag_M=0
flag_o=0
flag_shared=0
is_conftest=0
for x in "$@"; do
[ $any = 1 ] || set --
any=1
if [ "$x" = --version ]; then
printf '%s\n' \
'x86_64-linux-musl-gcc (GCC) 9.2.0' \
'Copyright (C) 2019 Free Software Foundation, Inc.' \
'This is free software; see the source for copying conditions. There is NO' \
'warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.' \
''
exit 0
elif is_in "$x" \
-fomit-frame-pointer -fPIC -fpic -lc -lc_r -lcma -ldl \
-lgcc -lm -lpthread -lpthreads -lresolv -lrt -lunwind \
-lutil -Wl,-Bdynamic -Wl,-Bstatic
then continue
elif [ "$x" = -c ]; then
flag_c=1
elif [ "$x" = -E ]; then
flag_E=1
elif [ "$x" = -M ] || [ "$x" = -MM ]; then
flag_M=1
elif [ "$x" = -shared ]; then
flag_shared=1
elif [ "$x" = -o ] || [ "${x#-o}" != "$x" ]; then
flag_o=1
elif [ "$x" = conftest.c ]; then
is_conftest=1
fi
set -- "$@" "$x"
done
if [ $flag_E = 1 ] || [ $flag_M = 1 ]; then
# only preprocess.
set -- $_CPPFLAGS "$@"
elif [ $flag_c = 1 ]; then
# preprocess and compile.
set -- $_CFLAGS $_CPPFLAGS "$@"
else
# preprocess, compile, and link.
# TODO: but then where's $_CFLAGS?
set -- $_LDFLAGS $_CPPFLAGS "$@" $_LDLIBS
fi
if [ $any = 0 ]; then
printf '\033[1m%s\033[m\n' >&2 'warning: cosmocc called without flags'
x86_64-linux-musl-gcc
else
if [ -w / ]; then
# probably a container being built.
printf '%s\n' >>/cosmocc.log "$*"
fi
if [ $is_conftest = 1 ]; then
x86_64-linux-musl-gcc -fmax-errors=3 >/tmp/fudged "$@" || exit
sed -e '/^typedef struct {/,/}/d' -e '/^typedef/d' /tmp/fudged
else
x86_64-linux-musl-gcc "$@"
fi
fi