[tz] [PROPOSED 2/7] Use C23 __has_include if available

Paul Eggert eggert at cs.ucla.edu
Thu Oct 20 19:44:01 UTC 2022


* private.h (HAS_INCLUDE): New macro.
(HAVE_GETTEXT, HAVE_SYS_STAT_H, HAVE_UNISTD_H, HAVE_UTMPX_H)
(HAVE_STDINT_H, HAVE_INTTYPES_H): Use it to simplify
builder configuration.
---
 Makefile  | 13 ++++++++-----
 NEWS      |  2 +-
 private.h | 39 +++++++++++++++++++++++----------------
 3 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/Makefile b/Makefile
index afe7af7..8a2856f 100644
--- a/Makefile
+++ b/Makefile
@@ -210,7 +210,7 @@ LDLIBS=
 #  -DHAVE_DECL_ENVIRON if <unistd.h> declares 'environ'
 #  -DHAVE_DIRECT_H if mkdir needs <direct.h> (MS-Windows)
 #  -DHAVE_GENERIC=0 if _Generic does not work
-#  -DHAVE_GETTEXT if 'gettext' works (e.g., GNU/Linux, FreeBSD, Solaris)
+#  -DHAVE_GETTEXT if 'gettext' works (e.g., GNU/Linux, FreeBSD, Solaris)*
 #  -DHAVE_INCOMPATIBLE_CTIME_R if your system's time.h declares
 #	ctime_r and asctime_r incompatibly with the POSIX standard
 #	(Solaris when _POSIX_PTHREAD_SEMANTICS is not defined).
@@ -223,14 +223,15 @@ LDLIBS=
 #  -DHAVE_POSIX_DECLS=0 if your system's include files do not declare
 #	functions like 'link' or variables like 'tzname' required by POSIX
 #  -DHAVE_SNPRINTF=0 if your system lacks the snprintf function
-#  -DHAVE_STDINT_H if you have a non-C99 compiler with <stdint.h>
+#  -DHAVE_STDINT_H if you have a non-C99 compiler with <stdint.h>*
 #  -DHAVE_STRFTIME_L if <time.h> declares locale_t and strftime_l
 #  -DHAVE_STRDUP=0 if your system lacks the strdup function
 #  -DHAVE_STRTOLL=0 if your system lacks the strtoll function
 #  -DHAVE_SYMLINK=0 if your system lacks the symlink function
-#  -DHAVE_SYS_STAT_H=0 if your compiler lacks a <sys/stat.h>
+#  -DHAVE_SYS_STAT_H=0 if your compiler lacks a <sys/stat.h>*
 #  -DHAVE_TZSET=0 if your system lacks a tzset function
-#  -DHAVE_UNISTD_H=0 if your compiler lacks a <unistd.h>
+#  -DHAVE_UNISTD_H=0 if your compiler lacks a <unistd.h>*
+#  -DHAVE_UTMPX_H=0 if your compiler lacks a <utmpx.h>*
 #  -Dlocale_t=XXX if your system uses XXX instead of locale_t
 #  -DRESERVE_STD_EXT_IDS if your platform reserves standard identifiers
 #	with external linkage, e.g., applications cannot define 'localtime'.
@@ -257,11 +258,13 @@ LDLIBS=
 #	ignore or otherwise mishandle 64-bit data in TZif files;
 #	however, fat TZif files may trigger bugs in newer TZif readers.
 #	Slim TZif files are more efficient, and are the default.
-
 #  -DZIC_MAX_ABBR_LEN_WO_WARN=3
 #	(or some other number) to set the maximum time zone abbreviation length
 #	that zic will accept without a warning (the default is 6)
 #  $(GCC_DEBUG_FLAGS) if you are using recent GCC and want lots of checking
+#
+# * Options marked "*" can be omitted if your compiler is C23 compatible.
+#
 # Select instrumentation via "make GCC_INSTRUMENT='whatever'".
 GCC_INSTRUMENT = \
   -fsanitize=undefined -fsanitize-address-use-after-scope \
diff --git a/NEWS b/NEWS
index 16f454c..2532691 100644
--- a/NEWS
+++ b/NEWS
@@ -38,7 +38,7 @@ Unreleased, experimental changes
     400-year Gregorian cycle.  (Problem reported by Bradley White.)
 
     Take advantage of the following C23 features if available:
-    bool/true/false keywords.
+    bool/true/false keywords and __has_include.
 
 
 Release 2022e - 2022-10-11 11:13:02 -0700
diff --git a/private.h b/private.h
index 058d3c0..6510c52 100644
--- a/private.h
+++ b/private.h
@@ -30,6 +30,14 @@
 /* This string was in the Factory zone through version 2016f.  */
 #define GRANDPARENTED	"Local time zone must be set--see zic manual page"
 
+/* True if "#include INCLUDE" includes something, false otherwise.
+   Yield DEFAULT on pre-C23 platforms that lack __has_include.  */
+#ifdef __has_include
+# define HAS_INCLUDE(include, default) __has_include(include)
+#else
+# define HAS_INCLUDE(include, default) (default)
+#endif
+
 /*
 ** Defaults for preprocessor symbols.
 ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
@@ -55,8 +63,8 @@
 #endif
 
 #ifndef HAVE_GETTEXT
-# define HAVE_GETTEXT 0
-#endif /* !defined HAVE_GETTEXT */
+# define HAVE_GETTEXT HAS_INCLUDE(<libintl.h>, false)
+#endif
 
 #ifndef HAVE_INCOMPATIBLE_CTIME_R
 # define HAVE_INCOMPATIBLE_CTIME_R 0
@@ -87,16 +95,16 @@
 #endif /* !defined HAVE_SYMLINK */
 
 #ifndef HAVE_SYS_STAT_H
-# define HAVE_SYS_STAT_H 1
-#endif /* !defined HAVE_SYS_STAT_H */
+# define HAVE_SYS_STAT_H HAS_INCLUDE(<sys/stat.h>, true)
+#endif
 
 #ifndef HAVE_UNISTD_H
-# define HAVE_UNISTD_H 1
-#endif /* !defined HAVE_UNISTD_H */
+# define HAVE_UNISTD_H HAS_INCLUDE(<unistd.h>, true)
+#endif
 
 #ifndef HAVE_UTMPX_H
-# define HAVE_UTMPX_H 1
-#endif /* !defined HAVE_UTMPX_H */
+# define HAVE_UTMPX_H HAS_INCLUDE(<utmpx.h>, true)
+#endif
 
 #ifndef NETBSD_INSPIRED
 # define NETBSD_INSPIRED 1
@@ -231,18 +239,17 @@
 ** stdint.h, even with pre-C99 compilers.
 */
 #ifndef HAVE_STDINT_H
-# define HAVE_STDINT_H \
-   (199901 <= __STDC_VERSION__ \
-    || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
-    || __CYGWIN__ || INTMAX_MAX)
-#endif /* !defined HAVE_STDINT_H */
-
+# define HAVE_STDINT_H HAS_INCLUDE(<stdint.h>, \
+				   (199901 <= __STDC_VERSION__ \
+				    || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
+				    || __CYGWIN__ || INTMAX_MAX))
+#endif
 #if HAVE_STDINT_H
 # include <stdint.h>
-#endif /* !HAVE_STDINT_H */
+#endif
 
 #ifndef HAVE_INTTYPES_H
-# define HAVE_INTTYPES_H HAVE_STDINT_H
+# define HAVE_INTTYPES_H HAS_INCLUDE(<inttypes.h>, HAVE_STDINT_H)
 #endif
 #if HAVE_INTTYPES_H
 # include <inttypes.h>
-- 
2.37.3




More information about the tz mailing list