Skip to content

Commit bb65606

Browse files
Martin Belangerigaw
authored andcommitted
Generate accessor functions (setter/getter)
This adds the ability to automatically generate accessor functions (setter/getter functions) by parsing structs definitions in header files. Two files are generated: [accessors.c, accessors.h]. Signed-off-by: Martin Belanger <martin.belanger@dell.com>
1 parent 3ec2bd0 commit bb65606

File tree

8 files changed

+2021
-7
lines changed

8 files changed

+2021
-7
lines changed

internal/meson.build

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@ config_dep = declare_dependency(
2020
include_directories : internal_incdir,
2121
sources: config_h)
2222

23-
config_h_path = meson.current_build_dir() / 'config.h'
24-
config_h_arg = [ '-include', config_h_path ]
23+
if meson.version().version_compare('>=1.4.0')
24+
config_h_path = config_h.full_path()
25+
else
26+
config_h_path = meson.current_build_dir() / 'config.h'
27+
endif
28+

meson.build

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ project(
2020
]
2121
)
2222

23+
fs = import('fs')
24+
2325
vstr = meson.project_version().split('-rc')[0]
2426
vstr = vstr.split('.dev')[0]
2527
vid = vstr.split('.')
@@ -272,9 +274,10 @@ add_project_arguments(
272274
],
273275
language : 'c',
274276
)
275-
incdir = include_directories(['.', 'ccan', 'src'])
277+
incdir = include_directories(['.', 'ccan', 'src', 'src/nvme'])
276278

277279
################################################################################
280+
subdir('tools')
278281
subdir('internal')
279282
subdir('ccan')
280283
subdir('src')

src/generated/meson.build

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# SPDX-License-Identifier: LGPL-2.1-or-later
2+
#
3+
# This file is part of libnvme.
4+
# Copyright (c) 2025, Dell Technologies Inc. or its subsidiaries.
5+
#
6+
# Authors: Martin Belanger <Martin.Belanger@dell.com>
7+
#
8+
# Run generate-accessors to generate the setter/getter functions.
9+
10+
# List of header files to parse for structs
11+
headers_to_scan = [
12+
'../nvme/private.h',
13+
]
14+
15+
# Append full path to each member of headers_to_scan
16+
headers_to_scan_full_path = []
17+
foreach hdr : headers_to_scan
18+
headers_to_scan_full_path += join_paths(meson.current_source_dir(), hdr)
19+
endforeach
20+
21+
# Generate accessors.c and accessors.h
22+
accessors_ch_custom_tgt = custom_target(
23+
'accessors.[ch]',
24+
input: headers_to_scan_full_path,
25+
output: [
26+
'accessors.h',
27+
'accessors.c',
28+
],
29+
command: [
30+
generate_accessors,
31+
'--h-out', '@OUTPUT0@',
32+
'--c-out', '@OUTPUT1@',
33+
'--incl', join_paths(meson.current_source_dir(), 'structs-to-include.txt'),
34+
'@INPUT@',
35+
],
36+
build_by_default: true,
37+
install: true,
38+
install_dir: [
39+
'nvme', # Install @OUTPUT0@=='accessors.h' in this directory.
40+
false, # Do not install @OUTPUT1@=='accessors.c'
41+
],
42+
install_mode: ['rw-r--r--', 0, 0],
43+
)
44+
45+
generated_accessors_h = accessors_ch_custom_tgt[0]
46+
generated_accessors_c = accessors_ch_custom_tgt[1]
47+
48+
if meson.version().version_compare('>=1.4.0')
49+
generated_accessors_h_path = generated_accessors_h.full_path()
50+
else
51+
generated_accessors_h_path = meson.current_build_dir() / 'accessor.h'
52+
endif
53+
54+
generated_accessors_dep = declare_dependency(
55+
sources: [
56+
accessors_ch_custom_tgt,
57+
]
58+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
nvme_path
2+
nvme_ns
3+
nvme_ctrl
4+
nvme_subsystem
5+
nvme_host
6+
nvme_fabric_options

src/meson.build

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ else
2727
sources += 'nvme/no-json.c'
2828
endif
2929

30+
# Generate accessors (setter/getter functions)
31+
subdir('generated')
32+
sources += generated_accessors_c
33+
3034
deps = [
3135
json_c_dep,
3236
keyutils_dep,
3337
libdbus_dep,
3438
liburing_dep,
3539
openssl_dep,
40+
config_dep,
41+
generated_accessors_dep,
3642
]
3743

3844
source_dir = meson.current_source_dir()
@@ -46,8 +52,11 @@ libnvme = library(
4652
link_args: ['-Wl,--version-script=' + version_script_arg],
4753
dependencies: deps,
4854
link_depends: mapfile,
49-
c_args: config_h_arg,
50-
include_directories: [incdir, internal_incdir],
55+
c_args: [
56+
'-include', config_h_path,
57+
'-include', generated_accessors_h_path,
58+
],
59+
include_directories: [incdir, ],
5160
install: true,
5261
link_with: libccan,
5362
)
@@ -75,8 +84,11 @@ libnvme_test = library(
7584
'nvme-test', # produces libnvme-test.so
7685
sources,
7786
dependencies: deps,
78-
c_args: config_h_arg,
79-
include_directories: [incdir, internal_incdir],
87+
c_args: [
88+
'-include', config_h_path,
89+
'-include', generated_accessors_h_path,
90+
],
91+
include_directories: [incdir, ],
8092
install: false,
8193
link_with: libccan,
8294
)

0 commit comments

Comments
 (0)