Skip to content

Commit

Permalink
build: Move feature test macros to prelude.h
Browse files Browse the repository at this point in the history
This replaces the explicit CPPFLAGS list in flags.mk with just
`-include src/prelude.h`, shortening our compiler command lines and
allowing them to be easily documented.
  • Loading branch information
tavianator committed Aug 28, 2024
1 parent d1febdd commit b3636fa
Show file tree
Hide file tree
Showing 59 changed files with 62 additions and 100 deletions.
14 changes: 1 addition & 13 deletions build/flags.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,7 @@ include build/prelude.mk
include gen/vars.mk

# Internal flags
_CPPFLAGS := \
-Isrc \
-Igen \
-D__EXTENSIONS__ \
-D_ATFILE_SOURCE \
-D_BSD_SOURCE \
-D_DARWIN_C_SOURCE \
-D_DEFAULT_SOURCE \
-D_GNU_SOURCE \
-D_POSIX_PTHREAD_SEMANTICS \
-D_FILE_OFFSET_BITS=64 \
-D_TIME_BITS=64

_CPPFLAGS := -Isrc -Igen -include src/prelude.h
_CFLAGS := -std=c17 -pthread
_LDFLAGS :=
_LDLIBS :=
Expand Down
1 change: 0 additions & 1 deletion src/alloc.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "alloc.h"

#include "bfs.h"
Expand Down
2 changes: 0 additions & 2 deletions src/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_ALLOC_H
#define BFS_ALLOC_H

#include "prelude.h"

#include "bfs.h"

#include <errno.h>
Expand Down
2 changes: 0 additions & 2 deletions src/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_ATOMIC_H
#define BFS_ATOMIC_H

#include "prelude.h"

#include "bfs.h"

#include <stdatomic.h>
Expand Down
1 change: 0 additions & 1 deletion src/bar.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "bar.h"

#include "alloc.h"
Expand Down
2 changes: 0 additions & 2 deletions src/bfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_H
#define BFS_H

#include "prelude.h"

// Standard versions

/** Possible __STDC_VERSION__ values. */
Expand Down
1 change: 0 additions & 1 deletion src/bfstd.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "bfstd.h"

#include "bfs.h"
Expand Down
2 changes: 0 additions & 2 deletions src/bfstd.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_BFSTD_H
#define BFS_BFSTD_H

#include "prelude.h"

#include "bfs.h"

#include <stddef.h>
Expand Down
1 change: 0 additions & 1 deletion src/bftw.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* various helper functions to take fewer parameters.
*/

#include "prelude.h"
#include "bftw.h"

#include "alloc.h"
Expand Down
2 changes: 0 additions & 2 deletions src/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_BIT_H
#define BFS_BIT_H

#include "prelude.h"

#include "bfs.h"

#include <limits.h>
Expand Down
1 change: 0 additions & 1 deletion src/color.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "color.h"

#include "alloc.h"
Expand Down
2 changes: 0 additions & 2 deletions src/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_COLOR_H
#define BFS_COLOR_H

#include "prelude.h"

#include "bfs.h"
#include "dstring.h"

Expand Down
2 changes: 0 additions & 2 deletions src/ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_CTX_H
#define BFS_CTX_H

#include "prelude.h"

#include "alloc.h"
#include "bftw.h"
#include "diag.h"
Expand Down
1 change: 0 additions & 1 deletion src/diag.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "diag.h"

#include "alloc.h"
Expand Down
2 changes: 0 additions & 2 deletions src/diag.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_DIAG_H
#define BFS_DIAG_H

#include "prelude.h"

#include "bfs.h"
#include "bfstd.h"

Expand Down
1 change: 0 additions & 1 deletion src/dir.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "dir.h"

#include "alloc.h"
Expand Down
2 changes: 0 additions & 2 deletions src/dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_DIR_H
#define BFS_DIR_H

#include "prelude.h"

#include "bfs.h"

#include <sys/types.h>
Expand Down
1 change: 0 additions & 1 deletion src/dstring.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "dstring.h"

#include "alloc.h"
Expand Down
2 changes: 0 additions & 2 deletions src/dstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_DSTRING_H
#define BFS_DSTRING_H

#include "prelude.h"

#include "bfs.h"
#include "bfstd.h"

Expand Down
1 change: 0 additions & 1 deletion src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Implementation of all the primary expressions.
*/

#include "prelude.h"
#include "eval.h"

#include "atomic.h"
Expand Down
2 changes: 0 additions & 2 deletions src/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#ifndef BFS_EVAL_H
#define BFS_EVAL_H

#include "prelude.h"

struct bfs_ctx;
struct bfs_expr;

Expand Down
1 change: 0 additions & 1 deletion src/exec.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "exec.h"

#include "alloc.h"
Expand Down
2 changes: 0 additions & 2 deletions src/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_EXPR_H
#define BFS_EXPR_H

#include "prelude.h"

#include "color.h"
#include "eval.h"
#include "stat.h"
Expand Down
1 change: 0 additions & 1 deletion src/fsade.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "fsade.h"

#include "atomic.h"
Expand Down
2 changes: 0 additions & 2 deletions src/fsade.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#ifndef BFS_FSADE_H
#define BFS_FSADE_H

#include "prelude.h"

#include "bfs.h"

#define BFS_CAN_CHECK_ACL (BFS_HAS_ACL_GET_FILE || BFS_HAS_ACL_TRIVIAL)
Expand Down
1 change: 0 additions & 1 deletion src/ioq.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
* [1]: https://arxiv.org/abs/2201.02179
*/

#include "prelude.h"
#include "ioq.h"

#include "alloc.h"
Expand Down
2 changes: 0 additions & 2 deletions src/ioq.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_IOQ_H
#define BFS_IOQ_H

#include "prelude.h"

#include "dir.h"
#include "stat.h"

Expand Down
4 changes: 1 addition & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
* - bftw.[ch] (an extended version of nftw(3))
*
* - Utilities:
* - prelude.h (feature test macros; automatically included)
* - alloc.[ch] (memory allocation)
* - atomic.h (atomic operations)
* - bar.[ch] (a terminal status bar)
* - bit.h (bit manipulation)
* - bfs.h (configuration and fundamental utilities)
* - bfstd.[ch] (standard library wrappers/polyfills)
* - color.[ch] (for pretty terminal colors)
* - prelude.h (configuration and feature/platform detection)
* - diag.[ch] (formats diagnostic messages)
* - dir.[ch] (a directory API facade)
* - dstring.[ch] (a dynamic string library)
Expand All @@ -48,8 +48,6 @@
* - xtime.[ch] (date/time handling utilities)
*/

#include "prelude.h"

#include "bfstd.h"
#include "ctx.h"
#include "diag.h"
Expand Down
1 change: 0 additions & 1 deletion src/mtab.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "mtab.h"

#include "alloc.h"
Expand Down
2 changes: 0 additions & 2 deletions src/mtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef BFS_MTAB_H
#define BFS_MTAB_H

#include "prelude.h"

struct bfs_stat;

/**
Expand Down
1 change: 0 additions & 1 deletion src/opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* effects are reachable at all, skipping the traversal if not.
*/

#include "prelude.h"
#include "opt.h"

#include "bfs.h"
Expand Down
1 change: 0 additions & 1 deletion src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* flags like always-true options, and skipping over paths wherever they appear.
*/

#include "prelude.h"
#include "parse.h"

#include "alloc.h"
Expand Down
58 changes: 56 additions & 2 deletions src/prelude.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,66 @@
// SPDX-License-Identifier: 0BSD

/**
* Configuration and feature/platform detection.
* Praeludium.
*
* This header is automatically included in every translation unit, before any
* other headers, so it can set feature test macros[1][2]. This sets up our own
* mini-dialect of C, which includes
*
* - Standard C17 and POSIX.1 2024 features
* - Portable and platform-specific extensions
* - Convenience macros like `bool`, `alignof`, etc.
* - Common compiler extensions like __has_include()
*
* Further bfs-specific utilities are defined in "bfs.h".
*
* [1]: https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
* [2]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html
*/

#ifndef BFS_PRELUDE_H
#define BFS_PRELUDE_H

// Feature test macros

/**
* Linux and BSD handle _POSIX_C_SOURCE differently: on Linux, it enables POSIX
* interfaces that are not visible by default. On BSD, it also *disables* most
* extensions, giving a strict POSIX environment. Since we want the extensions,
* we don't set _POSIX_C_SOURCE.
*/
// #define _POSIX_C_SOURCE 202405L

/** openat() etc. */
#define _ATFILE_SOURCE 1

/** BSD-derived extensions. */
#define _BSD_SOURCE 1

/** glibc successor to _BSD_SOURCE. */
#define _DEFAULT_SOURCE 1

/** GNU extensions. */
#define _GNU_SOURCE 1

/** Use 64-bit off_t. */
#define _FILE_OFFSET_BITS 64

/** Use 64-bit time_t. */
#define _TIME_BITS 64

/** macOS extensions. */
#if __APPLE__
# define _DARWIN_C_SOURCE 1
#endif

/** Solaris extensions. */
#if __sun
# define __EXTENSIONS__ 1
// https://illumos.org/man/3C/getpwnam#standard-conforming
# define _POSIX_PTHREAD_SEMANTICS 1
#endif

// Get the convenience macros that became standard spellings in C23
#if __STDC_VERSION__ < 202311L

Expand All @@ -20,7 +74,7 @@

/**
* C23 deprecates `noreturn void` in favour of `[[noreturn]] void`, so we expose
* _noreturn instead with the other attributes.
* _noreturn instead with the other attributes in "bfs.h".
*/
// #include <stdnoreturn.h>

Expand Down
1 change: 0 additions & 1 deletion src/printf.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "printf.h"

#include "alloc.h"
Expand Down
1 change: 0 additions & 1 deletion src/pwcache.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include "prelude.h"
#include "pwcache.h"

#include "alloc.h"
Expand Down
Loading

0 comments on commit b3636fa

Please sign in to comment.