feat(build-time-versioning): version macro/helper functions with unknown fallback

Adds xce_version_field()/xce_compose_version() (utils.h/utils.c) to
compose a version string carrying commit id + build time, rendering
"unknown" for either field when its underlying meson-supplied define is
empty -- ready for controller/scheduler/worker to consume.

MK_VERSION/STR (the pre-existing, compile-time-only macro pair) can't
express this empty-to-"unknown" substitution at the preprocessor level,
so this is a runtime mechanism instead. Round 1 review REJECTED an
earlier version of this task for silently orphaning MK_VERSION/STR
(the task explicitly asked to "extend" them) and for leaving an
undocumented architectural trap: argp_program_version is a file-scope
const char* initialized at compile time, which cannot call a runtime
function -- consuming code must instead compute into a static buffer
inside main() and assign the pointer before argp_parse() runs. Round 2
adds an explicit SUPERSEDED doc comment on MK_VERSION/STR plus NOTE FOR
CALLERS guidance covering both points, and was APPROVED.

Also expands task 3.2's boundary to include controller/testsuite/test.c
(a third MK_VERSION/STR call site discovered during review) so no
caller of the superseded macro survives implementation.

5 new unit tests in test_utils.c (RED->GREEN, TDD).

Task 3.1/13 of build-time-versioning.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Rongsong Shen 2026-08-20 10:16:17 +00:00
parent d08fda76a5
commit 48a86cdab6
No known key found for this signature in database
GPG key ID: 5BCDAAE0123E9FE2
4 changed files with 159 additions and 7 deletions

View file

@ -25,19 +25,20 @@
- _Boundary: Nix packaging_
- [ ] 3. Core: C daemon version display
- [ ] 3.1 Extend the shared version macro with build time and unknown fallback
- Extend `MK_VERSION`/`STR` in `src/include/utils.h` so the composed string carries both commit id and build time
- Both fields render as `unknown` when their underlying define is empty, never a blank/garbled string
- Observable: a build with both meson options unset produces a `--version` string containing `unknown` for both fields, not empty gaps
- [x] 3.1 Extend the shared version macro with build time and unknown fallback
- ~~Extend `MK_VERSION`/`STR`~~ — `MK_VERSION`/`STR` cannot express the empty-string-to-`unknown` substitution at the preprocessor level, so implemented as new runtime functions `xce_version_field()`/`xce_compose_version()` (`utils.h`/`utils.c`) instead; `MK_VERSION`/`STR` marked SUPERSEDED with an explicit doc comment and handoff notes for 3.2 (round 1 REJECTED for silently orphaning `MK_VERSION` + undocumented `argp_program_version` file-scope-const-initializer trap; round 2 APPROVED after adding the SUPERSEDED comment + NOTE FOR CALLERS guidance)
- Both fields render as `unknown` when their underlying define is empty (verified via `""`, not just `NULL`), never a blank/garbled string
- Observable: a build with both meson options unset produces a `--version` string containing `unknown` for both fields, not empty gaps -- 5 new unit tests in `test_utils.c` confirm this
- _Requirements: 1.4, 2.2_
- _Boundary: utils.h_
- [ ] 3.2 (P) Controller and scheduler: version in --version and startup log
- Update each daemon's `argp_program_version` construction to use the extended macro from 3.1
- Update each daemon's `argp_program_version` construction to use `xce_compose_version()`/`xce_version_field()` from 3.1 (NOT the now-superseded `MK_VERSION`/`STR`) -- since `argp_program_version` is a file-scope `const char*` global (mutable pointer, not `const char* const`) currently initialized at compile time, this requires computing the composed string into a static buffer inside `main()` and assigning the pointer there, before `argp_parse()` runs, rather than a one-line macro swap
- Add the version string to each daemon's existing first startup INFO log line (controller's `"My name is %s"` line, scheduler's `"HA cluster config..."` line) rather than a second, independently-formatted line
- Observable: `xce_controller --version` and `xce_scheduler --version` each print commit id + build time and exit without starting the daemon; each daemon's startup log's first lines include the identical two values
- `src/controller/testsuite/test.c:662` is a THIRD `MK_VERSION`/`STR` call site (the `xce_test` REPL driver's own version output) discovered during 3.1's review, outside this sub-task's original scope -- migrate it too in this same task (same file family, same daemon, same minimal fix shape), so no caller of the superseded macro remains after this task lands
- Observable: `xce_controller --version` and `xce_scheduler --version` each print commit id + build time and exit without starting the daemon; each daemon's startup log's first lines include the identical two values; `xce_test --version` (or equivalent) also reflects the new composed string, not the old commit-id-only one
- _Requirements: 1.1, 1.2, 2.1, 2.2, 3.1, 3.2, 4.3_
- _Boundary: controller/main.c, scheduler/main.c_
- _Boundary: controller/main.c, scheduler/main.c, controller/testsuite/test.c_
- _Depends: 3.1_
- [ ] 3.3 (P) Worker: add --version and startup log line