Improve cosmocc / cosmoc++ toolchain scripts

- Get out of the red zone
- Generate --ftrace nops unless -Os is passed
- Intercept -o path to generate .com / .com.dbg appropriately
This commit is contained in:
Justine Tunney 2023-06-08 14:29:22 -07:00
parent 25678db2a0
commit 22f81a8d50
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
9 changed files with 207 additions and 127 deletions

View file

@ -357,5 +357,25 @@ static const unsigned int __tmpcosmo_B600 = 15823554;
static const unsigned int __tmpcosmo_B75 = 15823536;
static const unsigned int __tmpcosmo_B9600 = 15823504;
static const unsigned short __tmpcosmo_AF_INET6 = 58236;
static const unsigned __tmpcosmo_RLIMIT_AS = 1377206941;
static const unsigned __tmpcosmo_RLIMIT_CORE = -671407877;
static const unsigned __tmpcosmo_RLIMIT_CPU = 628127131;
static const unsigned __tmpcosmo_RLIMIT_DATA = -808550207;
static const unsigned __tmpcosmo_RLIMIT_FSIZE = -961275389;
static const unsigned __tmpcosmo_RLIMIT_LOCKS = -606515345;
static const unsigned __tmpcosmo_RLIMIT_MEMLOCK = -638383635;
static const unsigned __tmpcosmo_RLIMIT_MSGQUEUE = -200712823;
static const unsigned __tmpcosmo_RLIMIT_NICE = -1263433607;
static const unsigned __tmpcosmo_RLIMIT_NOFILE = -974136242;
static const unsigned __tmpcosmo_RLIMIT_NPROC = 1234964928;
static const unsigned __tmpcosmo_RLIMIT_NPTS = 1890977319;
static const unsigned __tmpcosmo_RLIMIT_RSS = 1507822220;
static const unsigned __tmpcosmo_RLIMIT_RTPRIO = 343598136;
static const unsigned __tmpcosmo_RLIMIT_RTTIME = 1950976769;
static const unsigned __tmpcosmo_RLIMIT_SBSIZE = 914114957;
static const unsigned __tmpcosmo_RLIMIT_SIGPENDING = 1652182385;
static const unsigned __tmpcosmo_RLIMIT_STACK = 1324847124;
static const unsigned __tmpcosmo_RLIMIT_SWAP = 1231678046;
static const unsigned __tmpcosmo_RLIMIT_VMEM = 119083983;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* ACTUALLY_MODS */

View file

@ -88,7 +88,6 @@ __gc: .ftrace2
stp q4,q5,[sp,144]
stp q6,q7,[sp,176]
mov x1,x10
bl CheckStackIsAligned
blr x9
ldp q6,q7,[sp,176]
ldp q4,q5,[sp,144]

View file

@ -83,11 +83,6 @@ const char *FindDebugBinary(void) {
if (IsMyDebugBinary(buf)) {
res = buf;
}
} else if (n + 8 < ARRAYLEN(buf)) {
mempcpy(mempcpy(buf, p, n), ".com.dbg", 9);
if (IsMyDebugBinary(buf)) {
res = buf;
}
}
if (!res) {
res = getenv("COMDBG");

View file

@ -1,51 +1,28 @@
#ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_RLIMIT_H_
#define COSMOPOLITAN_LIBC_SYSV_CONSTS_RLIMIT_H_
#include "libc/runtime/symbolic.h"
#define RLIMIT_AS SYMBOLIC(RLIMIT_AS)
#define RLIMIT_CORE SYMBOLIC(RLIMIT_CORE)
#define RLIMIT_CPU SYMBOLIC(RLIMIT_CPU)
#define RLIMIT_DATA SYMBOLIC(RLIMIT_DATA)
#define RLIMIT_FSIZE SYMBOLIC(RLIMIT_FSIZE)
#define RLIMIT_LOCKS SYMBOLIC(RLIMIT_LOCKS)
#define RLIMIT_MEMLOCK SYMBOLIC(RLIMIT_MEMLOCK)
#define RLIMIT_MSGQUEUE SYMBOLIC(RLIMIT_MSGQUEUE)
#define RLIMIT_NICE SYMBOLIC(RLIMIT_NICE)
#define RLIMIT_NOFILE SYMBOLIC(RLIMIT_NOFILE)
#define RLIMIT_NPROC SYMBOLIC(RLIMIT_NPROC)
#define RLIMIT_NPTS SYMBOLIC(RLIMIT_NPTS)
#define RLIMIT_RSS SYMBOLIC(RLIMIT_RSS)
#define RLIMIT_RTPRIO SYMBOLIC(RLIMIT_RTPRIO)
#define RLIMIT_RTTIME SYMBOLIC(RLIMIT_RTTIME)
#define RLIMIT_SBSIZE SYMBOLIC(RLIMIT_SBSIZE)
#define RLIMIT_SIGPENDING SYMBOLIC(RLIMIT_SIGPENDING)
#define RLIMIT_STACK SYMBOLIC(RLIMIT_STACK)
#define RLIMIT_SWAP SYMBOLIC(RLIMIT_SWAP)
#define RLIMIT_VMEM SYMBOLIC(RLIMIT_VMEM)
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
extern const uint64_t RLIMIT_AS;
extern const uint64_t RLIMIT_CORE;
extern const uint64_t RLIMIT_CPU;
extern const uint64_t RLIMIT_DATA;
extern const uint64_t RLIMIT_FSIZE;
extern const uint64_t RLIMIT_LOCKS;
extern const uint64_t RLIMIT_MEMLOCK;
extern const uint64_t RLIMIT_MSGQUEUE;
extern const uint64_t RLIMIT_NICE;
extern const uint64_t RLIMIT_NOFILE;
extern const uint64_t RLIMIT_NPROC;
extern const uint64_t RLIMIT_NPTS;
extern const uint64_t RLIMIT_RSS;
extern const uint64_t RLIMIT_RTPRIO;
extern const uint64_t RLIMIT_RTTIME;
extern const uint64_t RLIMIT_SBSIZE;
extern const uint64_t RLIMIT_SIGPENDING;
extern const uint64_t RLIMIT_STACK;
extern const uint64_t RLIMIT_SWAP;
extern const uint64_t RLIMIT_VMEM;
extern const unsigned RLIMIT_AS;
extern const unsigned RLIMIT_CORE;
extern const unsigned RLIMIT_CPU;
extern const unsigned RLIMIT_DATA;
extern const unsigned RLIMIT_FSIZE;
extern const unsigned RLIMIT_LOCKS;
extern const unsigned RLIMIT_MEMLOCK;
extern const unsigned RLIMIT_MSGQUEUE;
extern const unsigned RLIMIT_NICE;
extern const unsigned RLIMIT_NOFILE;
extern const unsigned RLIMIT_NPROC;
extern const unsigned RLIMIT_NPTS;
extern const unsigned RLIMIT_RSS;
extern const unsigned RLIMIT_RTPRIO;
extern const unsigned RLIMIT_RTTIME;
extern const unsigned RLIMIT_SBSIZE;
extern const unsigned RLIMIT_SIGPENDING;
extern const unsigned RLIMIT_STACK;
extern const unsigned RLIMIT_SWAP;
extern const unsigned RLIMIT_VMEM;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -1,6 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_W_H_
#define COSMOPOLITAN_LIBC_SYSV_CONSTS_W_H_
#include "libc/runtime/symbolic.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -11,8 +10,8 @@ extern const int WCONTINUED;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#define WNOHANG LITERALLY(1)
#define WUNTRACED SYMBOLIC(WUNTRACED)
#define WCONTINUED SYMBOLIC(WCONTINUED)
#define WNOHANG 1
#define WUNTRACED WUNTRACED
#define WCONTINUED WCONTINUED
#endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_W_H_ */

View file

@ -38,7 +38,7 @@ FLAGS\n\
const char *prog;
static void Print(int fd, const char *s, ...) {
nullterminated() static void Print(int fd, const char *s, ...) {
va_list va;
char buf[2048];
va_start(va, s);

View file

@ -27,8 +27,6 @@
#include "libc/elf/struct/sym.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/mem/gc.internal.h"
@ -58,7 +56,7 @@ Usage: fixupobj.com [-h] ARGS...\n\
#define MRS_TPIDR_EL0 0xd53bd040u
#define MOV_REG(DST, SRC) (0xaa0003e0u | (SRC) << 16 | (DST))
const unsigned char kFatNops[8][8] = {
static const unsigned char kFatNops[8][8] = {
{}, //
{0x90}, // nop
{0x66, 0x90}, // xchg %ax,%ax
@ -69,16 +67,16 @@ const unsigned char kFatNops[8][8] = {
{0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00}, // nopl 0x00000000(%rax)
};
int mode;
char *symstrs;
char *secstrs;
ssize_t esize;
Elf64_Sym *syms;
const char *epath;
Elf64_Xword symcount;
const Elf64_Ehdr *elf;
static int mode;
static char *symstrs;
static char *secstrs;
static ssize_t esize;
static Elf64_Sym *syms;
static const char *epath;
static Elf64_Xword symcount;
static const Elf64_Ehdr *elf;
void Print(int fd, const char *s, ...) {
nullterminated() static void Print(int fd, const char *s, ...) {
va_list va;
char buf[2048];
va_start(va, s);
@ -90,14 +88,14 @@ void Print(int fd, const char *s, ...) {
va_end(va);
}
wontreturn void SysExit(const char *func) {
static wontreturn void SysExit(const char *func) {
const char *errstr;
if (!(errstr = _strerdoc(errno))) errstr = "EUNKNOWN";
Print(2, epath, ": ", func, " failed with ", errstr, "\n", NULL);
exit(1);
}
void GetOpts(int argc, char *argv[]) {
static void GetOpts(int argc, char *argv[]) {
int opt;
mode = O_RDWR;
while ((opt = getopt(argc, argv, GETOPTS)) != -1) {
@ -116,7 +114,7 @@ void GetOpts(int argc, char *argv[]) {
}
}
Elf64_Shdr *FindElfSectionByName(const char *name) {
static Elf64_Shdr *FindElfSectionByName(const char *name) {
long i;
Elf64_Shdr *shdr;
for (i = 0; i < elf->e_shnum; ++i) {
@ -128,7 +126,7 @@ Elf64_Shdr *FindElfSectionByName(const char *name) {
return 0;
}
void CheckPrivilegedCrossReferences(void) {
static void CheckPrivilegedCrossReferences(void) {
long i, x;
Elf64_Shdr *shdr;
const char *secname;
@ -156,7 +154,7 @@ void CheckPrivilegedCrossReferences(void) {
}
// Modify ARM64 code to use x28 for TLS rather than tpidr_el0.
void RewriteTlsCode(void) {
static void RewriteTlsCode(void) {
int i, dest;
Elf64_Shdr *shdr;
uint32_t *p, *pe;
@ -185,7 +183,7 @@ void RewriteTlsCode(void) {
* In order for this to work, the function symbol must be declared as
* `STT_FUNC` and `st_size` must have the function's byte length.
*/
void OptimizePatchableFunctionEntries(void) {
static void OptimizePatchableFunctionEntries(void) {
#ifdef __x86_64__
long i, n;
int nopcount;
@ -210,7 +208,7 @@ void OptimizePatchableFunctionEntries(void) {
#endif /* __x86_64__ */
}
void OptimizeRelocations(void) {
static void OptimizeRelocations(void) {
Elf64_Half i;
Elf64_Rela *rela;
unsigned char *code, *p;
@ -218,11 +216,9 @@ void OptimizeRelocations(void) {
for (i = 0; i < elf->e_shnum; ++i) {
shdr = GetElfSectionHeaderAddress(elf, esize, i);
if (shdr->sh_type == SHT_RELA) {
CHECK_EQ(sizeof(struct Elf64_Rela), shdr->sh_entsize);
CHECK_NOTNULL(
(shdrcode = GetElfSectionHeaderAddress(elf, esize, shdr->sh_info)));
shdrcode = GetElfSectionHeaderAddress(elf, esize, shdr->sh_info);
if (!(shdrcode->sh_flags & SHF_EXECINSTR)) continue;
CHECK_NOTNULL((code = GetElfSectionAddress(elf, esize, shdrcode)));
code = GetElfSectionAddress(elf, esize, shdrcode);
for (rela = GetElfSectionAddress(elf, esize, shdr);
((uintptr_t)rela + shdr->sh_entsize <=
MIN((uintptr_t)elf + esize,
@ -269,7 +265,7 @@ void OptimizeRelocations(void) {
}
}
void FixupObject(void) {
static void FixupObject(void) {
int fd;
if ((fd = open(epath, mode)) == -1) {
SysExit("open");

View file

@ -19,6 +19,9 @@
# make install
#
COSMO=/opt/cosmo
COSMOS=/opt/cosmos
if [ "$1" = "--version" ]; then
cat <<'EOF'
x86_64-unknown-cosmo-g++ (GCC) 11.2.0
@ -29,70 +32,115 @@ EOF
exit 0
fi
CXX="/opt/cosmo/o/third_party/gcc/bin/x86_64-linux-musl-g++"
CCFLAGS="-g -fdata-sections -ffunction-sections -fno-pie -pg -mnop-mcount -mno-tls-direct-seg-refs -fportcosmo -include /opt/cosmo/build/portcosmo.h"
CXX="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-g++"
OBJCOPY="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-objcopy"
CCFLAGS="-g -fdata-sections -ffunction-sections -fno-pie -mno-tls-direct-seg-refs -mno-red-zone -fportcosmo -include $COSMO/build/portcosmo.h"
CXXFLAGS="-fno-exceptions -fuse-cxa-atexit -fno-threadsafe-statics"
CPPFLAGS="-DNDEBUG -nostdinc -iquote /opt/cosmo -isystem /opt/cosmos/include -isystem /opt/cosmo/libc/isystem -include libc/integral/normalize.inc"
LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd -Wl,-melf_x86_64 -Wl,--gc-sections -L/opt/cosmos/lib -Wl,-T,/opt/cosmo/o/ape/public/ape.lds /opt/cosmo/o/ape/ape-no-modify-self.o /opt/cosmo/o/libc/crt/crt.o"
LDLIBS="/opt/cosmo/o/third_party/libcxx/libcxx.a /opt/cosmo/o/cosmopolitan.a"
CPPFLAGS="-DNDEBUG -nostdinc -iquote /opt/cosmo -isystem $COSMOS/include -isystem $COSMO/libc/isystem -include libc/integral/normalize.inc"
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/third_party/libcxx/libcxx.a $COSMO/o/cosmopolitan.a"
if [ ! -d $COSMO ]; then
echo you need to checkout cosmopolitan to your $COSMO directory >&2
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
if [ ! -d "$COSMOS" ]; then
echo "you need to create your $COSMOS directory" >&2
exit 1
fi
# auto-install some shell libraries
if [ ! -d $COSMOS/lib ]; then
mkdir $COSMOS/lib
if [ ! -d "$COSMOS/lib" ]; then
mkdir "$COSMOS/lib"
for lib in c dl gcc_s m pthread resolv rt z stdc++; do
printf '\041\074\141\162\143\150\076\012' >$COSMOS/lib/lib$lib.a
printf '\041\074\141\162\143\150\076\012' >"$COSMOS/lib/lib$lib.a"
done
fi
OPT=
HAS_C=0
HAS_O=0
HAS_E=0
FIRST=1
OUTPUT=
NEED_OUTPUT=
FRAME=-fno-omit-frame-pointer
for x; do
if [ $FIRST -eq 1 ]; then
set --
FIRST=0
fi
if [ "$x" = "-Werror" ]; then
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
fi
if [ "$x" = "-pedantic" ]; then
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
fi
if [ "$x" = "-c" ]; then
elif [ x"$x" != x"${x#-O}" ]; then
OPT=$x
elif [ x"$x" = x"-c" ]; then
HAS_C=1
fi
if [ "$x" = "-E" ]; then
elif [ x"$x" = x"-E" ]; then
HAS_E=1
fi
if [ "$x" = "-o" ] || [ "${x#-o}" != "$x" ]; then
elif [ x"$x" = x"-o" ]; then
HAS_O=1
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
fi
set -- "$@" "$x"
done
if [ x"$OPT" != x"-Os" ]; then
# support --ftrace unless optimizing for size
CXXFLAGS="$CXXFLAGS -fpatchable-function-entry=18,16"
fi
LINKING=
if [ "$HAS_E" = "1" ]; then
set -- $CCFLAGS $CPPFLAGS "$@"
elif [ "$HAS_C" = "1" ]; then
set -- $CCFLAGS $CXXFLAGS $CPPFLAGS "$@" -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
set -- $CCFLAGS $CXXFLAGS $CPPFLAGS "$@" $FRAME
else
set -- $LDFLAGS $CXXFLAGS $CPPFLAGS "$@" $LDLIBS -Wl,-z,common-page-size=65536 -Wl,-z,max-page-size=65536
LINKING=1
set -- $LDFLAGS $CXXFLAGS $CPPFLAGS "$@" $LDLIBS -Wl,-z,common-page-size=65536 -Wl,-z,max-page-size=65536 $FRAME
fi
set -- "$CXX" "$@"
printf '(cd %s; %s)\n' "$PWD" "$*" >>/tmp/build.log
exec "$@"
"$@" || 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

View file

@ -32,69 +32,115 @@ EOF
exit 0
fi
CC="/opt/cosmo/o/third_party/gcc/bin/x86_64-linux-musl-gcc"
CFLAGS="-g -fdata-sections -ffunction-sections -fno-pie -pg -mnop-mcount -mno-tls-direct-seg-refs -fportcosmo -include /opt/cosmo/build/portcosmo.h"
CPPFLAGS="-DNDEBUG -nostdinc -iquote /opt/cosmo -isystem $COSMOS/include -isystem /opt/cosmo/libc/isystem -include 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 -L$COSMOS/lib -Wl,-T,/opt/cosmo/o/ape/public/ape.lds /opt/cosmo/o/ape/ape-no-modify-self.o /opt/cosmo/o/libc/crt/crt.o"
LDLIBS="/opt/cosmo/o/cosmopolitan.a"
CC="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-gcc"
OBJCOPY="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-objcopy"
CCFLAGS="-g -fdata-sections -ffunction-sections -fno-pie -mno-tls-direct-seg-refs -mno-red-zone -fportcosmo -include $COSMO/build/portcosmo.h"
CFLAGS=
CPPFLAGS="-DNDEBUG -nostdinc -iquote /opt/cosmo -isystem $COSMOS/include -isystem $COSMO/libc/isystem -include libc/integral/normalize.inc"
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"
if [ ! -d $COSMO ]; then
echo you need to checkout cosmopolitan to your $COSMO directory >&2
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
if [ ! -d "$COSMOS" ]; then
echo "you need to create your $COSMOS directory" >&2
exit 1
fi
# auto-install some shell libraries
if [ ! -d $COSMOS/lib ]; then
mkdir $COSMOS/lib
if [ ! -d "$COSMOS/lib" ]; then
mkdir "$COSMOS/lib"
for lib in c dl gcc_s m pthread resolv rt z stdc++; do
printf '\041\074\141\162\143\150\076\012' >$COSMOS/lib/lib$lib.a
printf '\041\074\141\162\143\150\076\012' >"$COSMOS/lib/lib$lib.a"
done
fi
OPT=
HAS_C=0
HAS_O=0
HAS_E=0
FIRST=1
OUTPUT=
NEED_OUTPUT=
FRAME=-fno-omit-frame-pointer
for x; do
if [ $FIRST -eq 1 ]; then
set --
FIRST=0
fi
if [ "$x" = "-Werror" ]; then
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
fi
if [ "$x" = "-pedantic" ]; then
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
fi
if [ "$x" = "-c" ]; then
elif [ x"$x" != x"${x#-O}" ]; then
OPT=$x
elif [ x"$x" = x"-c" ]; then
HAS_C=1
fi
if [ "$x" = "-E" ]; then
elif [ x"$x" = x"-E" ]; then
HAS_E=1
fi
if [ "$x" = "-o" ] || [ "${x#-o}" != "$x" ]; then
elif [ x"$x" = x"-o" ]; then
HAS_O=1
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
fi
set -- "$@" "$x"
done
if [ x"$OPT" != x"-Os" ]; then
# support --ftrace unless optimizing for size
CFLAGS="$CFLAGS -fpatchable-function-entry=18,16"
fi
LINKING=
if [ "$HAS_E" = "1" ]; then
set -- $CPPFLAGS "$@"
set -- $CCFLAGS $CPPFLAGS "$@"
elif [ "$HAS_C" = "1" ]; then
set -- $CFLAGS $CPPFLAGS "$@" -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
set -- $CCFLAGS $CFLAGS $CPPFLAGS "$@" $FRAME
else
set -- $LDFLAGS $CFLAGS $CPPFLAGS "$@" $LDLIBS -Wl,-z,common-page-size=65536 -Wl,-z,max-page-size=65536
LINKING=1
set -- $LDFLAGS $CFLAGS $CPPFLAGS "$@" $LDLIBS -Wl,-z,common-page-size=65536 -Wl,-z,max-page-size=65536 $FRAME
fi
set -- "$CC" "$@"
printf '(cd %s; %s)\n' "$PWD" "$*" >>/tmp/build.log
exec "$@"
"$@" || 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