From 406495366c4331b4b96cc18ef714e757895eaab9 Mon Sep 17 00:00:00 2001 From: Aaron James Long Date: Thu, 15 Oct 2026 14:21:46 +0100 Subject: [PATCH] feat: define the version macros in seqlock.h seqlock_version.h was generated at configure time, so a copied-in header had no version. seqlock.h now defines the macros, including a long SEQLOCK_VERSION_NUMBER that cannot overflow a 16-bit int, and meson setup fails when they differ from project(version:). seqlock_version.h stays installed as a deprecated header that includes seqlock.h. The version block carries an inline Rule 2.5 deviation, because the library defines the macros for consumers and never uses them. Refs #8 --- AGENTS.md | 2 +- CHANGELOG.md | 1 + config/seqlock_version.h.in | 20 --------------- docs/api.md | 22 ++++++++++++++++ docs/design.md | 2 +- docs/integration.md | 5 ++-- include/seqlock.h | 26 +++++++++++++++++++ include/seqlock_version.h | 19 ++++++++++++++ meson.build | 50 +++++++++++++++++++++---------------- tests/meson.build | 18 ++++++------- tests/test_seqlock.c | 12 +++++++++ 11 files changed, 121 insertions(+), 56 deletions(-) delete mode 100644 config/seqlock_version.h.in create mode 100644 include/seqlock_version.h diff --git a/AGENTS.md b/AGENTS.md index 55b8a2a..583fb59 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -49,7 +49,7 @@ scripts/format.sh --fix # rewrite in place ### 1.2 Notes -- `meson setup` generates the optional `seqlock_version.h` into the build directory; `seqlock.h` does not include it. +- `include/seqlock.h` defines the version macros, and `meson setup` fails when they differ from `project(version:)`, so a release updates both. `include/seqlock_version.h` is a deprecated header that includes `seqlock.h`. - The build produces only a static library. The only Meson option is `build_tests`. - `SEQLOCK_COUNTER_TYPE` and the backend macros must be identical for the library and every caller; nothing detects a mismatch. `seqlock.h` includes `seqlock_conf.h` with quotes, so configuration is by `-D` only, never by a replacement header. - ThreadSanitizer needs a 4 KiB-page kernel with a 48-bit address space on AArch64, and AddressSanitizer does not start on a 39-bit one; use a plain native build there. diff --git a/CHANGELOG.md b/CHANGELOG.md index 674ffca..dc900cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Changed +- `seqlock.h` defines `SEQLOCK_VERSION_MAJOR`, `_MINOR`, `_PATCH`, `SEQLOCK_VERSION`, `SEQLOCK_VERSION_STRING` and a new `SEQLOCK_VERSION_NUMBER` (`long`, `MAJOR * 10000 + MINOR * 100 + PATCH`), so a copied-in header carries its version. `seqlock_version.h` is no longer generated; it is installed as a deprecated header that includes `seqlock.h`. Meson fails to configure when the header and `project(version:)` disagree, and the build needs Meson 0.57 or later. - The build produces and installs only the static library. The shared library, which neither `seqlock_dep` nor `seqlock.pc` linked, is no longer built. - CI installs Meson, Ninja and gcovr with `uv` instead of `pip`. - CI gates on 100% branch coverage as well as line coverage. The reader retry branches are reached deterministically by a single-threaded test, and the no-atomics backend is built and tested alongside the C11 backend. diff --git a/config/seqlock_version.h.in b/config/seqlock_version.h.in deleted file mode 100644 index c633298..0000000 --- a/config/seqlock_version.h.in +++ /dev/null @@ -1,20 +0,0 @@ -/** - * SPDX-License-Identifier: MIT - * - * @file: seqlock_version.h - * - * @brief - * Version information for the library. Generated by Meson. - */ - -#ifndef SEQLOCK_VERSION_H_ -#define SEQLOCK_VERSION_H_ - -#define SEQLOCK_VERSION @VERSION@ -#define SEQLOCK_VERSION_MAJOR @MAJOR@ -#define SEQLOCK_VERSION_MINOR @MINOR@ -#define SEQLOCK_VERSION_PATCH @PATCH@ - -#define SEQLOCK_VERSION_STRING "@MAJOR@.@MINOR@.@PATCH@" - -#endif /* SEQLOCK_VERSION_H_ */ diff --git a/docs/api.md b/docs/api.md index 243d490..7af7e0a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -171,3 +171,25 @@ record_load(const record_cell_t *cell, void *out) ``` `size` must not exceed `RECORD_MAX_BYTES` and must not change while the cell is in use, because the reader and the writer both copy that many bytes. + +## Version + +`seqlock.h` defines the release it belongs to, so a copied-in header still identifies itself: + + +```c +#define SEQLOCK_VERSION_MAJOR 0 +#define SEQLOCK_VERSION_MINOR 1 +#define SEQLOCK_VERSION_PATCH 1 +#define SEQLOCK_VERSION "0.1.1" +``` + +`SEQLOCK_VERSION_NUMBER` is `MAJOR * 10000 + MINOR * 100 + PATCH` as a `long`, so it holds any version where `int` is 16 bits. Compare it in the preprocessor to require a minimum release: + +```c +#if SEQLOCK_VERSION_NUMBER < 101L +#error "seqlock 0.1.1 or later is required" +#endif +``` + +`SEQLOCK_VERSION_STRING` is the same string as `SEQLOCK_VERSION`. `seqlock_version.h` is a deprecated header that includes `seqlock.h`, kept so existing includes compile until the next major release. diff --git a/docs/design.md b/docs/design.md index c41766f..0e57d8b 100644 --- a/docs/design.md +++ b/docs/design.md @@ -101,7 +101,7 @@ Breaking the single-writer rule, or calling `init` while the cell is in use, is ## MISRA C:2023 -`misch` analyses `include/` and `src/` once for each backend: the `__atomic` backend in the base configuration, and the C11 and uniprocessor backends through the `c11` and `no_atomics` profiles in `misra.toml`, because each backend compiles different code. Each analysis has a baseline in `analysis/baseline/` that records its accepted findings, and CI fails on any finding outside it. No deviation is recorded for any finding. +`misch` analyses `include/` and `src/` once for each backend: the `__atomic` backend in the base configuration, and the C11 and uniprocessor backends through the `c11` and `no_atomics` profiles in `misra.toml`, because each backend compiles different code. Each analysis has a baseline in `analysis/baseline/` that records its accepted findings, and CI fails on any finding outside it. The only recorded deviation is an inline one of Rule 2.5 (unused macro) for the version macros in `seqlock.h`, which exist for consumers and are not used by the library. The baselines hold 42, 36 and 35 findings. All three share the same core of Rules 2.5, 8.7, 12.1, 15.5 and 20.10. The rest follow from the backend: Rule 17.3 only for the `__atomic` backend, whose builtins have no declaration; Rule 10.4 only for the C11 backend; and Rules 12.3 and 13.4 only for the uniprocessor backend, whose counter-store macro uses the comma operator and an assignment as an operand. Full tool-driven compliance needs a certified static analyser, which this repository does not vendor. diff --git a/docs/integration.md b/docs/integration.md index 690005e..82f1cf1 100644 --- a/docs/integration.md +++ b/docs/integration.md @@ -25,7 +25,7 @@ Keep both headers in the same directory, because `seqlock.h` includes `seqlock_c #include "seqlock.h" ``` -`seqlock_version.h` exists only in Meson builds. +`seqlock.h` defines the version macros described in [api.md](api.md#version). ## Meson subproject @@ -55,10 +55,9 @@ The install contains `libseqlock.a`, the headers in a `seqlock/` subdirectory, a ```c #include -#include ``` -`seqlock_version.h` defines `SEQLOCK_VERSION_STRING` as the version string, and `SEQLOCK_VERSION_MAJOR`, `SEQLOCK_VERSION_MINOR` and `SEQLOCK_VERSION_PATCH` as integers. +`seqlock_version.h` is installed beside it as a deprecated header that includes `seqlock.h`. ## Configuration diff --git a/include/seqlock.h b/include/seqlock.h index b8a87c5..74fa5e7 100644 --- a/include/seqlock.h +++ b/include/seqlock.h @@ -34,6 +34,32 @@ #include #include +/* ================ VERSION ================================================= */ + +/** + * @name Version + * @brief Release of this header, for diagnostics and @c #if checks. + * + * @details + * @c SEQLOCK_VERSION_NUMBER is @c MAJOR*10000 @c + @c MINOR*100 + * @c + @c PATCH with type @c long, so it cannot overflow where @c int is + * 16 bits. The Meson build fails to configure when these values differ + * from the project version. + * @{ + */ +/* cppcheck-suppress-begin misra-c2012-2.5 ; @deviation Public version macros + * for consumer diagnostics and #if checks; the library does not use them. */ +#define SEQLOCK_VERSION_MAJOR 0 +#define SEQLOCK_VERSION_MINOR 1 +#define SEQLOCK_VERSION_PATCH 1 +#define SEQLOCK_VERSION "0.1.1" +#define SEQLOCK_VERSION_STRING SEQLOCK_VERSION +#define SEQLOCK_VERSION_NUMBER \ + ((SEQLOCK_VERSION_MAJOR * 10000L) + (SEQLOCK_VERSION_MINOR * 100L) \ + + SEQLOCK_VERSION_PATCH) +/* cppcheck-suppress-end misra-c2012-2.5 ; @deviation As above. */ +/** @} */ + /* ================ COMPILE-TIME GUARANTEES ================================= */ /* The protocol relies on unsigned wraparound and parity (even/odd); a signed diff --git a/include/seqlock_version.h b/include/seqlock_version.h new file mode 100644 index 0000000..8574c4b --- /dev/null +++ b/include/seqlock_version.h @@ -0,0 +1,19 @@ +/** + * SPDX-License-Identifier: MIT + * + * @file seqlock_version.h + * + * @brief + * Deprecated: the version macros are defined in seqlock.h. + * + * @details + * This header includes seqlock.h so that existing includes keep compiling. + * It will be removed in the next major release. + */ + +#ifndef SEQLOCK_VERSION_H_ +#define SEQLOCK_VERSION_H_ + +#include "seqlock.h" + +#endif /* SEQLOCK_VERSION_H_ */ diff --git a/meson.build b/meson.build index 8db8a89..99fc6c5 100644 --- a/meson.build +++ b/meson.build @@ -4,6 +4,7 @@ project( 'seqlock', 'c', version: '0.1.1', + meson_version: '>=0.57.0', default_options: [ 'c_std=c11', 'warning_level=3', @@ -13,43 +14,48 @@ project( project_description = 'A small Meson-based sequence lock library in C' -ver = meson.project_version().split('.') -major = ver[0].to_int() -minor = ver[1].to_int() -patch = ver[2].to_int() +# include/seqlock.h defines the version macros, so a copy-in build has them. +# Configuration fails when they differ from the project version. -conf = configuration_data() -conf.set_quoted('VERSION', meson.project_version()) -conf.set('MAJOR', major) -conf.set('MINOR', minor) -conf.set('PATCH', patch) - -generated_version_header = configure_file( - input: 'config' / 'seqlock_version.h.in', - output: 'seqlock_version.h', - configuration: conf, - install: true, - install_dir: get_option('includedir') / 'seqlock', -) +fs = import('fs') +version_parts = meson.project_version().split('.') +version_macros = { + 'SEQLOCK_VERSION_MAJOR': version_parts[0], + 'SEQLOCK_VERSION_MINOR': version_parts[1], + 'SEQLOCK_VERSION_PATCH': version_parts[2], + 'SEQLOCK_VERSION': '"' + meson.project_version() + '"', +} +header_lines = [] +foreach line : fs.read('include' / 'seqlock.h').split('\n') + header_lines += ' '.join(line.split()) +endforeach +foreach name, value : version_macros + if '#define @0@ @1@'.format(name, value) not in header_lines + error('include/seqlock.h must define @0@ as @1@'.format(name, value)) + endif +endforeach public_headers = include_directories('include') -generated_headers = include_directories('.') library_sources = files('src/seqlock.c') seqlock_lib = static_library( 'seqlock', library_sources, - include_directories: [public_headers, generated_headers], + include_directories: public_headers, install: true, ) -install_headers('include/seqlock.h', 'include/seqlock_conf.h', subdir: 'seqlock') +install_headers( + 'include/seqlock.h', + 'include/seqlock_conf.h', + 'include/seqlock_version.h', + subdir: 'seqlock', +) seqlock_dep = declare_dependency( - include_directories: [public_headers, generated_headers], + include_directories: public_headers, link_with: seqlock_lib, - sources: generated_version_header, ) meson.override_dependency('seqlock', seqlock_dep) diff --git a/tests/meson.build b/tests/meson.build index 783096c..6571b76 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -8,7 +8,7 @@ test_basic_exe = executable( 'test_seqlock', files('test_seqlock.c'), dependencies: [seqlock_dep], - include_directories: [public_headers, generated_headers], + include_directories: public_headers, ) test('seqlock basic', test_basic_exe) @@ -17,7 +17,7 @@ test_core_exe = executable( 'test_seqlock_core', files('test_seqlock_core.c'), dependencies: [seqlock_dep], - include_directories: [public_headers, generated_headers], + include_directories: public_headers, ) test('seqlock core', test_core_exe) @@ -26,7 +26,7 @@ test_try_exe = executable( 'test_seqlock_try_load', files('test_seqlock_try_load.c'), dependencies: [seqlock_dep], - include_directories: [public_headers, generated_headers], + include_directories: public_headers, ) test('seqlock try_load', test_try_exe) @@ -35,7 +35,7 @@ test_wrap_exe = executable( 'test_seqlock_wrap', files('test_seqlock_wrap.c'), dependencies: [seqlock_dep], - include_directories: [public_headers, generated_headers], + include_directories: public_headers, ) test('seqlock wrap', test_wrap_exe) @@ -50,7 +50,7 @@ foreach backend : [['c11', 'SEQLOCK_USE_C11_ATOMICS'], ['no_atomics', 'SEQLOCK_U 'test_seqlock_' + backend[0], files('test_seqlock_backend.c') + library_sources, c_args: ['-D@0@=1'.format(backend[1])], - include_directories: [public_headers, generated_headers], + include_directories: public_headers, ), ) endforeach @@ -63,7 +63,7 @@ test_retry_exe = executable( 'test_seqlock_retry', files('test_seqlock_retry.c'), dependencies: [seqlock_dep], - include_directories: [public_headers, generated_headers], + include_directories: public_headers, ) test('seqlock retry', test_retry_exe) @@ -74,7 +74,7 @@ test_retry_relax_exe = executable( '-DSEQLOCK_TEST_RELAX_HOOK=1', '-DSEQLOCK_CPU_RELAX()=do { extern void seqlock_test_relax(void); seqlock_test_relax(); } while (0)', ], - include_directories: [public_headers, generated_headers], + include_directories: public_headers, ) test('seqlock retry relax', test_retry_relax_exe) @@ -85,7 +85,7 @@ test_mt_exe = executable( 'test_seqlock_mt', files('test_seqlock_mt.c'), dependencies: [seqlock_dep, thread_dep], - include_directories: [public_headers, generated_headers], + include_directories: public_headers, ) test('seqlock mt', test_mt_exe, timeout: 120) @@ -94,7 +94,7 @@ test_soak_exe = executable( 'test_seqlock_soak', files('test_seqlock_soak.c'), dependencies: [seqlock_dep, thread_dep], - include_directories: [public_headers, generated_headers], + include_directories: public_headers, ) test('seqlock soak', test_soak_exe, timeout: 180) diff --git a/tests/test_seqlock.c b/tests/test_seqlock.c index f294b43..6b0f18f 100644 --- a/tests/test_seqlock.c +++ b/tests/test_seqlock.c @@ -10,6 +10,7 @@ #include "seqlock.h" #include "seqlock_test.h" +#include "seqlock_version.h" #include #include @@ -151,6 +152,16 @@ TEST_CASE(test_large_payload) } } +/* The deprecated version header still exposes the macros from seqlock.h. */ +TEST_CASE(test_version_macros) +{ + TEST_ASSERT(SEQLOCK_VERSION_NUMBER + == ((SEQLOCK_VERSION_MAJOR * 10000L) + + (SEQLOCK_VERSION_MINOR * 100L) + + SEQLOCK_VERSION_PATCH)); + TEST_ASSERT(SEQLOCK_VERSION_STRING[0] != '\0'); +} + int main(void) { @@ -162,6 +173,7 @@ main(void) run_test(test_even_counter_advances_by_two, "even_counter_advances"); run_test(test_null_validation, "null_validation"); run_test(test_large_payload, "large_payload"); + run_test(test_version_macros, "version_macros"); (void)fprintf(stdout, "\n=== all basic tests passed ===\n\n"); return EXIT_SUCCESS;