Skip to content

Commit

Permalink
Strongly link glob() into system() and popen()
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Nov 16, 2024
1 parent 4e9566c commit cafdb45
Show file tree
Hide file tree
Showing 24 changed files with 216 additions and 59 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ include libc/BUILD.mk #─┘
include libc/sock/BUILD.mk #─┐
include net/http/BUILD.mk # ├──ONLINE RUNTIME
include third_party/musl/BUILD.mk # │ You can communicate with the network
include libc/system/BUILD.mk #
include libc/x/BUILD.mk #
include dsp/scale/BUILD.mk #
include dsp/mpeg/BUILD.mk #
Expand Down Expand Up @@ -367,6 +368,7 @@ include test/libc/fmt/BUILD.mk
include test/libc/time/BUILD.mk
include test/libc/proc/BUILD.mk
include test/libc/stdio/BUILD.mk
include test/libc/system/BUILD.mk
include test/libc/BUILD.mk
include test/net/http/BUILD.mk
include test/net/https/BUILD.mk
Expand Down Expand Up @@ -449,14 +451,14 @@ COSMOPOLITAN = \
LIBC_NT_BCRYPTPRIMITIVES \
LIBC_NT_COMDLG32 \
LIBC_NT_GDI32 \
LIBC_NT_SHELL32 \
LIBC_NT_IPHLPAPI \
LIBC_NT_KERNEL32 \
LIBC_NT_NTDLL \
LIBC_NT_PDH \
LIBC_NT_POWRPROF \
LIBC_NT_PSAPI \
LIBC_NT_REALTIME \
LIBC_NT_SHELL32 \
LIBC_NT_SYNCHRONIZATION \
LIBC_NT_USER32 \
LIBC_NT_WS2_32 \
Expand All @@ -465,6 +467,7 @@ COSMOPOLITAN = \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSTEM \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_THREAD \
Expand Down
9 changes: 5 additions & 4 deletions examples/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ EXAMPLES_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_NT_ADVAPI32 \
LIBC_NT_IPHLPAPI \
LIBC_NT_MEMORY \
LIBC_NT_KERNEL32 \
LIBC_NT_MEMORY \
LIBC_NT_NTDLL \
LIBC_NT_USER32 \
LIBC_NT_WS2_32 \
Expand All @@ -64,6 +64,7 @@ EXAMPLES_DIRECTDEPS = \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSTEM \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_TESTLIB \
Expand All @@ -81,6 +82,8 @@ EXAMPLES_DIRECTDEPS = \
THIRD_PARTY_GETOPT \
THIRD_PARTY_HIREDIS \
THIRD_PARTY_LIBCXX \
THIRD_PARTY_LIBCXXABI \
THIRD_PARTY_LIBUNWIND \
THIRD_PARTY_LINENOISE \
THIRD_PARTY_LUA \
THIRD_PARTY_MBEDTLS \
Expand All @@ -94,12 +97,10 @@ EXAMPLES_DIRECTDEPS = \
THIRD_PARTY_TZ \
THIRD_PARTY_VQSORT \
THIRD_PARTY_XED \
THIRD_PARTY_LIBCXXABI \
THIRD_PARTY_LIBUNWIND \
THIRD_PARTY_ZLIB \
TOOL_ARGS \
TOOL_BUILD_LIB \
TOOL_VIZ_LIB
TOOL_VIZ_LIB \

EXAMPLES_DEPS := \
$(call uniq,$(foreach x,$(EXAMPLES_DIRECTDEPS),$($(x))))
Expand Down
81 changes: 81 additions & 0 deletions libc/system/BUILD.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘

PKGS += LIBC_SYSTEM

LIBC_SYSTEM_ARTIFACTS += LIBC_SYSTEM_A
LIBC_SYSTEM = $(LIBC_SYSTEM_A_DEPS) $(LIBC_SYSTEM_A)
LIBC_SYSTEM_A = o/$(MODE)/libc/system/system.a
LIBC_SYSTEM_A_FILES := $(wildcard libc/system/*)
LIBC_SYSTEM_A_HDRS = $(filter %.h,$(LIBC_SYSTEM_A_FILES))
LIBC_SYSTEM_A_INCS = $(filter %.inc,$(LIBC_SYSTEM_A_FILES))
LIBC_SYSTEM_A_SRCS_S = $(filter %.S,$(LIBC_SYSTEM_A_FILES))
LIBC_SYSTEM_A_SRCS_C = $(filter %.c,$(LIBC_SYSTEM_A_FILES))

LIBC_SYSTEM_A_SRCS = \
$(LIBC_SYSTEM_A_SRCS_S) \
$(LIBC_SYSTEM_A_SRCS_C)

LIBC_SYSTEM_A_OBJS = \
$(LIBC_SYSTEM_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_SYSTEM_A_SRCS_C:%.c=o/$(MODE)/%.o)

LIBC_SYSTEM_A_CHECKS = \
$(LIBC_SYSTEM_A).pkg \
$(LIBC_SYSTEM_A_HDRS:%=o/$(MODE)/%.ok)

LIBC_SYSTEM_A_DIRECTDEPS = \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_PROC \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
THIRD_PARTY_MUSL \

LIBC_SYSTEM_A_DEPS := \
$(call uniq,$(foreach x,$(LIBC_SYSTEM_A_DIRECTDEPS),$($(x))))

$(LIBC_SYSTEM_A):libc/system/ \
$(LIBC_SYSTEM_A).pkg \
$(LIBC_SYSTEM_A_OBJS)

$(LIBC_SYSTEM_A).pkg: \
$(LIBC_SYSTEM_A_OBJS) \
$(foreach x,$(LIBC_SYSTEM_A_DIRECTDEPS),$($(x)_A).pkg)

# offer assurances about the stack safety of cosmo libc
$(LIBC_SYSTEM_A_OBJS): private COPTS += -Wframe-larger-than=4096 -Walloca-larger-than=4096

$(LIBC_SYSTEM_A_OBJS): private \
CFLAGS += \
-fno-sanitize=all \
-Wframe-larger-than=4096 \
-Walloca-larger-than=4096

o/$(MODE)/libc/system/fputc.o: private \
CFLAGS += \
-O3

o//libc/system/appendw.o: private \
CFLAGS += \
-Os

o/$(MODE)/libc/system/dirstream.o \
o/$(MODE)/libc/system/mt19937.o: private \
CFLAGS += \
-ffunction-sections

LIBC_SYSTEM_LIBS = $(foreach x,$(LIBC_SYSTEM_ARTIFACTS),$($(x)))
LIBC_SYSTEM_SRCS = $(foreach x,$(LIBC_SYSTEM_ARTIFACTS),$($(x)_SRCS))
LIBC_SYSTEM_HDRS = $(foreach x,$(LIBC_SYSTEM_ARTIFACTS),$($(x)_HDRS))
LIBC_SYSTEM_INCS = $(foreach x,$(LIBC_SYSTEM_ARTIFACTS),$($(x)_INCS))
LIBC_SYSTEM_CHECKS = $(foreach x,$(LIBC_SYSTEM_ARTIFACTS),$($(x)_CHECKS))
LIBC_SYSTEM_OBJS = $(foreach x,$(LIBC_SYSTEM_ARTIFACTS),$($(x)_OBJS))
$(LIBC_SYSTEM_OBJS): $(BUILD_FILES) libc/system/BUILD.mk

.PHONY: o/$(MODE)/libc/system
o/$(MODE)/libc/system: $(LIBC_SYSTEM_CHECKS)
27 changes: 11 additions & 16 deletions libc/proc/cocmd.c → libc/system/cocmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1055,12 +1055,9 @@ int _cocmd(int argc, char **argv, char **envp) {
unsupported['('] = true;
unsupported[')'] = true;
unsupported['{'] = true;
unsupported['}'] = false; // Perl t/op/exec.t depends on unpaired } being
// passed from the shell to Perl
if (!_weaken(glob)) {
unsupported['*'] = true;
unsupported['?'] = true;
}
// Perl t/op/exec.t depends on unpaired } being
// passed from the shell to Perl
unsupported['}'] = false;

if (argc >= 3 && !strcmp(argv[1], "--")) {
for (i = 2; i < argc; ++i) {
Expand Down Expand Up @@ -1121,18 +1118,16 @@ int _cocmd(int argc, char **argv, char **envp) {
Open(GetRedirectArg(prog, arg, 1), 0, O_RDONLY);
} else {
int globrc = GLOB_NOMATCH;
if (_weaken(glob)) {
globrc = _weaken(glob)(arg, globFlags, NULL, &globTheBuilder);
if (globrc == 0) {
for (; globCount < globTheBuilder.gl_pathc; globCount++) {
args[n++] = globTheBuilder.gl_pathv[globCount];
}
} else if (globrc != GLOB_NOMATCH) {
tinyprint(2, prog, ": error: with glob\n", NULL);
_Exit(16);
globrc = glob(arg, globFlags, NULL, &globTheBuilder);
if (globrc == 0) {
for (; globCount < globTheBuilder.gl_pathc; globCount++) {
args[n++] = globTheBuilder.gl_pathv[globCount];
}
globFlags |= GLOB_APPEND;
} else if (globrc != GLOB_NOMATCH) {
tinyprint(2, prog, ": error: with glob\n", NULL);
_Exit(16);
}
globFlags |= GLOB_APPEND;
if (globrc == GLOB_NOMATCH) {
args[n++] = arg;
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion net/turfwar/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ NET_TURFWAR_DIRECTDEPS = \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSTEM \
LIBC_SYSV \
LIBC_THREAD \
LIBC_X \
Expand All @@ -39,7 +40,7 @@ NET_TURFWAR_DIRECTDEPS = \
THIRD_PARTY_SQLITE3 \
THIRD_PARTY_STB \
THIRD_PARTY_TZ \
THIRD_PARTY_ZLIB
THIRD_PARTY_ZLIB \

NET_TURFWAR_DEPS := \
$(call uniq,$(foreach x,$(NET_TURFWAR_DIRECTDEPS),$($(x))))
Expand Down
2 changes: 1 addition & 1 deletion test/ctl/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TEST_CTL_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_PROC \
LIBC_STDIO \
LIBC_STDIO \
LIBC_SYSTEM \
LIBC_THREAD \
THIRD_PARTY_LIBCXX \
THIRD_PARTY_LIBCXXABI \
Expand Down
12 changes: 0 additions & 12 deletions test/libc/proc/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,6 @@ o/$(MODE)/test/libc/proc/posix_spawn_test.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)

o/$(MODE)/test/libc/proc/system_test.dbg: \
$(TEST_LIBC_PROC_DEPS) \
o/$(MODE)/test/libc/proc/system_test.o \
o/$(MODE)/test/libc/proc/proc.pkg \
o/$(MODE)/tool/build/echo.zip.o \
o/$(MODE)/tool/build/cocmd.zip.o \
o/$(MODE)/tool/build/false.zip.o \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)

o/$(MODE)/test/libc/proc/execve_test.dbg: \
$(TEST_LIBC_PROC_DEPS) \
o/$(MODE)/test/libc/proc/execve_test.o \
Expand Down
17 changes: 4 additions & 13 deletions test/libc/stdio/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,27 @@ TEST_LIBC_STDIO_DIRECTDEPS = \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_PROC \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSTEM \
LIBC_SYSV \
LIBC_TINYMATH \
LIBC_TESTLIB \
LIBC_THREAD \
LIBC_LOG \
LIBC_TINYMATH \
LIBC_X \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_GDTOA \
THIRD_PARTY_MBEDTLS \
THIRD_PARTY_MUSL \
THIRD_PARTY_NSYNC \
THIRD_PARTY_TZ \
THIRD_PARTY_ZLIB \
THIRD_PARTY_ZLIB_GZ \
THIRD_PARTY_TZ

TEST_LIBC_STDIO_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_STDIO_DIRECTDEPS),$($(x))))
Expand All @@ -66,16 +67,6 @@ o/$(MODE)/test/libc/stdio/%.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)

o/$(MODE)/test/libc/stdio/popen_test.dbg: \
$(TEST_LIBC_STDIO_DEPS) \
o/$(MODE)/test/libc/stdio/popen_test.o \
o/$(MODE)/test/libc/stdio/stdio.pkg \
o/$(MODE)/tool/build/echo.zip.o \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)

$(TEST_LIBC_STDIO_OBJS): private \
DEFAULT_CCFLAGS += \
-fno-builtin
Expand Down
88 changes: 88 additions & 0 deletions test/libc/system/BUILD.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘

PKGS += TEST_LIBC_SYSTEM

TEST_LIBC_SYSTEM_FILES := $(wildcard test/libc/system/*)
TEST_LIBC_SYSTEM_SRCS = $(filter %.c,$(TEST_LIBC_SYSTEM_FILES))
TEST_LIBC_SYSTEM_INCS = $(filter %.inc,$(TEST_LIBC_SYSTEM_FILES))
TEST_LIBC_SYSTEM_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_SYSTEM_SRCS))

TEST_LIBC_SYSTEM_OBJS = \
$(TEST_LIBC_SYSTEM_SRCS:%.c=o/$(MODE)/%.o)

TEST_LIBC_SYSTEM_COMS = \
$(TEST_LIBC_SYSTEM_SRCS:%.c=o/$(MODE)/%)

TEST_LIBC_SYSTEM_BINS = \
$(TEST_LIBC_SYSTEM_COMS) \
$(TEST_LIBC_SYSTEM_COMS:%=%.dbg)

TEST_LIBC_SYSTEM_TESTS = \
$(TEST_LIBC_SYSTEM_SRCS_TEST:%.c=o/$(MODE)/%.ok)

TEST_LIBC_SYSTEM_CHECKS = \
$(TEST_LIBC_SYSTEM_SRCS_TEST:%.c=o/$(MODE)/%.runs)

TEST_LIBC_SYSTEM_DIRECTDEPS = \
LIBC_CALLS \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STDIO \
LIBC_SYSTEM \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_THREAD \
LIBC_X \
THIRD_PARTY_MUSL \
THIRD_PARTY_TR \

TEST_LIBC_SYSTEM_DEPS := \
$(call uniq,$(foreach x,$(TEST_LIBC_SYSTEM_DIRECTDEPS),$($(x))))

o/$(MODE)/test/libc/system/system.pkg: \
$(TEST_LIBC_SYSTEM_OBJS) \
$(foreach x,$(TEST_LIBC_SYSTEM_DIRECTDEPS),$($(x)_A).pkg)

o/$(MODE)/test/libc/system/%.dbg: \
$(TEST_LIBC_SYSTEM_DEPS) \
o/$(MODE)/test/libc/system/%.o \
o/$(MODE)/test/libc/system/system.pkg \
o/$(MODE)/tool/build/echo.zip.o \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)

o/$(MODE)/test/libc/system/popen_test.dbg: \
$(TEST_LIBC_SYSTEM_DEPS) \
o/$(MODE)/test/libc/system/popen_test.o \
o/$(MODE)/test/libc/system/system.pkg \
o/$(MODE)/tool/build/echo.zip.o \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)

o/$(MODE)/test/libc/system/system_test.dbg: \
$(TEST_LIBC_SYSTEM_DEPS) \
o/$(MODE)/test/libc/system/system_test.o \
o/$(MODE)/test/libc/system/system.pkg \
o/$(MODE)/tool/build/echo.zip.o \
o/$(MODE)/tool/build/cocmd.zip.o \
o/$(MODE)/tool/build/false.zip.o \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)

$(TEST_LIBC_SYSTEM_OBJS): test/libc/system/BUILD.mk

.PHONY: o/$(MODE)/test/libc/system
o/$(MODE)/test/libc/system: \
$(TEST_LIBC_SYSTEM_BINS) \
$(TEST_LIBC_SYSTEM_CHECKS)
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit cafdb45

Please sign in to comment.