2021-02-07 14:11:44 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2021-02-07 14:11:44 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2021 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"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/calls/struct/itimerval.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/calls/struct/rlimit.h"
|
2021-08-13 18:18:25 +00:00
|
|
|
#include "libc/calls/struct/rusage.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2021-02-07 14:11:44 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/calls/struct/stat.h"
|
|
|
|
#include "libc/calls/struct/timeval.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/calls/struct/winsize.h"
|
2023-06-15 00:02:57 +00:00
|
|
|
#include "libc/calls/termios.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/dce.h"
|
2021-02-07 14:11:44 +00:00
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/fmt/conv.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
2023-06-18 12:39:31 +00:00
|
|
|
#include "libc/fmt/libgen.h"
|
2023-06-09 08:23:18 +00:00
|
|
|
#include "libc/fmt/magnumstrs.internal.h"
|
2022-08-15 22:18:37 +00:00
|
|
|
#include "libc/intrin/safemacros.internal.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/limits.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/log/appendresourcereport.internal.h"
|
2021-02-20 06:20:38 +00:00
|
|
|
#include "libc/log/color.internal.h"
|
2021-02-07 14:11:44 +00:00
|
|
|
#include "libc/log/log.h"
|
2021-08-13 18:18:25 +00:00
|
|
|
#include "libc/macros.internal.h"
|
|
|
|
#include "libc/math.h"
|
2022-08-15 22:18:37 +00:00
|
|
|
#include "libc/mem/alg.h"
|
2024-01-08 18:07:35 +00:00
|
|
|
#include "libc/mem/gc.h"
|
2021-02-07 14:11:44 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2021-08-13 18:18:25 +00:00
|
|
|
#include "libc/nexgen32e/kcpuids.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/nexgen32e/x86feature.h"
|
2023-07-10 02:47:46 +00:00
|
|
|
#include "libc/nexgen32e/x86info.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#include "libc/proc/posix_spawn.h"
|
2021-02-07 14:11:44 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2024-01-22 15:13:49 +00:00
|
|
|
#include "libc/serialize.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/stdio/append.h"
|
2021-02-07 14:11:44 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/sysv/consts/auxv.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/sysv/consts/clock.h"
|
|
|
|
#include "libc/sysv/consts/itimer.h"
|
2022-08-11 05:08:43 +00:00
|
|
|
#include "libc/sysv/consts/madv.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2021-08-13 18:18:25 +00:00
|
|
|
#include "libc/sysv/consts/rlimit.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/sysv/consts/s.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/sysv/consts/sa.h"
|
2021-02-07 14:11:44 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/sysv/consts/termios.h"
|
2023-08-17 18:12:10 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/time/time.h"
|
2021-02-07 14:11:44 +00:00
|
|
|
#include "libc/x/x.h"
|
2023-07-03 02:57:43 +00:00
|
|
|
#include "third_party/getopt/getopt.internal.h"
|
2021-02-07 14:11:44 +00:00
|
|
|
|
2023-07-30 06:50:15 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
__static_yoink("zipos");
|
|
|
|
#endif
|
|
|
|
|
2021-02-07 14:11:44 +00:00
|
|
|
#define MANUAL \
|
|
|
|
"\
|
2021-02-20 06:20:38 +00:00
|
|
|
SYNOPSIS\n\
|
|
|
|
\n\
|
|
|
|
compile.com [FLAGS] COMMAND [ARGS...]\n\
|
|
|
|
\n\
|
2021-02-07 14:11:44 +00:00
|
|
|
OVERVIEW\n\
|
|
|
|
\n\
|
2021-09-28 05:58:51 +00:00
|
|
|
Build Command Harness\n\
|
2021-02-07 14:11:44 +00:00
|
|
|
\n\
|
|
|
|
DESCRIPTION\n\
|
|
|
|
\n\
|
2021-02-20 06:20:38 +00:00
|
|
|
This is a generic command wrapper, e.g.\n\
|
|
|
|
\n\
|
|
|
|
compile.com gcc -o program program.c\n\
|
|
|
|
\n\
|
|
|
|
This wrapper provides the following services:\n\
|
2021-02-07 14:11:44 +00:00
|
|
|
\n\
|
2021-09-28 05:58:51 +00:00
|
|
|
- Logging latency and memory usage\n\
|
2021-02-20 06:20:38 +00:00
|
|
|
- Ensures the output directory exists\n\
|
2021-09-28 05:58:51 +00:00
|
|
|
- Discarding stderr when commands succeed\n\
|
|
|
|
- Imposing cpu / memory / file size quotas\n\
|
|
|
|
- Mapping stdin to /dev/null when not a file or fifo\n\
|
|
|
|
- Buffering stderr to minimize build log interleaving\n\
|
|
|
|
- Schlepping stdout into stderr when not a file or fifo\n\
|
2021-02-20 06:20:38 +00:00
|
|
|
- Magic filtering of GCC vs. Clang flag incompatibilities\n\
|
2021-09-28 05:58:51 +00:00
|
|
|
- Echo the launched subcommand (silent mode supported if V=0)\n\
|
2021-02-20 06:20:38 +00:00
|
|
|
- Unzips the vendored GCC toolchain if it hasn't happened yet\n\
|
|
|
|
- Making temporary copies of APE executables w/o side-effects\n\
|
|
|
|
- Truncating long lines in \"TERM=dumb\" terminals like emacs\n\
|
2021-02-07 14:11:44 +00:00
|
|
|
\n\
|
2021-09-28 05:58:51 +00:00
|
|
|
Programs running under make that don't wish to have their output\n\
|
|
|
|
suppressed (e.g. unit tests with the -b benchmarking flag) shall\n\
|
|
|
|
use the exit code `254` which is remapped to `0` with the output\n\
|
2021-02-20 06:20:38 +00:00
|
|
|
\n\
|
|
|
|
FLAGS\n\
|
|
|
|
\n\
|
2021-09-28 05:58:51 +00:00
|
|
|
-t touch target on success\n\
|
2021-02-20 06:20:38 +00:00
|
|
|
-T TARGET specifies target name for V=0 logging\n\
|
2021-09-28 05:58:51 +00:00
|
|
|
-A ACTION specifies short command name for V=0 logging\n\
|
2021-02-20 20:39:39 +00:00
|
|
|
-V NUMBER specifies compiler version\n\
|
2022-03-16 20:33:13 +00:00
|
|
|
-C SECS set cpu limit [default 16]\n\
|
|
|
|
-L SECS set lat limit [default 90]\n\
|
2024-01-22 15:13:49 +00:00
|
|
|
-P PROCS set pro limit [default 4096]\n\
|
2023-09-10 15:12:43 +00:00
|
|
|
-S BYTES set stk limit [default 8m]\n\
|
2024-01-22 15:13:49 +00:00
|
|
|
-M BYTES set mem limit [default 2048m]\n\
|
2022-03-21 10:46:16 +00:00
|
|
|
-F BYTES set fsz limit [default 256m]\n\
|
2021-09-28 05:58:51 +00:00
|
|
|
-O BYTES set out limit [default 1m]\n\
|
|
|
|
-s decrement verbosity [default 4]\n\
|
|
|
|
-v increments verbosity [default 4]\n\
|
|
|
|
-n do nothing (prime ape executable)\n\
|
2022-08-11 05:08:43 +00:00
|
|
|
-w disable landlock tmp workaround\n\
|
2021-08-19 04:57:11 +00:00
|
|
|
-h print help\n\
|
2021-09-28 05:58:51 +00:00
|
|
|
\n\
|
|
|
|
ENVIRONMENT\n\
|
|
|
|
\n\
|
|
|
|
V=0 print shortened ephemerally\n\
|
|
|
|
V=1 print shortened\n\
|
|
|
|
V=2 print command\n\
|
|
|
|
V=3 print shortened w/ wall+cpu+mem usage\n\
|
|
|
|
V=4 print command w/ wall+cpu+mem usage\n\
|
|
|
|
V=5 print output when exitcode is zero\n\
|
|
|
|
COLUMNS=INT explicitly set terminal width for output truncation\n\
|
2022-07-20 21:01:15 +00:00
|
|
|
TERM=dumb disable ansi x3.64 sequences and thousands separators\n\
|
2021-02-07 14:11:44 +00:00
|
|
|
\n"
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
struct Strings {
|
2023-09-10 15:12:43 +00:00
|
|
|
int n;
|
|
|
|
int c;
|
2021-02-07 14:11:44 +00:00
|
|
|
char **p;
|
|
|
|
};
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
bool isar;
|
2021-02-07 14:11:44 +00:00
|
|
|
bool iscc;
|
2021-09-28 05:58:51 +00:00
|
|
|
bool ispkg;
|
2021-02-07 14:11:44 +00:00
|
|
|
bool isgcc;
|
2021-09-28 05:58:51 +00:00
|
|
|
bool isbfd;
|
|
|
|
bool wantpg;
|
2021-02-07 14:11:44 +00:00
|
|
|
bool wantnop;
|
2021-09-28 05:58:51 +00:00
|
|
|
bool isclang;
|
2021-02-07 14:11:44 +00:00
|
|
|
bool wantnopg;
|
2021-09-28 05:58:51 +00:00
|
|
|
bool wantasan;
|
|
|
|
bool wantframe;
|
2021-02-07 14:11:44 +00:00
|
|
|
bool wantubsan;
|
2021-09-28 05:58:51 +00:00
|
|
|
bool wantfentry;
|
|
|
|
bool wantrecord;
|
|
|
|
bool fulloutput;
|
2021-02-20 06:20:38 +00:00
|
|
|
bool touchtarget;
|
2022-08-11 05:08:43 +00:00
|
|
|
bool noworkaround;
|
2021-10-15 02:36:49 +00:00
|
|
|
bool wantnoredzone;
|
2021-09-28 05:58:51 +00:00
|
|
|
bool stdoutmustclose;
|
2021-05-14 12:36:58 +00:00
|
|
|
bool no_sanitize_null;
|
|
|
|
bool no_sanitize_alignment;
|
|
|
|
bool no_sanitize_pointer_overflow;
|
2021-02-07 14:11:44 +00:00
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
int verbose;
|
|
|
|
int timeout;
|
|
|
|
int gotalrm;
|
|
|
|
int gotchld;
|
|
|
|
int ccversion;
|
|
|
|
int pipefds[2];
|
|
|
|
|
|
|
|
long cpuquota;
|
|
|
|
long fszquota;
|
|
|
|
long memquota;
|
2023-08-17 18:12:10 +00:00
|
|
|
long stkquota;
|
2022-04-21 04:59:25 +00:00
|
|
|
long proquota;
|
2021-09-28 05:58:51 +00:00
|
|
|
long outquota;
|
|
|
|
|
2021-02-20 06:20:38 +00:00
|
|
|
char *cmd;
|
2021-09-28 05:58:51 +00:00
|
|
|
char *mode;
|
2021-02-07 14:11:44 +00:00
|
|
|
char *outdir;
|
2021-02-20 06:20:38 +00:00
|
|
|
char *action;
|
|
|
|
char *target;
|
2021-09-28 05:58:51 +00:00
|
|
|
char *output;
|
|
|
|
char *outpath;
|
|
|
|
char *command;
|
2022-08-09 13:28:41 +00:00
|
|
|
char *movepath;
|
2021-09-28 05:58:51 +00:00
|
|
|
char *shortened;
|
|
|
|
char *colorflag;
|
2022-04-28 16:42:36 +00:00
|
|
|
char ccpath[PATH_MAX];
|
2021-09-28 05:58:51 +00:00
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
struct Strings env;
|
|
|
|
struct Strings args;
|
|
|
|
struct sigaction sa;
|
|
|
|
struct rusage usage;
|
|
|
|
struct timespec start;
|
|
|
|
struct timespec finish;
|
|
|
|
struct itimerval timer;
|
|
|
|
struct timespec signalled;
|
2021-02-20 06:20:38 +00:00
|
|
|
|
|
|
|
sigset_t mask;
|
2023-07-30 01:44:15 +00:00
|
|
|
char buf[4096];
|
2021-02-20 06:20:38 +00:00
|
|
|
sigset_t savemask;
|
2022-08-09 13:28:41 +00:00
|
|
|
char tmpout[PATH_MAX];
|
2023-09-10 15:12:43 +00:00
|
|
|
posix_spawnattr_t spawnattr;
|
|
|
|
posix_spawn_file_actions_t spawnfila;
|
2021-02-07 14:11:44 +00:00
|
|
|
|
2023-07-30 01:44:15 +00:00
|
|
|
char *g_tmpout;
|
|
|
|
const char *g_tmpout_original;
|
|
|
|
|
2021-02-20 20:39:39 +00:00
|
|
|
const char *const kSafeEnv[] = {
|
2023-07-28 13:17:34 +00:00
|
|
|
"ADDR2LINE", // needed by GetAddr2linePath
|
|
|
|
"HOME", // needed by ~/.runit.psk
|
|
|
|
"HOMEDRIVE", // needed by ~/.runit.psk
|
|
|
|
"HOMEPATH", // needed by ~/.runit.psk
|
|
|
|
"MAKEFLAGS", // needed by IsRunningUnderMake
|
|
|
|
"MODE", // needed by test scripts
|
|
|
|
"PATH", // needed by clang
|
|
|
|
"PWD", // just seems plain needed
|
|
|
|
"STRACE", // useful for troubleshooting
|
|
|
|
"TERM", // needed to detect colors
|
|
|
|
"TMPDIR", // needed by compiler
|
|
|
|
"SYSTEMROOT", // needed by socket()
|
2021-02-20 20:39:39 +00:00
|
|
|
};
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
void OnAlrm(int sig) {
|
|
|
|
++gotalrm;
|
|
|
|
}
|
|
|
|
|
2022-09-02 12:08:35 +00:00
|
|
|
void OnChld(int sig, siginfo_t *si, void *ctx) {
|
2021-09-28 05:58:51 +00:00
|
|
|
if (!gotchld++) {
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &signalled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintBold(void) {
|
2022-03-18 09:33:37 +00:00
|
|
|
if (!__nocolor) {
|
2021-09-28 05:58:51 +00:00
|
|
|
appends(&output, "\e[1m");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintRed(void) {
|
2022-03-18 09:33:37 +00:00
|
|
|
if (!__nocolor) {
|
2021-09-28 05:58:51 +00:00
|
|
|
appends(&output, "\e[91;1m");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintReset(void) {
|
2022-03-18 09:33:37 +00:00
|
|
|
if (!__nocolor) {
|
2021-09-28 05:58:51 +00:00
|
|
|
appends(&output, "\e[0m");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintMakeCommand(void) {
|
|
|
|
appends(&output, "make MODE=");
|
|
|
|
appends(&output, mode);
|
|
|
|
appends(&output, " -j");
|
2023-08-17 07:25:01 +00:00
|
|
|
appendd(&output, buf, FormatUint64(buf, __get_cpu_count()) - buf);
|
2021-09-28 05:58:51 +00:00
|
|
|
appendw(&output, ' ');
|
|
|
|
appends(&output, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t GetTimevalMicros(struct timeval tv) {
|
|
|
|
return tv.tv_sec * 1000000ull + tv.tv_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t GetTimespecMicros(struct timespec ts) {
|
|
|
|
return ts.tv_sec * 1000000ull + ts.tv_nsec / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t WriteAllUntilSignalledOrError(int fd, const char *p, size_t n) {
|
|
|
|
ssize_t rc;
|
|
|
|
size_t i, got;
|
|
|
|
for (i = 0; i < n; i += got) {
|
|
|
|
if ((rc = write(fd, p + i, n - i)) != -1) {
|
|
|
|
got = rc;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetTerminalWidth(void) {
|
|
|
|
char *s;
|
|
|
|
struct winsize ws;
|
|
|
|
if ((s = getenv("COLUMNS"))) {
|
|
|
|
return atoi(s);
|
|
|
|
} else {
|
|
|
|
ws.ws_col = 0;
|
2023-06-15 00:02:57 +00:00
|
|
|
tcgetwinsize(2, &ws);
|
2021-09-28 05:58:51 +00:00
|
|
|
return ws.ws_col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
int GetLineWidth(bool *isineditor) {
|
2021-09-28 05:58:51 +00:00
|
|
|
char *s;
|
|
|
|
struct winsize ws = {0};
|
2022-03-16 20:33:13 +00:00
|
|
|
s = getenv("COLUMNS");
|
|
|
|
if (isineditor) {
|
|
|
|
*isineditor = !!s;
|
|
|
|
}
|
|
|
|
if (s) {
|
2021-09-28 05:58:51 +00:00
|
|
|
return atoi(s);
|
2023-06-15 00:02:57 +00:00
|
|
|
} else if (tcgetwinsize(2, &ws) != -1) {
|
2021-09-28 05:58:51 +00:00
|
|
|
if (ws.ws_col && ws.ws_row) {
|
|
|
|
return ws.ws_col * ws.ws_row / 3;
|
|
|
|
} else {
|
|
|
|
return 2048;
|
2021-02-20 06:20:38 +00:00
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
} else {
|
|
|
|
return INT_MAX;
|
2021-02-20 06:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 20:39:39 +00:00
|
|
|
bool IsSafeEnv(const char *s) {
|
|
|
|
const char *p;
|
|
|
|
int n, m, l, r, x;
|
|
|
|
p = strchr(s, '=');
|
|
|
|
n = p ? p - s : -1;
|
|
|
|
l = 0;
|
|
|
|
r = ARRAYLEN(kSafeEnv) - 1;
|
|
|
|
while (l <= r) {
|
2023-07-10 17:16:55 +00:00
|
|
|
m = (l & r) + ((l ^ r) >> 1); // floor((a+b)/2)
|
2023-07-28 13:17:34 +00:00
|
|
|
if (IsWindows()) {
|
|
|
|
x = strncasecmp(s, kSafeEnv[m], n);
|
|
|
|
} else {
|
|
|
|
x = strncmp(s, kSafeEnv[m], n);
|
|
|
|
}
|
2021-02-20 20:39:39 +00:00
|
|
|
if (x < 0) {
|
|
|
|
r = m - 1;
|
|
|
|
} else if (x > 0) {
|
|
|
|
l = m + 1;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:39:46 +00:00
|
|
|
char *Slurp(const char *path) {
|
|
|
|
int fd;
|
|
|
|
char *res = 0;
|
|
|
|
if ((fd = open(path, O_RDONLY)) != -1) {
|
|
|
|
ssize_t size;
|
|
|
|
if ((size = lseek(fd, 0, SEEK_END)) != -1) {
|
|
|
|
char *buf;
|
|
|
|
if ((buf = calloc(1, size + 1))) {
|
|
|
|
if (pread(fd, buf, size, 0) == size) {
|
|
|
|
res = buf;
|
|
|
|
} else {
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 17:19:00 +00:00
|
|
|
}
|
2024-02-01 11:39:46 +00:00
|
|
|
close(fd);
|
2021-02-08 17:19:00 +00:00
|
|
|
}
|
2024-02-01 11:39:46 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasFlag(const char *flags, const char *s) {
|
|
|
|
char buf[256];
|
|
|
|
size_t n = strlen(s);
|
|
|
|
if (!flags) return false;
|
|
|
|
if (n + 2 > sizeof(buf)) return false;
|
|
|
|
memcpy(buf, s, n);
|
|
|
|
buf[n] = '\n';
|
|
|
|
buf[n + 1] = 0;
|
|
|
|
return !!strstr(flags, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsGccOnlyFlag(const char *s) {
|
2023-09-10 15:12:43 +00:00
|
|
|
if (s[0] == '-') {
|
|
|
|
if (s[1] == 'f') {
|
|
|
|
if (startswith(s, "-ffixed-")) return true;
|
|
|
|
if (startswith(s, "-fcall-saved")) return true;
|
|
|
|
if (startswith(s, "-fcall-used")) return true;
|
|
|
|
if (startswith(s, "-fgcse-")) return true;
|
|
|
|
if (startswith(s, "-fvect-cost-model=")) return true;
|
|
|
|
if (startswith(s, "-fsimd-cost-model=")) return true;
|
|
|
|
if (startswith(s, "-fopt-info")) return true;
|
|
|
|
}
|
|
|
|
if (startswith(s, "-mstringop-strategy=")) return true;
|
|
|
|
if (startswith(s, "-mpreferred-stack-boundary=")) return true;
|
|
|
|
if (startswith(s, "-Wframe-larger-than=")) return true;
|
2024-02-01 11:39:46 +00:00
|
|
|
if (startswith(s, "-Walloca-larger-than=")) return true;
|
2023-09-10 15:12:43 +00:00
|
|
|
}
|
2024-02-01 11:39:46 +00:00
|
|
|
static bool once;
|
|
|
|
static char *gcc_only_flags;
|
|
|
|
if (!once) {
|
|
|
|
gcc_only_flags = Slurp("build/bootstrap/gcc-only-flags.txt");
|
|
|
|
once = true;
|
|
|
|
}
|
|
|
|
return HasFlag(gcc_only_flags, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsClangOnlyFlag(const char *s) {
|
|
|
|
static bool once;
|
|
|
|
static char *clang_only_flags;
|
|
|
|
if (!once) {
|
|
|
|
clang_only_flags = Slurp("build/bootstrap/clang-only-flags.txt");
|
|
|
|
once = true;
|
|
|
|
}
|
|
|
|
return HasFlag(clang_only_flags, s);
|
2021-02-08 17:19:00 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 20:39:39 +00:00
|
|
|
bool FileExistsAndIsNewerThan(const char *filepath, const char *thanpath) {
|
|
|
|
struct stat st1, st2;
|
|
|
|
if (stat(filepath, &st1) == -1) return false;
|
|
|
|
if (stat(thanpath, &st2) == -1) return false;
|
|
|
|
if (st1.st_mtim.tv_sec < st2.st_mtim.tv_sec) return false;
|
|
|
|
if (st1.st_mtim.tv_sec > st2.st_mtim.tv_sec) return true;
|
|
|
|
return st1.st_mtim.tv_nsec >= st2.st_mtim.tv_nsec;
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
static size_t TallyArgs(char **p) {
|
|
|
|
size_t n;
|
|
|
|
for (n = 0; *p; ++p) {
|
|
|
|
n += sizeof(*p);
|
|
|
|
n += strlen(*p) + 1;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
void AddStr(struct Strings *l, char *s) {
|
2023-09-10 15:12:43 +00:00
|
|
|
if (l->n == l->c) {
|
|
|
|
if (l->c) {
|
|
|
|
l->c += l->c >> 1;
|
|
|
|
} else {
|
|
|
|
l->c = 16;
|
|
|
|
}
|
|
|
|
l->p = realloc(l->p, (l->c + 1) * sizeof(*l->p));
|
|
|
|
}
|
|
|
|
l->p[l->n++] = s;
|
|
|
|
l->p[l->n] = 0;
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
|
2021-02-20 20:39:39 +00:00
|
|
|
void AddEnv(char *s) {
|
2021-09-28 05:58:51 +00:00
|
|
|
AddStr(&env, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *StripPrefix(char *s, char *p) {
|
2023-08-14 03:31:27 +00:00
|
|
|
if (startswith(s, p)) {
|
2021-09-28 05:58:51 +00:00
|
|
|
return s + strlen(p);
|
|
|
|
} else {
|
|
|
|
return s;
|
|
|
|
}
|
2021-02-20 20:39:39 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 01:44:15 +00:00
|
|
|
void AddArg(char *actual) {
|
|
|
|
const char *s;
|
|
|
|
if (actual == g_tmpout) {
|
|
|
|
s = g_tmpout_original;
|
|
|
|
} else {
|
|
|
|
s = actual;
|
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
if (args.n) {
|
|
|
|
appendw(&command, ' ');
|
|
|
|
}
|
|
|
|
appends(&command, s);
|
|
|
|
if (!args.n) {
|
2023-09-02 03:49:13 +00:00
|
|
|
appends(&shortened,
|
|
|
|
StripPrefix(basename(gc(strdup(s))), "x86_64-linux-musl-"));
|
2021-09-28 05:58:51 +00:00
|
|
|
} else if (*s != '-') {
|
|
|
|
appendw(&shortened, ' ');
|
|
|
|
if ((isar || isbfd || ispkg) &&
|
|
|
|
(strcmp(args.p[args.n - 1], "-o") &&
|
2023-08-14 03:31:27 +00:00
|
|
|
(endswith(s, ".o") || endswith(s, ".pkg") ||
|
|
|
|
(endswith(s, ".a") && !isar)))) {
|
2023-09-02 03:49:13 +00:00
|
|
|
appends(&shortened, basename(gc(strdup(s))));
|
2021-02-07 14:11:44 +00:00
|
|
|
} else {
|
2021-09-28 05:58:51 +00:00
|
|
|
appends(&shortened, s);
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
} else if (/*
|
|
|
|
* a in ('-', '--') or
|
2023-08-14 03:31:27 +00:00
|
|
|
* a.startswith('-o') or
|
2022-03-16 20:33:13 +00:00
|
|
|
* c == 'ld' and a == '-T' or
|
2023-08-14 03:31:27 +00:00
|
|
|
* c == 'cc' and a.startswith('-O') or
|
|
|
|
* c == 'cc' and a.startswith('-x') or
|
2022-03-16 20:33:13 +00:00
|
|
|
* c == 'cc' and a in ('-c', '-E', '-S')
|
|
|
|
*/
|
|
|
|
s[0] == '-' && (!s[1] || s[1] == 'o' || (s[1] == '-' && !s[2]) ||
|
2021-09-28 05:58:51 +00:00
|
|
|
(isbfd && (s[1] == 'T' && !s[2])) ||
|
|
|
|
(iscc && (s[1] == 'O' || s[1] == 'x' ||
|
|
|
|
(!s[2] && (s[1] == 'c' || s[1] == 'E' ||
|
|
|
|
s[1] == 'S')))))) {
|
|
|
|
appendw(&shortened, ' ');
|
|
|
|
appends(&shortened, s);
|
2021-02-20 06:20:38 +00:00
|
|
|
}
|
2023-07-30 01:44:15 +00:00
|
|
|
AddStr(&args, actual);
|
2021-02-20 06:20:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 05:42:57 +00:00
|
|
|
static int GetBaseCpuFreqMhz(void) {
|
2021-08-13 18:18:25 +00:00
|
|
|
return KCPUIDS(16H, EAX) & 0x7fff;
|
|
|
|
}
|
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
void PlanResource(int resource, struct rlimit rlim) {
|
|
|
|
struct rlimit prior;
|
|
|
|
if (getrlimit(resource, &prior)) return;
|
|
|
|
rlim.rlim_cur = MIN(rlim.rlim_cur, prior.rlim_max);
|
|
|
|
rlim.rlim_max = MIN(rlim.rlim_max, prior.rlim_max);
|
|
|
|
posix_spawnattr_setrlimit(&spawnattr, resource, &rlim);
|
|
|
|
}
|
|
|
|
|
2021-08-13 18:18:25 +00:00
|
|
|
void SetCpuLimit(int secs) {
|
2021-09-28 05:58:51 +00:00
|
|
|
if (secs <= 0) return;
|
2021-08-13 18:18:25 +00:00
|
|
|
if (IsWindows()) return;
|
2023-05-13 05:42:57 +00:00
|
|
|
#ifdef __x86_64__
|
2023-09-02 03:49:13 +00:00
|
|
|
int mhz, lim;
|
2021-08-13 18:18:25 +00:00
|
|
|
if (!(mhz = GetBaseCpuFreqMhz())) return;
|
2021-09-28 05:58:51 +00:00
|
|
|
lim = ceil(3100. / mhz * secs);
|
2023-09-10 15:12:43 +00:00
|
|
|
PlanResource(RLIMIT_CPU, (struct rlimit){lim, lim + 1});
|
2023-05-13 05:42:57 +00:00
|
|
|
#endif
|
2021-08-13 18:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetFszLimit(long n) {
|
2021-09-28 05:58:51 +00:00
|
|
|
if (n <= 0) return;
|
2021-08-13 18:18:25 +00:00
|
|
|
if (IsWindows()) return;
|
2023-09-10 15:12:43 +00:00
|
|
|
PlanResource(RLIMIT_FSIZE, (struct rlimit){n, n + (n >> 1)});
|
2021-08-13 18:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetMemLimit(long n) {
|
2021-09-28 05:58:51 +00:00
|
|
|
if (n <= 0) return;
|
2021-08-13 18:18:25 +00:00
|
|
|
if (IsWindows() || IsXnu()) return;
|
2023-09-10 15:12:43 +00:00
|
|
|
PlanResource(RLIMIT_AS, (struct rlimit){n, n});
|
2021-08-13 18:18:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 18:12:10 +00:00
|
|
|
void SetStkLimit(long n) {
|
|
|
|
if (IsWindows()) return;
|
|
|
|
if (n <= 0) return;
|
|
|
|
n = MAX(n, PTHREAD_STACK_MIN * 2);
|
2023-09-10 15:12:43 +00:00
|
|
|
PlanResource(RLIMIT_STACK, (struct rlimit){n, n});
|
2023-08-17 18:12:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 04:59:25 +00:00
|
|
|
void SetProLimit(long n) {
|
|
|
|
if (n <= 0) return;
|
2023-09-10 15:12:43 +00:00
|
|
|
PlanResource(RLIMIT_NPROC, (struct rlimit){n, n});
|
2022-04-21 04:59:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
bool ArgNeedsShellQuotes(const char *s) {
|
|
|
|
if (*s) {
|
|
|
|
for (;;) {
|
|
|
|
switch (*s++ & 255) {
|
|
|
|
case 0:
|
|
|
|
return false;
|
|
|
|
case '-':
|
|
|
|
case '.':
|
|
|
|
case '/':
|
|
|
|
case '_':
|
|
|
|
case '=':
|
|
|
|
case ':':
|
|
|
|
case '0' ... '9':
|
|
|
|
case 'A' ... 'Z':
|
|
|
|
case 'a' ... 'z':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *AddShellQuotes(const char *s) {
|
|
|
|
char *p, *q;
|
|
|
|
size_t i, j, n;
|
|
|
|
n = strlen(s);
|
|
|
|
p = malloc(1 + n * 5 + 1 + 1);
|
|
|
|
j = 0;
|
|
|
|
p[j++] = '\'';
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (s[i] != '\'') {
|
|
|
|
p[j++] = s[i];
|
|
|
|
} else {
|
|
|
|
p[j + 0] = '\'';
|
|
|
|
p[j + 1] = '"';
|
|
|
|
p[j + 2] = '\'';
|
|
|
|
p[j + 3] = '"';
|
|
|
|
p[j + 4] = '\'';
|
|
|
|
j += 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p[j++] = '\'';
|
|
|
|
p[j] = 0;
|
|
|
|
if ((q = realloc(p, j + 1))) p = q;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2022-09-08 05:52:24 +00:00
|
|
|
void MakeDirs(const char *path, int mode) {
|
|
|
|
if (makedirs(path, mode)) {
|
2023-09-10 15:12:43 +00:00
|
|
|
perror(path);
|
2022-09-08 05:52:24 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
int Launch(void) {
|
|
|
|
size_t got;
|
|
|
|
ssize_t rc;
|
2023-09-10 15:12:43 +00:00
|
|
|
errno_t err;
|
2021-02-20 06:20:38 +00:00
|
|
|
int ws, pid;
|
2021-09-28 05:58:51 +00:00
|
|
|
uint64_t us;
|
|
|
|
gotchld = 0;
|
2022-09-08 05:52:24 +00:00
|
|
|
|
|
|
|
if (pipe2(pipefds, O_CLOEXEC) == -1) {
|
2023-09-10 15:12:43 +00:00
|
|
|
perror("pipe2");
|
2022-09-08 05:52:24 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
posix_spawnattr_init(&spawnattr);
|
|
|
|
posix_spawnattr_setsigmask(&spawnattr, &savemask);
|
2023-11-07 00:38:44 +00:00
|
|
|
posix_spawnattr_setflags(&spawnattr,
|
|
|
|
POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETRLIMIT);
|
2023-09-10 15:12:43 +00:00
|
|
|
SetCpuLimit(cpuquota);
|
|
|
|
SetFszLimit(fszquota);
|
|
|
|
SetMemLimit(memquota);
|
|
|
|
SetStkLimit(stkquota);
|
|
|
|
SetProLimit(proquota);
|
|
|
|
|
|
|
|
posix_spawn_file_actions_init(&spawnfila);
|
|
|
|
if (stdoutmustclose)
|
|
|
|
posix_spawn_file_actions_adddup2(&spawnfila, pipefds[1], 1);
|
|
|
|
posix_spawn_file_actions_adddup2(&spawnfila, pipefds[1], 2);
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
if (timeout > 0) {
|
|
|
|
timer.it_value.tv_sec = timeout;
|
|
|
|
timer.it_interval.tv_sec = timeout;
|
|
|
|
setitimer(ITIMER_REAL, &timer, 0);
|
|
|
|
}
|
2022-09-08 05:52:24 +00:00
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
err = posix_spawn(&pid, cmd, &spawnfila, &spawnattr, args.p, env.p);
|
|
|
|
if (err) {
|
|
|
|
tinyprint(2, program_invocation_short_name, ": failed to spawn ", cmd, ": ",
|
|
|
|
strerror(err), " (see --strace for further details)\n", NULL);
|
2022-09-08 05:52:24 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
posix_spawn_file_actions_destroy(&spawnfila);
|
|
|
|
posix_spawnattr_destroy(&spawnattr);
|
2021-09-28 05:58:51 +00:00
|
|
|
close(pipefds[1]);
|
2022-09-08 05:52:24 +00:00
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
for (;;) {
|
|
|
|
if (gotalrm) {
|
|
|
|
PrintRed();
|
|
|
|
appends(&output, "\n\n`");
|
|
|
|
PrintMakeCommand();
|
|
|
|
appends(&output, "` timed out after ");
|
|
|
|
appendd(&output, buf, FormatInt64(buf, timeout) - buf);
|
|
|
|
appends(&output, " seconds! ");
|
|
|
|
PrintReset();
|
|
|
|
kill(pid, SIGXCPU);
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((rc = read(pipefds[0], buf, sizeof(buf))) != -1) {
|
|
|
|
if (!(got = rc)) break;
|
|
|
|
appendd(&output, buf, got);
|
|
|
|
if (outquota > 0 && appendz(output).i > outquota) {
|
|
|
|
kill(pid, SIGXFSZ);
|
|
|
|
PrintRed();
|
|
|
|
appendw(&output, '`');
|
|
|
|
PrintMakeCommand();
|
|
|
|
appends(&output, "` printed ");
|
|
|
|
appendd(&output, buf,
|
|
|
|
FormatUint64Thousands(buf, appendz(output).i) - buf);
|
|
|
|
appends(&output, " bytes of output which exceeds the limit ");
|
|
|
|
appendd(&output, buf, FormatUint64Thousands(buf, outquota) - buf);
|
|
|
|
appendw(&output, READ16LE("! "));
|
|
|
|
PrintReset();
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* this should never happen */
|
|
|
|
PrintRed();
|
|
|
|
appends(&output, "error: compile.com read() failed w/ ");
|
|
|
|
appendd(&output, buf, FormatInt64(buf, errno) - buf);
|
|
|
|
PrintReset();
|
|
|
|
appendw(&output, READ16LE(": "));
|
|
|
|
kill(pid, SIGTERM);
|
|
|
|
break;
|
|
|
|
}
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
close(pipefds[0]);
|
|
|
|
while (wait4(pid, &ws, 0, &usage) == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
if (gotalrm > 1) {
|
|
|
|
PrintRed();
|
|
|
|
appends(&output, "and it willfully ignored our SIGXCPU signal! ");
|
|
|
|
PrintReset();
|
|
|
|
kill(pid, SIGKILL);
|
|
|
|
gotalrm = 1;
|
|
|
|
}
|
2022-05-25 18:31:08 +00:00
|
|
|
} else if (errno == ECHILD) {
|
|
|
|
break;
|
2021-09-28 05:58:51 +00:00
|
|
|
} else {
|
|
|
|
/* this should never happen */
|
|
|
|
PrintRed();
|
|
|
|
appends(&output, "error: compile.com wait4() failed w/ ");
|
|
|
|
appendd(&output, buf, FormatInt64(buf, errno) - buf);
|
|
|
|
PrintReset();
|
|
|
|
appendw(&output, READ16LE(": "));
|
|
|
|
ws = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &finish);
|
|
|
|
if (gotchld) {
|
|
|
|
us = GetTimespecMicros(finish) - GetTimespecMicros(signalled);
|
|
|
|
if (us > 1000000) {
|
|
|
|
appends(&output, "wut: compile.com needed ");
|
|
|
|
appendd(&output, buf, FormatUint64Thousands(buf, us) - buf);
|
|
|
|
appends(&output, "µs to wait() for zombie ");
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
if (gotchld > 1) {
|
|
|
|
appends(&output, "wut: compile.com got multiple sigchld?! ");
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ws | rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReportResources(void) {
|
|
|
|
uint64_t us;
|
|
|
|
appendw(&output, '\n');
|
|
|
|
if ((us = GetTimespecMicros(finish) - GetTimespecMicros(start))) {
|
|
|
|
appends(&output, "consumed ");
|
|
|
|
appendd(&output, buf, FormatUint64Thousands(buf, us) - buf);
|
|
|
|
appends(&output, "µs wall time\n");
|
|
|
|
}
|
|
|
|
AppendResourceReport(&output, &usage, "\n");
|
|
|
|
appendw(&output, '\n');
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 13:28:41 +00:00
|
|
|
bool MovePreservingDestinationInode(const char *from, const char *to) {
|
|
|
|
bool res;
|
2022-08-11 05:08:43 +00:00
|
|
|
ssize_t rc;
|
|
|
|
size_t remain;
|
2022-08-09 13:28:41 +00:00
|
|
|
struct stat st;
|
|
|
|
int fdin, fdout;
|
|
|
|
if ((fdin = open(from, O_RDONLY)) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fstat(fdin, &st);
|
|
|
|
if ((fdout = creat(to, st.st_mode)) == -1) {
|
|
|
|
close(fdin);
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-11 05:08:43 +00:00
|
|
|
fadvise(fdin, 0, st.st_size, MADV_SEQUENTIAL);
|
|
|
|
ftruncate(fdout, st.st_size);
|
|
|
|
for (res = true, remain = st.st_size; remain;) {
|
|
|
|
rc = copy_file_range(fdin, 0, fdout, 0, remain, 0);
|
|
|
|
if (rc != -1) {
|
|
|
|
remain -= rc;
|
2022-09-19 22:01:48 +00:00
|
|
|
} else if (errno == EXDEV || errno == ENOSYS) {
|
2022-08-11 05:08:43 +00:00
|
|
|
if (lseek(fdin, 0, SEEK_SET) == -1) {
|
|
|
|
res = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (lseek(fdout, 0, SEEK_SET) == -1) {
|
|
|
|
res = false;
|
|
|
|
break;
|
|
|
|
}
|
2023-06-15 20:50:42 +00:00
|
|
|
res = copyfd(fdin, fdout, -1) != -1;
|
2022-08-11 05:08:43 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
res = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 13:28:41 +00:00
|
|
|
close(fdin);
|
|
|
|
close(fdout);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-08-11 05:08:43 +00:00
|
|
|
char *MakeTmpOut(const char *path) {
|
|
|
|
int c;
|
|
|
|
char *p = tmpout;
|
|
|
|
char *e = tmpout + sizeof(tmpout) - 1;
|
2023-07-30 01:44:15 +00:00
|
|
|
g_tmpout_original = path;
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
p = stpcpy(p, __get_tmpdir());
|
2022-08-11 05:08:43 +00:00
|
|
|
while ((c = *path++)) {
|
|
|
|
if (c == '/') c = '_';
|
|
|
|
if (p == e) {
|
2023-09-10 15:12:43 +00:00
|
|
|
tinyprint(2, program_invocation_short_name,
|
|
|
|
": fatal error: MakeTmpOut() generated temporary filename "
|
|
|
|
"that's too long: ",
|
|
|
|
tmpout, "\n", NULL);
|
2022-08-11 05:08:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*p++ = c;
|
|
|
|
}
|
|
|
|
*p = 0;
|
2023-07-30 01:44:15 +00:00
|
|
|
g_tmpout = tmpout;
|
2022-08-11 05:08:43 +00:00
|
|
|
return tmpout;
|
|
|
|
}
|
|
|
|
|
2021-02-07 14:11:44 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2021-09-28 05:58:51 +00:00
|
|
|
uint64_t us;
|
2022-03-16 20:33:13 +00:00
|
|
|
bool isineditor;
|
2021-09-28 05:58:51 +00:00
|
|
|
size_t i, j, n, m;
|
|
|
|
bool isproblematic;
|
2023-09-02 03:49:13 +00:00
|
|
|
char *s, *q, **envp;
|
2021-09-28 05:58:51 +00:00
|
|
|
int ws, opt, exitcode;
|
2021-02-07 14:11:44 +00:00
|
|
|
|
2023-11-19 00:56:11 +00:00
|
|
|
#ifdef MODE_DBG
|
2023-07-30 06:50:15 +00:00
|
|
|
ShowCrashReports();
|
|
|
|
#endif
|
|
|
|
|
2022-03-21 10:46:16 +00:00
|
|
|
mode = firstnonnull(getenv("MODE"), MODE);
|
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// parse prefix arguments
|
2021-09-28 05:58:51 +00:00
|
|
|
verbose = 4;
|
2024-01-22 15:13:49 +00:00
|
|
|
timeout = 90; // secs
|
|
|
|
cpuquota = 32; // secs
|
|
|
|
proquota = 4096; // procs
|
|
|
|
stkquota = 8 * 1024 * 1024; // bytes
|
|
|
|
fszquota = 256 * 1000 * 1000; // bytes
|
|
|
|
memquota = 2048L * 1024 * 1024; // bytes
|
2021-09-28 05:58:51 +00:00
|
|
|
if ((s = getenv("V"))) verbose = atoi(s);
|
2023-08-17 18:12:10 +00:00
|
|
|
while ((opt = getopt(argc, argv, "hnstvwA:C:F:L:M:O:P:T:V:S:")) != -1) {
|
2021-02-20 06:20:38 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'n':
|
|
|
|
exit(0);
|
2021-09-28 05:58:51 +00:00
|
|
|
case 's':
|
|
|
|
--verbose;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
++verbose;
|
2021-02-20 06:20:38 +00:00
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
action = optarg;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
target = optarg;
|
|
|
|
break;
|
2021-09-28 05:58:51 +00:00
|
|
|
case 't':
|
|
|
|
touchtarget = true;
|
|
|
|
break;
|
2022-08-11 05:08:43 +00:00
|
|
|
case 'w':
|
|
|
|
noworkaround = true;
|
|
|
|
break;
|
2021-09-28 05:58:51 +00:00
|
|
|
case 'L':
|
|
|
|
timeout = atoi(optarg);
|
2021-02-20 20:39:39 +00:00
|
|
|
break;
|
2021-08-13 18:18:25 +00:00
|
|
|
case 'C':
|
|
|
|
cpuquota = atoi(optarg);
|
|
|
|
break;
|
2021-09-28 05:58:51 +00:00
|
|
|
case 'V':
|
|
|
|
ccversion = atoi(optarg);
|
2021-08-13 18:18:25 +00:00
|
|
|
break;
|
2022-04-21 04:59:25 +00:00
|
|
|
case 'P':
|
2022-07-14 11:32:33 +00:00
|
|
|
proquota = atoi(optarg);
|
2022-04-21 04:59:25 +00:00
|
|
|
break;
|
2021-08-13 18:18:25 +00:00
|
|
|
case 'F':
|
|
|
|
fszquota = sizetol(optarg, 1000);
|
|
|
|
break;
|
2021-09-28 05:58:51 +00:00
|
|
|
case 'M':
|
|
|
|
memquota = sizetol(optarg, 1024);
|
|
|
|
break;
|
2023-08-17 18:12:10 +00:00
|
|
|
case 'S':
|
|
|
|
stkquota = sizetol(optarg, 1024);
|
|
|
|
break;
|
2021-09-28 05:58:51 +00:00
|
|
|
case 'O':
|
|
|
|
outquota = sizetol(optarg, 1024);
|
|
|
|
break;
|
2021-02-20 06:20:38 +00:00
|
|
|
case 'h':
|
2023-09-10 15:12:43 +00:00
|
|
|
tinyprint(1, MANUAL, NULL);
|
2021-02-20 06:20:38 +00:00
|
|
|
exit(0);
|
|
|
|
default:
|
2023-09-10 15:12:43 +00:00
|
|
|
tinyprint(2, MANUAL, NULL);
|
2021-02-20 06:20:38 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (optind == argc) {
|
2023-09-10 15:12:43 +00:00
|
|
|
tinyprint(2, program_invocation_short_name, ": missing arguments\n", NULL);
|
2021-02-07 14:11:44 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// extend limits for slow UBSAN in particular
|
2022-03-21 10:46:16 +00:00
|
|
|
if (!strcmp(mode, "dbg") || !strcmp(mode, "ubsan")) {
|
|
|
|
cpuquota *= 2;
|
|
|
|
fszquota *= 2;
|
2023-09-10 15:12:43 +00:00
|
|
|
stkquota *= 2;
|
2022-03-21 10:46:16 +00:00
|
|
|
memquota *= 2;
|
|
|
|
timeout *= 2;
|
|
|
|
}
|
|
|
|
|
2021-02-20 06:20:38 +00:00
|
|
|
cmd = argv[optind];
|
|
|
|
if (!strchr(cmd, '/')) {
|
2022-04-24 16:59:22 +00:00
|
|
|
if (!(cmd = commandv(cmd, ccpath, sizeof(ccpath)))) exit(127);
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
s = basename(strdup(cmd));
|
2024-02-01 11:39:46 +00:00
|
|
|
if (strstr(s, "clang") || strstr(s, "clang++")) {
|
2021-09-28 05:58:51 +00:00
|
|
|
iscc = true;
|
|
|
|
isclang = true;
|
2024-02-01 11:39:46 +00:00
|
|
|
} else if (strstr(s, "gcc") || strstr(s, "g++")) {
|
|
|
|
iscc = true;
|
|
|
|
isgcc = true;
|
2021-09-28 05:58:51 +00:00
|
|
|
} else if (strstr(s, "ld.bfd")) {
|
|
|
|
isbfd = true;
|
|
|
|
} else if (strstr(s, "ar.com")) {
|
|
|
|
isar = true;
|
|
|
|
} else if (strstr(s, "package.com")) {
|
|
|
|
ispkg = true;
|
|
|
|
}
|
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// ingest arguments
|
2021-02-20 06:20:38 +00:00
|
|
|
for (i = optind; i < argc; ++i) {
|
2022-08-11 05:08:43 +00:00
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// replace output filename argument
|
|
|
|
//
|
|
|
|
// some commands (e.g. ar) don't use the `-o PATH` notation. in that
|
|
|
|
// case we assume the output path was passed to compile.com -TTARGET
|
|
|
|
// which means we can replace the appropriate command line argument.
|
2022-08-11 05:08:43 +00:00
|
|
|
if (!noworkaround && //
|
|
|
|
!movepath && //
|
|
|
|
!outpath && //
|
|
|
|
target && //
|
|
|
|
!strcmp(target, argv[i])) {
|
|
|
|
AddArg(MakeTmpOut(argv[i]));
|
|
|
|
outpath = target;
|
2022-08-09 13:28:41 +00:00
|
|
|
movepath = target;
|
|
|
|
MovePreservingDestinationInode(target, tmpout);
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-11 05:08:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* capture arguments
|
|
|
|
*/
|
2021-02-07 14:11:44 +00:00
|
|
|
if (argv[i][0] != '-') {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg(argv[i]);
|
2021-02-07 14:11:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-11 05:08:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* capture flags
|
|
|
|
*/
|
2023-08-14 03:31:27 +00:00
|
|
|
if (startswith(argv[i], "-o")) {
|
2021-09-28 05:58:51 +00:00
|
|
|
if (!strcmp(argv[i], "-o")) {
|
2022-08-09 13:28:41 +00:00
|
|
|
outpath = argv[++i];
|
2021-09-28 05:58:51 +00:00
|
|
|
} else {
|
|
|
|
outpath = argv[i] + 2;
|
|
|
|
}
|
2022-08-09 13:28:41 +00:00
|
|
|
AddArg("-o");
|
2022-08-11 05:08:43 +00:00
|
|
|
if (noworkaround) {
|
|
|
|
AddArg(outpath);
|
|
|
|
} else {
|
|
|
|
movepath = outpath;
|
|
|
|
AddArg(MakeTmpOut(outpath));
|
|
|
|
}
|
2021-02-07 14:11:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-08 17:19:00 +00:00
|
|
|
if (!iscc) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg(argv[i]);
|
2021-02-07 14:11:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-02-01 11:39:46 +00:00
|
|
|
if (isgcc && IsClangOnlyFlag(argv[i])) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-08 17:19:00 +00:00
|
|
|
if (isclang && IsGccOnlyFlag(argv[i])) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-20 16:56:53 +00:00
|
|
|
if (!X86_HAVE(AVX) &&
|
|
|
|
(!strcmp(argv[i], "-msse2avx") || !strcmp(argv[i], "-Wa,-msse2avx"))) {
|
|
|
|
// Avoid any chance of people using Intel's older or low power
|
|
|
|
// CPUs encountering a SIGILL error due to these awesome flags
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-07 14:11:44 +00:00
|
|
|
if (!strcmp(argv[i], "-w")) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg(argv[i]);
|
|
|
|
AddArg("-D__W__");
|
2021-02-07 14:11:44 +00:00
|
|
|
} else if (!strcmp(argv[i], "-Oz")) {
|
|
|
|
if (isclang) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg(argv[i]);
|
2021-02-07 14:11:44 +00:00
|
|
|
} else {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-Os");
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[i], "-pg")) {
|
|
|
|
wantpg = true;
|
|
|
|
} else if (!strcmp(argv[i], "-x-no-pg")) {
|
|
|
|
wantnopg = true;
|
|
|
|
} else if (!strcmp(argv[i], "-mfentry")) {
|
|
|
|
wantfentry = true;
|
|
|
|
} else if (!strcmp(argv[i], "-mnop-mcount")) {
|
|
|
|
wantnop = true;
|
|
|
|
} else if (!strcmp(argv[i], "-mrecord-mcount")) {
|
|
|
|
wantrecord = true;
|
|
|
|
} else if (!strcmp(argv[i], "-fno-omit-frame-pointer")) {
|
|
|
|
wantframe = true;
|
|
|
|
} else if (!strcmp(argv[i], "-fomit-frame-pointer")) {
|
|
|
|
wantframe = false;
|
2021-10-15 02:36:49 +00:00
|
|
|
} else if (!strcmp(argv[i], "-mno-red-zone")) {
|
|
|
|
wantnoredzone = true;
|
|
|
|
} else if (!strcmp(argv[i], "-mred-zone")) {
|
|
|
|
wantnoredzone = false;
|
2021-02-07 14:11:44 +00:00
|
|
|
} else if (!strcmp(argv[i], "-mno-vzeroupper")) {
|
|
|
|
if (isgcc) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-Wa,-msse2avx");
|
|
|
|
AddArg("-D__MNO_VZEROUPPER__");
|
2021-02-07 14:11:44 +00:00
|
|
|
} else if (isclang) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-mllvm");
|
|
|
|
AddArg("-x86-use-vzeroupper=0");
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[i], "-msse2avx")) {
|
|
|
|
if (isgcc) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg(argv[i]);
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
2023-07-10 02:47:46 +00:00
|
|
|
|
|
|
|
#ifdef __x86_64__
|
|
|
|
} else if (!strcmp(argv[i], "-march=native")) {
|
2023-09-02 03:49:13 +00:00
|
|
|
const struct X86ProcessorModel *model;
|
2023-07-10 02:47:46 +00:00
|
|
|
if (X86_HAVE(XOP)) AddArg("-mxop");
|
|
|
|
if (X86_HAVE(SSE4A)) AddArg("-msse4a");
|
|
|
|
if (X86_HAVE(SSE3)) AddArg("-msse3");
|
|
|
|
if (X86_HAVE(SSSE3)) AddArg("-mssse3");
|
|
|
|
if (X86_HAVE(SSE4_1)) AddArg("-msse4.1");
|
|
|
|
if (X86_HAVE(SSE4_2)) AddArg("-msse4.2");
|
|
|
|
if (X86_HAVE(AVX)) AddArg("-mavx");
|
|
|
|
if (X86_HAVE(AVX2)) {
|
|
|
|
AddArg("-mavx2");
|
|
|
|
if (isgcc) {
|
|
|
|
AddArg("-msse2avx");
|
|
|
|
AddArg("-Wa,-msse2avx");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (X86_HAVE(AVX512F)) AddArg("-mavx512f");
|
|
|
|
if (X86_HAVE(AVX512PF)) AddArg("-mavx512pf");
|
|
|
|
if (X86_HAVE(AVX512ER)) AddArg("-mavx512er");
|
|
|
|
if (X86_HAVE(AVX512CD)) AddArg("-mavx512cd");
|
|
|
|
if (X86_HAVE(AVX512VL)) AddArg("-mavx512vl");
|
|
|
|
if (X86_HAVE(AVX512BW)) AddArg("-mavx512bw");
|
|
|
|
if (X86_HAVE(AVX512DQ)) AddArg("-mavx512dq");
|
|
|
|
if (X86_HAVE(AVX512IFMA)) AddArg("-mavx512ifma");
|
|
|
|
if (X86_HAVE(AVX512VBMI)) AddArg("-mavx512vbmi");
|
|
|
|
if (X86_HAVE(SHA)) AddArg("-msha");
|
|
|
|
if (X86_HAVE(AES)) AddArg("-maes");
|
|
|
|
if (X86_HAVE(VAES)) AddArg("-mvaes");
|
|
|
|
if (X86_HAVE(PCLMUL)) AddArg("-mpclmul");
|
|
|
|
if (X86_HAVE(FSGSBASE)) AddArg("-mfsgsbase");
|
|
|
|
if (X86_HAVE(F16C)) AddArg("-mf16c");
|
|
|
|
if (X86_HAVE(FMA)) AddArg("-mfma");
|
|
|
|
if (X86_HAVE(POPCNT)) AddArg("-mpopcnt");
|
|
|
|
if (X86_HAVE(BMI)) AddArg("-mbmi");
|
|
|
|
if (X86_HAVE(BMI2)) AddArg("-mbmi2");
|
|
|
|
if (X86_HAVE(ADX)) AddArg("-madx");
|
|
|
|
if (X86_HAVE(FXSR)) AddArg("-mfxsr");
|
|
|
|
if ((model = getx86processormodel(kX86ProcessorModelKey))) {
|
|
|
|
switch (model->march) {
|
|
|
|
case X86_MARCH_CORE2:
|
|
|
|
AddArg("-march=core2");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_NEHALEM:
|
|
|
|
AddArg("-march=nehalem");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_WESTMERE:
|
|
|
|
AddArg("-march=westmere");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_SANDYBRIDGE:
|
|
|
|
AddArg("-march=sandybridge");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_IVYBRIDGE:
|
|
|
|
AddArg("-march=ivybridge");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_HASWELL:
|
|
|
|
AddArg("-march=haswell");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_BROADWELL:
|
|
|
|
AddArg("-march=broadwell");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_SKYLAKE:
|
|
|
|
case X86_MARCH_KABYLAKE:
|
|
|
|
AddArg("-march=skylake");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_CANNONLAKE:
|
|
|
|
AddArg("-march=cannonlake");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_ICELAKE:
|
|
|
|
if (model->grade >= X86_GRADE_SERVER) {
|
|
|
|
AddArg("-march=icelake-server");
|
|
|
|
} else {
|
|
|
|
AddArg("-march=icelake-client");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case X86_MARCH_TIGERLAKE:
|
|
|
|
AddArg("-march=tigerlake");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_BONNELL:
|
|
|
|
case X86_MARCH_SALTWELL:
|
|
|
|
AddArg("-march=bonnell");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_SILVERMONT:
|
|
|
|
case X86_MARCH_AIRMONT:
|
|
|
|
AddArg("-march=silvermont");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_GOLDMONT:
|
|
|
|
AddArg("-march=goldmont");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_GOLDMONTPLUS:
|
|
|
|
AddArg("-march=goldmont-plus");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_TREMONT:
|
|
|
|
AddArg("-march=tremont");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_KNIGHTSLANDING:
|
|
|
|
AddArg("-march=knl");
|
|
|
|
break;
|
|
|
|
case X86_MARCH_KNIGHTSMILL:
|
|
|
|
AddArg("-march=knm");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* __x86_64__ */
|
|
|
|
|
2021-02-07 14:11:44 +00:00
|
|
|
} else if (!strcmp(argv[i], "-fsanitize=address")) {
|
|
|
|
if (isgcc && ccversion >= 6) wantasan = true;
|
|
|
|
} else if (!strcmp(argv[i], "-fsanitize=undefined")) {
|
|
|
|
if (isgcc && ccversion >= 6) wantubsan = true;
|
|
|
|
} else if (!strcmp(argv[i], "-fno-sanitize=address")) {
|
|
|
|
wantasan = false;
|
|
|
|
} else if (!strcmp(argv[i], "-fno-sanitize=undefined")) {
|
|
|
|
wantubsan = false;
|
|
|
|
} else if (!strcmp(argv[i], "-fno-sanitize=all")) {
|
|
|
|
wantasan = false;
|
|
|
|
wantubsan = false;
|
2021-05-14 12:36:58 +00:00
|
|
|
} else if (!strcmp(argv[i], "-fno-sanitize=null")) {
|
|
|
|
if (isgcc && ccversion >= 6) no_sanitize_null = true;
|
|
|
|
} else if (!strcmp(argv[i], "-fno-sanitize=alignment")) {
|
|
|
|
if (isgcc && ccversion >= 6) no_sanitize_alignment = true;
|
|
|
|
} else if (!strcmp(argv[i], "-fno-sanitize=pointer-overflow")) {
|
|
|
|
if (isgcc && ccversion >= 6) no_sanitize_pointer_overflow = true;
|
2023-08-14 03:31:27 +00:00
|
|
|
} else if (startswith(argv[i], "-fsanitize=implicit") &&
|
2021-02-07 14:11:44 +00:00
|
|
|
strstr(argv[i], "integer")) {
|
2021-02-20 20:39:39 +00:00
|
|
|
if (isgcc) AddArg(argv[i]);
|
2023-07-10 11:29:46 +00:00
|
|
|
} else if (strstr(argv[i], "stack-protector")) {
|
|
|
|
if (isclang || (isgcc && ccversion >= 6)) {
|
|
|
|
AddArg(argv[i]);
|
|
|
|
}
|
2023-08-14 03:31:27 +00:00
|
|
|
} else if (startswith(argv[i], "-fvect-cost") ||
|
|
|
|
startswith(argv[i], "-mstringop") ||
|
|
|
|
startswith(argv[i], "-gz") || strstr(argv[i], "sanitize") ||
|
|
|
|
startswith(argv[i], "-fvect-cost") ||
|
|
|
|
startswith(argv[i], "-fvect-cost")) {
|
2021-02-07 14:11:44 +00:00
|
|
|
if (isgcc && ccversion >= 6) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg(argv[i]);
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
2023-08-14 03:31:27 +00:00
|
|
|
} else if (startswith(argv[i], "-fdiagnostic-color=")) {
|
2021-02-07 14:11:44 +00:00
|
|
|
colorflag = argv[i];
|
2023-08-14 03:31:27 +00:00
|
|
|
} else if (startswith(argv[i], "-R") ||
|
2021-02-07 14:11:44 +00:00
|
|
|
!strcmp(argv[i], "-fsave-optimization-record")) {
|
2021-02-20 20:39:39 +00:00
|
|
|
if (isclang) AddArg(argv[i]);
|
2023-08-14 03:31:27 +00:00
|
|
|
} else if (isclang && startswith(argv[i], "--debug-prefix-map")) {
|
2021-02-08 17:19:00 +00:00
|
|
|
/* llvm doesn't provide a gas interface so simulate w/ clang */
|
2021-09-28 05:58:51 +00:00
|
|
|
AddArg(xstrcat("-f", argv[i] + 2));
|
2021-04-10 23:22:35 +00:00
|
|
|
} else if (isgcc && (!strcmp(argv[i], "-Os") || !strcmp(argv[i], "-O2") ||
|
|
|
|
!strcmp(argv[i], "-O3"))) {
|
|
|
|
/* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97623 */
|
|
|
|
AddArg(argv[i]);
|
2024-02-01 11:39:46 +00:00
|
|
|
if (!isclang) {
|
|
|
|
AddArg("-fno-code-hoisting");
|
|
|
|
}
|
2021-02-07 14:11:44 +00:00
|
|
|
} else {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg(argv[i]);
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
if (outpath) {
|
|
|
|
if (!target) {
|
|
|
|
target = outpath;
|
|
|
|
}
|
|
|
|
} else if (target) {
|
2021-02-20 06:20:38 +00:00
|
|
|
outpath = target;
|
2021-09-28 05:58:51 +00:00
|
|
|
} else {
|
|
|
|
fputs("error: compile.com needs -TTARGET or -oOUTPATH\n", stderr);
|
|
|
|
exit(7);
|
2021-02-20 06:20:38 +00:00
|
|
|
}
|
2021-02-07 14:11:44 +00:00
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// append special args
|
2021-02-07 14:11:44 +00:00
|
|
|
if (iscc) {
|
|
|
|
if (isclang) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-Wno-unused-command-line-argument");
|
|
|
|
AddArg("-Wno-incompatible-pointer-types-discards-qualifiers");
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
2022-03-18 09:33:37 +00:00
|
|
|
if (!__nocolor) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg(firstnonnull(colorflag, "-fdiagnostics-color=always"));
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
if (wantpg && !wantnopg) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-pg");
|
|
|
|
AddArg("-D__PG__");
|
2021-02-07 14:11:44 +00:00
|
|
|
if (wantnop && !isclang) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-mnop-mcount");
|
|
|
|
AddArg("-D__MNOP_MCOUNT__");
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
if (wantrecord) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-mrecord-mcount");
|
|
|
|
AddArg("-D__MRECORD_MCOUNT__");
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
if (wantfentry) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-mfentry");
|
|
|
|
AddArg("-D__MFENTRY__");
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wantasan) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-fsanitize=address");
|
2022-03-21 10:46:16 +00:00
|
|
|
/* compiler adds this by default */
|
|
|
|
/* AddArg("-D__SANITIZE_ADDRESS__"); */
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
if (wantubsan) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-fsanitize=undefined");
|
|
|
|
AddArg("-fno-data-sections");
|
2022-03-21 10:46:16 +00:00
|
|
|
AddArg("-D__SANITIZE_UNDEFINED__");
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
2021-05-14 12:36:58 +00:00
|
|
|
if (no_sanitize_null) {
|
|
|
|
AddArg("-fno-sanitize=null");
|
|
|
|
}
|
|
|
|
if (no_sanitize_alignment) {
|
|
|
|
AddArg("-fno-sanitize=alignment");
|
|
|
|
}
|
|
|
|
if (no_sanitize_pointer_overflow) {
|
|
|
|
AddArg("-fno-sanitize=pointer-overflow");
|
|
|
|
}
|
2021-10-15 02:36:49 +00:00
|
|
|
if (wantnoredzone) {
|
|
|
|
AddArg("-mno-red-zone");
|
|
|
|
AddArg("-D__MNO_RED_ZONE__");
|
|
|
|
}
|
2021-02-08 17:19:00 +00:00
|
|
|
if (wantframe) {
|
2021-02-20 20:39:39 +00:00
|
|
|
AddArg("-fno-omit-frame-pointer");
|
2021-10-15 02:36:49 +00:00
|
|
|
AddArg("-D__FNO_OMIT_FRAME_POINTER__");
|
2022-03-16 20:33:13 +00:00
|
|
|
} else {
|
|
|
|
AddArg("-fomit-frame-pointer");
|
2021-02-08 17:19:00 +00:00
|
|
|
}
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// scrub environment for determinism and great justice
|
2021-02-20 20:39:39 +00:00
|
|
|
for (envp = environ; *envp; ++envp) {
|
2023-08-14 03:31:27 +00:00
|
|
|
if (startswith(*envp, "MODE=")) {
|
2021-09-28 05:58:51 +00:00
|
|
|
mode = *envp + 5;
|
|
|
|
}
|
2021-02-20 20:39:39 +00:00
|
|
|
if (IsSafeEnv(*envp)) {
|
|
|
|
AddEnv(*envp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AddEnv("LC_ALL=C");
|
|
|
|
AddEnv("SOURCE_DATE_EPOCH=0");
|
2021-02-07 14:11:44 +00:00
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// ensure output directory exists
|
2021-02-07 14:11:44 +00:00
|
|
|
if (outpath) {
|
|
|
|
outdir = xdirname(outpath);
|
|
|
|
if (!isdirectory(outdir)) {
|
2022-09-08 05:52:24 +00:00
|
|
|
MakeDirs(outdir, 0755);
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// make sense of standard i/o file descriptors
|
|
|
|
// we want to permit pipelines but prevent talking to terminal
|
2021-09-28 05:58:51 +00:00
|
|
|
stdoutmustclose = fstat(1, &st) == -1 || S_ISCHR(st.st_mode);
|
|
|
|
if (fstat(0, &st) == -1 || S_ISCHR(st.st_mode)) {
|
|
|
|
close(0);
|
|
|
|
open("/dev/null", O_RDONLY);
|
|
|
|
}
|
2021-02-20 20:39:39 +00:00
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// SIGINT (CTRL-C) and SIGQUIT (CTRL-\) are delivered to the child
|
|
|
|
// process, so we should ignore it and wait for the child to die.
|
|
|
|
// SIGPIPE shouldn't happen until the very end since we buffer so it
|
|
|
|
// is safe to let it kill the prog.
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
sigaddset(&mask, SIGQUIT);
|
2021-02-07 14:11:44 +00:00
|
|
|
sigprocmask(SIG_BLOCK, &mask, &savemask);
|
2023-09-10 15:12:43 +00:00
|
|
|
|
|
|
|
// we want SIGCHLD to interrupt the read() operation
|
2021-09-28 05:58:51 +00:00
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
|
|
|
|
sa.sa_sigaction = OnChld;
|
2023-09-10 15:12:43 +00:00
|
|
|
sigaction(SIGCHLD, &sa, 0);
|
|
|
|
|
|
|
|
// set a death clock if requested
|
2021-09-28 05:58:51 +00:00
|
|
|
if (timeout > 0) {
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sa.sa_handler = OnAlrm;
|
|
|
|
sigaction(SIGALRM, &sa, 0);
|
|
|
|
}
|
2021-02-20 06:20:38 +00:00
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// run command
|
2021-09-28 05:58:51 +00:00
|
|
|
ws = Launch();
|
2021-02-07 14:11:44 +00:00
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// propagate exit
|
2021-09-28 05:58:51 +00:00
|
|
|
if (ws != -1) {
|
|
|
|
if (WIFEXITED(ws)) {
|
|
|
|
if (!(exitcode = WEXITSTATUS(ws)) || exitcode == 254) {
|
|
|
|
if (touchtarget && target) {
|
2022-09-08 05:52:24 +00:00
|
|
|
MakeDirs(xdirname(target), 0755);
|
2022-08-15 22:18:37 +00:00
|
|
|
if (touch(target, 0644)) {
|
|
|
|
exitcode = 90;
|
|
|
|
appends(&output, "\nfailed to touch output file\n");
|
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
}
|
2022-08-09 13:28:41 +00:00
|
|
|
if (movepath) {
|
|
|
|
if (!MovePreservingDestinationInode(tmpout, movepath)) {
|
|
|
|
unlink(tmpout);
|
|
|
|
exitcode = 90;
|
|
|
|
appends(&output, "\nfailed to move output file\n");
|
|
|
|
appends(&output, tmpout);
|
|
|
|
appends(&output, "\n");
|
|
|
|
appends(&output, movepath);
|
|
|
|
appends(&output, "\n");
|
|
|
|
} else {
|
|
|
|
unlink(tmpout);
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
} else {
|
|
|
|
appendw(&output, '\n');
|
|
|
|
PrintRed();
|
|
|
|
appendw(&output, '`');
|
|
|
|
PrintMakeCommand();
|
|
|
|
appends(&output, "` exited with ");
|
|
|
|
appendd(&output, buf, FormatUint64(buf, exitcode) - buf);
|
|
|
|
PrintReset();
|
|
|
|
appendw(&output, READ16LE(":\n"));
|
2021-02-20 06:20:38 +00:00
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
} else if (WTERMSIG(ws) == SIGINT) {
|
|
|
|
appendr(&output, 0);
|
|
|
|
appends(&output, "\rinterrupted: ");
|
|
|
|
PrintMakeCommand();
|
|
|
|
appendw(&output, '\n');
|
|
|
|
WriteAllUntilSignalledOrError(2, output, appendz(output).i);
|
|
|
|
return 128 + SIGINT;
|
2021-02-20 06:20:38 +00:00
|
|
|
} else {
|
2021-09-28 05:58:51 +00:00
|
|
|
exitcode = 128 + WTERMSIG(ws);
|
|
|
|
appendw(&output, '\n');
|
|
|
|
PrintRed();
|
|
|
|
appendw(&output, '`');
|
|
|
|
PrintMakeCommand();
|
|
|
|
appends(&output, "` terminated by ");
|
|
|
|
appends(&output, strsignal(WTERMSIG(ws)));
|
|
|
|
PrintReset();
|
|
|
|
appendw(&output, READ16LE(":\n"));
|
2022-03-16 20:33:13 +00:00
|
|
|
appends(&output, "env -i ");
|
|
|
|
for (i = 0; i < env.n; ++i) {
|
|
|
|
if (ArgNeedsShellQuotes(env.p[i])) {
|
|
|
|
q = AddShellQuotes(env.p[i]);
|
|
|
|
appends(&output, q);
|
|
|
|
free(q);
|
|
|
|
} else {
|
|
|
|
appends(&output, env.p[i]);
|
|
|
|
}
|
|
|
|
appendw(&output, ' ');
|
|
|
|
}
|
2021-02-20 06:20:38 +00:00
|
|
|
}
|
2021-02-07 14:11:44 +00:00
|
|
|
} else {
|
2021-09-28 05:58:51 +00:00
|
|
|
exitcode = 89;
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
2021-02-20 06:20:38 +00:00
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// describe command that was run
|
2021-09-28 05:58:51 +00:00
|
|
|
if (!exitcode || exitcode == 254) {
|
|
|
|
if (exitcode == 254) {
|
|
|
|
exitcode = 0;
|
|
|
|
fulloutput = true;
|
|
|
|
} else if (verbose < 5) {
|
|
|
|
appendr(&output, 0);
|
|
|
|
fulloutput = false;
|
|
|
|
} else {
|
|
|
|
fulloutput = !!appendz(output).i;
|
|
|
|
}
|
|
|
|
if (fulloutput) {
|
|
|
|
ReportResources();
|
|
|
|
}
|
2022-03-18 09:33:37 +00:00
|
|
|
if (!__nocolor && ischardev(2)) {
|
2021-09-28 05:58:51 +00:00
|
|
|
/* clear line forward */
|
|
|
|
appendw(&output, READ32LE("\e[K"));
|
|
|
|
}
|
|
|
|
if (verbose < 1) {
|
|
|
|
/* make silent mode, i.e. `V=0 make o//target` */
|
|
|
|
appendr(&command, 0);
|
|
|
|
if (!action) action = "BUILD";
|
|
|
|
if (!outpath) outpath = shortened;
|
|
|
|
n = strlen(action);
|
|
|
|
appends(&command, action);
|
2022-09-13 08:48:28 +00:00
|
|
|
do appendw(&command, ' '), ++n;
|
|
|
|
while (n < 15);
|
2021-09-28 05:58:51 +00:00
|
|
|
appends(&command, outpath);
|
|
|
|
n += strlen(outpath);
|
|
|
|
m = GetTerminalWidth();
|
|
|
|
if (m > 3 && n > m) {
|
|
|
|
appendd(&output, command, m - 3);
|
|
|
|
appendw(&output, READ32LE("..."));
|
|
|
|
} else {
|
2022-09-13 08:48:28 +00:00
|
|
|
if (n < m && (__nocolor || !ischardev(2))) {
|
|
|
|
while (n < m) appendw(&command, ' '), ++n;
|
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
appendd(&output, command, n);
|
|
|
|
}
|
2022-09-13 08:48:28 +00:00
|
|
|
appendw(&output, m > 0 ? '\r' : '\n');
|
2021-09-28 05:58:51 +00:00
|
|
|
} else {
|
|
|
|
n = 0;
|
|
|
|
if (verbose >= 3) {
|
|
|
|
/* make bonus mode (shows resource usage) */
|
|
|
|
if (timeout > 0) {
|
|
|
|
us = GetTimespecMicros(finish) - GetTimespecMicros(start);
|
|
|
|
i = FormatUint64Thousands(buf, us) - buf;
|
|
|
|
j = ceil(log10(timeout));
|
|
|
|
j += (j - 1) / 3;
|
|
|
|
j += 1 + 3;
|
|
|
|
j += 1 + 3;
|
|
|
|
while (i < j) appendw(&output, ' '), ++i;
|
|
|
|
if (us > timeout * 1000000ull / 2) {
|
|
|
|
if (us > timeout * 1000000ull) {
|
|
|
|
PrintRed();
|
|
|
|
} else {
|
|
|
|
PrintBold();
|
|
|
|
}
|
|
|
|
appends(&output, buf);
|
|
|
|
PrintReset();
|
|
|
|
} else {
|
|
|
|
appends(&output, buf);
|
|
|
|
}
|
|
|
|
appendw(&output, READ32LE("⏰ "));
|
|
|
|
n += i + 2 + 1;
|
|
|
|
}
|
|
|
|
if (cpuquota > 0) {
|
|
|
|
if (!(us = GetTimevalMicros(usage.ru_utime) +
|
|
|
|
GetTimevalMicros(usage.ru_stime))) {
|
|
|
|
us = GetTimespecMicros(finish) - GetTimespecMicros(start);
|
|
|
|
}
|
|
|
|
i = FormatUint64Thousands(buf, us) - buf;
|
|
|
|
j = ceil(log10(cpuquota));
|
|
|
|
j += (j - 1) / 3;
|
|
|
|
j += 1 + 3;
|
|
|
|
j += 1 + 3;
|
|
|
|
while (i < j) appendw(&output, ' '), ++i;
|
|
|
|
if ((isproblematic = us > cpuquota * 1000000ull / 2)) {
|
|
|
|
if (us > cpuquota * 1000000ull - (cpuquota * 1000000ull) / 5) {
|
|
|
|
PrintRed();
|
|
|
|
} else {
|
|
|
|
PrintBold();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appends(&output, buf);
|
|
|
|
appendw(&output, READ32LE("⏳ "));
|
|
|
|
if (isproblematic) {
|
|
|
|
PrintReset();
|
|
|
|
}
|
|
|
|
n += i + 2 + 1;
|
|
|
|
}
|
|
|
|
if (memquota > 0) {
|
|
|
|
i = FormatUint64Thousands(buf, usage.ru_maxrss) - buf;
|
|
|
|
j = ceil(log10(memquota / 1024));
|
|
|
|
j += (j - 1) / 3;
|
|
|
|
while (i < j) appendw(&output, ' '), ++i;
|
|
|
|
if ((isproblematic = usage.ru_maxrss * 1024 > memquota / 2)) {
|
|
|
|
if (usage.ru_maxrss * 1024 > memquota - memquota / 5) {
|
|
|
|
PrintRed();
|
|
|
|
} else {
|
|
|
|
PrintBold();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appends(&output, buf);
|
|
|
|
appendw(&output, READ16LE("k "));
|
|
|
|
if (isproblematic) {
|
|
|
|
PrintReset();
|
|
|
|
}
|
|
|
|
n += i + 1 + 1;
|
|
|
|
}
|
|
|
|
if (fszquota > 0) {
|
|
|
|
us = usage.ru_inblock + usage.ru_oublock;
|
|
|
|
i = FormatUint64Thousands(buf, us) - buf;
|
|
|
|
while (i < 7) appendw(&output, ' '), ++i;
|
|
|
|
appends(&output, buf);
|
|
|
|
appendw(&output, READ32LE("iop "));
|
|
|
|
n += i + 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* make normal mode (echos run commands) */
|
|
|
|
if (verbose < 2 || verbose == 3) {
|
|
|
|
command = shortened;
|
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
m = GetLineWidth(&isineditor);
|
2021-09-28 05:58:51 +00:00
|
|
|
if (m > n + 3 && appendz(command).i > m - n) {
|
2022-03-16 20:33:13 +00:00
|
|
|
if (isineditor) {
|
|
|
|
if (m > n + 3 && appendz(shortened).i > m - n) {
|
|
|
|
appendd(&output, shortened, m - n - 3);
|
|
|
|
appendw(&output, READ32LE("..."));
|
|
|
|
} else {
|
|
|
|
appendd(&output, shortened, appendz(shortened).i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
appendd(&output, command, m - n - 3);
|
|
|
|
appendw(&output, READ32LE("..."));
|
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
} else {
|
|
|
|
appendd(&output, command, appendz(command).i);
|
|
|
|
}
|
|
|
|
appendw(&output, '\n');
|
|
|
|
}
|
2021-09-07 02:24:10 +00:00
|
|
|
} else {
|
2021-09-28 05:58:51 +00:00
|
|
|
n = appendz(command).i;
|
|
|
|
if (n > 2048) {
|
|
|
|
appendr(&command, (n = 2048));
|
|
|
|
appendw(&command, READ32LE("..."));
|
|
|
|
}
|
|
|
|
appendd(&output, command, n);
|
|
|
|
ReportResources();
|
|
|
|
}
|
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
// flush output
|
2021-09-28 05:58:51 +00:00
|
|
|
if (WriteAllUntilSignalledOrError(2, output, appendz(output).i) == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
s = "notice: compile.com output truncated\n";
|
|
|
|
} else {
|
|
|
|
if (!exitcode) exitcode = 55;
|
|
|
|
s = "error: compile.com failed to write result\n";
|
|
|
|
}
|
|
|
|
write(2, s, strlen(s));
|
2021-09-07 02:24:10 +00:00
|
|
|
}
|
2021-09-28 05:58:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* all done!
|
|
|
|
*/
|
|
|
|
return exitcode;
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|