Skip to content

Commit a49c53c

Browse files
Martin Belangerigaw
authored andcommitted
libnvme: commit pre-generated accessor files and simplify build
The accessor functions (getters/setters for nvme_path, nvme_ns, nvme_ctrl, nvme_subsystem, nvme_host, nvme_fabric_options) were previously generated at build time by a custom_target in Meson. They are now committed to the source tree and treated as maintainer-generated source files, similar to how the Linux kernel and systemd handle infrequently-changing generated code. Changes: - Commit accessors.h, accessors.c, accessors.ld to the source tree - Remove the custom_target that ran the generator on every build; generate-accessors is no longer compiled during a normal build - Add accessors.c to the sources list and accessors.h to install_headers() alongside their sibling files in libnvme/src/meson.build - Remove accessors_dep (was only needed to forward the custom_target outputs; plain files don't require this indirection) - Enhance generate-accessors.c to emit mechanical Doxygen stubs in accessors.h, covering all three member kinds: value types, dynamic char * (with NULL/copy semantics), and fixed char arrays - Add update-accessors.sh: atomically regenerates accessors.h and accessors.c when content changes, and reports symbol additions and removals against accessors.ld without overwriting it - Add a Meson run_target and Makefile target 'update-accessors' for on-demand regeneration: make update-accessors - Keep accessors.ld manually maintained; its version section labels (e.g. LIBNVME_ACCESSORS_3_0) must be assigned by the maintainer to preserve ABI compatibility - Add .github/workflows/check-accessors.yml to fail CI when the committed accessor files drift from the generator output - Update README.md with a developer guide covering regeneration, accessors.ld maintenance, and the ABI versioning convention - Fix three stale test reference files that were missing dhchap_ctrl_key from their expected JSON output following an earlier json.c update Signed-off-by: Martin Belanger <[email protected]>
1 parent 8a9d01d commit a49c53c

18 files changed

Lines changed: 3257 additions & 858 deletions
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
# SPDX-License-Identifier: GPL-2.0-or-later
3+
#
4+
# This file is part of nvme.
5+
# Copyright (c) 2025 Dell Technologies Inc. or its subsidiaries.
6+
#
7+
# Authors: Martin Belanger <[email protected]>
8+
#
9+
# Verify that the committed accessor files (accessors.h, accessors.c) are
10+
# up to date with the generate-accessors tool and the structs in private.h.
11+
#
12+
# If this check fails, run the following command and commit the result:
13+
#
14+
# meson compile -C <build-dir> update-accessors
15+
16+
name: Check accessors
17+
18+
on:
19+
push:
20+
branches: [master]
21+
pull_request:
22+
branches: [master]
23+
24+
workflow_dispatch:
25+
26+
jobs:
27+
check-accessors:
28+
name: check generated accessor files are up to date
29+
runs-on: ubuntu-latest
30+
container:
31+
image: ghcr.io/linux-nvme/debian:latest
32+
steps:
33+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
34+
- name: Mark repo as safe for git
35+
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
36+
- name: Configure
37+
run: meson setup .build-ci
38+
- name: Regenerate accessor files
39+
run: meson compile -C .build-ci update-accessors
40+
- name: Check for uncommitted differences
41+
run: |
42+
if ! git diff --exit-code libnvme/src/nvme/accessors.h \
43+
libnvme/src/nvme/accessors.c; then
44+
echo ""
45+
echo "ERROR: accessor files are out of date."
46+
echo "Run 'meson compile -C <build-dir> update-accessors' and commit the result."
47+
exit 1
48+
fi

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ install: ${NAME}
4545
uninstall:
4646
cd ${BUILD-DIR} && meson --internal uninstall
4747

48+
.PHONY: update-accessors
49+
update-accessors: ${BUILD-DIR}
50+
meson compile -C ${BUILD-DIR} update-accessors
51+
4852
.PHONY: dist
4953
dist: ${NAME}
5054
meson dist -C ${BUILD-DIR} --formats gztar

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Note for nvme-cli the 'default' is set to nofallback.
6161
#### Installing
6262

6363
# meson install -C .build
64-
64+
6565
### Build with build.sh wrapper
6666

6767
The `scripts/build.sh` is used for the CI build but can also be used for
@@ -237,6 +237,93 @@ After that, you just need to implement the functions you defined in each
237237
ENTRY, then append the object file name to the meson.build "sources".
238238

239239

240+
### Updating the libnvme accessor functions
241+
242+
libnvme exposes a set of getter and setter functions (accessors) for its core
243+
internal structs (`nvme_path`, `nvme_ns`, `nvme_ctrl`, `nvme_subsystem`,
244+
`nvme_host`, `nvme_fabric_options`). These are generated from the struct
245+
definitions in `libnvme/src/nvme/private.h` by the tool
246+
`libnvme/src/nvme/generate-accessors.c`.
247+
248+
The generated files are committed to the source tree and are **not**
249+
regenerated during a normal build:
250+
251+
```
252+
libnvme/src/nvme/accessors.h # public API declarations (with Doxygen stubs)
253+
libnvme/src/nvme/accessors.c # implementations
254+
libnvme/src/nvme/accessors.ld # linker version script (manually maintained)
255+
```
256+
257+
#### When to regenerate
258+
259+
Regeneration is needed when a struct member is added, removed, or renamed in
260+
`private.h`, or when a struct is added to or removed from
261+
`generate-accessors-include.list` or excluded in `generate-accessors-exclude.list`.
262+
263+
#### How to regenerate
264+
265+
```shell
266+
$ make update-accessors
267+
```
268+
269+
or equivalently:
270+
271+
```shell
272+
$ meson compile -C .build update-accessors
273+
```
274+
275+
The script compiles the generator, runs it, and atomically updates
276+
`accessors.h` and `accessors.c` only when their content changes.
277+
Commit the updated files afterward:
278+
279+
```shell
280+
$ git add libnvme/src/nvme/accessors.h libnvme/src/nvme/accessors.c
281+
$ git commit -m "libnvme: regenerate accessors following <struct> changes"
282+
```
283+
284+
#### Maintaining accessors.ld
285+
286+
`accessors.ld` is a GNU linker version script that controls which accessor
287+
symbols are exported from `libnvme.so` and under which ABI version label they
288+
were introduced (e.g. `LIBNVME_ACCESSORS_3_0`).
289+
290+
This file is **not** updated automatically, because each symbol must be placed
291+
in the correct version section by the maintainer. Adding a symbol to an
292+
already-published version section would break binary compatibility for
293+
existing users of the library.
294+
295+
When `make update-accessors` detects that the symbol list has drifted from
296+
`accessors.ld`, it prints a report like the following:
297+
298+
```
299+
WARNING: accessors.ld needs manual attention.
300+
301+
Symbols to ADD (place in a new version section, e.g. LIBNVME_ACCESSORS_X_Y):
302+
nvme_ctrl_get_new_field
303+
nvme_ctrl_set_new_field
304+
```
305+
306+
New symbols must be added to a **new** version section that chains the
307+
previous one. For example, if the current latest section is
308+
`LIBNVME_ACCESSORS_3_0`, add a new section for the next release:
309+
310+
```
311+
LIBNVME_ACCESSORS_3_1 {
312+
global:
313+
nvme_ctrl_get_new_field;
314+
nvme_ctrl_set_new_field;
315+
} LIBNVME_ACCESSORS_3_0;
316+
```
317+
318+
Then commit `accessors.ld` together with the regenerated source files.
319+
320+
#### CI enforcement
321+
322+
A GitHub Actions workflow (`.github/workflows/check-accessors.yml`) runs on
323+
every push and pull request. It regenerates the accessor files and fails if
324+
the result differs from what is committed, ensuring the source tree never
325+
drifts silently.
326+
240327
## Dependency
241328

242329
libnvme depends on the /sys/class/nvme-subsystem interface which was

libnvme/src/libnvme.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern "C" {
2525
#include <nvme/tree.h>
2626
#include <nvme/types.h>
2727
#include <nvme/util.h>
28+
#include <nvme/accessors.h>
2829

2930
#ifdef __cplusplus
3031
}

libnvme/src/meson.build

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Authors: Martin Belanger <[email protected]>
77
#
88
sources = [
9+
'nvme/accessors.c',
910
'nvme/base64.c',
1011
'nvme/cmds.c',
1112
'nvme/crc32.c',
@@ -29,8 +30,7 @@ else
2930
sources += 'nvme/no-json.c'
3031
endif
3132

32-
# Generate accessors (setter/getter functions)
33-
subdir('nvme') # declares: accessors_dep, accessors_ld_full_path
33+
subdir('nvme') # declares: accessors_ld_full_path
3434

3535
deps = [
3636
config_dep,
@@ -40,7 +40,6 @@ deps = [
4040
libdbus_dep,
4141
liburing_dep,
4242
openssl_dep,
43-
accessors_dep,
4443
]
4544

4645
ldfile = 'libnvme.ld'
@@ -98,6 +97,7 @@ install_headers(
9897
)
9998
install_headers(
10099
[
100+
'nvme/accessors.h',
101101
'nvme/cmds.h',
102102
'nvme/fabrics.h',
103103
'nvme/filters.h',

0 commit comments

Comments
 (0)