Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libnvme/src/libnvme.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
#include <nvme/tree.h>
#include <nvme/types.h>
#include <nvme/util.h>
#include <nvme/accessors.h>

#ifdef __cplusplus
}
Expand Down
4 changes: 4 additions & 0 deletions libnvme/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pkg.generate(libnvme,
libnvme_dep = declare_dependency(
dependencies: [
json_c_dep.partial_dependency(compile_args: true, includes: true),
accessors_dep,
],
link_with: libnvme,
include_directories: '.',
Expand All @@ -86,6 +87,9 @@ libnvme_test = library(
libnvme_test_dep = declare_dependency(
link_with: libnvme_test,
include_directories: '.',
dependencies: [
accessors_dep,
],
)

mode = 'rw-r--r--'
Expand Down
10 changes: 10 additions & 0 deletions libnvme/src/nvme/generate-accessors-exclude.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add your Copyright statement here? It seems it is necessary to fully 'best open source practice'. Yeah, we have some more files to decorated ...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

nvme_ns::generic_name
nvme_ctrl::state
nvme_ctrl::address
nvme_ctrl::phy_slot
nvme_ctrl::subsysnqn
nvme_path::queue_depth
nvme_host::pdc_enabled

57 changes: 34 additions & 23 deletions libnvme/src/nvme/generate-accessors.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
#define SPACES " \t\n\r"
#define streq(a, b) (strcmp((a), (b)) == 0)

/**
* Function naming convention:
* This controls whether to generate the functions as:
* nvme_foo_get() / nvme_foo_set()
* Or:
* nvme_get_foo() / nvme_set_foo()
*/
#define SET_FMT "%s_set_%s" /* alternate name: "%s_%s_set" */
#define GET_FMT "%s_get_%s" /* alternate name: "%s_%s_get" */

static const char *banner =
"// SPDX-License-Identifier: LGPL-2.1-or-later\n"
"/**\n"
Expand Down Expand Up @@ -207,8 +217,8 @@ static char *to_uppercase(char *s)
* Any character that violates these rules is replaced with an underscore ('_').
* The string is always modified in place; no new memory is allocated.
*
* @param s Pointer to the NUL-terminated string to sanitize.
* If @p s is NULL or points to an empty string, the function does nothing.
* @param s Pointer to the NUL-terminated string to sanitize. If @s is NULL or
* points to an empty string, the function does nothing.
*
* @note This function does not check for C keywords or identifier length limits.
*
Expand Down Expand Up @@ -808,7 +818,7 @@ typedef struct Conf {
bool verbose;
const char *c_fname; /* Generated output *.c file name */
const char *h_fname; /* Generated output *.h file name */
const char *l_fname; /* Generated ou5tput *.ld file name */
const char *l_fname; /* Generated output *.ld file name */
const char *prefix; /* Prefix added to each functions */
StringList_t hdr_files; /* Input header file list */
StringList_t incl_list; /* Inclusion list (read from --incl) */
Expand Down Expand Up @@ -1269,19 +1279,20 @@ static void generate_hdr(FILE *generated_hdr, StructInfo_t *si, Conf_t *conf)
if (!members->is_const) { /* No setter on const members */
if (members->is_char_array || streq(members->type, "const char *"))
fprintf(generated_hdr,
"void %s%s_%s_set(struct %s *p, const char *%s);\n",
conf->prefix, si->name,
members->name, si->name, members->name);
"void %s" SET_FMT "(struct %s *p, const char *%s);\n",
conf->prefix, si->name, members->name,
si->name, members->name);
else
fprintf(generated_hdr,
"void %s%s_%s_set(struct %s *p, %s %s);\n",
conf->prefix, si->name,
members->name, si->name, members->type, members->name);
"void %s" SET_FMT "(struct %s *p, %s %s);\n",
conf->prefix, si->name, members->name,
si->name, members->type, members->name);
}

/* Getter method */
fprintf(generated_hdr, "%s %s%s_%s_get(const struct %s *p);\n\n",
members->type, conf->prefix, si->name, members->name, si->name);
fprintf(generated_hdr, "%s %s" GET_FMT "(const struct %s *p);\n\n",
members->type, conf->prefix, si->name, members->name,
si->name);
}
}

Expand Down Expand Up @@ -1309,7 +1320,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
if (!member->is_char_array && streq(member->type, "const char *")) {
/* dynamic string */
fprintf(generated_src,
"void %s%s_%s_set(struct %s *p, const char *%s) {\n"
"void %s" SET_FMT "(struct %s *p, const char *%s) {\n"
" free(p->%s);\n"
" p->%s = %s ? strdup(%s) : NULL;\n"
"}\n\n",
Expand All @@ -1323,7 +1334,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
unsigned long sz = strtoul(member->array_size, NULL, 10);

fprintf(generated_src,
"void %s%s_%s_set(struct %s *p, const char *%s) {\n"
"void %s" SET_FMT "(struct %s *p, const char *%s) {\n"
" strncpy(p->%s, %s, %lu);\n"
" p->%s[%lu] = '\\0';\n"
"}\n\n",
Expand All @@ -1333,7 +1344,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
member->name, sz - 1);
} else {
fprintf(generated_src,
"void %s%s_%s_set(struct %s *p, const char *%s) {\n"
"void %s" SET_FMT "(struct %s *p, const char *%s) {\n"
" strncpy(p->%s, %s, %s);\n"
" p->%s[%s - 1] = '\\0';\n"
"}\n\n",
Expand All @@ -1344,22 +1355,22 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
}
} else { /* numeric or struct */
fprintf(generated_src,
"void %s%s_%s_set(struct %s *p, %s %s) {\n"
"void %s" SET_FMT "(struct %s *p, %s %s) {\n"
" p->%s = %s;\n"
"}\n\n",
conf->prefix, si->name, member->name, si->name,
member->type, member->name,
conf->prefix, si->name, member->name,
si->name, member->type, member->name,
member->name, member->name);

}
}

/* Getter method */
fprintf(generated_src, "%s %s%s_%s_get(const struct %s *p) {\n"
fprintf(generated_src, "%s %s" GET_FMT "(const struct %s *p) {\n"
" return p->%s;\n"
"}\n\n",
member->type, conf->prefix, si->name, member->name, si->name,
member->name);
member->type, conf->prefix, si->name, member->name,
si->name, member->name);
}
}

Expand All @@ -1368,7 +1379,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
* one struct.
*
* Writes linker entries for each member in @si to the provided output
* FILE (@generated_ld). Handles special
* FILE (@generated_ld).
*
* @param generated_ld: FILE* to write implementations to.
* @param si: Pointer to the struct description.
Expand All @@ -1380,8 +1391,8 @@ static void generate_ld(FILE *generated_ld, StructInfo_t *si, Conf_t *conf)
Member_t *member = &si->members[m];

fprintf(generated_ld,
"\t\t%s%s_%s_get;\n"
"\t\t%s%s_%s_set;\n",
"\t\t%s" GET_FMT ";\n"
"\t\t%s" SET_FMT ";\n",
conf->prefix, si->name, member->name,
conf->prefix, si->name, member->name);
}
Expand Down
6 changes: 5 additions & 1 deletion libnvme/src/nvme/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ accessors_ch_custom_tgt = custom_target(
'--h-out', '@OUTPUT0@',
'--c-out', '@OUTPUT1@',
'--ld-out', '@OUTPUT2@',
'--incl', join_paths(meson.current_source_dir(), 'structs-to-include.txt'),
'--incl', join_paths(meson.current_source_dir(), 'generate-accessors-include.list'),
'--excl', join_paths(meson.current_source_dir(), 'generate-accessors-exclude.list'),
'@INPUT@',
],
build_by_default: true,
Expand All @@ -64,6 +65,9 @@ accessors_dep = declare_dependency(
tgt_accessors_h,
],
include_directories: '.',
dependencies: [
ccan_dep,
],
)

if meson.version().version_compare('>=1.4.0')
Expand Down
Loading