pybind_wrap.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 """
3 Helper script to wrap C++ to Python with Pybind.
4 This script is installed via CMake to the user's binary directory
5 and invoked during the wrapping by CMake.
6 """
7 
8 # pylint: disable=import-error
9 
10 import argparse
11 
12 from gtwrap.pybind_wrapper import PybindWrapper
13 
14 
15 def main():
16  """Main runner."""
17  arg_parser = argparse.ArgumentParser(
18  formatter_class=argparse.ArgumentDefaultsHelpFormatter)
19  arg_parser.add_argument("--src",
20  type=str,
21  required=True,
22  help="Input interface .i/.h file(s)")
23  arg_parser.add_argument(
24  "--module_name",
25  type=str,
26  required=True,
27  help="Name of the Python module to be generated and "
28  "used in the Python `import` statement.",
29  )
30  arg_parser.add_argument(
31  "--out",
32  type=str,
33  required=True,
34  help="Name of the output pybind .cc file(s)",
35  )
36  arg_parser.add_argument(
37  "--use-boost-serialization",
38  action="store_true",
39  help="Allow boost based serialization methods",
40  )
41  arg_parser.add_argument(
42  "--top_module_namespaces",
43  type=str,
44  default="",
45  help="C++ namespace for the top module, e.g. `ns1::ns2::ns3`. "
46  "Only the content within this namespace and its sub-namespaces "
47  "will be wrapped. The content of this namespace will be available at "
48  "the top module level, and its sub-namespaces' in the submodules.\n"
49  "For example, `import <module_name>` gives you access to a Python "
50  "`<module_name>.Class` of the corresponding C++ `ns1::ns2::ns3::Class`"
51  "and `from <module_name> import ns4` gives you access to a Python "
52  "`ns4.Class` of the C++ `ns1::ns2::ns3::ns4::Class`. ",
53  )
54  arg_parser.add_argument(
55  "--ignore",
56  nargs='*',
57  type=str,
58  help="A space-separated list of classes to ignore. "
59  "Class names must include their full namespaces.",
60  )
61  arg_parser.add_argument("--template",
62  type=str,
63  help="The module template file (e.g. module.tpl).")
64  arg_parser.add_argument("--is_submodule",
65  default=False,
66  action="store_true")
67  args = arg_parser.parse_args()
68 
69  top_module_namespaces = args.top_module_namespaces.split("::")
70  if top_module_namespaces[0]:
71  top_module_namespaces = [''] + top_module_namespaces
72 
73  with open(args.template, "r", encoding="UTF-8") as f:
74  template_content = f.read()
75 
76  wrapper = PybindWrapper(
77  module_name=args.module_name,
78  use_boost_serialization=args.use_boost_serialization,
79  top_module_namespaces=top_module_namespaces,
80  ignore_classes=args.ignore,
81  module_template=template_content,
82  )
83 
84  if args.is_submodule:
85  wrapper.wrap_submodule(args.src)
86 
87  else:
88  # Wrap the code and get back the cpp/cc code.
89  sources = args.src.split(';')
90  wrapper.wrap(sources, args.out)
91 
92 
93 if __name__ == "__main__":
94  main()
gtwrap.pybind_wrapper.PybindWrapper
Definition: pybind_wrapper.py:23
pybind_wrap.main
def main()
Definition: pybind_wrap.py:15
gtwrap.pybind_wrapper
Definition: pybind_wrapper.py:1


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:02:24