Skip to content

Commit bd3ef32

Browse files
author
Martin Belanger
committed
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 4dc70ec commit bd3ef32

File tree

8 files changed

+2027
-7
lines changed

8 files changed

+2027
-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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
# These files contain the setter/getter functions.
23+
# NOTE: We only want the 'install' to apply to accessors.h
24+
# and not to accessors.c. Therefore, 'install' must
25+
# be set to 'false' here. We're using a second (dummy)
26+
# custom_target below to install accessors.h by itself.
27+
# I know it's ugly, but meson does not give us the
28+
# flexibility to install just a subset of custom target files.
29+
accessors_ch_custom_tgt = custom_target(
30+
'[_accessors.h, accessors.c]',
31+
input: headers_to_scan_full_path,
32+
output: [
33+
'_accessors.h',
34+
'accessors.c',
35+
],
36+
command: [
37+
generate_accessors,
38+
'--h-out', '@OUTPUT0@',
39+
'--c-out', '@OUTPUT1@',
40+
'--incl', join_paths(meson.current_source_dir(), 'structs-to-include.txt'),
41+
'@INPUT@',
42+
],
43+
build_by_default: true,
44+
install: false,
45+
)
46+
47+
generated_accessors_c = accessors_ch_custom_tgt[1]
48+
generated_accessors_dep = declare_dependency(
49+
sources: [
50+
accessors_ch_custom_tgt,
51+
]
52+
)
53+
54+
# Install accessors.h.
55+
# Because the 'install' argument of the custom_target()
56+
# function intalls all the files listed in 'output',
57+
# it is not possible in the previous generation to only
58+
# install accessors.h. Therefore, we use a second (dummy)
59+
# custom_target to install accessor.h by itself.
60+
accessors_h_custom_tgt = custom_target(
61+
'accessor.h',
62+
input: accessors_ch_custom_tgt[0], # depends on previous generation
63+
output: 'accessor.h',
64+
command: ['cp', '@INPUT@', '@OUTPUT@'],
65+
install: true,
66+
install_dir: 'nvme',
67+
install_mode: ['rw-r--r--', 0, 0],
68+
)
69+
70+
generated_accessors_h = accessors_h_custom_tgt[0]
71+
72+
if meson.version().version_compare('>=1.4.0')
73+
generated_accessors_h_path = generated_accessors_h.full_path()
74+
else
75+
generated_accessors_h_path = meson.current_build_dir() / 'accessor.h'
76+
endif
77+
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)