Introduce new fatcosmocc command

This new script is an alternative to the `cosmocc` command. It's still a
work in progress. It abstracts all the gory details of building separate
copies of your executable and then running the apelink.com program.
This commit is contained in:
Justine Tunney 2023-08-11 22:52:11 -07:00
parent 8fc778162e
commit d53c335a45
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
35 changed files with 1151 additions and 525 deletions

View file

@ -1,216 +0,0 @@
#!/bin/sh
#
# cosmopolitan c++ compiler
#
# getting started
#
# sudo chmod 1777 /opt
# git clone https://github.com/jart/cosmopolitan /opt/cosmo
# (cd /opt/cosmo; make -j8 toolchain)
# sudo ln -sf /opt/cosmo/tool/scripts/cosmocc /usr/local/bin/cosmocc
# sudo ln -sf /opt/cosmo/tool/scripts/cosmoc++ /usr/local/bin/cosmoc++
# cosmoc++ -o hello.com hello.cc
# ./foo.com
# ./foo.com.dbg
#
# building open source projects
#
# export CC=cosmocc
# export CXX=cosmoc++
# ./configure --prefix=/opt/cosmos
# make -j
# make install
#
# cosmopolitan runtime flags
#
# ./hello.com --strace
# ./hello.com --ftrace
#
# cosmpolitan runtime libraries
#
# #include <cosmo.h>
# int main() {
# ShowCrashReports();
# __builtin_trap();
# }
#
# building in tiny mode
#
# export MODE=tiny
# (cd /opt/cosmo; make -j8 toolchain)
# cosmoc++ -Os -o foo.com foo.cc
#
# building in tiniest mode (linux only)
#
# export MODE=tinylinux
# (cd /opt/cosmo; make -j8 toolchain)
# cosmoc++ -Os -o foo.com foo.c
#
# hardening programs with memory safety
#
# export MODE=asan
# (cd /opt/cosmo; make -j8 toolchain)
# cosmoc++ -o foo.com foo.cc
#
MODE=${MODE:-$m}
COSMO=${COSMO:-/opt/cosmo}
COSMOS=${COSMOS:-/opt/cosmos}
if [ "$1" = "--version" ]; then
cat <<'EOF'
x86_64-unknown-cosmo-g++ (GCC) 11.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.
EOF
exit 0
fi
ORIGINAL="$0 $*"
PLATFORM="-D__COSMOPOLITAN__"
PREDEF="-include libc/integral/normalize.inc"
CCFLAGS="-fdata-sections -ffunction-sections -fno-pie -mno-tls-direct-seg-refs -mno-red-zone -fportcosmo"
CXXFLAGS="-fno-rtti -fno-exceptions -fuse-cxa-atexit -fno-threadsafe-statics"
CPPFLAGS="-nostdinc -iquote $COSMO -isystem $COSMOS/include -isystem $COSMO/libc/isystem"
LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd -Wl,-melf_x86_64"
APEFLAGS="-L$COSMOS/lib -Wl,--gc-sections -Wl,-T,$COSMO/o/$MODE/ape/public/ape.lds $COSMO/o/$MODE/ape/ape-no-modify-self.o $COSMO/o/$MODE/libc/crt/crt.o"
LDLIBS="$COSMO/o/$MODE/third_party/libcxx/libcxx.a $COSMO/o/$MODE/cosmopolitan.a"
CXX="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-g++"
STRIP="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-strip"
OBJCOPY="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-objcopy"
FIXUPOBJ="$COSMO/o/$MODE/tool/build/fixupobj.com"
ZIPCOPY="$COSMO/o/$MODE/tool/build/zipcopy.com"
if [ ! -d "$COSMO" ]; then
echo "you need to checkout cosmopolitan to your $COSMO directory" >&2
exit 1
fi
if [ ! -d "$COSMOS" ]; then
echo "you need to create your $COSMOS directory" >&2
exit 1
fi
if [ ! -f "$CXX" ] ||
[ ! -f "$OBJCOPY" ] ||
[ ! -f "$FIXUPOBJ" ] ||
[ ! -f "$ZIPCOPY" ]; then
echo "error: cosmopolitan artifacts missing; please run" >&2
echo " cd $COSMOS" >&2
echo " make -j8 m=$MODE toolchain" >&2
exit 1
fi
# auto-install some shell libraries
if [ ! -d "$COSMOS/lib" ]; then
mkdir -p "$COSMOS/lib"
fi
for lib in c dl gcc_s m pthread resolv rt dl z stdc++; do
if [ ! -f "$COSMOS/lib/lib$lib.a" ]; then
printf '\041\074\141\162\143\150\076\012' >"$COSMOS/lib/lib$lib.a"
fi
done
OPT=
FIRST=1
OUTPUT=
SFLAG=0
INTENT=ld
NEED_OUTPUT=
RELOCATABLE=0
FRAME=-fno-omit-frame-pointer
for x; do
if [ $FIRST -eq 1 ]; then
set --
FIRST=0
fi
if [ x"$x" = x"-Werror" ]; then
# this toolchain is intended for building other people's code
# elevating warnings into errors, should only be done by devs
continue
elif [ x"$x" = x"-pedantic" ]; then
# this toolchain is intended for building other people's code
# we don't need the compiler's assistance to be more portable
continue
elif [ x"$x" = x"-static-libstdc++" ]; then
continue
elif [ x"$x" = x"-static-libgcc" ]; then
continue
elif [ x"$x" != x"${x#-O}" ]; then
OPT=$x
elif [ x"$x" = x"-c" ]; then
INTENT=cc
elif [ x"$x" = x"-s" ]; then
SFLAG=1
continue
elif [ x"$x" = x"-r" ]; then
RELOCATABLE=1
elif [ x"$x" = x"-E" ]; then
INTENT=cpp
elif [ x"$x" = x"-o" ]; then
NEED_OUTPUT=1
elif [ x"$x" != x"${x#-o}" ]; then
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
fi
set -- "$@" "$x"
done
if [ $RELOCATABLE -eq 1 ]; then
APEFLAGS="-r"
fi
if [ x"$MODE" = x"nox87" ]; then
CCFLAGS="$CCFLAGS -mlong-double-64"
fi
if [ x"$OPT" != x"-Os" ] && [ x"${MODE#tiny}" != x"${MODE}" ]; then
# support --ftrace unless optimizing for size
CXXFLAGS="$CXXFLAGS -fpatchable-function-entry=18,16"
fi
if [ $INTENT = cpp ]; then
set -- $PLATFORM $CCFLAGS $CPPFLAGS "$@"
elif [ $INTENT = cc ]; then
set -- $PLATFORM $PREDEF $CCFLAGS $CXXFLAGS $CPPFLAGS "$@" $FRAME
else
set -- $PLATFORM $PREDEF $CCFLAGS $CXXFLAGS $CPPFLAGS $LDFLAGS $APEFLAGS $CXXFLAGS $CPPFLAGS "$@" \
$LDLIBS -Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=4096 $FRAME
fi
set -- "$CXX" "$@"
printf '# %s\n(cd %s; %s)\n' "$ORIGINAL" "$PWD" "$*" >>/tmp/build.log
"$@" || exit
if [ -n "$OUTPUT" ] && [ -f "$OUTPUT" ]; then
if [ $INTENT = cc ] || [ $INTENT = ld ]; then
"$FIXUPOBJ" "$OUTPUT" || exit
fi
if [ $INTENT = ld ]; 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
"$ZIPCOPY" "$OUTPUT.dbg" "$OUTPUT" || exit
elif [ $SFLAG -eq 1 ]; then
"$STRIP" "$OUTPUT" || exit
fi
fi
fi

View file

@ -1,214 +0,0 @@
#!/bin/sh
#
# cosmopolitan c compiler
#
# getting started
#
# sudo chmod 1777 /opt
# git clone https://github.com/jart/cosmopolitan /opt/cosmo
# (cd /opt/cosmo; make -j8 toolchain)
# sudo ln -sf /opt/cosmo/tool/scripts/cosmocc /usr/local/bin/cosmocc
# sudo ln -sf /opt/cosmo/tool/scripts/cosmoc++ /usr/local/bin/cosmoc++
# cosmocc -o hello.com hello.c
# ./foo.com
# ./foo.com.dbg
#
# building open source projects
#
# export CC=cosmocc
# export CXX=cosmoc++
# ./configure --prefix=/opt/cosmos
# make -j
# make install
#
# cosmopolitan runtime flags
#
# ./hello.com --strace
# ./hello.com --ftrace
#
# cosmpolitan runtime libraries
#
# #include <cosmo.h>
# int main() {
# ShowCrashReports();
# __builtin_trap();
# }
#
# building in tiny mode
#
# export MODE=tiny
# (cd /opt/cosmo; make -j8 toolchain)
# cosmocc -Os -o foo.com foo.c
#
# building in tiniest mode (linux only)
#
# export MODE=tinylinux
# (cd /opt/cosmo; make -j8 toolchain)
# cosmocc -Os -o foo.com foo.c
#
# hardening programs with memory safety
#
# export MODE=asan
# (cd /opt/cosmo; make -j8 toolchain)
# cosmocc -o foo.com foo.c
#
MODE=${MODE:-$m}
COSMO=${COSMO:-/opt/cosmo}
COSMOS=${COSMOS:-/opt/cosmos}
if [ "$1" = "--version" ]; then
cat <<'EOF'
x86_64-unknown-cosmo-gcc (GCC) 11.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.
EOF
exit 0
fi
ORIGINAL="$0 $*"
PLATFORM="-D__COSMOPOLITAN__"
PREDEF="-include libc/integral/normalize.inc"
CCFLAGS="-fdata-sections -ffunction-sections -fno-pie -mno-tls-direct-seg-refs -mno-red-zone -fportcosmo"
CFLAGS=
CPPFLAGS="-nostdinc -iquote $COSMO -isystem $COSMOS/include -isystem $COSMO/libc/isystem"
LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd -Wl,-melf_x86_64"
APEFLAGS="-L$COSMOS/lib -Wl,--gc-sections -Wl,-T,$COSMO/o/$MODE/ape/public/ape.lds $COSMO/o/$MODE/ape/ape-no-modify-self.o $COSMO/o/$MODE/libc/crt/crt.o"
LDLIBS="$COSMO/o/$MODE/cosmopolitan.a"
CC="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-gcc"
STRIP="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-strip"
OBJCOPY="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-objcopy"
FIXUPOBJ="$COSMO/o/$MODE/tool/build/fixupobj.com"
ZIPCOPY="$COSMO/o/$MODE/tool/build/zipcopy.com"
if [ ! -d "$COSMO" ]; then
echo "you need to checkout cosmopolitan to your $COSMO directory" >&2
exit 1
fi
if [ ! -d "$COSMOS" ]; then
echo "you need to create your $COSMOS directory" >&2
exit 1
fi
if [ ! -f "$CC" ] ||
[ ! -f "$OBJCOPY" ] ||
[ ! -f "$FIXUPOBJ" ] ||
[ ! -f "$ZIPCOPY" ]; then
echo "error: cosmopolitan artifacts missing; please run" >&2
echo " cd $COSMOS" >&2
echo " make -j8 m=$MODE toolchain" >&2
exit 1
fi
# auto-install some shell libraries
if [ ! -d "$COSMOS/lib" ]; then
mkdir -p "$COSMOS/lib"
fi
for lib in c dl gcc_s m pthread resolv rt dl z stdc++; do
if [ ! -f "$COSMOS/lib/lib$lib.a" ]; then
printf '\041\074\141\162\143\150\076\012' >"$COSMOS/lib/lib$lib.a"
fi
done
OPT=
FIRST=1
OUTPUT=
SFLAG=0
INTENT=ld
NEED_OUTPUT=
RELOCATABLE=0
FRAME=-fno-omit-frame-pointer
for x; do
if [ $FIRST -eq 1 ]; then
set --
FIRST=0
fi
if [ x"$x" = x"-Werror" ]; then
# this toolchain is intended for building other people's code
# elevating warnings into errors, should only be done by devs
continue
elif [ x"$x" = x"-pedantic" ]; then
# this toolchain is intended for building other people's code
# we don't need the compiler's assistance to be more portable
continue
elif [ x"$x" = x"-static-libgcc" ]; then
continue
elif [ x"$x" != x"${x#-O}" ]; then
OPT=$x
elif [ x"$x" = x"-c" ]; then
INTENT=cc
elif [ x"$x" = x"-s" ]; then
SFLAG=1
continue
elif [ x"$x" = x"-r" ]; then
RELOCATABLE=1
elif [ x"$x" = x"-E" ]; then
INTENT=cpp
elif [ x"$x" = x"-o" ]; then
NEED_OUTPUT=1
elif [ x"$x" != x"${x#-o}" ]; then
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
fi
set -- "$@" "$x"
done
if [ $RELOCATABLE -eq 1 ]; then
APEFLAGS="-r"
fi
if [ x"$MODE" = x"nox87" ]; then
CCFLAGS="$CCFLAGS -mlong-double-64"
fi
if [ x"$OPT" != x"-Os" ] && [ x"${MODE#tiny}" != x"${MODE}" ]; then
# support --ftrace unless optimizing for size
CFLAGS="$CFLAGS -fpatchable-function-entry=18,16"
fi
if [ $INTENT = cpp ]; then
set -- $PLATFORM $CCFLAGS $CPPFLAGS "$@"
elif [ $INTENT = cc ]; then
set -- $PLATFORM $PREDEF $CCFLAGS $CFLAGS $CPPFLAGS "$@" $FRAME
else
set -- $PLATFORM $PREDEF $CCFLAGS $CFLAGS $CPPFLAGS $LDFLAGS $APEFLAGS $CFLAGS $CPPFLAGS "$@" \
$LDLIBS -Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=4096 $FRAME
fi
set -- "$CC" "$@"
printf '# %s\n(cd %s; %s)\n' "$ORIGINAL" "$PWD" "$*" >>/tmp/build.log
"$@" || exit
if [ -n "$OUTPUT" ] && [ -f "$OUTPUT" ]; then
if [ $INTENT = cc ] || [ $INTENT = ld ]; then
"$FIXUPOBJ" "$OUTPUT" || exit
fi
if [ $INTENT = ld ]; 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
"$ZIPCOPY" "$OUTPUT.dbg" "$OUTPUT" || exit
elif [ $SFLAG -eq 1 ]; then
"$STRIP" "$OUTPUT" || exit
fi
fi
fi

51
tool/scripts/fat-aarch64 Executable file
View file

@ -0,0 +1,51 @@
#!/bin/sh
# arm64 backend compiler for fatcosmocc
CC="$COSMO/o/third_party/gcc/bin/aarch64-linux-musl-gcc"
CCFLAGS="-ffixed-x18 -ffixed-x28 -mno-outline-atomics"
LDFLAGS="-static -nostdlib -no-pie -Wl,-z,norelro"
APEFLAGS="-L$COSMOS/lib/.aarch64 -Wl,--gc-sections -Wl,-T,$COSMO/o/$MODE/ape/aarch64.lds $COSMO/o/$MODE/libc/crt/crt.o"
LDLIBS="$COSMO/o/$MODE/cosmopolitan.a"
if [ x"$PROG" != x"${PROG%++}" ]; then
CC="$COSMO/o/third_party/gcc/bin/aarch64-linux-musl-g++"
CCFLAGS="$CCFLAGS -fno-rtti -fno-exceptions -fuse-cxa-atexit -fno-threadsafe-statics"
LDLIBS="$COSMO/o/$MODE/third_party/libcxx/libcxx.a $LDLIBS"
fi
OPT=
FIRST=1
OUTPUT=
INTENT=ld
for x; do
if [ $FIRST -eq 1 ]; then
set --
FIRST=0
fi
if [ x"$x" != x"${x#-O}" ]; then
OPT=$x
elif [ x"$x" = x"-c" ]; then
INTENT=cc
elif [ x"$x" != x"${x#-o}" ]; then
OUTPUT=${x#-o}
elif [ x"$x" = x"-march=native" ]; then
continue # doesn't make sense for a cross compiler
fi
set -- "$@" "$x"
done
if [ x"$OPT" != x"-Os" ] && [ x"${MODE#aarch64-tiny}" != x"${MODE}" ]; then
# support --ftrace unless optimizing for size
CCFLAGS="$CCFLAGS -fpatchable-function-entry=7,6"
fi
if [ $INTENT = cc ]; then
set -- "$CC" $CCFLAGS "$@"
else
set -- "$CC" $CCFLAGS $LDFLAGS $APEFLAGS $CPPFLAGS "$@" \
$LDLIBS -Wl,-z,common-page-size=16384 -Wl,-z,max-page-size=16384
fi
printf '# %s\n(cd %s; %s)\n' "$ORIGINAL" "$PWD" "$*" >>"${TMPDIR:-/tmp}/build.log"
"$@" || exit
"$FIXUPOBJ" "$OUTPUT" || exit

56
tool/scripts/fat-x86_64 Executable file
View file

@ -0,0 +1,56 @@
#!/bin/sh
# amd64 backend compiler for fatcosmocc
CC="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-gcc"
CCFLAGS="-mno-tls-direct-seg-refs -mno-red-zone"
LDFLAGS="-static -nostdlib -no-pie -Wl,-melf_x86_64"
APEFLAGS="-L$COSMOS/lib -Wl,--gc-sections -Wl,-T,$COSMO/o/$MODE/ape/public/ape.lds $COSMO/o/$MODE/ape/ape.o $COSMO/o/$MODE/libc/crt/crt.o"
LDLIBS="$COSMO/o/$MODE/cosmopolitan.a"
if [ x"$PROG" != x"${PROG%++}" ]; then
CC="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-g++"
CCFLAGS="$CCFLAGS -fno-rtti -fno-exceptions -fuse-cxa-atexit -fno-threadsafe-statics"
LDLIBS="$COSMO/o/$MODE/third_party/libcxx/libcxx.a $LDLIBS"
fi
OPT=
FIRST=1
OUTPUT=
INTENT=ld
for x; do
if [ $FIRST -eq 1 ]; then
set --
FIRST=0
fi
if [ x"$x" != x"${x#-O}" ]; then
OPT=$x
elif [ x"$x" = x"-c" ]; then
INTENT=cc
elif [ x"$x" != x"${x#-o}" ]; then
OUTPUT=${x#-o}
elif [ x"$x" = x"-march=native" ]; then
continue # doesn't make sense for a cross compiler
fi
set -- "$@" "$x"
done
if [ x"$MODE" = x"nox87" ]; then
CCFLAGS="$CCFLAGS -mlong-double-64"
fi
if [ x"$OPT" != x"-Os" ] && [ x"${MODE#tiny}" != x"${MODE}" ]; then
# support --ftrace unless optimizing for size
CCFLAGS="$CCFLAGS -fpatchable-function-entry=18,16"
fi
if [ $INTENT = cc ]; then
set -- "$CC" $CCFLAGS "$@"
else
set -- "$CC" $CCFLAGS $LDFLAGS $APEFLAGS $CPPFLAGS "$@" \
$LDLIBS -Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=4096 \
-fuse-ld=bfd
fi
printf '# %s\n(cd %s; %s)\n' "$ORIGINAL" "$PWD" "$*" >>"${TMPDIR:-/tmp}/build.log"
"$@" || exit
"$FIXUPOBJ" "$OUTPUT" || exit

14
tool/scripts/setup-cosmos Executable file
View file

@ -0,0 +1,14 @@
#!/bin/sh
COSMOS=${COSMOS:-/opt/cosmos}
for arch in "" .aarch64/; do
if [ ! -d "$COSMOS/lib/${arch}" ]; then
mkdir -p "$COSMOS/lib/${arch}"
fi
for lib in c dl gcc_s m pthread resolv rt dl z stdc++; do
if [ ! -f "$COSMOS/lib/${arch}lib${lib}.a" ]; then
printf '\041\074\141\162\143\150\076\012' >"$COSMOS/lib/${arch}lib${lib}.a"
fi
done
done