Skip to content

Commit

Permalink
virtual: Factor out code generation templates
Browse files Browse the repository at this point in the history
This makes the code generation script more generic by factoring out
templates into separate files.

Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Aug 15, 2023
1 parent 91bec95 commit b8685d5
Show file tree
Hide file tree
Showing 13 changed files with 489 additions and 400 deletions.
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 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
52 changes: 35 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,36 @@ 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 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)/pkcs11-json/pkcs11.json --outfile $@

p11-kit/virtual-fixed-wrappers.h: Makefile p11-kit/gen-wrappers.py 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)/pkcs11-json/pkcs11.json --outfile $@

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

p11-kit/virtual-stack-generated.h: Makefile p11-kit/gen-wrappers.py 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)/pkcs11-json/pkcs11.json --outfile $@

p11-kit/virtual-base-generated.h: Makefile p11-kit/gen-wrappers.py 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)/pkcs11-json/pkcs11.json --outfile $@

lib_LTLIBRARIES += \
libp11-kit.la
Expand Down Expand Up @@ -220,7 +236,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 +570,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
46 changes: 46 additions & 0 deletions p11-kit/gen-fixed-closures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/python

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

import io
import sys

INDENT = " "


def fixed_instantiate(closures):
output = io.StringIO()
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})
""")
return output.getvalue()


if __name__ == "__main__":
import argparse
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()

args.outfile.write(fixed_instantiate(args.closures))
Loading

0 comments on commit b8685d5

Please sign in to comment.