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 24, 2023
1 parent b6411d7 commit 3c2575c
Show file tree
Hide file tree
Showing 13 changed files with 483 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
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 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
$(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 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 +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

"""
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
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

0 comments on commit 3c2575c

Please sign in to comment.