Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: Generate wrappers with script #550

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ frob-*
/p11-kit/p11-kit-1.pc
/p11-kit/pkcs11.conf.example
/p11-kit/p11-kit-server.service
/p11-kit/proxy-generated.h
/p11-kit/virtual-*-generated.h
/p11-kit/virtual-fixed-wrappers.h
/p11-kit/virtual-fixed-closures.h
Expand Down
23 changes: 18 additions & 5 deletions p11-kit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ VIRTUAL_GENERATED = \
p11-kit/virtual-base-generated.h \
$(NULL)

BUILT_SOURCES += $(VIRTUAL_GENERATED) \
$(NULL)
BUILT_SOURCES += $(VIRTUAL_GENERATED)

p11-kit/virtual.c: $(VIRTUAL_GENERATED)

CLEANFILES += \
$(VIRTUAL_GENERATED) \
$(NULL)
CLEANFILES += $(VIRTUAL_GENERATED)

p11-kit/virtual-ffi-generated.h: Makefile p11-kit/gen-wrappers.py subprojects/pkcs11-json/pkcs11.json
$(AM_V_GEN)$(PYTHON) $(srcdir)/p11-kit/gen-wrappers.py \
Expand Down Expand Up @@ -107,6 +104,22 @@ else
libp11_kit_la_LDFLAGS += -export-symbols-regex '^C_GetFunctionList|^C_GetInterface|^p11_kit_'
endif

PROXY_GENERATED = \
p11-kit/proxy-generated.h \
$(NULL)

BUILT_SOURCES += $(PROXY_GENERATED)

p11-kit/proxy.c: $(PROXY_GENERATED)

CLEANFILES += $(PROXY_GENERATED)

p11-kit/proxy-generated.h: Makefile p11-kit/gen-wrappers.py subprojects/pkcs11-json/pkcs11.json
$(AM_V_GEN)$(PYTHON) $(srcdir)/p11-kit/gen-wrappers.py \
--template $(srcdir)/p11-kit/templates/proxy-wrappers.py \
--excludes $(srcdir)/p11-kit/templates/proxy-excludes.list \
--infile $(srcdir)/subprojects/pkcs11-json/pkcs11.json --outfile $@

libp11_kit_la_SOURCES = \
p11-kit/proxy.c p11-kit/proxy.h p11-kit/proxy-init.c \
p11-kit/rpc-server.c \
Expand Down
32 changes: 29 additions & 3 deletions p11-kit/gen-wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
import sys

INDENT = " "
X_EXCLUDES = [
"C_GetFunctionList",
"C_GetFunctionStatus",
"C_CancelFunction",
"C_GetInterfaceList",
"C_GetInterface",
]


def emit_wrapper_function(function, templates, concat_lines=False):
Expand Down Expand Up @@ -45,11 +52,30 @@ def emit_wrapper_function(function, templates, concat_lines=False):
for index, argument in enumerate(function["arguments"])
])

function_body_template = templates["function_body"]
if function["version"] >= 3:
has_slot_id = next((argument for argument in function["arguments"] if argument["type"] == "CK_SLOT_ID"), None)
has_session_handle = next((argument for argument in function["arguments"] if argument["type"] == "CK_SESSION_HANDLE"), None)
assert not (has_slot_id and has_session_handle)

function_body_template = templates.get("function_body")
if function_body_template and function["version"] >= 3:
function_body_template = templates.get("function_body_v3",
function_body_template)

if has_slot_id:
template = templates.get("function_body_with_slot")
if template is not None:
function_body_template = templates.get(
"function_body_with_slot_v3",
template,
)
elif has_session_handle:
template = templates.get("function_body_with_session")
if template is not None:
function_body_template = templates.get(
"function_body_with_session_v3",
template,
)

return function_body_template.format(
indent=INDENT,
function_name=function_name,
Expand Down Expand Up @@ -126,7 +152,7 @@ def emit_wrapper_entry(function, templates, suffix=None):
templates,
renames.get(function["name"]),
)
for function in functions if function["name"] not in excludes
for function in functions if function["name"] not in X_EXCLUDES
])
entries = separator.join([
INDENT + emit_wrapper_entry(
Expand Down
12 changes: 12 additions & 0 deletions p11-kit/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ libp11_kit_sources = [
'util.c'
]

libp11_kit_sources += custom_target('generate proxy-generated.h',
input: pkcs11_json,
output: 'proxy-generated.h',
command: [
python,
meson.current_source_dir() / 'gen-wrappers.py',
'--template', meson.current_source_dir() / 'templates' / 'proxy-wrappers.py',
'--excludes', meson.current_source_dir() / 'templates' / 'proxy-excludes.list',
'--infile', '@INPUT@',
'--outfile', '@OUTPUT@',
])

libp11_kit_symbol_map = meson.current_source_dir() / 'libp11-kit.map'
libp11_kit_ldflags = cc.get_supported_link_arguments([
'-Wl,--version-script,' + libp11_kit_symbol_map
Expand Down
Loading