2022-09-20 11:34:03 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# replacement for cc command
|
|
|
|
#
|
|
|
|
# we assume you run the following beforehand
|
|
|
|
#
|
|
|
|
# sudo chmod 1777 /opt
|
|
|
|
# cd /opt
|
|
|
|
# git clone https://github.com/jart/cosmopolitan cosmo
|
|
|
|
# cd cosmo
|
|
|
|
# make -j
|
|
|
|
#
|
|
|
|
# you can then use it to build open source projects, e.g.
|
|
|
|
#
|
|
|
|
# export CC=cosmocc
|
|
|
|
# export CXX=cosmoc++
|
|
|
|
# export LD=cosmoc++
|
|
|
|
# ./configure --prefix=/opt/cosmos
|
|
|
|
# make -j
|
|
|
|
# make install
|
|
|
|
#
|
|
|
|
|
2022-11-14 21:45:57 +00:00
|
|
|
COSMO=/opt/cosmo
|
|
|
|
COSMOS=/opt/cosmos
|
|
|
|
|
2022-09-20 11:34:03 +00:00
|
|
|
if [ "$1" = "--version" ]; then
|
|
|
|
cat <<'EOF'
|
2023-06-05 07:37:25 +00:00
|
|
|
x86_64-unknown-cosmo-gcc (GCC) 11.2.0
|
2022-09-20 11:34:03 +00:00
|
|
|
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.
|
|
|
|
EOF
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2023-06-09 06:44:03 +00:00
|
|
|
UNDEF="-D__COSMOPOLITAN__"
|
|
|
|
PREDEF="-include libc/integral/normalize.inc"
|
2023-06-08 21:29:22 +00:00
|
|
|
CC="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-gcc"
|
|
|
|
OBJCOPY="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-objcopy"
|
2023-06-09 06:44:03 +00:00
|
|
|
CCFLAGS="-g -fdata-sections -ffunction-sections -fno-pie -mno-tls-direct-seg-refs -mno-red-zone -fportcosmo"
|
2023-06-08 21:29:22 +00:00
|
|
|
CFLAGS=
|
2023-06-09 06:44:03 +00:00
|
|
|
CPPFLAGS="-DNDEBUG -nostdinc -iquote /opt/cosmo -isystem $COSMOS/include -isystem $COSMO/libc/isystem"
|
2023-06-08 21:29:22 +00:00
|
|
|
LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd -Wl,-melf_x86_64 -Wl,--gc-sections -L$COSMOS/lib -Wl,-T,$COSMO/o/ape/public/ape.lds $COSMO/o/ape/ape-no-modify-self.o $COSMO/o/libc/crt/crt.o"
|
|
|
|
LDLIBS="$COSMO/o/cosmopolitan.a"
|
2022-09-20 11:34:03 +00:00
|
|
|
|
2023-06-08 21:29:22 +00:00
|
|
|
if [ ! -d "$COSMO" ]; then
|
|
|
|
echo "you need to checkout cosmopolitan to your $COSMO directory" >&2
|
2022-11-14 21:45:57 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-06-08 21:29:22 +00:00
|
|
|
if [ ! -d "$COSMOS" ]; then
|
|
|
|
echo "you need to create your $COSMOS directory" >&2
|
2022-11-14 21:45:57 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# auto-install some shell libraries
|
2023-06-08 21:29:22 +00:00
|
|
|
if [ ! -d "$COSMOS/lib" ]; then
|
|
|
|
mkdir "$COSMOS/lib"
|
2022-11-14 21:45:57 +00:00
|
|
|
for lib in c dl gcc_s m pthread resolv rt z stdc++; do
|
2023-06-08 21:29:22 +00:00
|
|
|
printf '\041\074\141\162\143\150\076\012' >"$COSMOS/lib/lib$lib.a"
|
2022-11-14 21:45:57 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2023-06-08 21:29:22 +00:00
|
|
|
OPT=
|
2022-09-20 11:34:03 +00:00
|
|
|
HAS_C=0
|
|
|
|
HAS_O=0
|
|
|
|
HAS_E=0
|
|
|
|
FIRST=1
|
2023-06-08 21:29:22 +00:00
|
|
|
OUTPUT=
|
|
|
|
NEED_OUTPUT=
|
|
|
|
FRAME=-fno-omit-frame-pointer
|
2022-09-20 11:34:03 +00:00
|
|
|
for x; do
|
|
|
|
if [ $FIRST -eq 1 ]; then
|
|
|
|
set --
|
|
|
|
FIRST=0
|
|
|
|
fi
|
2023-06-08 21:29:22 +00:00
|
|
|
if [ x"$x" = x"-Werror" ]; then
|
2022-11-14 21:45:57 +00:00
|
|
|
# this toolchain is intended for building other people's code
|
|
|
|
# elevating warnings into errors, should only be done by devs
|
|
|
|
continue
|
2023-06-08 21:29:22 +00:00
|
|
|
elif [ x"$x" = x"-pedantic" ]; then
|
2022-11-14 21:45:57 +00:00
|
|
|
# this toolchain is intended for building other people's code
|
|
|
|
# we don't need the compiler's assistance to be more portable
|
|
|
|
continue
|
2023-06-08 21:29:22 +00:00
|
|
|
elif [ x"$x" != x"${x#-O}" ]; then
|
|
|
|
OPT=$x
|
|
|
|
elif [ x"$x" = x"-c" ]; then
|
2022-09-20 11:34:03 +00:00
|
|
|
HAS_C=1
|
2023-06-08 21:29:22 +00:00
|
|
|
elif [ x"$x" = x"-E" ]; then
|
2022-09-20 11:34:03 +00:00
|
|
|
HAS_E=1
|
2023-06-08 21:29:22 +00:00
|
|
|
elif [ x"$x" = x"-o" ]; then
|
2022-09-20 11:34:03 +00:00
|
|
|
HAS_O=1
|
2023-06-08 21:29:22 +00:00
|
|
|
NEED_OUTPUT=1
|
|
|
|
elif [ x"$x" != x"${x#-o}" ]; then
|
|
|
|
HAS_O=1
|
|
|
|
OUTPUT=${x#-o}
|
|
|
|
elif [ -n "$NEED_OUTPUT" ]; then
|
|
|
|
NEED_OUTPUT=
|
|
|
|
OUTPUT=$x
|
|
|
|
elif [ x"$x" = x"-fpic" ]; then
|
|
|
|
continue
|
|
|
|
elif [ x"$x" = x"-fPIC" ]; then
|
|
|
|
continue
|
|
|
|
elif [ x"$x" = x"-shared" ]; then
|
|
|
|
echo "error: cosmocc -shared isn't supported" >&2
|
|
|
|
exit 1
|
|
|
|
elif [ x"$x" = x"-fomit-frame-pointer" ] || [ x"$x" = x"-fno-omit-frame-pointer" ]; then
|
|
|
|
FRAME=$x
|
|
|
|
continue
|
2022-09-20 11:34:03 +00:00
|
|
|
fi
|
|
|
|
set -- "$@" "$x"
|
|
|
|
done
|
|
|
|
|
2023-06-08 21:29:22 +00:00
|
|
|
if [ x"$OPT" != x"-Os" ]; then
|
|
|
|
# support --ftrace unless optimizing for size
|
|
|
|
CFLAGS="$CFLAGS -fpatchable-function-entry=18,16"
|
|
|
|
fi
|
|
|
|
|
|
|
|
LINKING=
|
2022-09-20 11:34:03 +00:00
|
|
|
if [ "$HAS_E" = "1" ]; then
|
2023-06-09 06:44:03 +00:00
|
|
|
set -- $UNDEF $CCFLAGS $CPPFLAGS "$@"
|
2022-09-20 11:34:03 +00:00
|
|
|
elif [ "$HAS_C" = "1" ]; then
|
2023-06-09 06:44:03 +00:00
|
|
|
set -- $UNDEF $PREDEF $CCFLAGS $CFLAGS $CPPFLAGS "$@" $FRAME
|
2022-09-20 11:34:03 +00:00
|
|
|
else
|
2023-06-08 21:29:22 +00:00
|
|
|
LINKING=1
|
2023-06-09 06:44:03 +00:00
|
|
|
set -- $UNDEF $PREDEF $LDFLAGS $CFLAGS $CPPFLAGS "$@" $LDLIBS -Wl,-z,common-page-size=65536 -Wl,-z,max-page-size=65536 $FRAME
|
2022-09-20 11:34:03 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
set -- "$CC" "$@"
|
|
|
|
printf '(cd %s; %s)\n' "$PWD" "$*" >>/tmp/build.log
|
2023-06-08 21:29:22 +00:00
|
|
|
"$@" || exit
|
|
|
|
|
|
|
|
if [ -n "$LINKING" ] && [ x"$OUTPUT" != x"" ]; then
|
|
|
|
if [ x"$OUTPUT" != x"${OUTPUT%.com}" ] ||
|
|
|
|
[ x"$OUTPUT" != x"${OUTPUT%.exe}" ]; then
|
|
|
|
# cosmocc -o foo.com ...
|
|
|
|
# -> foo.com (ape)
|
|
|
|
# -> foo.com.dbg (elf)
|
|
|
|
mv -f "$OUTPUT" "$OUTPUT.dbg" || exit
|
|
|
|
"$OBJCOPY" -S -O binary "$OUTPUT.dbg" "$OUTPUT" || exit
|
|
|
|
else
|
|
|
|
# cosmocc -o foo ...
|
|
|
|
# -> foo (elf)
|
|
|
|
# -> foo.com (ape)
|
|
|
|
# -> foo.com.dbg (elf)
|
|
|
|
cp -f "$OUTPUT" "$OUTPUT.com.dbg" || exit
|
|
|
|
"$OBJCOPY" -S -O binary "$OUTPUT" "$OUTPUT.com" || exit
|
|
|
|
fi
|
|
|
|
fi
|