From 39b2eaebb6639fcb9c291099a0681bf9ea9aac3b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 7 Feb 2023 21:15:17 -0500 Subject: Check for POSIX features with the build system --- src/serd_config.h | 85 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 31 deletions(-) (limited to 'src/serd_config.h') diff --git a/src/serd_config.h b/src/serd_config.h index df73522b..e54b8688 100644 --- a/src/serd_config.h +++ b/src/serd_config.h @@ -1,15 +1,35 @@ -// Copyright 2021-2022 David Robillard +// Copyright 2021-2023 David Robillard // SPDX-License-Identifier: ISC /* - Configuration header that defines reasonable defaults at compile time. - - This allows compile-time configuration from the command line (typically via - the build system) while still allowing the source to be built without any - configuration. The build system can define SERD_NO_DEFAULT_CONFIG to disable - defaults, in which case it must define things like HAVE_FEATURE to enable - features. The design here ensures that compiler warnings or - include-what-you-use will catch any mistakes. + Configuration header that defines reasonable defaults at compile-time. + + This allows configuration from the command-line (usually by the build system) + while still allowing the code to compile "as-is" with reasonable default + features on supported platforms. + + This system is designed so that, ideally, no command-line or build-system + configuration is needed, but automatic feature detection can be disabled or + overridden for maximum control. It should never be necessary to edit the + source code to achieve a given configuration. + + Usage: + + - By default, features are enabled if they can be detected or assumed to be + available from the build environment, unless `SERD_NO_DEFAULT_CONFIG` is + defined, which disables everything by default. + + - If a symbol like `HAVE_SOMETHING` is defined to non-zero, then the + "something" feature is assumed to be available. + + Code rules: + + - To check for a feature, this header must be included, and the symbol + `USE_SOMETHING` used as a boolean in an `#if` expression. + + - None of the other configuration symbols described here may be used + directly. In particular, this header should be the only place in the + source code that touches `HAVE` symbols. */ #ifndef SERD_CONFIG_H @@ -21,62 +41,65 @@ #if !defined(SERD_NO_DEFAULT_CONFIG) // We need unistd.h to check _POSIX_VERSION -# ifndef SERD_NO_POSIX -# ifdef __has_include -# if __has_include() -# include -# endif -# elif defined(__unix__) +# ifdef __has_include +# if __has_include() # include # endif +# elif defined(__unix__) +# include +# endif + +// Define SERD_POSIX_VERSION unconditionally for convenience +# if defined(_POSIX_VERSION) +# define SERD_POSIX_VERSION _POSIX_VERSION +# else +# define SERD_POSIX_VERSION 0 # endif // POSIX.1-2001: fileno() # ifndef HAVE_FILENO -# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L -# define HAVE_FILENO +# if SERD_POSIX_VERSION >= 200112L || defined(_WIN32) +# define HAVE_FILENO 1 # endif # endif // POSIX.1-2001: posix_fadvise() # ifndef HAVE_POSIX_FADVISE -# ifndef __APPLE__ -# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L -# define HAVE_POSIX_FADVISE -# endif +# if SERD__POSIX_VERSION >= 200112L && !defined(__APPLE__) +# define HAVE_POSIX_FADVISE 1 # endif # endif // POSIX.1-2001: posix_memalign() # ifndef HAVE_POSIX_MEMALIGN -# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L -# define HAVE_POSIX_MEMALIGN +# if SERD_POSIX_VERSION >= 200112L +# define HAVE_POSIX_MEMALIGN 1 # endif # endif #endif // !defined(SERD_NO_DEFAULT_CONFIG) /* - Make corresponding USE_FEATURE defines based on the HAVE_FEATURE defines from - above or the command line. The code checks for these using #if (not #ifdef), - so there will be an undefined warning if it checks for an unknown feature, - and this header is always required by any code that checks for features, even - if the build system defines them all. + Unconditionally define symbols like USE_SOMETHING based on HAVE_SOMETHING, if + it's defined. The code checks these using #if (not #ifdef), so there will be + a warning if it checks for an unknown feature or doesn't include this header. + This header should be the only file in the source code that touches symbols + like HAVE_SOMETHING. */ -#ifdef HAVE_FILENO +#if defined(HAVE_FILENO) && HAVE_FILENO # define USE_FILENO 1 #else # define USE_FILENO 0 #endif -#ifdef HAVE_POSIX_FADVISE +#if defined(HAVE_POSIX_FADVISE) && HAVE_POSIX_FADVISE # define USE_POSIX_FADVISE 1 #else # define USE_POSIX_FADVISE 0 #endif -#ifdef HAVE_POSIX_MEMALIGN +#if defined(HAVE_POSIX_MEMALIGN) && HAVE_POSIX_MEMALIGN # define USE_POSIX_MEMALIGN 1 #else # define USE_POSIX_MEMALIGN 0 -- cgit v1.2.1