From ad775a75b8a89f17c4d011feaffbf04e7d117209 Mon Sep 17 00:00:00 2001
From: Justine Tunney <jtunney@gmail.com>
Date: Sat, 13 Aug 2022 16:02:01 -0700
Subject: [PATCH] Restore strict header checking

---
 Makefile                                    | 11 +++++++++++
 build/rules.mk                              | 18 ------------------
 libc/isystem/tgmath.h                       |  7 +++----
 libc/isystem/winternl.h                     |  5 +----
 libc/sock/sock.h                            |  2 --
 libc/sock/struct/ifreq.h                    |  3 +++
 libc/sock/struct/msghdr.internal.h          |  1 +
 third_party/libcxx/__bsd_locale_fallbacks.h |  3 +++
 third_party/libcxx/__config                 |  1 +
 third_party/libcxx/atomic                   | 10 ----------
 third_party/libcxx/clocale                  |  1 +
 third_party/libcxx/cwchar                   |  7 +++++++
 third_party/libcxx/thread                   |  4 +---
 third_party/mbedtls/mbedtls.mk              |  2 ++
 third_party/python/Modules/socketmodule.h   |  1 +
 third_party/quickjs/quickjs.mk              |  8 ++++++++
 16 files changed, 43 insertions(+), 41 deletions(-)

diff --git a/Makefile b/Makefile
index 4cfa46c9b..a4cdd2b80 100644
--- a/Makefile
+++ b/Makefile
@@ -377,6 +377,17 @@ $(SRCS):					\
 	libc/integral/lp64arg.inc		\
 	libc/integral/lp64.inc
 
+o/%.h.ok: %.h $(HDRS) $(INCS)
+	@$(COMPILE) -ACHECK.h $(COMPILE.c) -xc -g0 -o $@ $<
+o/$(MODE)/%.h.ok: %.h $(HDRS) $(INCS)
+	@$(COMPILE) -ACHECK.h $(COMPILE.c) -xc -g0 -o $@ $<
+o/$(MODE)/%.hh.ok: %.hh $(HDRS) $(INCS)
+	@$(COMPILE) -ACHECK.h $(COMPILE.cxx) -xc++ -g0 -o $@ $<
+o/%.okk: % $(HDRS) $(INCS)
+	@$(COMPILE) -ACHECK.h $(COMPILE.cxx) -xc++ -g0 -o $@ $<
+o/$(MODE)/%.okk: % $(HDRS) $(INCS)
+	@$(COMPILE) -ACHECK.h $(COMPILE.cxx) -xc++ -g0 -o $@ $<
+
 # UNSPECIFIED PREREQUISITES TUTORIAL
 #
 # A build rule must exist for all files that make needs to consider in
diff --git a/build/rules.mk b/build/rules.mk
index 309dedd6d..0d0a50265 100644
--- a/build/rules.mk
+++ b/build/rules.mk
@@ -99,21 +99,3 @@ o/$(MODE)/%.lua.runs: %.lua o/$(MODE)/tool/net/redbean.com
 o/$(MODE)/%: o/$(MODE)/%.com o/$(MODE)/tool/build/cp.com o/$(MODE)/tool/build/assimilate.com
 	@$(COMPILE) -wACP -T$@ o/$(MODE)/tool/build/cp.com $< $@
 	@$(COMPILE) -wAASSIMILATE -T$@ o/$(MODE)/tool/build/assimilate.com $@
-
-# TODO(jart): find a way to generate dependencies
-#             or alternatively disable sandboxing
-o/%.h.ok: %.h
-	@$(COMPILE) -wACHECK.h -T$@ build/bootstrap/touch.com $@
-#	@$(COMPILE) -ACHECK.h $(COMPILE.c) -xc -g0 -o $@ $<
-o/$(MODE)/%.h.ok: %.h
-	@$(COMPILE) -wACHECK.h -T$@ build/bootstrap/touch.com $@
-#	@$(COMPILE) -ACHECK.h $(COMPILE.c) -xc -g0 -o $@ $<
-o/$(MODE)/%.hh.ok: %.hh
-	@$(COMPILE) -wACHECK.h -T$@ build/bootstrap/touch.com $@
-#	@$(COMPILE) -ACHECK.h $(COMPILE.cxx) -xc++ -g0 -o $@ $<
-o/%.okk: %
-	@$(COMPILE) -wACHECK.h -T$@ build/bootstrap/touch.com $@
-#	@$(COMPILE) -ACHECK.h $(COMPILE.cxx) -xc++ -g0 -o $@ $<
-o/$(MODE)/%.okk: %
-	@$(COMPILE) -wACHECK.h -T$@ build/bootstrap/touch.com $@
-#	@$(COMPILE) -ACHECK.h $(COMPILE.cxx) -xc++ -g0 -o $@ $<
diff --git a/libc/isystem/tgmath.h b/libc/isystem/tgmath.h
index 041f6b0fa..1150fe8e3 100644
--- a/libc/isystem/tgmath.h
+++ b/libc/isystem/tgmath.h
@@ -1,9 +1,7 @@
 #ifndef LIBC_ISYSTEM_TGMATH_H_
 #define LIBC_ISYSTEM_TGMATH_H_
-
 #include "libc/complex.h"
 #include "libc/math.h"
-
 #if !(__ASSEMBLER__ + __LINKER__ + 0)
 #if __STDC_VERSION__ + 0 >= 201112
 COSMOPOLITAN_C_START_
@@ -306,12 +304,14 @@ COSMOPOLITAN_C_START_
            : conj, complex long double \
            : conjl)(x)
 
+#undef creal
 #define creal(x)                        \
   _Generic((x), complex float           \
            : crealf, complex default    \
            : creal, complex long double \
            : creall)(x)
 
+#undef cimag
 #define cimag(x)                        \
   _Generic((x), complex float           \
            : cimagf, complex default    \
@@ -328,5 +328,4 @@ COSMOPOLITAN_C_END_
 
 #endif /* C11 */
 #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
-
-#endif
+#endif /* LIBC_ISYSTEM_TGMATH_H_ */
diff --git a/libc/isystem/winternl.h b/libc/isystem/winternl.h
index f368314c3..d5d1b691b 100644
--- a/libc/isystem/winternl.h
+++ b/libc/isystem/winternl.h
@@ -1,10 +1,7 @@
 #ifndef COSMOPOLITAN_LIBC_ISYSTEM_WINTERNL_H_
 #define COSMOPOLITAN_LIBC_ISYSTEM_WINTERNL_H_
-#include <Windows.h>
-
-#include "libc/nt/struct/ldr.h"
 #include "libc/nt/ntdll.h"
+#include "libc/nt/struct/ldr.h"
 #include "libc/nt/struct/peb.h"
 #include "libc/nt/struct/teb.h"
-
 #endif /* COSMOPOLITAN_LIBC_ISYSTEM_WINTERNL_H_ */
diff --git a/libc/sock/sock.h b/libc/sock/sock.h
index 800851433..f07ad3af9 100644
--- a/libc/sock/sock.h
+++ b/libc/sock/sock.h
@@ -15,8 +15,6 @@ COSMOPOLITAN_C_START_
 #define ntohl(u32) bswap_32(u32)
 
 #define IFHWADDRLEN 6
-#define IF_NAMESIZE 16
-#define IFNAMSIZ    IF_NAMESIZE
 
 const char *inet_ntop(int, const void *, char *, uint32_t);
 int inet_pton(int, const char *, void *);
diff --git a/libc/sock/struct/ifreq.h b/libc/sock/struct/ifreq.h
index a67ed063a..3a3bd7209 100644
--- a/libc/sock/struct/ifreq.h
+++ b/libc/sock/struct/ifreq.h
@@ -4,6 +4,9 @@
 #if !(__ASSEMBLER__ + __LINKER__ + 0)
 COSMOPOLITAN_C_START_
 
+#define IF_NAMESIZE 16
+#define IFNAMSIZ    IF_NAMESIZE
+
 struct ifreq {
   union {
     char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0".  */
diff --git a/libc/sock/struct/msghdr.internal.h b/libc/sock/struct/msghdr.internal.h
index 66d2cff92..a2d25063b 100644
--- a/libc/sock/struct/msghdr.internal.h
+++ b/libc/sock/struct/msghdr.internal.h
@@ -1,5 +1,6 @@
 #ifndef COSMOPOLITAN_LIBC_SOCK_STRUCT_MSGHDR_INTERNAL_H_
 #define COSMOPOLITAN_LIBC_SOCK_STRUCT_MSGHDR_INTERNAL_H_
+#include "libc/sock/struct/msghdr.h"
 #if !(__ASSEMBLER__ + __LINKER__ + 0)
 COSMOPOLITAN_C_START_
 
diff --git a/third_party/libcxx/__bsd_locale_fallbacks.h b/third_party/libcxx/__bsd_locale_fallbacks.h
index 019fb3f97..469981afd 100644
--- a/third_party/libcxx/__bsd_locale_fallbacks.h
+++ b/third_party/libcxx/__bsd_locale_fallbacks.h
@@ -16,7 +16,10 @@
 #include "third_party/libcxx/stdlib.h"
 #include "libc/mem/fmt.h"
 #include "libc/str/unicode.h"
+#include "libc/fmt/fmt.h"
+#include "libc/str/locale.h"
 #include "third_party/libcxx/memory"
+#include "third_party/libcxx/__locale"
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #pragma GCC system_header
diff --git a/third_party/libcxx/__config b/third_party/libcxx/__config
index 63f470912..1f839256e 100644
--- a/third_party/libcxx/__config
+++ b/third_party/libcxx/__config
@@ -1152,6 +1152,7 @@ extern "C" _LIBCPP_FUNC_VIS  void __sanitizer_annotate_contiguous_container(
      !defined(_LIBCPP_HAS_EXTERNAL_ATOMIC_IMP)) \
      || defined(_LIBCPP_HAS_NO_THREADS)
 #  define _LIBCPP_HAS_NO_ATOMIC_HEADER
+#  define _LIBCPP_ATOMIC_FLAG_TYPE bool
 #else
 #  ifndef _LIBCPP_ATOMIC_FLAG_TYPE
 #    define _LIBCPP_ATOMIC_FLAG_TYPE bool
diff --git a/third_party/libcxx/atomic b/third_party/libcxx/atomic
index 9608e0208..5c07e0d2b 100644
--- a/third_party/libcxx/atomic
+++ b/third_party/libcxx/atomic
@@ -21,16 +21,6 @@
 #pragma GCC system_header
 #endif
 
-#ifdef _LIBCPP_HAS_NO_THREADS
-# error <atomic> is not supported on this single threaded system
-#endif
-#ifdef _LIBCPP_HAS_NO_ATOMIC_HEADER
-# error <atomic> is not implemented
-#endif
-#ifdef kill_dependency
-# error C++ standard library is incompatible with <stdatomic.h>
-#endif
-
 #define _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) \
   _LIBCPP_DIAGNOSE_WARNING(__m == memory_order_consume || \
                            __m == memory_order_acquire || \
diff --git a/third_party/libcxx/clocale b/third_party/libcxx/clocale
index 5c6653892..6fc1a1785 100644
--- a/third_party/libcxx/clocale
+++ b/third_party/libcxx/clocale
@@ -36,6 +36,7 @@ lconv* localeconv();
 */
 
 #include "third_party/libcxx/__config"
+#include "libc/str/unicode.h"
 #include "libc/str/locale.h"
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
diff --git a/third_party/libcxx/cwchar b/third_party/libcxx/cwchar
index f88570b19..5931d53cc 100644
--- a/third_party/libcxx/cwchar
+++ b/third_party/libcxx/cwchar
@@ -13,6 +13,13 @@
 #include "third_party/libcxx/__config"
 #include "third_party/libcxx/cwctype"
 #include "third_party/libcxx/wchar.h"
+#include "libc/stdio/stdio.h"
+#include "libc/str/str.h"
+#include "libc/time/struct/tm.h"
+#include "libc/stdio/stdio.h"
+#include "libc/stdio/stdio.h"
+#include "libc/fmt/conv.h"
+#include "third_party/gdtoa/gdtoa.h"
 #include "libc/time/struct/tm.h"
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
diff --git a/third_party/libcxx/thread b/third_party/libcxx/thread
index f9e37e94b..9e2c4fbb4 100644
--- a/third_party/libcxx/thread
+++ b/third_party/libcxx/thread
@@ -34,9 +34,7 @@
 _LIBCPP_PUSH_MACROS
 #include "third_party/libcxx/__undef_macros"
 
-#ifdef _LIBCPP_HAS_NO_THREADS
-#error <thread> is not supported on this single threaded system
-#else // !_LIBCPP_HAS_NO_THREADS
+#ifndef _LIBCPP_HAS_NO_THREADS
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
diff --git a/third_party/mbedtls/mbedtls.mk b/third_party/mbedtls/mbedtls.mk
index 9cb296a37..66b2c4265 100644
--- a/third_party/mbedtls/mbedtls.mk
+++ b/third_party/mbedtls/mbedtls.mk
@@ -7,6 +7,7 @@ THIRD_PARTY_MBEDTLS_ARTIFACTS += THIRD_PARTY_MBEDTLS_A
 THIRD_PARTY_MBEDTLS = $(THIRD_PARTY_MBEDTLS_A_DEPS) $(THIRD_PARTY_MBEDTLS_A)
 THIRD_PARTY_MBEDTLS_A = o/$(MODE)/third_party/mbedtls/mbedtls.a
 THIRD_PARTY_MBEDTLS_A_FILES := $(wildcard third_party/mbedtls/*)
+THIRD_PARTY_MBEDTLS_A_INCS = $(filter %.inc,$(THIRD_PARTY_MBEDTLS_A_FILES))
 THIRD_PARTY_MBEDTLS_A_HDRS = $(filter %.h,$(THIRD_PARTY_MBEDTLS_A_FILES))
 THIRD_PARTY_MBEDTLS_A_SRCS = $(filter %.c,$(THIRD_PARTY_MBEDTLS_A_FILES))
 THIRD_PARTY_MBEDTLS_A_OBJS = $(THIRD_PARTY_MBEDTLS_A_SRCS:%.c=o/$(MODE)/%.o)
@@ -78,6 +79,7 @@ o/$(MODE)/third_party/mbedtls/zeroize.o: private			\
 THIRD_PARTY_MBEDTLS_LIBS = $(foreach x,$(THIRD_PARTY_MBEDTLS_ARTIFACTS),$($(x)))
 THIRD_PARTY_MBEDTLS_SRCS = $(foreach x,$(THIRD_PARTY_MBEDTLS_ARTIFACTS),$($(x)_SRCS))
 THIRD_PARTY_MBEDTLS_HDRS = $(foreach x,$(THIRD_PARTY_MBEDTLS_ARTIFACTS),$($(x)_HDRS))
+THIRD_PARTY_MBEDTLS_INCS = $(foreach x,$(THIRD_PARTY_MBEDTLS_ARTIFACTS),$($(x)_INCS))
 THIRD_PARTY_MBEDTLS_CHECKS = $(foreach x,$(THIRD_PARTY_MBEDTLS_ARTIFACTS),$($(x)_CHECKS))
 THIRD_PARTY_MBEDTLS_OBJS = $(foreach x,$(THIRD_PARTY_MBEDTLS_ARTIFACTS),$($(x)_OBJS))
 $(THIRD_PARTY_MBEDTLS_A_OBJS): third_party/mbedtls/mbedtls.mk
diff --git a/third_party/python/Modules/socketmodule.h b/third_party/python/Modules/socketmodule.h
index 735d75000..429411a0a 100644
--- a/third_party/python/Modules/socketmodule.h
+++ b/third_party/python/Modules/socketmodule.h
@@ -1,6 +1,7 @@
 #ifndef Py__SOCKET_H
 #define Py__SOCKET_H
 #include "libc/sock/sock.h"
+#include "libc/sock/struct/sockaddr.h"
 #include "third_party/python/Include/object.h"
 #include "third_party/python/Include/pytime.h"
 COSMOPOLITAN_C_START_
diff --git a/third_party/quickjs/quickjs.mk b/third_party/quickjs/quickjs.mk
index bf0987a12..265f46e04 100644
--- a/third_party/quickjs/quickjs.mk
+++ b/third_party/quickjs/quickjs.mk
@@ -8,6 +8,7 @@ THIRD_PARTY_QUICKJS_BINS = $(THIRD_PARTY_QUICKJS_COMS) $(THIRD_PARTY_QUICKJS_COM
 THIRD_PARTY_QUICKJS = $(THIRD_PARTY_QUICKJS_A_DEPS) $(THIRD_PARTY_QUICKJS_A)
 THIRD_PARTY_QUICKJS_A = o/$(MODE)/third_party/quickjs/quickjs.a
 THIRD_PARTY_QUICKJS_HDRS = $(foreach x,$(THIRD_PARTY_QUICKJS_ARTIFACTS),$($(x)_HDRS))
+THIRD_PARTY_QUICKJS_INCS = $(foreach x,$(THIRD_PARTY_QUICKJS_ARTIFACTS),$($(x)_INCS))
 
 THIRD_PARTY_QUICKJS_A_SRCS =							\
 	third_party/quickjs/array.c						\
@@ -66,6 +67,13 @@ THIRD_PARTY_QUICKJS_A_HDRS =							\
 	third_party/quickjs/quickjs-libc.h					\
 	third_party/quickjs/quickjs.h
 
+THIRD_PARTY_QUICKJS_A_INCS =							\
+	third_party/quickjs/libregexp-opcode.inc				\
+	third_party/quickjs/libunicode-table.inc				\
+	third_party/quickjs/quickjs-atom.inc					\
+	third_party/quickjs/quickjs-opcode.inc					\
+	third_party/quickjs/unicode_gen_def.inc
+
 THIRD_PARTY_QUICKJS_A_OBJS =							\
 	$(THIRD_PARTY_QUICKJS_A_SRCS:%.c=o/$(MODE)/%.o)