Include more programs in cosmocc

The Cosmopolitan Compiler Collection now includes the following programs

- `ar.ape` is a faster alternative to `ar rcsD` for creating determistic
  static archives. It's ~10x faster than GNU because it isn't quadratic.
  It'll even outperform LLVM ar by 2x, thanks to writev/copy_file_range.

- `sha256sum.ape` is a faster alternative to the `sha256sum` command. It
  goes 2x faster since it leverages vectorized assembly implementations.

- `resymbol` is a brand new program we invented, like objcopy, that lets
  you rename all the global symbols in a .o file to have a new suffix or
  prefix. In the future, this will be used by cosmocc automatically when
  building -O3 math kernels, that need to be vectorized for all hardware

- `gzip.ape` is a faster version of the `gzip` command, that is included
  by most Linux distros. It gains better performance using Chromium Zlib
  which, once again, includes highly optimized assembly, that Mark Adler
  won't merge into the official MS-DOS compatible zlib codebase.

- `cocmd` is the cosmopolitan shell. It can function as a faster `sh -c`
  alternative than bash and dash as the `SHELL = /opt/cosmocc/bin/cocmd`
  at the top of your Makefile. Please note you should be using the cosmo
  fork of GNU make (already included), since normal make won't recognize
  this as a bourne-compatible shell and remove the execve() optimization
  which makes things slower. In some ways that's true. This doesn't have
  a complete POSIX shell implementation. However it's enough for cosmo's
  mono repo. It also implements faster behaviors in some respects.

The following programs are also introduced, which aren't as interesting.
The main reason why they're here is so Cosmopolitan's mono repo shall be
able to remove build/bootstrap/ in future editions. That way we can keep
build utilities better up to date, without bloating the git history much

- `chmod.ape` for hermeticity
- `cp.ape` for hermeticity
- `echo.ape` for hermeticity
- `objbincopy` is an objcopy-like tool that's used to build ape loader
- `package.ape` is used for strict dependency checking of object graph
- `rm.ape` for hermeticity
- `touch.ape` for hermeticity
This commit is contained in:
Justine Tunney 2024-07-01 01:32:25 -07:00
parent 78d3b86ec7
commit ca4cf67eb8
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
4 changed files with 35 additions and 178 deletions

Binary file not shown.

View file

@ -427,6 +427,7 @@ is_bourne_compatible_shell (const char *path)
"sh", "sh",
"bash", "bash",
"dash", "dash",
"cocmd",
"ksh", "ksh",
"rksh", "rksh",
"zsh", "zsh",

View file

@ -1,177 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 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/calls/struct/stat.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/append.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "tool/build/lib/getargs.h"
#include "tool/build/lib/interner.h"
#define LOOKINGAT(p, pe, s) LookingAt(p, pe, s, strlen(s))
struct Visited {
size_t i, n;
const char **p;
};
char *output;
const char *prog;
struct Interner *visited;
void Visit(const char *);
static wontreturn void Die(const char *reason) {
tinyprint(2, prog, ": ", reason, "\n", NULL);
exit(1);
}
static wontreturn void DieSys(const char *thing) {
perror(thing);
exit(1);
}
static wontreturn void DieOom(void) {
Die("out of memory");
}
static void Appends(char **b, const char *s) {
if (appends(b, s) == -1)
DieOom();
}
static void Appendd(char **b, const void *p, size_t n) {
if (appendd(b, p, n) == -1)
DieOom();
}
size_t GetFdSize(int fd) {
struct stat st;
if (fstat(fd, &st)) {
DieSys("fstat");
}
return st.st_size;
}
bool LookingAt(const char *p, const char *pe, const char *s, size_t n) {
return pe - p >= n && memcmp(p, s, n) == 0;
}
void Process(const char *p, const char *pe, const char *path, bool isheader) {
int level;
bool noformat;
const char *p2, *dq, *name;
for (noformat = false, level = 0; p < pe; p = p2) {
p2 = memchr(p, '\n', pe - p);
p2 = p2 ? p2 + 1 : pe;
if (LOOKINGAT(p, pe, "#if")) {
if (isheader && !level++)
continue;
}
if (LOOKINGAT(p, pe, "#endif")) {
if (isheader && !--level)
continue;
}
if (LOOKINGAT(p, pe, "/* clang-format off */")) {
noformat = true;
} else if (LOOKINGAT(p, pe, "/* clang-format on */")) {
noformat = false;
}
if (LOOKINGAT(p, pe, "#include \"")) {
name = p + strlen("#include \"");
dq = memchr(name, '"', pe - name);
if (dq) {
Visit(strndup(name, dq - name));
continue;
}
}
Appendd(&output, p, p2 - p);
}
if (noformat) {
Appends(&output, "/* clang-format on */\n");
}
}
void Visit(const char *path) {
int fd;
char *map;
size_t size;
bool isheader;
if (!endswith(path, ".h") && !endswith(path, ".inc"))
return;
if (endswith(path, ".internal.h"))
return;
if (endswith(path, "/internal.h"))
return;
if (endswith(path, ".internal.inc"))
return;
if (endswith(path, "/internal.inc"))
return;
if (startswith(path, "libc/isystem/"))
return;
isheader = endswith(path, ".h");
if (isheader && isinterned(visited, path))
return;
Appends(&output, "\n\f\n/*!BEGIN ");
Appends(&output, path);
Appends(&output, " */\n\n");
intern(visited, path);
if ((fd = open(path, O_RDONLY)) == -1)
DieSys(path);
if ((size = GetFdSize(fd))) {
map = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (map == MAP_FAILED)
DieSys(path);
Process(map, map + size, path, isheader);
if (munmap(map, size))
DieSys(path);
}
if (close(fd))
DieSys(path);
}
int main(int argc, char *argv[]) {
size_t bytes;
const char *src;
struct GetArgs ga;
prog = argv[0];
if (!prog)
prog = "rollup";
visited = newinterner();
Appends(&output, "#ifndef COSMOPOLITAN_H_\n");
Appends(&output, "#define COSMOPOLITAN_H_\n");
getargs_init(&ga, argv + 1);
while ((src = getargs_next(&ga))) {
Visit(src);
}
getargs_destroy(&ga);
Appends(&output, "\n");
Appends(&output, "#endif /* COSMOPOLITAN_H_ */\n");
bytes = appendz(output).i;
if (write(1, output, bytes) != bytes) {
DieSys(prog);
}
freeinterner(visited);
return 0;
}

View file

@ -44,6 +44,19 @@ make -j64 m=$AMD64 \
o/$AMD64/tool/build/zipobj.dbg \ o/$AMD64/tool/build/zipobj.dbg \
o/$AMD64/tool/build/apelink.dbg \ o/$AMD64/tool/build/apelink.dbg \
o/$AMD64/tool/build/pecheck.dbg \ o/$AMD64/tool/build/pecheck.dbg \
o/$AMD64/tool/build/ar.dbg \
o/$AMD64/tool/build/chmod.dbg \
o/$AMD64/tool/build/cocmd.dbg \
o/$AMD64/tool/build/compile.dbg \
o/$AMD64/tool/build/cp.dbg \
o/$AMD64/tool/build/echo.dbg \
o/$AMD64/tool/build/gzip.dbg \
o/$AMD64/tool/build/objbincopy.dbg \
o/$AMD64/tool/build/package.dbg \
o/$AMD64/tool/build/rm.dbg \
o/$AMD64/tool/build/touch.dbg \
o/$AMD64/tool/build/sha256sum.dbg \
o/$AMD64/tool/build/resymbol.dbg \
o/$AMD64/third_party/make/make.dbg \ o/$AMD64/third_party/make/make.dbg \
o/$AMD64/third_party/ctags/ctags.dbg o/$AMD64/third_party/ctags/ctags.dbg
@ -64,6 +77,19 @@ make -j64 m=$ARM64 \
o/$ARM64/tool/build/zipobj.dbg \ o/$ARM64/tool/build/zipobj.dbg \
o/$ARM64/tool/build/apelink.dbg \ o/$ARM64/tool/build/apelink.dbg \
o/$ARM64/tool/build/pecheck.dbg \ o/$ARM64/tool/build/pecheck.dbg \
o/$ARM64/tool/build/ar.dbg \
o/$ARM64/tool/build/chmod.dbg \
o/$ARM64/tool/build/cocmd.dbg \
o/$ARM64/tool/build/compile.dbg \
o/$ARM64/tool/build/cp.dbg \
o/$ARM64/tool/build/echo.dbg \
o/$ARM64/tool/build/gzip.dbg \
o/$ARM64/tool/build/objbincopy.dbg \
o/$ARM64/tool/build/package.dbg \
o/$ARM64/tool/build/rm.dbg \
o/$ARM64/tool/build/touch.dbg \
o/$ARM64/tool/build/sha256sum.dbg \
o/$ARM64/tool/build/resymbol.dbg \
o/$ARM64/third_party/make/make.dbg \ o/$ARM64/third_party/make/make.dbg \
o/$ARM64/third_party/ctags/ctags.dbg o/$ARM64/third_party/ctags/ctags.dbg
@ -146,7 +172,9 @@ cp -af tool/cosmocc/bin/* "$OUTDIR/bin/"
cp -f o/$AMD64/ape/ape.elf "$OUTDIR/bin/ape-x86_64.elf" cp -f o/$AMD64/ape/ape.elf "$OUTDIR/bin/ape-x86_64.elf"
cp -f o/$AMD64/ape/ape.macho "$OUTDIR/bin/ape-x86_64.macho" cp -f o/$AMD64/ape/ape.macho "$OUTDIR/bin/ape-x86_64.macho"
cp -f o/$ARM64/ape/ape.elf "$OUTDIR/bin/ape-aarch64.elf" cp -f o/$ARM64/ape/ape.elf "$OUTDIR/bin/ape-aarch64.elf"
for x in assimilate march-native mktemper fixupobj zipcopy apelink pecheck mkdeps zipobj; do
for x in assimilate march-native mktemper fixupobj zipcopy apelink pecheck mkdeps zipobj \
ar chmod cocmd cp echo gzip objbincopy package rm touch sha256sum resymbol; do
ape $APELINK \ ape $APELINK \
-l o/$AMD64/ape/ape.elf \ -l o/$AMD64/ape/ape.elf \
-l o/$ARM64/ape/ape.elf \ -l o/$ARM64/ape/ape.elf \
@ -155,6 +183,11 @@ for x in assimilate march-native mktemper fixupobj zipcopy apelink pecheck mkdep
o/$AMD64/tool/build/$x.dbg \ o/$AMD64/tool/build/$x.dbg \
o/$ARM64/tool/build/$x.dbg o/$ARM64/tool/build/$x.dbg
done done
for x in ar chmod cp echo gzip package rm touch sha256sum; do
mv "$OUTDIR/bin/$x" "$OUTDIR/bin/$x.ape"
done
for x in make ctags; do for x in make ctags; do
ape $APELINK \ ape $APELINK \
-l o/$AMD64/ape/ape.elf \ -l o/$AMD64/ape/ape.elf \