Skip to content

Commit

Permalink
Improve mkdeps
Browse files Browse the repository at this point in the history
Our makefile generator now accepts badly formatted include lines. It's
now more hermetic with better error checking in the cosmo repo, and it
can be configured to not be hermetic at all.
  • Loading branch information
jart committed Nov 10, 2023
1 parent 241f949 commit d2f49ca
Show file tree
Hide file tree
Showing 69 changed files with 465 additions and 532 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ o/$(MODE)/hdrs.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(HDRS) $(
o/$(MODE)/incs.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(INCS) $(INCS),$(dir $(x)))) $(INCS) $(INCS)
$(file >$@,$(INCS))
o/$(MODE)/depend: o/$(MODE)/.x o/$(MODE)/srcs.txt o/$(MODE)/hdrs.txt o/$(MODE)/incs.txt $(SRCS) $(HDRS) $(INCS)
$(COMPILE) -AMKDEPS -L320 $(MKDEPS) -o $@ -r o/$(MODE)/ @o/$(MODE)/srcs.txt @o/$(MODE)/hdrs.txt @o/$(MODE)/incs.txt
$(COMPILE) -AMKDEPS -L320 $(MKDEPS) -o $@ -s -r o/$(MODE)/ @o/$(MODE)/srcs.txt @o/$(MODE)/hdrs.txt @o/$(MODE)/incs.txt

o/$(MODE)/srcs-old.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(SRCS),$(dir $(x))))
$(file >$@) $(foreach x,$(SRCS),$(file >>$@,$(x)))
Expand Down
12 changes: 12 additions & 0 deletions bin/fatcosmocc
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ NEED_JOIN=
NEED_EQUAL=
NEED_OUTPUT=
APELINKFLAGS=
FLAGS_X86_64=
FLAGS_AARCH64=
INPUT_FILE_COUNT=0
for x; do
if [ x"$x" != x"${x#* }" ]; then
Expand Down Expand Up @@ -335,6 +337,14 @@ for x; do
continue
elif [ x"$x" = x"-march=native" ]; then
fatal_error "-march=native can't be used when building fat binaries"
elif [ x"$x" != x"${x#-Xx86_64}" ]; then
x=${x#-Xx86_64} # e.g. -Xx86_64,-msse3,-mavx,-mavx2,-mf16c,-mfma
FLAGS_X86_64="$FLAGS_X86_64 ${x//,/ }"
continue
elif [ x"$x" != x"${x#-Xaarch64}" ]; then
x=${x#-Xaarch64}
FLAGS_aarch64="$FLAGS_aarch64 ${x//,/ }"
continue
elif [ x"$x" = x"-dumpversion" ]; then
echo $GCC_VERSION
Exit 0
Expand Down Expand Up @@ -473,6 +483,7 @@ build_object() {
$CFLAGS_X86_64 \
$CPPFLAGS_X86_64 \
"$@" \
$FLAGS_X86_64 \
$PRECIOUS
log_command "$@"
"$@" || exit
Expand All @@ -492,6 +503,7 @@ build_object() {
$CFLAGS_AARCH64 \
$CPPFLAGS_AARCH64 \
"$@" \
$FLAGS_AARCH64 \
$PRECIOUS &&
log_command "$@"
"$@" || exit
Expand Down
Binary file modified build/bootstrap/mkdeps.com
Binary file not shown.
1 change: 1 addition & 0 deletions libc/calls/getrlimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/runtime/syslib.internal.h"
#include "libc/sysv/consts/rlimit.h"
Expand Down
4 changes: 0 additions & 4 deletions libc/calls/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ int64_t GetConsoleOutputHandle(void);
int IsWindowsExecutable(int64_t, const char16_t *);
void InterceptTerminalCommands(const char *, size_t);

forceinline int64_t __getfdhandleactual(int fd) {
return g_fds.p[fd].handle;
}

forceinline bool __isfdopen(int fd) {
return 0 <= fd && fd < g_fds.n && g_fds.p[fd].kind != kFdEmpty;
}
Expand Down
2 changes: 1 addition & 1 deletion libc/calls/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static int ioctl_default(int fd, unsigned long request, void *arg) {
return sys_ioctl(fd, request, arg);
} else if (__isfdopen(fd)) {
if (g_fds.p[fd].kind == kFdSocket) {
handle = __getfdhandleactual(fd);
handle = g_fds.p[fd].handle;
if ((rc = _weaken(__sys_ioctlsocket_nt)(handle, request, arg)) != -1) {
return rc;
} else {
Expand Down
2 changes: 1 addition & 1 deletion libc/calls/ischardev.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ bool32 ischardev(int fd) {
} else {
return __isfdkind(fd, kFdConsole) || __isfdkind(fd, kFdDevNull) ||
(__isfdkind(fd, kFdFile) &&
GetFileType(__getfdhandleactual(fd)) == kNtFileTypeChar);
GetFileType(g_fds.p[fd].handle) == kNtFileTypeChar);
}
}
3 changes: 1 addition & 2 deletions libc/calls/pthread_yield.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/nexgen32e/yield.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/syslib.internal.h"
#include "libc/thread/thread.h"
Expand All @@ -32,7 +31,7 @@ int pthread_yield(void) {
if (IsXnuSilicon()) {
__syslib->__pthread_yield_np();
} else if (IsOpenbsd()) {
spin_yield(); // sched_yield() is punishingly slow on OpenBSD
pthread_pause_np(); // sched_yield() is punishingly slow on OpenBSD
} else {
sched_yield();
}
Expand Down
20 changes: 10 additions & 10 deletions libc/calls/read-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct Keystrokes {

static struct Keystrokes __keystroke;

textwindows void __keystroke_wipe(void) {
textwindows void WipeKeystrokes(void) {
bzero(&__keystroke, sizeof(__keystroke));
}

Expand Down Expand Up @@ -754,8 +754,8 @@ static textwindows ssize_t ReadFromConsole(struct Fd *f, void *data,
return rc;
}

textwindows ssize_t sys_read_nt_impl(int fd, void *data, size_t size,
int64_t offset, sigset_t waitmask) {
textwindows ssize_t ReadBuffer(int fd, void *data, size_t size, int64_t offset,
sigset_t waitmask) {

// switch to terminal polyfill if reading from win32 console
struct Fd *f = g_fds.p + fd;
Expand Down Expand Up @@ -786,18 +786,18 @@ textwindows ssize_t sys_read_nt_impl(int fd, void *data, size_t size,
}
}

static textwindows ssize_t sys_read_nt2(int fd, const struct iovec *iov,
size_t iovlen, int64_t opt_offset,
sigset_t waitmask) {
static textwindows ssize_t ReadIovecs(int fd, const struct iovec *iov,
size_t iovlen, int64_t opt_offset,
sigset_t waitmask) {
ssize_t rc;
size_t i, total;
if (opt_offset < -1) return einval();
while (iovlen && !iov[0].iov_len) iov++, iovlen--;
if (iovlen) {
for (total = i = 0; i < iovlen; ++i) {
if (!iov[i].iov_len) continue;
rc = sys_read_nt_impl(fd, iov[i].iov_base, iov[i].iov_len, opt_offset,
waitmask);
rc =
ReadBuffer(fd, iov[i].iov_base, iov[i].iov_len, opt_offset, waitmask);
if (rc == -1) {
if (total && errno != ECANCELED) {
return total;
Expand All @@ -811,15 +811,15 @@ static textwindows ssize_t sys_read_nt2(int fd, const struct iovec *iov,
}
return total;
} else {
return sys_read_nt_impl(fd, NULL, 0, opt_offset, waitmask);
return ReadBuffer(fd, NULL, 0, opt_offset, waitmask);
}
}

textwindows ssize_t sys_read_nt(int fd, const struct iovec *iov, size_t iovlen,
int64_t opt_offset) {
ssize_t rc;
sigset_t m = __sig_block();
rc = sys_read_nt2(fd, iov, iovlen, opt_offset, m);
rc = ReadIovecs(fd, iov, iovlen, opt_offset, m);
__sig_unblock(m);
return rc;
}
Expand Down
1 change: 1 addition & 0 deletions libc/calls/setrlimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/syslib.internal.h"
#include "libc/sysv/consts/rlimit.h"
#include "libc/sysv/errfuns.h"
Expand Down
2 changes: 0 additions & 2 deletions libc/calls/sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ textwindows int __sig_raise(volatile int sig, int sic) {
STRACE("__sig_raise(%G, %t) mask %s", sig, __sig_handler(rva),
(DescribeSigset)(ssbuf, 0, (sigset_t *)&pt->tib->tib_sigmask));
__sig_handler(rva)(sig, &si, &ctx);
(void)ssbuf;

// record this handler
if (flags & SA_RESTART) {
Expand Down Expand Up @@ -271,7 +270,6 @@ static textwindows wontreturn void __sig_tramp(struct SignalFrame *sf) {
(DescribeSigset)(ssbuf[0], 0, &sf->ctx.uc_sigmask),
(DescribeSigset)(ssbuf[1], 0, (sigset_t *)&tib->tib_sigmask));
__sig_handler(sf->rva)(sig, &sf->si, &sf->ctx);
(void)ssbuf;

// restore the signal mask that was used by the interrupted code
// this may have been modified by the signal handler in the callback
Expand Down
1 change: 0 additions & 1 deletion libc/calls/sigtimedwait.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ int sigtimedwait(const sigset_t *set, siginfo_t *info,
struct timespec ts;
union siginfo_meta si = {0};
BEGIN_CANCELATION_POINT;
(void)strsig;

if (IsAsan() && (!__asan_is_valid(set, sizeof(*set)) ||
(info && !__asan_is_valid(info, sizeof(*info))) ||
Expand Down
1 change: 1 addition & 0 deletions libc/calls/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h"

/**
* Flushes file system changes to disk by any means necessary.
Expand Down
1 change: 1 addition & 0 deletions libc/calls/sys_ptrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h"

#define IsPeek(request) (IsLinux() && (request)-1u < 3)

Expand Down
5 changes: 3 additions & 2 deletions libc/calls/winexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
#include "libc/str/tab.internal.h"
#include "third_party/linenoise/linenoise.h"

#define EXT(s) READ32LE(s "\0\0")
#define Read32(s) (s[3] << 24 | s[2] << 16 | s[1] << 8 | s[0])
#define EXT(s) Read32(s "\0\0")

static bool IsGraph(wint_t c) {
static inline bool IsGraph(wint_t c) {
return 0x21 <= c && c <= 0x7E;
}

Expand Down
1 change: 1 addition & 0 deletions libc/intrin/__getenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/dce.h"
#include "libc/intrin/getenv.internal.h"
#include "libc/intrin/kprintf.h"

privileged struct Env __getenv(char **p, const char *k) {
char *t;
Expand Down
4 changes: 0 additions & 4 deletions libc/intrin/createfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@ CreateFile(const char16_t *lpFileName, //
int64_t hHandle;
uint32_t micros = 1;
char buf_accessflags[512];
(void)buf_accessflags;
char buf_shareflags[64];
(void)buf_shareflags;
char buf_secattr[32];
(void)buf_secattr;
char buf_flagattr[256];
(void)buf_flagattr;
TryAgain:
hHandle = __imp_CreateFileW(lpFileName, dwDesiredAccess, dwShareMode,
opt_lpSecurity, dwCreationDisposition,
Expand Down
7 changes: 5 additions & 2 deletions libc/intrin/cxalock.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ void __cxa_unlock(void) {
pthread_mutex_unlock(&__cxa_lock_obj);
}

__attribute__((__constructor__)) static void __cxa_init(void) {
__cxa_wipe();
static textstartup void __cxa_init() {
pthread_atfork(__cxa_lock, __cxa_unlock, __cxa_wipe);
}

const void *const __cxa_ctor[] initarray = {
__cxa_init,
};
2 changes: 1 addition & 1 deletion libc/intrin/ftrace_enabled.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @param delta is added to enabled state
* @return enabled state before `delta` was applied
*/
dontinstrument int ftrace_enabled(int delta) {
dontinstrument int(ftrace_enabled)(int delta) {
int res;
struct CosmoTib *tib;
if (__tls_enabled) {
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/isdebuggerpresent.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int IsDebuggerPresent(bool force) {
int e, fd, res;
char *p, buf[1024];
if (!force && IsGenuineBlink()) return 0;
if (!force && __getenv(environ, "HEISENDEBUG").s) return 0;
if (!force && environ && __getenv(environ, "HEISENDEBUG").s) return 0;
if (IsWindows()) return IsBeingDebugged();
if (__isworker) return false;
if (!PLEDGED(RPATH)) return false;
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/kprintf.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ privileged long kloghandle(void) {
// it's too early in the initialization process for kprintf
return -1;
}
path = __getenv(__envp, "KPRINTF_LOG").s;
path = environ ? __getenv(environ, "KPRINTF_LOG").s : 0;
closefd = false;
if (!path) {
fd = STDERR_FILENO;
Expand Down
5 changes: 2 additions & 3 deletions libc/intrin/pthread_mutex_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "libc/intrin/atomic.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/nexgen32e/yield.h"
#include "libc/runtime/internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
Expand Down Expand Up @@ -83,7 +82,7 @@ errno_t pthread_mutex_lock(pthread_mutex_t *mutex) {

if (mutex->_type == PTHREAD_MUTEX_NORMAL) {
while (atomic_exchange_explicit(&mutex->_lock, 1, memory_order_acquire)) {
spin_yield();
pthread_pause_np();
}
return 0;
}
Expand All @@ -103,7 +102,7 @@ errno_t pthread_mutex_lock(pthread_mutex_t *mutex) {
}

while (atomic_exchange_explicit(&mutex->_lock, 1, memory_order_acquire)) {
spin_yield();
pthread_pause_np();
}

mutex->_depth = 0;
Expand Down
4 changes: 2 additions & 2 deletions libc/intrin/pthread_next.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
#include "libc/assert.h"
#include "libc/dce.h"
#include "libc/intrin/atomic.h"
#include "libc/nexgen32e/yield.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"

intptr_t _pthread_syshand(struct PosixThread *pt) {
intptr_t syshand;
unassert(IsWindows() || IsXnuSilicon());
for (;;) {
syshand = atomic_load_explicit(&pt->tib->tib_syshand, memory_order_acquire);
if (syshand) return syshand;
spin_yield();
pthread_pause_np();
}
}
4 changes: 2 additions & 2 deletions libc/intrin/pthread_tid.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/intrin/atomic.h"
#include "libc/nexgen32e/yield.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"

int _pthread_tid(struct PosixThread *pt) {
int tid = 0;
while (pt && !(tid = atomic_load_explicit(&pt->ptid, memory_order_acquire))) {
spin_yield();
pthread_pause_np();
}
return tid;
}
Loading

0 comments on commit d2f49ca

Please sign in to comment.