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

virtual: Factor out code generation templates #540

Merged
merged 2 commits into from
Aug 24, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ frob-*
/p11-kit/pkcs11.conf.example
/p11-kit/p11-kit-server.service
/p11-kit/virtual-*-generated.h
/p11-kit/virtual-fixed-wrappers.h
/p11-kit/virtual-fixed-closures.h
/p11-kit/*.gnu.c

/p11-kit/p11-kit-testable
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "pkcs11-json"]
path = pkcs11-json
path = subprojects/pkcs11-json
url = https:/p11-glue/pkcs11-json.git
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CLEANFILES =
EXTRA_DIST = CONTRIBUTING.md meson.build meson_options.txt po/meson.build \
doc/manual/meson.build doc/manual/userdir.xml.in \
doc/manual/sysdir.xml.in doc/manual/version.xml.in \
bash-completion pkcs11-json
bash-completion subprojects

incdir = $(includedir)/p11-kit-1/p11-kit
inc_HEADERS =
Expand Down Expand Up @@ -57,7 +57,7 @@ endif

PKCS11_JSON_INPUT = $(srcdir)/common/pkcs11.h

include pkcs11-json/Makefile.am
include subprojects/pkcs11-json/Makefile.am

SUBDIRS = . doc po

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ AC_PROG_CC
AC_PROG_CPP
AM_PROG_CC_C_O
PKG_PROG_PKG_CONFIG
AM_MISSING_PROG([PYTHON], [python])
AM_PATH_PYTHON([3.6],, [:])
AM_MISSING_PROG([CASTXML], [castxml])

LINGUAS=""
Expand Down
4 changes: 3 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,9 @@ if not python_version.version_compare(python_version_req)
error('Requires Python @0@, @1@ found.'.format(python_version_req, python_version))
endif

subdir('pkcs11-json')
pkcs11_json_project = subproject('pkcs11-json')
pkcs11_json = pkcs11_json_project.get_variable('pkcs11_json')

subdir('common')
subdir('p11-kit')
subdir('fuzz')
Expand Down
51 changes: 34 additions & 17 deletions p11-kit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ COMMON_SRCS = \

VIRTUAL_GENERATED = \
p11-kit/virtual-ffi-generated.h \
p11-kit/virtual-fixed-generated.h \
p11-kit/virtual-fixed-wrappers.h \
p11-kit/virtual-fixed-closures.h \
p11-kit/virtual-stack-generated.h \
p11-kit/virtual-base-generated.h \
$(NULL)
Expand All @@ -43,21 +44,35 @@ CLEANFILES += \
$(VIRTUAL_GENERATED) \
$(NULL)

p11-kit/virtual-ffi-generated.h: Makefile p11-kit/gen-virtual.py pkcs11-json/pkcs11.json
$(AM_V_GEN)$(PYTHON) $(srcdir)/p11-kit/gen-virtual.py --ffi \
$(srcdir)/pkcs11-json/pkcs11.json $@

p11-kit/virtual-fixed-generated.h: Makefile p11-kit/gen-virtual.py pkcs11-json/pkcs11.json
$(AM_V_GEN)$(PYTHON) $(srcdir)/p11-kit/gen-virtual.py --fixed $(closures) \
$(srcdir)/pkcs11-json/pkcs11.json $@

p11-kit/virtual-stack-generated.h: Makefile p11-kit/gen-virtual.py pkcs11-json/pkcs11.json
$(AM_V_GEN)$(PYTHON) $(srcdir)/p11-kit/gen-virtual.py --stack \
$(srcdir)/pkcs11-json/pkcs11.json $@

p11-kit/virtual-base-generated.h: Makefile p11-kit/gen-virtual.py pkcs11-json/pkcs11.json
$(AM_V_GEN)$(PYTHON) $(srcdir)/p11-kit/gen-virtual.py --base \
$(srcdir)/pkcs11-json/pkcs11.json $@
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 \
--template $(srcdir)/p11-kit/templates/binding-wrappers.py \
--excludes $(srcdir)/p11-kit/templates/virtual-excludes.list \
--infile $(srcdir)/subprojects/pkcs11-json/pkcs11.json --outfile $@

p11-kit/virtual-fixed-wrappers.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/fixed-wrappers.py \
--excludes $(srcdir)/p11-kit/templates/virtual-excludes.list \
--renames C_GetFunctionStatus:short C_CancelFunction:short \
--concat-lines \
--infile $(srcdir)/subprojects/pkcs11-json/pkcs11.json --outfile $@

p11-kit/virtual-fixed-closures.h: Makefile p11-kit/gen-fixed-closures.py
$(AM_V_GEN)$(PYTHON) $(srcdir)/p11-kit/gen-fixed-closures.py \
--closures $(closures) --outfile $@

p11-kit/virtual-stack-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/stack-wrappers.py \
--excludes $(srcdir)/p11-kit/templates/virtual-excludes.list \
--infile $(srcdir)/subprojects/pkcs11-json/pkcs11.json --outfile $@

p11-kit/virtual-base-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/base-wrappers.py \
--excludes $(srcdir)/p11-kit/templates/virtual-excludes.list \
--infile $(srcdir)/subprojects/pkcs11-json/pkcs11.json --outfile $@

lib_LTLIBRARIES += \
libp11-kit.la
Expand Down Expand Up @@ -220,7 +235,8 @@ example_DATA = p11-kit/pkcs11.conf.example
EXTRA_DIST += \
p11-kit/docs.h \
p11-kit/gen-pkcs11-gnu.sh \
p11-kit/gen-virtual.py \
p11-kit/gen-wrappers.py \
p11-kit/gen-fixed-closures.py \
p11-kit/meson.build \
p11-kit/meson_post_install.sh \
p11-kit/libp11-kit.map \
Expand Down Expand Up @@ -553,6 +569,7 @@ mock_eleven_la_LIBADD = $(mock_one_la_LIBADD)

EXTRA_DIST += \
p11-kit/fixtures \
p11-kit/templates \
p11-kit/test-mock.c \
p11-kit/test-transport-base.c \
p11-kit/test-profiles.sh \
Expand Down
43 changes: 43 additions & 0 deletions p11-kit/gen-fixed-closures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/python
ZoltanFridrich marked this conversation as resolved.
Show resolved Hide resolved

"""
SPDX-License-Identifier: BSD-3-Clause
"""

import sys

INDENT = " "


def write_fixed_instantiate(output, closures):
for i in range(closures):
output.write(f"P11_VIRTUAL_FIXED_FUNCTIONS({i})\n")

initializers_concatenated = ",\n".join([
f"{INDENT}P11_VIRTUAL_FIXED_INITIALIZER({i})"
for i in range(closures)
])
output.write(f'''
CK_FUNCTION_LIST_3_0 p11_virtual_fixed[P11_VIRTUAL_MAX_FIXED] = {{
{initializers_concatenated}
}};

''')
for i in range(closures):
output.write(f"""\
P11_VIRTUAL_FIXED_GET_FUNCTION_LIST({i})
P11_VIRTUAL_FIXED_GET_INTERFACE_LIST({i})
P11_VIRTUAL_FIXED_GET_INTERFACE({i})
""")


if __name__ == "__main__":
import argparse
ueno marked this conversation as resolved.
Show resolved Hide resolved
parser = argparse.ArgumentParser()
parser.add_argument("--closures", type=int,
default=64)
parser.add_argument("--outfile", type=argparse.FileType("w"),
default=sys.stdout)
args = parser.parse_args()

write_fixed_instantiate(args.outfile, args.closures)
Loading
Loading