From 4890e2754c238c51497a19b67fb2458ef108f558 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 27 Aug 2024 16:23:47 -0400 Subject: prelude: Rely more on __has_include() Rather than a bunch of manual fallback macros, just provide a fallback definition that returns false. --- src/bfstd.c | 6 ++-- src/bit.h | 2 +- src/fsade.c | 14 +++++---- src/fsade.h | 4 ++- src/prelude.h | 97 +++++++++-------------------------------------------------- src/stat.h | 2 +- src/xspawn.c | 2 +- 7 files changed, 31 insertions(+), 96 deletions(-) (limited to 'src') diff --git a/src/bfstd.c b/src/bfstd.c index 9a3562d..a76725d 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -27,13 +27,13 @@ #include #include -#if BFS_USE_SYS_SYSMACROS_H +#if __has_include() # include -#elif BFS_USE_SYS_MKDEV_H +#elif __has_include() # include #endif -#if BFS_USE_UTIL_H +#if __has_include() # include #endif diff --git a/src/bit.h b/src/bit.h index c494130..7546aba 100644 --- a/src/bit.h +++ b/src/bit.h @@ -12,7 +12,7 @@ #include #include -#if BFS_HAS_STDBIT_H +#if __has_include() # include #endif diff --git a/src/fsade.c b/src/fsade.c index 11dcd9b..a5073ab 100644 --- a/src/fsade.c +++ b/src/fsade.c @@ -26,10 +26,12 @@ # include #endif -#if BFS_USE_SYS_EXTATTR_H +#if __has_include() # include -#elif BFS_USE_SYS_XATTR_H +# define BFS_USE_EXTATTR true +#elif __has_include() # include +# define BFS_USE_XATTR true #endif /** @@ -344,7 +346,7 @@ int bfs_check_capabilities(const struct BFTW *ftwbuf) { #if BFS_CAN_CHECK_XATTRS -#if BFS_USE_SYS_EXTATTR_H +#if BFS_USE_EXTATTR /** Wrapper for extattr_list_{file,link}. */ static ssize_t bfs_extattr_list(const char *path, enum bfs_type type, int namespace) { @@ -390,13 +392,13 @@ static ssize_t bfs_extattr_get(const char *path, enum bfs_type type, int namespa #endif } -#endif // BFS_USE_SYS_EXTATTR_H +#endif // BFS_USE_EXTATTR int bfs_check_xattrs(const struct BFTW *ftwbuf) { const char *path = fake_at(ftwbuf); ssize_t len; -#if BFS_USE_SYS_EXTATTR_H +#if BFS_USE_EXTATTR len = bfs_extattr_list(path, ftwbuf->type, EXTATTR_NAMESPACE_SYSTEM); if (len <= 0) { len = bfs_extattr_list(path, ftwbuf->type, EXTATTR_NAMESPACE_USER); @@ -432,7 +434,7 @@ int bfs_check_xattr_named(const struct BFTW *ftwbuf, const char *name) { const char *path = fake_at(ftwbuf); ssize_t len; -#if BFS_USE_SYS_EXTATTR_H +#if BFS_USE_EXTATTR len = bfs_extattr_get(path, ftwbuf->type, EXTATTR_NAMESPACE_SYSTEM, name); if (len < 0) { len = bfs_extattr_get(path, ftwbuf->type, EXTATTR_NAMESPACE_USER, name); diff --git a/src/fsade.h b/src/fsade.h index 4465017..4a9c6fa 100644 --- a/src/fsade.h +++ b/src/fsade.h @@ -17,7 +17,9 @@ #define BFS_CAN_CHECK_CONTEXT BFS_WITH_LIBSELINUX -#define BFS_CAN_CHECK_XATTRS (BFS_USE_SYS_EXTATTR_H || BFS_USE_SYS_XATTR_H) +#if __has_include() || __has_include() +# define BFS_CAN_CHECK_XATTRS true +#endif struct BFTW; diff --git a/src/prelude.h b/src/prelude.h index 48f377c..7df178c 100644 --- a/src/prelude.h +++ b/src/prelude.h @@ -60,100 +60,31 @@ extern const char bfs_cflags[]; extern const char bfs_ldflags[]; extern const char bfs_ldlibs[]; -// Check for system headers +// Feature detection -#ifdef __has_include - -#if __has_include() -# define BFS_HAS_MNTENT_H true -#endif -#if __has_include() -# define BFS_HAS_PATHS_H true -#endif -#if __has_include() -# define BFS_HAS_STDBIT_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_EXTATTR_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_MKDEV_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_PARAM_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_SYSMACROS_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_XATTR_H true -#endif -#if __has_include() -# define BFS_HAS_THREADS_H true -#endif -#if __has_include() -# define BFS_HAS_UTIL_H true -#endif - -#else // !__has_include - -#define BFS_HAS_MNTENT_H __GLIBC__ -#define BFS_HAS_PATHS_H true -#define BFS_HAS_STDBIT_H (__STDC_VERSION__ >= C23) -#define BFS_HAS_SYS_EXTATTR_H __FreeBSD__ -#define BFS_HAS_SYS_MKDEV_H false -#define BFS_HAS_SYS_PARAM_H true -#define BFS_HAS_SYS_SYSMACROS_H __GLIBC__ -#define BFS_HAS_SYS_XATTR_H __linux__ -#define BFS_HAS_THREADS_H (!__STDC_NO_THREADS__) -#define BFS_HAS_UTIL_H __NetBSD__ - -#endif // !__has_include - -#ifndef BFS_USE_MNTENT_H -# define BFS_USE_MNTENT_H BFS_HAS_MNTENT_H -#endif -#ifndef BFS_USE_PATHS_H -# define BFS_USE_PATHS_H BFS_HAS_PATHS_H -#endif -#ifndef BFS_USE_SYS_EXTATTR_H -# define BFS_USE_SYS_EXTATTR_H BFS_HAS_SYS_EXTATTR_H -#endif -#ifndef BFS_USE_SYS_MKDEV_H -# define BFS_USE_SYS_MKDEV_H BFS_HAS_SYS_MKDEV_H -#endif -#ifndef BFS_USE_SYS_PARAM_H -# define BFS_USE_SYS_PARAM_H BFS_HAS_SYS_PARAM_H -#endif -#ifndef BFS_USE_SYS_SYSMACROS_H -# define BFS_USE_SYS_SYSMACROS_H BFS_HAS_SYS_SYSMACROS_H -#endif -#ifndef BFS_USE_SYS_XATTR_H -# define BFS_USE_SYS_XATTR_H BFS_HAS_SYS_XATTR_H -#endif -#ifndef BFS_USE_THREADS_H -# define BFS_USE_THREADS_H BFS_HAS_THREADS_H -#endif -#ifndef BFS_USE_UTIL_H -# define BFS_USE_UTIL_H BFS_HAS_UTIL_H +// https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute +#ifndef __has_attribute +# define __has_attribute(attr) false #endif -// Stub out feature detection on old/incompatible compilers - -#ifndef __has_feature -# define __has_feature(feat) false +// https://clang.llvm.org/docs/LanguageExtensions.html#has-builtin +#ifndef __has_builtin +# define __has_builtin(builtin) false #endif +// https://en.cppreference.com/w/c/language/attributes#Attribute_testing #ifndef __has_c_attribute # define __has_c_attribute(attr) false #endif -#ifndef __has_attribute -# define __has_attribute(attr) false +// https://clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension +#ifndef __has_feature +# define __has_feature(feat) false #endif -#ifndef __has_builtin -# define __has_builtin(builtin) false +// https://en.cppreference.com/w/c/preprocessor/include +#ifndef __has_include +# define __has_include(header) false #endif // Sanitizer macros (GCC defines these but Clang does not) diff --git a/src/stat.h b/src/stat.h index 8d7144d..6a8c3f5 100644 --- a/src/stat.h +++ b/src/stat.h @@ -25,7 +25,7 @@ # define BFS_USE_STATX (BFS_HAS_STATX || BFS_HAS_STATX_SYSCALL) #endif -#if BFS_USE_SYS_PARAM_H +#if __has_include() # include #endif diff --git a/src/xspawn.c b/src/xspawn.c index 5e0f989..2385a6e 100644 --- a/src/xspawn.c +++ b/src/xspawn.c @@ -16,7 +16,7 @@ #include #include -#if BFS_USE_PATHS_H +#if __has_include() # include #endif -- cgit v1.2.3