mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-17 08:00:32 +00:00
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:
parent
8fc778162e
commit
d53c335a45
35 changed files with 1151 additions and 525 deletions
132
bin/ape-install
Executable file
132
bin/ape-install
Executable file
|
@ -0,0 +1,132 @@
|
|||
#!/bin/sh
|
||||
|
||||
PROG=${0##*/}
|
||||
MODE=${MODE:-$m}
|
||||
COSMO=${COSMO:-/opt/cosmo}
|
||||
COSMOS=${COSMOS:-/opt/cosmos}
|
||||
|
||||
if [ ! -f ape/loader.c ]; then
|
||||
cd "$COSMO" || exit
|
||||
fi
|
||||
|
||||
if [ "$(id -u)" -eq 0 ]; then
|
||||
SUDO=
|
||||
else
|
||||
SUDO=sudo
|
||||
fi
|
||||
|
||||
echo "Actually Portable Executable (APE) Installer" >&2
|
||||
echo "Author: Justine Tunney <jtunney@gmail.com>" >&2
|
||||
|
||||
################################################################################
|
||||
# INSTALL APE LOADER SYSTEMWIDE
|
||||
|
||||
if [ -f o/depend ] && make -j8 o//ape; then
|
||||
echo "successfully recompiled ape loader" >&2
|
||||
elif [ -d build/bootstrap ]; then
|
||||
# if make isn't being used then it's unlikely the user changed the sources
|
||||
# in that case the prebuilt binaries should be completely up-to-date
|
||||
echo "using prebuilt ape loader from cosmo repo" >&2
|
||||
mkdir -p o//ape || exit
|
||||
cp -af build/bootstrap/ape.elf o//ape/ape.elf || exit
|
||||
cp -af build/bootstrap/ape.macho o//ape/ape.macho || exit
|
||||
else
|
||||
echo "no cosmopolitan libc repository here" >&2
|
||||
echo "fetching ape loader from justine.lol" >&2
|
||||
mkdir -p o//ape || exit
|
||||
if command -v wget >/dev/null 2>&1; then
|
||||
wget -qO o//ape/ape.elf https://justine.lol/ape.elf || exit
|
||||
wget -qO o//ape/ape.macho https://justine.lol/ape.macho || exit
|
||||
else
|
||||
curl -Rso o//ape/ape.elf https://justine.lol/ape.elf || exit
|
||||
curl -Rso o//ape/ape.macho https://justine.lol/ape.macho || exit
|
||||
fi
|
||||
chmod +x o//ape/ape.elf || exit
|
||||
chmod +x o//ape/ape.macho || exit
|
||||
fi
|
||||
|
||||
if [ "$(uname -s)" = "Darwin" ]; then
|
||||
if ! [ /usr/bin/ape -nt o//ape/ape.macho ]; then
|
||||
echo >&2
|
||||
echo "installing o//ape/ape.elf to /usr/bin/ape" >&2
|
||||
echo "$SUDO mv -f o//ape/ape.elf /usr/bin/ape" >&2
|
||||
$SUDO cp -f o//ape/ape.macho /usr/bin/ape || exit
|
||||
echo "done" >&2
|
||||
fi
|
||||
else
|
||||
if ! [ /usr/bin/ape -nt o//ape/ape.elf ]; then
|
||||
echo >&2
|
||||
echo "installing o//ape/ape.elf to /usr/bin/ape" >&2
|
||||
echo "$SUDO mv -f o//ape/ape.elf /usr/bin/ape" >&2
|
||||
$SUDO cp -f o//ape/ape.elf /usr/bin/ape || exit
|
||||
echo "done" >&2
|
||||
fi
|
||||
fi
|
||||
|
||||
################################################################################
|
||||
# REGISTER APE LOADER WITH BINFMT_MISC TOO (LINUX-ONLY)
|
||||
|
||||
if [ x"$(uname -s)" = xLinux ]; then
|
||||
|
||||
if [ -e /proc/sys/fs/binfmt_misc/APE ]; then
|
||||
echo >&2
|
||||
echo it looks like APE is already registered with binfmt_misc >&2
|
||||
echo To reinstall please run ape/apeuninstall.sh first >&2
|
||||
echo please check that it is mapped to ape not /bin/sh >&2
|
||||
echo cat /proc/sys/fs/binfmt_misc/APE >&2
|
||||
cat /proc/sys/fs/binfmt_misc/APE >&2
|
||||
exit
|
||||
fi
|
||||
|
||||
if ! [ -e /proc/sys/fs/binfmt_misc ]; then
|
||||
echo >&2
|
||||
echo loading binfmt_misc into your kernel >&2
|
||||
echo you may need to edit configs to persist across reboot >&2
|
||||
echo $SUDO modprobe binfmt_misc >&2
|
||||
$SUDO modprobe binfmt_misc || exit
|
||||
echo done >&2
|
||||
fi
|
||||
|
||||
if ! [ -e /proc/sys/fs/binfmt_misc/register ]; then
|
||||
echo >&2
|
||||
echo mounting binfmt_misc into your kernel >&2
|
||||
echo you may need to edit configs to persist across reboot >&2
|
||||
echo $SUDO mount -t binfmt_misc none /proc/sys/fs/binfmt_misc >&2
|
||||
$SUDO mount -t binfmt_misc none /proc/sys/fs/binfmt_misc || exit
|
||||
echo done >&2
|
||||
fi
|
||||
|
||||
echo >&2
|
||||
echo registering APE with binfmt_misc >&2
|
||||
echo you may need to edit configs to persist across reboot >&2
|
||||
echo '$SUDO sh -c "echo '"'"':APE:M::MZqFpD::/usr/bin/ape:'"'"' >/proc/sys/fs/binfmt_misc/register"' >&2
|
||||
$SUDO sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register" || exit
|
||||
echo '$SUDO sh -c "echo '"'"':APE-jart:M::jartsr::/usr/bin/ape:'"'"' >/proc/sys/fs/binfmt_misc/register"' >&2
|
||||
$SUDO sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register" || exit
|
||||
echo done >&2
|
||||
|
||||
if [ x"$(cat /proc/sys/fs/binfmt_misc/status)" = xdisabled ]; then
|
||||
echo >&2
|
||||
echo enabling binfmt_misc >&2
|
||||
echo you may need to edit configs to persist across reboot >&2
|
||||
echo $SUDO sh -c 'echo 1 >/proc/sys/fs/binfmt_misc/status' >&2
|
||||
$SUDO sh -c 'echo 1 >/proc/sys/fs/binfmt_misc/status' || exit
|
||||
echo done >&2
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
################################################################################
|
||||
|
||||
{
|
||||
echo
|
||||
echo "------------------------------------------------------------------"
|
||||
echo
|
||||
echo "APE INSTALL COMPLETE"
|
||||
echo
|
||||
echo "If you decide to uninstall APE later on"
|
||||
echo "you may do so using ape/apeuninstall.sh"
|
||||
echo
|
||||
echo "Enjoy your APE loader (>'.')>"
|
||||
echo
|
||||
} >&2
|
58
bin/ape-uninstall
Executable file
58
bin/ape-uninstall
Executable file
|
@ -0,0 +1,58 @@
|
|||
#!/bin/sh
|
||||
|
||||
PROG=${0##*/}
|
||||
MODE=${MODE:-$m}
|
||||
COSMO=${COSMO:-/opt/cosmo}
|
||||
COSMOS=${COSMOS:-/opt/cosmos}
|
||||
|
||||
if [ ! -f ape/loader.c ]; then
|
||||
cd "$COSMO" || exit
|
||||
fi
|
||||
|
||||
if [ "$UID" = "0" ]; then
|
||||
SUDO=
|
||||
else
|
||||
SUDO=sudo
|
||||
fi
|
||||
|
||||
{
|
||||
echo
|
||||
echo "APE Uninstaller intends to run (in pseudo-shell)"
|
||||
echo
|
||||
echo " sudo echo -1 into /proc/sys/fs/binfmt_misc/APE*"
|
||||
echo " sudo rm -f /usr/bin/ape ~/.ape /tmp/.ape # etc."
|
||||
echo
|
||||
echo "You may then use ape/apeinstall.sh to reinstall it"
|
||||
echo
|
||||
} >&2
|
||||
|
||||
set -ex
|
||||
for f in /proc/sys/fs/binfmt_misc/APE*; do
|
||||
if [ -f $f ]; then
|
||||
$SUDO sh -c "echo -1 >$f" || exit
|
||||
fi
|
||||
done
|
||||
|
||||
# system installation
|
||||
if [ -f /usr/bin/ape ]; then
|
||||
$SUDO rm -f /usr/bin/ape
|
||||
fi
|
||||
|
||||
# legacy installations
|
||||
rm -f o/tmp/ape /tmp/ape "${TMPDIR:-/tmp}/ape"
|
||||
|
||||
# ad-hoc installations
|
||||
for x in .ape \
|
||||
.ape-1.1 \
|
||||
.ape-1.3 \
|
||||
.ape-1.4 \
|
||||
.ape-1.5 \
|
||||
.ape-1.6 \
|
||||
.ape-blink-0.9.2 \
|
||||
.ape-blink-1.0.0; do
|
||||
rm -f \
|
||||
~/$x \
|
||||
/tmp/$x \
|
||||
o/tmp/$x \
|
||||
"${TMPDIR:-/tmp}/$x"
|
||||
done
|
1
bin/cosmoc++
Symbolic link
1
bin/cosmoc++
Symbolic link
|
@ -0,0 +1 @@
|
|||
cosmocc
|
259
bin/cosmocc
Executable file
259
bin/cosmocc
Executable file
|
@ -0,0 +1,259 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# non-fat cosmopolitan c compiler
|
||||
#
|
||||
# getting started
|
||||
#
|
||||
# sudo chmod 1777 /opt
|
||||
# git clone https://github.com/jart/cosmopolitan /opt/cosmo
|
||||
# export PATH="$PATH:/opt/cosmo/bin:/opt/cosmos/bin"
|
||||
# echo 'export PATH="$PATH:/opt/cosmo/bin:/opt/cosmos/bin"' >>~/.profile
|
||||
# cosmocc --update # pull and rebuild toolchain artifacts
|
||||
#
|
||||
# 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
|
||||
#
|
||||
|
||||
PROG=${0##*/}
|
||||
MODE=${MODE:-$m}
|
||||
COSMO=${COSMO:-/opt/cosmo}
|
||||
COSMOS=${COSMOS:-/opt/cosmos}
|
||||
|
||||
if [ "$1" = "--version" ]; then
|
||||
cat <<EOF
|
||||
$PROG (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
|
||||
|
||||
if [ "$1" = "--update" ]; then
|
||||
cd /opt/cosmo || exit
|
||||
if GIT=$(command -v git); then
|
||||
echo "$PROG: running git pull on cosmo..." >&2
|
||||
"$GIT" pull --quiet || exit
|
||||
fi
|
||||
echo "$PROG: building cosmo x86_64 toolchain..." >&2
|
||||
make --silent -j toolchain MODE="${MODE}" || exit
|
||||
"$COSMO/tool/scripts/setup-cosmos"
|
||||
echo "$PROG: successfully updated your cosmo toolchain" >&2
|
||||
exit
|
||||
fi
|
||||
|
||||
CC="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-gcc"
|
||||
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"
|
||||
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"
|
||||
|
||||
if [ x"$0" != x"${0%++}" ]; 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
|
||||
|
||||
if [ ! -d "$COSMO" ]; then
|
||||
echo "$PROG: you need to clone cosmopolitan to your $COSMO directory" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -d "$COSMOS" ] ||
|
||||
[ ! -x "$COSMO/o/$MODE/cosmopolitan.a" ]; then
|
||||
echo "$PROG: you need to run: $PROG --update" >&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
|
||||
GOT_SOME=0
|
||||
NEED_OUTPUT=
|
||||
RELOCATABLE=0
|
||||
for x; do
|
||||
if [ $FIRST -eq 1 ]; then
|
||||
set --
|
||||
FIRST=0
|
||||
fi
|
||||
if [ -n "$NEED_OUTPUT" ]; then
|
||||
NEED_OUTPUT=
|
||||
OUTPUT=$x
|
||||
set -- "$@" "$x"
|
||||
continue
|
||||
fi
|
||||
if [ x"$x" = x"${x#-*}" ]; then
|
||||
GOT_SOME=1
|
||||
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 [ x"$x" = x"-fpic" ]; then
|
||||
continue
|
||||
elif [ x"$x" = x"-fPIC" ]; then
|
||||
continue
|
||||
elif [ x"$x" = x"-r" ] ||
|
||||
[ x"$x" = x"-pie" ] ||
|
||||
[ x"$x" = x"-frtti" ] ||
|
||||
[ x"$x" = x"-shared" ] ||
|
||||
[ x"$x" = x"-nostdlib" ] ||
|
||||
[ x"$x" = x"-mred-zone" ] ||
|
||||
[ x"$x" = x"-fexceptions" ] ||
|
||||
[ x"$x" = x"-fsanitize=thread" ]; then
|
||||
echo "$PROG: $x not supported" >&2
|
||||
exit 1
|
||||
elif [ x"$x" = x"-fsanitize=all" ] ||
|
||||
[ x"$x" = x"-fsanitize=address" ] ||
|
||||
[ x"$x" = x"-fsanitize=undefined" ]; then
|
||||
echo "$PROG: use cosmo MODE=dbg rather than passing $x" >&2
|
||||
exit 1
|
||||
elif [ x"$x" = x"-fomit-frame-pointer" ]; then
|
||||
# Quoth Apple "The frame pointer register must always address a
|
||||
# valid frame record. Some functions — such as leaf functions or
|
||||
# tail calls — may opt not to create an entry in this list. As a
|
||||
# result, stack traces are always meaningful, even without debug
|
||||
# information."
|
||||
x="-momit-leaf-frame-pointer"
|
||||
elif [ x"$x" = x"-Werror" ] || \
|
||||
[ x"$x" = x"-pedantic-errors" ]; 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"-march=native" ]; then
|
||||
set -- "$@" $("$COSMO/o/$MODE/tool/build/march-native.com")
|
||||
continue
|
||||
fi
|
||||
set -- "$@" "$x"
|
||||
done
|
||||
|
||||
if [ "$GOT_SOME" -eq 0 ]; then
|
||||
echo "$PROG: fatal error: no input files" >&2
|
||||
echo "compilation terminated." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
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
|
||||
CCFLAGS="$CCFLAGS -fpatchable-function-entry=18,16"
|
||||
fi
|
||||
|
||||
if [ x"$MODE" = x"dbg" ]; then
|
||||
set -- \
|
||||
-fsanitize=address \
|
||||
-fsanitize=undefined \
|
||||
"$@"
|
||||
fi
|
||||
|
||||
if [ x"$OPT" != x"-Os" ] &&
|
||||
[ x"${MODE#tiny}" != x"${MODE}" ]; then
|
||||
set -- \
|
||||
-fno-optimize-sibling-calls \
|
||||
-mno-omit-leaf-frame-pointer \
|
||||
"$@"
|
||||
fi
|
||||
|
||||
if [ $INTENT = cpp ]; then
|
||||
set -- "$CC" $PLATFORM $CCFLAGS $CPPFLAGS "$@"
|
||||
elif [ $INTENT = cc ]; then
|
||||
set -- "$CC" $PLATFORM $PREDEF $CCFLAGS $CPPFLAGS "$@" -fno-omit-frame-pointer
|
||||
else
|
||||
set -- "$CC" $PLATFORM $PREDEF $CCFLAGS $CPPFLAGS $LDFLAGS $APEFLAGS $CPPFLAGS "$@" \
|
||||
$LDLIBS -Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=4096 -fno-omit-frame-pointer
|
||||
fi
|
||||
|
||||
printf '# %s\n(cd %s; %s)\n' "$ORIGINAL" "$PWD" "$*" >>"${TMPDIR:-/tmp}/build.log"
|
||||
"$@" || exit
|
||||
|
||||
if [ -n "$OUTPUT" ] && [ -f "$OUTPUT" ]; then
|
||||
if [ $INTENT = cc ] || [ $INTENT = ld ]; then
|
||||
"$COSMO/o/$MODE/tool/build/fixupobj.com" "$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
|
||||
"$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-objcopy" -S -O binary "$OUTPUT.dbg" "$OUTPUT" || exit
|
||||
"$COSMO/o/$MODE/tool/build/zipcopy.com" "$OUTPUT.dbg" "$OUTPUT" || exit
|
||||
elif [ $SFLAG -eq 1 ]; then
|
||||
"$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-strip" "$OUTPUT" || exit
|
||||
fi
|
||||
fi
|
||||
fi
|
1
bin/fatcosmoc++
Symbolic link
1
bin/fatcosmoc++
Symbolic link
|
@ -0,0 +1 @@
|
|||
fatcosmocc
|
310
bin/fatcosmocc
Executable file
310
bin/fatcosmocc
Executable file
|
@ -0,0 +1,310 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# fat cosmopolitan c compiler
|
||||
#
|
||||
# - this command is a drop-in replacement for the cc or gcc command.
|
||||
# the difference is that (1) your binaries will be linked with the
|
||||
# cosmopolitan c library, rather than your system specific tooling
|
||||
# and (2) they'll be fat ape executables that run on the platforms
|
||||
#
|
||||
# * amd64
|
||||
# + linux
|
||||
# + macos
|
||||
# + windows
|
||||
# + freebsd
|
||||
# + openbsd
|
||||
# + netbsd
|
||||
# * arm64
|
||||
# + linux
|
||||
# + macos
|
||||
# + windows (non-native)
|
||||
#
|
||||
# - you need to use linux to build your binaries currently, but you
|
||||
# can scp and distribute the output files to the above platforms!
|
||||
#
|
||||
# installation
|
||||
#
|
||||
# sudo chmod 1777 /opt
|
||||
# git clone https://github.com/jart/cosmopolitan /opt/cosmo
|
||||
# export PATH="$PATH:/opt/cosmo/bin:/opt/cosmos/bin"
|
||||
# echo 'export PATH="$PATH:/opt/cosmo/bin:/opt/cosmos/bin"' >>~/.profile
|
||||
# fatcosmocc --update # pull and rebuild toolchain artifacts
|
||||
#
|
||||
# getting started
|
||||
#
|
||||
# fatcosmocc -o hello.com hello.c
|
||||
# ./foo.com
|
||||
# ./foo.com.dbg
|
||||
#
|
||||
# building open source projects
|
||||
#
|
||||
# export CC=fatcosmocc
|
||||
# export CXX=fatcosmoc++
|
||||
# ./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
|
||||
# fatcosmocc --update
|
||||
# fatcosmocc -Os -o foo.com foo.c
|
||||
#
|
||||
# building in debug mode
|
||||
#
|
||||
# export MODE=dbg
|
||||
# fatcosmocc --update
|
||||
# fatcosmocc -g -o foo.com foo.c
|
||||
#
|
||||
|
||||
export PROG=${0##*/}
|
||||
export COSMO=${COSMO:-/opt/cosmo}
|
||||
export COSMOS=${COSMOS:-/opt/cosmos}
|
||||
export ORIGINAL="$0 $*"
|
||||
|
||||
if [ "$1" = "--version" ]; then
|
||||
cat <<EOF
|
||||
$PROG (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
|
||||
|
||||
MODE=${MODE:-$m}
|
||||
if [ x"$MODE" = x"" ]; then
|
||||
MODE_AARCH64=aarch64
|
||||
elif [ x"$MODE" = x"tiny" ]; then
|
||||
MODE_AARCH64=aarch64-tiny
|
||||
elif [ x"$MODE" = x"zero" ]; then
|
||||
MODE_AARCH64=aarch64-zero
|
||||
elif [ x"$MODE" = x"dbg" ]; then
|
||||
MODE_AARCH64=aarch64-dbg
|
||||
else
|
||||
echo "$PROG: build MODE=$MODE not supported by fatcosmocc" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "$1" = "--update" ]; then
|
||||
cd /opt/cosmo || exit
|
||||
if GIT=$(command -v git); then
|
||||
echo "$PROG: running git pull on cosmo..." >&2
|
||||
"$GIT" pull --quiet || exit
|
||||
fi
|
||||
echo "$PROG: building cosmo x86_64 toolchain..." >&2
|
||||
make --silent -j toolchain MODE="${MODE}" || exit
|
||||
echo "$PROG: building cosmo aarch64 toolchain..." >&2
|
||||
make --silent -j toolchain MODE="${MODE_AARCH64}" || exit
|
||||
"$COSMO/tool/scripts/setup-cosmos"
|
||||
echo "$PROG: successfully updated your cosmo toolchain" >&2
|
||||
exit
|
||||
fi
|
||||
|
||||
if [ ! -d "$COSMO" ]; then
|
||||
echo "$PROG: you need to clone cosmopolitan to your $COSMO directory" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -d "$COSMOS" ] ||
|
||||
[ ! -x "$COSMO/o/$MODE/cosmopolitan.a" ] ||
|
||||
[ ! -x "$COSMO/o/$MODE_AARCH64/cosmopolitan.a" ]; then
|
||||
echo "$PROG: you need to run: $PROG --update" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
export FIXUPOBJ="$COSMO/o/$MODE/tool/build/fixupobj.com"
|
||||
|
||||
OPT=
|
||||
FIRST=1
|
||||
OUTPUT=
|
||||
STRIP=0
|
||||
INTENT=ld
|
||||
GOT_SOME=0
|
||||
SAVE_TEMPS=0
|
||||
NEED_OUTPUT=
|
||||
FRAME=""
|
||||
for x; do
|
||||
if [ $FIRST -eq 1 ]; then
|
||||
set --
|
||||
FIRST=0
|
||||
fi
|
||||
if [ -n "$NEED_OUTPUT" ]; then
|
||||
NEED_OUTPUT=
|
||||
OUTPUT=$x
|
||||
continue
|
||||
fi
|
||||
if [ x"$x" = x"${x#-*}" ]; then
|
||||
GOT_SOME=1
|
||||
elif [ x"$x" = x"-o" ]; then
|
||||
NEED_OUTPUT=1
|
||||
continue
|
||||
elif [ x"$x" != x"${x#-o}" ]; then
|
||||
OUTPUT=${x#-o}
|
||||
continue
|
||||
elif [ x"$x" != x"${x#-O}" ]; then
|
||||
OPT=$x
|
||||
elif [ x"$x" = x"-c" ]; then
|
||||
INTENT=cc
|
||||
elif [ x"$x" = x"-E" ]; then
|
||||
INTENT=cpp
|
||||
elif [ x"$x" = x"-s" ]; then
|
||||
STRIP=1
|
||||
continue
|
||||
elif [ x"$x" = x"-save-temps" ]; then
|
||||
SAVE_TEMPS=1
|
||||
elif [ x"$x" = x"-fomit-frame-pointer" ]; then
|
||||
# Quoth Apple "The frame pointer register must always address a
|
||||
# valid frame record. Some functions — such as leaf functions or
|
||||
# tail calls — may opt not to create an entry in this list. As a
|
||||
# result, stack traces are always meaningful, even without debug
|
||||
# information."
|
||||
x="-momit-leaf-frame-pointer"
|
||||
elif [ x"$x" = x"-r" ] ||
|
||||
[ x"$x" = x"-pie" ] ||
|
||||
[ x"$x" = x"-frtti" ] ||
|
||||
[ x"$x" = x"-shared" ] ||
|
||||
[ x"$x" = x"-nostdlib" ] ||
|
||||
[ x"$x" = x"-mred-zone" ] ||
|
||||
[ x"$x" = x"-fexceptions" ] ||
|
||||
[ x"$x" = x"-fsanitize=thread" ]; then
|
||||
echo "$PROG: $x not supported" >&2
|
||||
exit 1
|
||||
elif [ x"$x" = x"-fsanitize=all" ] ||
|
||||
[ x"$x" = x"-fsanitize=address" ] ||
|
||||
[ x"$x" = x"-fsanitize=undefined" ]; then
|
||||
echo "$PROG: use cosmo MODE=dbg rather than passing $x" >&2
|
||||
exit 1
|
||||
elif [ x"$x" = x"-mno-red-zone" ]; then
|
||||
# "Any memory below the stack beyond the red zone is considered
|
||||
# volatile and may be modified by the operating system at any time."
|
||||
# https://devblogs.microsoft.com/oldnewthing/20190111-00/?p=100685
|
||||
continue
|
||||
elif [ x"$x" = x"-fpic" ] || [ x"$x" = x"-fPIC" ]; then
|
||||
# no support for building dynamic shared objects yet. reports
|
||||
# indicate that ignoring these flags, helps let autoconf know
|
||||
continue
|
||||
elif [ x"$x" = x"-Werror" ] || \
|
||||
[ x"$x" = x"-pedantic-errors" ]; 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"-static-libgcc" ] || \
|
||||
[ x"$x" = x"-shared-libgcc" ]; then
|
||||
# cosmopolitan.a always has llvm compiler runtime static code
|
||||
continue
|
||||
fi
|
||||
set -- "$@" "$x"
|
||||
done
|
||||
|
||||
if [ x"$MODE" = x"dbg" ]; then
|
||||
set -- \
|
||||
-fsanitize=address \
|
||||
-fsanitize=undefined \
|
||||
"$@"
|
||||
fi
|
||||
|
||||
if [ x"$OPT" != x"-Os" ] &&
|
||||
[ x"${MODE#tiny}" != x"${MODE}" ]; then
|
||||
set -- \
|
||||
-fno-optimize-sibling-calls \
|
||||
-mno-omit-leaf-frame-pointer \
|
||||
"$@"
|
||||
fi
|
||||
|
||||
set -- \
|
||||
-fno-pie \
|
||||
-fportcosmo \
|
||||
-fno-dwarf2-cfi-asm \
|
||||
-fno-unwind-tables \
|
||||
-fno-asynchronous-unwind-tables \
|
||||
-fno-semantic-interposition \
|
||||
-fno-math-errno \
|
||||
"$@" \
|
||||
-fno-omit-frame-pointer
|
||||
|
||||
PLATFORM="-D__COSMOPOLITAN__"
|
||||
PREDEF="-include libc/integral/normalize.inc"
|
||||
CPPFLAGS="-nostdinc -iquote $COSMO -isystem $COSMOS/include -isystem $COSMO/libc/isystem"
|
||||
|
||||
if [ "$GOT_SOME" -eq 0 ]; then
|
||||
echo "$PROG: fatal error: no input files" >&2
|
||||
echo "compilation terminated." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ $INTENT = cpp ]; then
|
||||
if [ -n "$OUTPUT" ]; then
|
||||
set -- "$@" -o"$OUTPUT"
|
||||
fi
|
||||
MODE="$MODE" "$COSMO/tool/scripts/fat-x86_64" \
|
||||
-U__x86_64__ $PLATFORM $CPPFLAGS "$@"
|
||||
exit
|
||||
fi
|
||||
|
||||
if [ -z "$OUTPUT" ]; then
|
||||
if [ $INTENT = cc ]; then
|
||||
echo "$PROG: passing -c without -o flag not supported" >&2
|
||||
exit 1
|
||||
fi
|
||||
OUTPUT=a.out
|
||||
fi
|
||||
|
||||
set -- $PLATFORM $PREDEF $CPPFLAGS "$@"
|
||||
|
||||
out2=$(mktemp "${TMPDIR:-/tmp}/ccc.XXXXXX") || exit
|
||||
|
||||
MODE="$MODE" \
|
||||
"$COSMO/tool/scripts/fat-x86_64" -o"$OUTPUT.x86_64" "$@" &
|
||||
pid1=$!
|
||||
|
||||
MODE="$MODE_AARCH64" \
|
||||
"$COSMO/tool/scripts/fat-aarch64" -o"$OUTPUT.aarch64" "$@" 2>"$out2" &
|
||||
pid2=$!
|
||||
|
||||
if ! wait $pid1; then
|
||||
wait
|
||||
rm -f "$out2"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! wait $pid2; then
|
||||
wait
|
||||
echo "$PROG: aarch64 compiler failed with:" >&2
|
||||
cat "$out2" >&2
|
||||
rm -f "$out2"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
rm -f "$out2"
|
||||
|
||||
set -- \
|
||||
"$COSMO/o/$MODE/tool/build/apelink.com" \
|
||||
-l "$COSMO/o/$MODE/ape/ape.elf" \
|
||||
-l "$COSMO/o/$MODE_AARCH64/ape/ape.elf" \
|
||||
-M "$COSMO/ape/ape-m1.c" \
|
||||
-o "$OUTPUT" \
|
||||
"$OUTPUT.x86_64" \
|
||||
"$OUTPUT.aarch64"
|
||||
printf '# %s\n(cd %s; %s)\n' "$ORIGINAL" "$PWD" "$*" >>"${TMPDIR:-/tmp}/build.log"
|
||||
"$@" || exit
|
||||
|
||||
"$COSMO/o/$MODE/tool/build/pecheck.com" "$OUTPUT" || exit
|
||||
|
||||
if [ $SAVE_TEMPS -eq 0 ]; then
|
||||
rm -f "$OUTPUT.x86_64" \
|
||||
"$OUTPUT.aarch64"
|
||||
fi
|
1
bin/unknown-unknown-cosmo-ar
Executable file
1
bin/unknown-unknown-cosmo-ar
Executable file
|
@ -0,0 +1 @@
|
|||
#!/bin/sh
|
1
bin/unknown-unknown-cosmo-c++
Symbolic link
1
bin/unknown-unknown-cosmo-c++
Symbolic link
|
@ -0,0 +1 @@
|
|||
fatcosmocc
|
1
bin/unknown-unknown-cosmo-cc
Symbolic link
1
bin/unknown-unknown-cosmo-cc
Symbolic link
|
@ -0,0 +1 @@
|
|||
fatcosmocc
|
1
bin/unknown-unknown-cosmo-g++
Symbolic link
1
bin/unknown-unknown-cosmo-g++
Symbolic link
|
@ -0,0 +1 @@
|
|||
fatcosmocc
|
1
bin/unknown-unknown-cosmo-gcc
Symbolic link
1
bin/unknown-unknown-cosmo-gcc
Symbolic link
|
@ -0,0 +1 @@
|
|||
fatcosmocc
|
13
bin/x86_64-unknown-cosmo-addr2line
Executable file
13
bin/x86_64-unknown-cosmo-addr2line
Executable file
|
@ -0,0 +1,13 @@
|
|||
#!/bin/sh
|
||||
|
||||
MODE=${MODE:-$m}
|
||||
COSMO=${COSMO:-/opt/cosmo}
|
||||
COSMOS=${COSMOS:-/opt/cosmos}
|
||||
TOOL="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-addr2line"
|
||||
|
||||
if [ ! -x "$TOOL" ]; then
|
||||
echo "$0: you need to run: cosmocc --update" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec "$TOOL" "$@"
|
18
bin/x86_64-unknown-cosmo-ar
Executable file
18
bin/x86_64-unknown-cosmo-ar
Executable file
|
@ -0,0 +1,18 @@
|
|||
#!/bin/sh
|
||||
|
||||
MODE=${MODE:-$m}
|
||||
COSMO=${COSMO:-/opt/cosmo}
|
||||
COSMOS=${COSMOS:-/opt/cosmos}
|
||||
|
||||
if [ x"$1" = x"rcs" ] || [ x"$1" = x"rcsD" ]; then
|
||||
TOOL="$COSMO/o/$MODE/tool/build/ar.com"
|
||||
else
|
||||
TOOL="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-ar"
|
||||
fi
|
||||
|
||||
if [ ! -x "$TOOL" ]; then
|
||||
echo "$0: you need to run: cosmocc --update" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec "$TOOL" "$@"
|
13
bin/x86_64-unknown-cosmo-as
Executable file
13
bin/x86_64-unknown-cosmo-as
Executable file
|
@ -0,0 +1,13 @@
|
|||
#!/bin/sh
|
||||
|
||||
MODE=${MODE:-$m}
|
||||
COSMO=${COSMO:-/opt/cosmo}
|
||||
COSMOS=${COSMOS:-/opt/cosmos}
|
||||
TOOL="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-as"
|
||||
|
||||
if [ ! -x "$TOOL" ]; then
|
||||
echo "$0: you need to run: cosmocc --update" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec "$TOOL" "$@"
|
1
bin/x86_64-unknown-cosmo-c++
Symbolic link
1
bin/x86_64-unknown-cosmo-c++
Symbolic link
|
@ -0,0 +1 @@
|
|||
cosmocc
|
1
bin/x86_64-unknown-cosmo-cc
Symbolic link
1
bin/x86_64-unknown-cosmo-cc
Symbolic link
|
@ -0,0 +1 @@
|
|||
cosmocc
|
1
bin/x86_64-unknown-cosmo-g++
Symbolic link
1
bin/x86_64-unknown-cosmo-g++
Symbolic link
|
@ -0,0 +1 @@
|
|||
cosmocc
|
1
bin/x86_64-unknown-cosmo-gcc
Symbolic link
1
bin/x86_64-unknown-cosmo-gcc
Symbolic link
|
@ -0,0 +1 @@
|
|||
cosmocc
|
13
bin/x86_64-unknown-cosmo-ld
Executable file
13
bin/x86_64-unknown-cosmo-ld
Executable file
|
@ -0,0 +1,13 @@
|
|||
#!/bin/sh
|
||||
|
||||
MODE=${MODE:-$m}
|
||||
COSMO=${COSMO:-/opt/cosmo}
|
||||
COSMOS=${COSMOS:-/opt/cosmos}
|
||||
TOOL="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-ld"
|
||||
|
||||
if [ ! -x "$TOOL" ]; then
|
||||
echo "$0: you need to run: cosmocc --update" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec "$TOOL" "$@"
|
13
bin/x86_64-unknown-cosmo-objcopy
Executable file
13
bin/x86_64-unknown-cosmo-objcopy
Executable file
|
@ -0,0 +1,13 @@
|
|||
#!/bin/sh
|
||||
|
||||
MODE=${MODE:-$m}
|
||||
COSMO=${COSMO:-/opt/cosmo}
|
||||
COSMOS=${COSMOS:-/opt/cosmos}
|
||||
TOOL="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-objcopy"
|
||||
|
||||
if [ ! -x "$TOOL" ]; then
|
||||
echo "$0: you need to run: cosmocc --update" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec "$TOOL" "$@"
|
13
bin/x86_64-unknown-cosmo-strip
Executable file
13
bin/x86_64-unknown-cosmo-strip
Executable file
|
@ -0,0 +1,13 @@
|
|||
#!/bin/sh
|
||||
|
||||
MODE=${MODE:-$m}
|
||||
COSMO=${COSMO:-/opt/cosmo}
|
||||
COSMOS=${COSMOS:-/opt/cosmos}
|
||||
TOOL="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-strip"
|
||||
|
||||
if [ ! -x "$TOOL" ]; then
|
||||
echo "$0: you need to run: cosmocc --update" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec "$TOOL" "$@"
|
Loading…
Add table
Add a link
Reference in a new issue