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

@ -77,6 +77,8 @@
"\n" \
" -o OUTPUT set output path\n" \
"\n" \
" -s never embed symbol table\n" \
"\n" \
" -l PATH bundle ape loader executable [repeatable]\n" \
" if no ape loaders are specified then your\n" \
" executable will self-modify its header on\n" \
@ -86,7 +88,7 @@
" processors running the xnu kernel so that\n" \
" it can be compiled on the fly by xcode\n" \
"\n" \
" -s BITS set OS support vector\n" \
" -V BITS set OS support vector\n" \
"\n" \
" the default value is -1 which sets the bits\n" \
" for all supported operating systems to true\n" \
@ -232,6 +234,7 @@ struct Assets {
static int outfd;
static int hashes;
static const char *prog;
static bool want_stripped;
static int support_vector;
static int macholoadcount;
static const char *outpath;
@ -246,6 +249,7 @@ static bool dont_path_lookup_ape_loader;
_Alignas(4096) static char prologue[1048576];
static uint8_t hashpool[BLAKE2B256_DIGEST_LENGTH];
static const char *macos_silicon_loader_source_path;
static const char *macos_silicon_loader_source_text;
static char *macos_silicon_loader_source_ddarg_skip;
static char *macos_silicon_loader_source_ddarg_size;
@ -329,6 +333,16 @@ static const char *DescribePhdrType(Elf64_Word p_type) {
}
}
static bool IsBinary(const char *p, size_t n) {
size_t i;
for (i = 0; i < n; ++i) {
if ((p[i] & 255) < '\t') {
return true;
}
}
return false;
}
static void BlendHashes(uint8_t out[static BLAKE2B256_DIGEST_LENGTH],
uint8_t inp[static BLAKE2B256_DIGEST_LENGTH]) {
int i;
@ -349,6 +363,28 @@ static void HashInputString(const char *str) {
HashInput(str, strlen(str));
}
static char *LoadSourceCode(const char *path) {
int fd;
size_t i;
char *text;
ssize_t rc, size;
if ((fd = open(path, O_RDONLY)) == -1) DieSys(path);
if ((size = lseek(fd, 0, SEEK_END)) == -1) DieSys(path);
text = Malloc(size + 1);
text[size] = 0;
for (i = 0; i < size; i += rc) {
if ((rc = pread(fd, text, size - i, i)) <= 0) {
DieSys(path);
}
if (IsBinary(text, rc)) {
Die(path, "source code contains binary data");
}
}
if (close(fd)) DieSys(path);
HashInput(text, size);
return text;
}
static void Pwrite(const void *data, size_t size, uint64_t offset) {
ssize_t rc;
const char *p, *e;
@ -928,13 +964,17 @@ static void GetOpts(int argc, char *argv[]) {
char *endptr;
int opt, bits;
bool got_support_vector = false;
while ((opt = getopt(argc, argv, "hvgGBo:s:l:S:M:")) != -1) {
while ((opt = getopt(argc, argv, "hvgsGBo:l:S:M:V:")) != -1) {
switch (opt) {
case 'o':
outpath = optarg;
break;
case 's':
HashInputString("-s");
want_stripped = true;
break;
case 'V':
HashInputString("-V");
HashInputString(optarg);
if (ParseSupportVector(optarg, &bits)) {
support_vector |= bits;
@ -945,22 +985,30 @@ static void GetOpts(int argc, char *argv[]) {
got_support_vector = true;
break;
case 'l':
HashInputString("-l");
AddLoader(optarg);
break;
case 'S':
HashInputString("-S");
HashInputString(optarg);
custom_sh_code = optarg;
break;
case 'B':
HashInputString("-B");
force_bypass_binfmt_misc = true;
break;
case 'g':
HashInputString("-g");
generate_debuggable_binary = true;
break;
case 'G':
HashInputString("-G");
dont_path_lookup_ape_loader = true;
break;
case 'M':
HashInputString("-M");
macos_silicon_loader_source_path = optarg;
macos_silicon_loader_source_text = LoadSourceCode(optarg);
break;
case 'v':
tinyprint(0, VERSION, NULL);
@ -1740,7 +1788,7 @@ static void CopyZips(Elf64_Off offset) {
int main(int argc, char *argv[]) {
char *p;
int i, opt;
int i, j, opt;
Elf64_Off offset;
char empty[64] = {0};
Elf64_Xword prologue_bytes;
@ -1776,11 +1824,26 @@ int main(int argc, char *argv[]) {
for (i = 0; i < loaders.n; ++i) {
OpenLoader(loaders.p + i);
}
for (i = 0; i < loaders.n; ++i) {
for (j = i + 1; j < loaders.n; ++j) {
if (loaders.p[i].os == loaders.p[j].os &&
loaders.p[i].machine == loaders.p[j].machine) {
Die(prog, "multiple ape loaders specified for the same platform");
}
}
}
// open input files
for (i = optind; i < argc; ++i) {
OpenInput(argv[i]);
}
for (i = 0; i < inputs.n; ++i) {
for (j = i + 1; j < inputs.n; ++j) {
if (inputs.p[i].elf->e_machine == inputs.p[j].elf->e_machine) {
Die(prog, "multiple executables passed for the same machine type");
}
}
}
// validate input files
for (i = 0; i < inputs.n; ++i) {
@ -1789,15 +1852,17 @@ int main(int argc, char *argv[]) {
}
// load symbols
for (i = 0; i < inputs.n; ++i) {
struct Input *in = inputs.p + i;
if (GetElfSymbol(in, "__zipos_get")) {
LoadSymbols(in->elf, in->size, in->path);
} else {
tinyprint(2, in->path,
": warning: won't generate symbol table unless "
"__static_yoink(\"zipos\") is linked\n",
NULL);
if (!want_stripped) {
for (i = 0; i < inputs.n; ++i) {
struct Input *in = inputs.p + i;
if (GetElfSymbol(in, "__zipos_get")) {
LoadSymbols(in->elf, in->size, in->path);
} else {
tinyprint(2, in->path,
": warning: won't generate symbol table unless "
"__static_yoink(\"zipos\") is linked\n",
NULL);
}
}
}
@ -2046,11 +2111,7 @@ int main(int argc, char *argv[]) {
}
p = stpcpy(p, "fi\n"); // if [ -d /Applications ]; then
} else {
if (macos_silicon_loader_source_path) {
Die(macos_silicon_loader_source_path,
"won't embed macos arm64 ape loader source code because xnu isn't "
"in the support vector");
}
macos_silicon_loader_source_path = 0;
}
// extract the ape loader for open platforms
@ -2131,25 +2192,16 @@ int main(int argc, char *argv[]) {
// concatenate ape loader source code
if (macos_silicon_loader_source_path) {
int fd;
char *map;
ssize_t size;
char *compressed_data;
size_t compressed_size;
fd = open(macos_silicon_loader_source_path, O_RDONLY);
if (fd == -1) DieSys(macos_silicon_loader_source_path);
size = lseek(fd, 0, SEEK_END);
if (size == -1) DieSys(macos_silicon_loader_source_path);
map = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (map == MAP_FAILED) DieSys(macos_silicon_loader_source_path);
compressed_data = Gzip(map, size, &compressed_size);
compressed_data =
Gzip(macos_silicon_loader_source_text,
strlen(macos_silicon_loader_source_text), &compressed_size);
FixupWordAsDecimal(macos_silicon_loader_source_ddarg_skip, offset);
FixupWordAsDecimal(macos_silicon_loader_source_ddarg_size, compressed_size);
Pwrite(compressed_data, compressed_size, offset);
offset += compressed_size;
free(compressed_data);
munmap(map, size);
close(fd);
}
// add the zip files

184
tool/build/march-native.c Normal file
View file

@ -0,0 +1,184 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/nexgen32e/x86info.h"
#include "libc/runtime/runtime.h"
#include "third_party/getopt/getopt.internal.h"
/**
* @fileoverview Command for printing `-march=native` flags.
*
* In recent years (2023) compilers have decided to remove support for
* the `-march=native` flag, even on x86. That's unfortunate, since as
* we can see below, grokking all the various microarchitecture is not
* something a compiler should reasonably expect from users especially
* not for a flag as important as this one, which can have a night and
* day impact for apps that do scientific computing.
*
* This is a tiny program, that makes it easy for shell scripts to get
* these flags.
*/
#define VERSION \
"-march=native flag printer v0.1\n" \
"copyright 2023 justine alexandra roberts tunney\n"
#define USAGE \
"usage: march-native.com [-hvc]\n" \
" -h show help\n" \
" -v show version\n" \
" -c assume we're using clang (not gcc)\n"
static bool isclang;
static void GetOpts(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "hvc")) != -1) {
switch (opt) {
case 'c':
isclang = true;
break;
case 'v':
tinyprint(1, VERSION, NULL);
exit(0);
case 'h':
tinyprint(1, VERSION, USAGE, NULL);
exit(0);
default:
tinyprint(2, VERSION, USAGE, NULL);
exit(1);
}
}
}
static void Puts(const char *s) {
tinyprint(1, s, "\n", NULL);
}
int main(int argc, char *argv[]) {
GetOpts(argc, argv);
#ifdef __x86_64__
struct X86ProcessorModel *model;
if (X86_HAVE(XOP)) Puts("-mxop");
if (X86_HAVE(SSE4A)) Puts("-msse4a");
if (X86_HAVE(SSE3)) Puts("-msse3");
if (X86_HAVE(SSSE3)) Puts("-mssse3");
if (X86_HAVE(SSE4_1)) Puts("-msse4.1");
if (X86_HAVE(SSE4_2)) Puts("-msse4.2");
if (X86_HAVE(AVX)) Puts("-mavx");
if (X86_HAVE(AVX2)) {
Puts("-mavx2");
if (!isclang) {
Puts("-msse2avx");
Puts("-Wa,-msse2avx");
}
}
if (X86_HAVE(AVX512F)) Puts("-mavx512f");
if (X86_HAVE(AVX512PF)) Puts("-mavx512pf");
if (X86_HAVE(AVX512ER)) Puts("-mavx512er");
if (X86_HAVE(AVX512CD)) Puts("-mavx512cd");
if (X86_HAVE(AVX512VL)) Puts("-mavx512vl");
if (X86_HAVE(AVX512BW)) Puts("-mavx512bw");
if (X86_HAVE(AVX512DQ)) Puts("-mavx512dq");
if (X86_HAVE(AVX512IFMA)) Puts("-mavx512ifma");
if (X86_HAVE(AVX512VBMI)) Puts("-mavx512vbmi");
if (X86_HAVE(SHA)) Puts("-msha");
if (X86_HAVE(AES)) Puts("-maes");
if (X86_HAVE(VAES)) Puts("-mvaes");
if (X86_HAVE(PCLMUL)) Puts("-mpclmul");
if (X86_HAVE(FSGSBASE)) Puts("-mfsgsbase");
if (X86_HAVE(F16C)) Puts("-mf16c");
if (X86_HAVE(FMA)) Puts("-mfma");
if (X86_HAVE(POPCNT)) Puts("-mpopcnt");
if (X86_HAVE(BMI)) Puts("-mbmi");
if (X86_HAVE(BMI2)) Puts("-mbmi2");
if (X86_HAVE(ADX)) Puts("-madx");
if (X86_HAVE(FXSR)) Puts("-mfxsr");
if ((model = getx86processormodel(kX86ProcessorModelKey))) {
switch (model->march) {
case X86_MARCH_CORE2:
Puts("-march=core2");
break;
case X86_MARCH_NEHALEM:
Puts("-march=nehalem");
break;
case X86_MARCH_WESTMERE:
Puts("-march=westmere");
break;
case X86_MARCH_SANDYBRIDGE:
Puts("-march=sandybridge");
break;
case X86_MARCH_IVYBRIDGE:
Puts("-march=ivybridge");
break;
case X86_MARCH_HASWELL:
Puts("-march=haswell");
break;
case X86_MARCH_BROADWELL:
Puts("-march=broadwell");
break;
case X86_MARCH_SKYLAKE:
case X86_MARCH_KABYLAKE:
Puts("-march=skylake");
break;
case X86_MARCH_CANNONLAKE:
Puts("-march=cannonlake");
break;
case X86_MARCH_ICELAKE:
if (model->grade >= X86_GRADE_SERVER) {
Puts("-march=icelake-server");
} else {
Puts("-march=icelake-client");
}
break;
case X86_MARCH_TIGERLAKE:
Puts("-march=tigerlake");
break;
case X86_MARCH_BONNELL:
case X86_MARCH_SALTWELL:
Puts("-march=bonnell");
break;
case X86_MARCH_SILVERMONT:
case X86_MARCH_AIRMONT:
Puts("-march=silvermont");
break;
case X86_MARCH_GOLDMONT:
Puts("-march=goldmont");
break;
case X86_MARCH_GOLDMONTPLUS:
Puts("-march=goldmont-plus");
break;
case X86_MARCH_TREMONT:
Puts("-march=tremont");
break;
case X86_MARCH_KNIGHTSLANDING:
Puts("-march=knl");
break;
case X86_MARCH_KNIGHTSMILL:
Puts("-march=knm");
break;
}
}
#elif defined(__aarch64__)
// TODO(jart): How can we determine CPU features on AARCH64?
#else
// otherwise do nothing (it's usually best)
#endif
}

View file

@ -54,7 +54,7 @@ o/$(MODE)/tool/hello/hello-elf.com: \
o/$(MODE)/tool/hello/hello.com.dbg \
o/$(MODE)/tool/build/apelink.com \
o/$(MODE)/ape/ape.elf
@$(COMPILE) -ALINK.ape o/$(MODE)/tool/build/apelink.com -s elf -o $@ -l o/$(MODE)/ape/ape.elf $<
@$(COMPILE) -ALINK.ape o/$(MODE)/tool/build/apelink.com -V elf -o $@ -l o/$(MODE)/ape/ape.elf $<
# uses apelink to generate non-pe ape executable
# support vector = macos/linux/freebsd/openbsd/netbsd
@ -64,7 +64,7 @@ o/$(MODE)/tool/hello/hello-unix.com: \
o/$(MODE)/tool/hello/hello.com.dbg \
o/$(MODE)/tool/build/apelink.com \
o/$(MODE)/ape/ape.elf
@$(COMPILE) -ALINK.ape o/$(MODE)/tool/build/apelink.com -s unix -o $@ -l o/$(MODE)/ape/ape.elf $<
@$(COMPILE) -ALINK.ape o/$(MODE)/tool/build/apelink.com -V unix -o $@ -l o/$(MODE)/ape/ape.elf $<
# elf2pe generates optimal pe binaries
# windows is the only platform supported

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