2022-09-20 11:34:03 +00:00
|
|
|
#!/bin/sh
|
2023-11-11 22:04:26 +00:00
|
|
|
# cosmopolitan c/c++ cross compiler
|
|
|
|
# https://github.com/jart/cosmopolitan
|
|
|
|
# https://cosmo.zip/
|
2022-09-20 11:34:03 +00:00
|
|
|
|
2023-11-11 22:04:26 +00:00
|
|
|
BIN=${0%/*}
|
2023-08-12 05:52:11 +00:00
|
|
|
PROG=${0##*/}
|
2023-08-12 23:44:04 +00:00
|
|
|
GCC_VERSION=11.2.0
|
2022-11-14 21:45:57 +00:00
|
|
|
|
2022-09-20 11:34:03 +00:00
|
|
|
if [ "$1" = "--version" ]; then
|
2023-08-12 05:52:11 +00:00
|
|
|
cat <<EOF
|
2023-08-12 23:44:04 +00:00
|
|
|
$PROG (GCC) $GCC_VERSION
|
2023-08-13 21:38:33 +00:00
|
|
|
Copyright (c) 2023 Justine Alexandra Roberts Tunney
|
|
|
|
Cosmopolitan Libc and LLVM libcxx/compiler-rt are subject to non-GPL
|
|
|
|
notice licenses, e.g. ISC, MIT, etc. Your compiled programs must embed
|
|
|
|
our copyright notices. This toolchain is configured to do so default.
|
|
|
|
Cosmopolitan comes with absolutely NO WARRANTY of any kind.
|
|
|
|
For more information, see the Cosmopolitan LICENSE files.
|
2022-09-20 11:34:03 +00:00
|
|
|
Copyright (C) 2019 Free Software Foundation, Inc.
|
2023-08-13 21:38:33 +00:00
|
|
|
This launches GNU GCC/Binutils subprocesses, which is free software; see
|
2024-01-05 15:58:07 +00:00
|
|
|
cosmocc's LICENSE files for source code and copying conditions. There is
|
|
|
|
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
2022-09-20 11:34:03 +00:00
|
|
|
EOF
|
2023-08-13 22:03:09 +00:00
|
|
|
exit
|
2022-09-20 11:34:03 +00:00
|
|
|
fi
|
|
|
|
|
2023-11-11 22:04:26 +00:00
|
|
|
if [ "$1" = "--help" ]; then
|
|
|
|
if [ -t 1 ]; then
|
|
|
|
exec less "$BIN/../README.md"
|
|
|
|
else
|
|
|
|
exec cat "$BIN/../README.md"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-08-13 21:38:33 +00:00
|
|
|
fatal_error() {
|
|
|
|
echo "$PROG: fatal error: $1" >&2
|
|
|
|
echo "compilation terminated." >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
log_command() {
|
|
|
|
if [ -n "$BUILDLOG" ]; then
|
|
|
|
printf '# %s\n(cd %s; %s)\n' "$ORIGINAL" "$PWD" "$*" >>"$BUILDLOG"
|
2023-08-12 05:52:11 +00:00
|
|
|
fi
|
2023-08-13 21:38:33 +00:00
|
|
|
}
|
2023-08-12 05:52:11 +00:00
|
|
|
|
2023-06-18 12:39:31 +00:00
|
|
|
ORIGINAL="$0 $*"
|
2023-08-13 21:38:33 +00:00
|
|
|
PLATFORM="-D__COSMOPOLITAN__ -D__COSMOCC__"
|
2023-06-09 06:44:03 +00:00
|
|
|
PREDEF="-include libc/integral/normalize.inc"
|
2023-08-15 05:22:58 +00:00
|
|
|
CFLAGS="-fportcosmo -fno-dwarf2-cfi-asm -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-semantic-interposition"
|
2023-11-11 22:04:26 +00:00
|
|
|
CPPFLAGS="-fno-pie -nostdinc -fno-math-errno -isystem $BIN/../include"
|
Release Cosmopolitan v3.3
This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker
appears to have changed things so that only a single de-duplicated str
table is present in the binary, and it gets placed wherever the linker
wants, regardless of what the linker script says. To cope with that we
need to stop using .ident to embed licenses. As such, this change does
significant work to revamp how third party licenses are defined in the
codebase, using `.section .notice,"aR",@progbits`.
This new GCC 12.3 toolchain has support for GNU indirect functions. It
lets us support __target_clones__ for the first time. This is used for
optimizing the performance of libc string functions such as strlen and
friends so far on x86, by ensuring AVX systems favor a second codepath
that uses VEX encoding. It shaves some latency off certain operations.
It's a useful feature to have for scientific computing for the reasons
explained by the test/libcxx/openmp_test.cc example which compiles for
fifteen different microarchitectures. Thanks to the upgrades, it's now
also possible to use newer instruction sets, such as AVX512FP16, VNNI.
Cosmo now uses the %gs register on x86 by default for TLS. Doing it is
helpful for any program that links `cosmo_dlopen()`. Such programs had
to recompile their binaries at startup to change the TLS instructions.
That's not great, since it means every page in the executable needs to
be faulted. The work of rewriting TLS-related x86 opcodes, is moved to
fixupobj.com instead. This is great news for MacOS x86 users, since we
previously needed to morph the binary every time for that platform but
now that's no longer necessary. The only platforms where we need fixup
of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On
Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc
assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the
kernels do not allow us to specify a value for the %gs register.
OpenBSD users are now required to use APE Loader to run Cosmo binaries
and assimilation is no longer possible. OpenBSD kernel needs to change
to allow programs to specify a value for the %gs register, or it needs
to stop marking executable pages loaded by the kernel as mimmutable().
This release fixes __constructor__, .ctor, .init_array, and lastly the
.preinit_array so they behave the exact same way as glibc.
We no longer use hex constants to define math.h symbols like M_PI.
2024-02-20 19:12:09 +00:00
|
|
|
LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd -Wl,-z,noexecstack"
|
2023-08-13 21:38:33 +00:00
|
|
|
APEFLAGS="-Wl,--gc-sections"
|
|
|
|
PRECIOUS="-fno-omit-frame-pointer"
|
|
|
|
|
|
|
|
CROSS=1
|
2023-11-11 22:04:26 +00:00
|
|
|
ARCH=${PROG%%-*}
|
2023-08-13 21:38:33 +00:00
|
|
|
if [ x"$ARCH" = x"$PROG" ]; then
|
2023-11-11 22:04:26 +00:00
|
|
|
fatal_error "cosmocross must be run via cross compiler"
|
2023-10-15 03:57:15 +00:00
|
|
|
fi
|
|
|
|
|
2023-11-11 22:04:26 +00:00
|
|
|
CC="$BIN/$ARCH-linux-cosmo-gcc"
|
|
|
|
CRT="$BIN/../$ARCH-linux-cosmo/lib/crt.o"
|
|
|
|
LDLIBS="-lcosmo"
|
2023-11-12 06:11:11 +00:00
|
|
|
if [ -z "$COSMOS" ]; then
|
|
|
|
LDFLAGS="$LDFLAGS -L$BIN/../$ARCH-linux-cosmo/lib"
|
|
|
|
else
|
|
|
|
LDFLAGS="$LDFLAGS -L$COSMOS/lib -L$BIN/../$ARCH-linux-cosmo/lib"
|
|
|
|
CPPFLAGS="$CPPFLAGS -I$COSMOS/include"
|
|
|
|
fi
|
2023-08-13 21:38:33 +00:00
|
|
|
if [ x"$PROG" != x"${PROG%++}" ]; then
|
2023-11-11 22:04:26 +00:00
|
|
|
CC="$BIN/$ARCH-linux-cosmo-g++"
|
2023-08-13 21:38:33 +00:00
|
|
|
CFLAGS="$CFLAGS -fno-rtti -fno-exceptions -fuse-cxa-atexit"
|
2023-11-11 22:04:26 +00:00
|
|
|
LDLIBS="-lcxx $LDLIBS"
|
2022-11-14 21:45:57 +00:00
|
|
|
fi
|
|
|
|
|
2023-08-13 21:38:33 +00:00
|
|
|
PAGESZ=4096
|
|
|
|
if [ x"$ARCH" = x"x86_64" ]; then
|
|
|
|
OBJCOPYFLAGS="-S -O binary"
|
2023-11-11 22:04:26 +00:00
|
|
|
CRT="$BIN/../$ARCH-linux-cosmo/lib/ape-no-modify-self.o $CRT"
|
2023-08-13 21:38:33 +00:00
|
|
|
CPPFLAGS="$CPPFLAGS -mno-red-zone"
|
|
|
|
CFLAGS="$CFLAGS -mno-tls-direct-seg-refs"
|
2023-11-11 22:04:26 +00:00
|
|
|
LDFLAGS="$LDFLAGS -Wl,-T,$BIN/../$ARCH-linux-cosmo/lib/ape.lds"
|
2023-08-13 21:38:33 +00:00
|
|
|
elif [ x"$ARCH" = x"aarch64" ]; then
|
|
|
|
OBJCOPYFLAGS="-S"
|
|
|
|
PAGESZ=16384
|
|
|
|
CFLAGS="$CFLAGS -ffixed-x18 -ffixed-x28 -mno-outline-atomics"
|
2023-11-11 22:04:26 +00:00
|
|
|
LDFLAGS="$LDFLAGS -Wl,-T,$BIN/../$ARCH-linux-cosmo/lib/aarch64.lds"
|
2023-08-13 21:38:33 +00:00
|
|
|
else
|
|
|
|
fatal_error "$ARCH: unsupported architecture"
|
|
|
|
fi
|
|
|
|
|
2023-08-17 18:12:10 +00:00
|
|
|
LDFLAGS="$LDFLAGS -Wl,-z,common-page-size=$PAGESZ -Wl,-z,max-page-size=16384"
|
2023-08-13 21:38:33 +00:00
|
|
|
|
2023-06-08 21:29:22 +00:00
|
|
|
OPT=
|
2022-09-20 11:34:03 +00:00
|
|
|
FIRST=1
|
2023-06-08 21:29:22 +00:00
|
|
|
OUTPUT=
|
2023-07-10 12:55:00 +00:00
|
|
|
SFLAG=0
|
2023-06-10 16:15:19 +00:00
|
|
|
INTENT=ld
|
2023-08-12 05:52:11 +00:00
|
|
|
GOT_SOME=0
|
2023-06-08 21:29:22 +00:00
|
|
|
NEED_OUTPUT=
|
2023-06-18 11:02:01 +00:00
|
|
|
RELOCATABLE=0
|
2022-09-20 11:34:03 +00:00
|
|
|
for x; do
|
|
|
|
if [ $FIRST -eq 1 ]; then
|
|
|
|
set --
|
|
|
|
FIRST=0
|
|
|
|
fi
|
2023-08-12 05:52:11 +00:00
|
|
|
if [ -n "$NEED_OUTPUT" ]; then
|
|
|
|
NEED_OUTPUT=
|
|
|
|
OUTPUT=$x
|
|
|
|
set -- "$@" "$x"
|
2022-11-14 21:45:57 +00:00
|
|
|
continue
|
2023-08-12 05:52:11 +00:00
|
|
|
fi
|
2023-08-12 23:44:04 +00:00
|
|
|
if [ x"$x" = x"-" ] || # is an argument
|
2023-11-01 01:20:28 +00:00
|
|
|
[ x"$x" = x"${x#-*}" ]; then # !startswith(x, "-")
|
2023-08-12 05:52:11 +00:00
|
|
|
GOT_SOME=1
|
|
|
|
elif [ x"$x" = x"-static-libstdc++" ]; then
|
2022-11-14 21:45:57 +00:00
|
|
|
continue
|
2023-06-18 12:39:31 +00:00
|
|
|
elif [ x"$x" = x"-static-libgcc" ]; then
|
|
|
|
continue
|
2023-06-08 21:29:22 +00:00
|
|
|
elif [ x"$x" != x"${x#-O}" ]; then
|
|
|
|
OPT=$x
|
|
|
|
elif [ x"$x" = x"-c" ]; then
|
2023-06-10 16:15:19 +00:00
|
|
|
INTENT=cc
|
2023-07-10 12:55:00 +00:00
|
|
|
elif [ x"$x" = x"-s" ]; then
|
|
|
|
SFLAG=1
|
|
|
|
continue
|
2023-06-18 11:02:01 +00:00
|
|
|
elif [ x"$x" = x"-r" ]; then
|
|
|
|
RELOCATABLE=1
|
2024-01-22 18:22:16 +00:00
|
|
|
elif [ x"$x" = x"-E" ] ||
|
|
|
|
[ x"$x" = x"-M" ] ||
|
|
|
|
[ x"$x" = x"-MM" ]; then
|
2023-06-10 16:15:19 +00:00
|
|
|
INTENT=cpp
|
2023-06-08 21:29:22 +00:00
|
|
|
elif [ x"$x" = x"-o" ]; then
|
|
|
|
NEED_OUTPUT=1
|
2023-08-15 05:22:58 +00:00
|
|
|
elif [ x"$x" = x"-mcosmo" ]; then
|
|
|
|
CPPFLAGS="$CPPFLAGS -D_COSMO_SOURCE"
|
|
|
|
continue
|
2023-06-08 21:29:22 +00:00
|
|
|
elif [ x"$x" != x"${x#-o}" ]; then
|
|
|
|
OUTPUT=${x#-o}
|
|
|
|
elif [ x"$x" = x"-fpic" ]; then
|
|
|
|
continue
|
|
|
|
elif [ x"$x" = x"-fPIC" ]; then
|
|
|
|
continue
|
2023-08-12 05:52:11 +00:00
|
|
|
elif [ x"$x" = x"-r" ] ||
|
|
|
|
[ x"$x" = x"-pie" ] ||
|
|
|
|
[ x"$x" = x"-shared" ] ||
|
|
|
|
[ x"$x" = x"-nostdlib" ] ||
|
|
|
|
[ x"$x" = x"-mred-zone" ] ||
|
|
|
|
[ x"$x" = x"-fsanitize=thread" ]; then
|
|
|
|
echo "$PROG: $x not supported" >&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."
|
2023-08-12 23:44:04 +00:00
|
|
|
set -- "$@" -momit-leaf-frame-pointer -foptimize-sibling-calls
|
|
|
|
continue
|
2023-08-21 19:16:52 +00:00
|
|
|
elif [ x"$x" != x"${x#-Werror}" ] || \
|
2023-08-12 05:52:11 +00:00
|
|
|
[ 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
|
2023-08-13 21:38:33 +00:00
|
|
|
if [ $CROSS -eq 0 ]; then
|
2023-11-11 22:04:26 +00:00
|
|
|
set -- "$@" $("$BIN/march-native")
|
2023-08-13 21:38:33 +00:00
|
|
|
else
|
|
|
|
fatal_error "-march=native can't be used when cross compiling"
|
|
|
|
fi
|
2023-06-08 21:29:22 +00:00
|
|
|
continue
|
2023-08-12 23:44:04 +00:00
|
|
|
elif [ x"$x" = x"-dumpversion" ]; then
|
|
|
|
echo $GCC_VERSION
|
|
|
|
exit 0
|
2023-11-01 01:20:28 +00:00
|
|
|
elif [ x"$x" = x"-Wl,--version" ]; then
|
|
|
|
GOT_SOME=1
|
2023-11-02 16:52:25 +00:00
|
|
|
elif [ x"$x" = x"-dumpmachine" ]; then
|
|
|
|
GOT_SOME=1
|
2022-09-20 11:34:03 +00:00
|
|
|
fi
|
|
|
|
set -- "$@" "$x"
|
|
|
|
done
|
|
|
|
|
2023-08-12 05:52:11 +00:00
|
|
|
if [ "$GOT_SOME" -eq 0 ]; then
|
2023-08-13 21:38:33 +00:00
|
|
|
fatal_error "no input files"
|
2023-08-12 05:52:11 +00:00
|
|
|
fi
|
|
|
|
|
2023-06-18 11:02:01 +00:00
|
|
|
if [ $RELOCATABLE -eq 1 ]; then
|
2023-08-13 21:38:33 +00:00
|
|
|
LDFLAGS="$LDFLAGS -r"
|
2023-06-18 11:02:01 +00:00
|
|
|
fi
|
|
|
|
|
2023-08-13 21:38:33 +00:00
|
|
|
# support --ftrace unless optimizing for size
|
2023-08-13 15:25:36 +00:00
|
|
|
if [ x"$OPT" != x"-Os" ] && # $OPT != -Os
|
|
|
|
[ x"${MODE%tiny}" = x"${MODE}" ]; then # $MODE not in (tiny, aarch64-tiny)
|
2023-08-13 21:38:33 +00:00
|
|
|
if [ x"$ARCH" = x"x86_64" ]; then
|
|
|
|
CFLAGS="$CFLAGS -fpatchable-function-entry=18,16"
|
|
|
|
elif [ x"$ARCH" = x"aarch64" ]; then
|
|
|
|
CFLAGS="$CFLAGS -fpatchable-function-entry=7,6"
|
|
|
|
fi
|
2023-08-12 05:52:11 +00:00
|
|
|
fi
|
|
|
|
|
2023-08-13 21:38:33 +00:00
|
|
|
# maximize frame pointers unless optimizing for size
|
|
|
|
if [ x"$OPT" != x"-Os" ] && # $OPT != "-Os"
|
|
|
|
[ x"$MODE" != x"${MODE%tiny}" ]; then # endswith($MODE, "tiny")
|
|
|
|
CFLAGS="$CFLAGS -fno-optimize-sibling-calls -mno-omit-leaf-frame-pointer"
|
2023-06-08 21:29:22 +00:00
|
|
|
fi
|
|
|
|
|
2023-06-10 16:15:19 +00:00
|
|
|
if [ $INTENT = cpp ]; then
|
2023-08-13 21:38:33 +00:00
|
|
|
set -- "$CC" $PLATFORM $CPPFLAGS "$@"
|
2023-06-10 16:15:19 +00:00
|
|
|
elif [ $INTENT = cc ]; then
|
2023-08-13 21:38:33 +00:00
|
|
|
set -- "$CC" $PLATFORM $PREDEF $CFLAGS $CPPFLAGS "$@" $PRECIOUS
|
2022-09-20 11:34:03 +00:00
|
|
|
else
|
2023-08-13 21:38:33 +00:00
|
|
|
set -- "$CC" $PLATFORM $PREDEF $CFLAGS $CPPFLAGS $CRT "$@" $LDFLAGS $LDLIBS $PRECIOUS
|
2022-09-20 11:34:03 +00:00
|
|
|
fi
|
|
|
|
|
2023-08-13 21:38:33 +00:00
|
|
|
log_command "$@"
|
2023-06-08 21:29:22 +00:00
|
|
|
"$@" || exit
|
|
|
|
|
2023-06-10 22:50:01 +00:00
|
|
|
if [ -n "$OUTPUT" ] && [ -f "$OUTPUT" ]; then
|
|
|
|
if [ $INTENT = cc ] || [ $INTENT = ld ]; then
|
2024-01-22 18:22:16 +00:00
|
|
|
"$BIN/fixupobj" "$OUTPUT" || exit
|
2023-06-10 22:50:01 +00:00
|
|
|
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
|
2023-11-11 22:04:26 +00:00
|
|
|
"$BIN/$ARCH-linux-cosmo-objcopy" \
|
2023-08-13 21:38:33 +00:00
|
|
|
$OBJCOPYFLAGS \
|
|
|
|
"$OUTPUT.dbg" \
|
|
|
|
"$OUTPUT" || exit
|
2023-11-11 22:04:26 +00:00
|
|
|
"$BIN/zipcopy" \
|
2023-08-13 21:38:33 +00:00
|
|
|
"$OUTPUT.dbg" \
|
|
|
|
"$OUTPUT" || exit
|
2023-07-10 12:55:00 +00:00
|
|
|
elif [ $SFLAG -eq 1 ]; then
|
2023-11-11 22:04:26 +00:00
|
|
|
"$BIN/$ARCH-linux-cosmo-strip" \
|
2023-08-13 21:38:33 +00:00
|
|
|
"$OUTPUT" || exit
|
2023-06-10 22:50:01 +00:00
|
|
|
fi
|
2023-06-08 21:29:22 +00:00
|
|
|
fi
|
|
|
|
fi
|