-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateBindingFromHeader.py
More file actions
67 lines (55 loc) · 2.14 KB
/
Copy pathgenerateBindingFromHeader.py
File metadata and controls
67 lines (55 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Run using `python3 scripts/generateBindingFromHeader.py /include/circuits/UserCircuits.h`
import re
import os
import argparse
def extractCircuitClasses(headerFilePath):
# Read C++ code
with open(headerFilePath, 'r') as headerFile:
headerCode = headerFile.read()
# Extract circuit classes
pattern = r'class\s+(\w+)\s*:\s*public\s*(\w+)'
matches = re.findall(pattern, headerCode)
return matches
def generateBindingCode(headerFilePath, moduleName, matches):
# Generate pybind11 binding code
pybindCode = """#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
""" + f"""#include "../include/circuits/{headerFilePath}"
""" + """#include "../PointToPoint_SDK/inc/Circuit.h"
namespace py = pybind11;
""" + f"""
PYBIND11_MODULE({moduleName}, m)""" + """ {
py::module_::import("CircuitProcessor");
"""
# Generate pybind11 bindings for each class
for match in matches:
className = match[0]
baseClass = match[1]
pybindCode += f"""
py::class_<PointToPoint::{className}, PointToPoint::{baseClass}>(m, "{className}")
.def(py::init<>());
"""
# End pybind11 module definition
pybindCode += """
}"""
return pybindCode
def writeToBindingCpp(pybindCode, moduleName):
# Write pybind11 code to a new file
cppFilePath = f'python/bindings/{moduleName}Binding.cpp'
with open(cppFilePath, 'w') as cppFile:
cppFile.write(pybindCode)
print(f"Pybind11 binding code has been written to {cppFilePath}")
def main():
parser = argparse.ArgumentParser(
description="Generate a pybind11 binding .cpp for a given header file."
)
parser.add_argument("filePath", help="Relative file path to header file containing custom circuits.")
args = parser.parse_args()
headerFilePath = args.filePath
moduleName = os.path.splitext(os.path.basename(headerFilePath))[0]
matches = extractCircuitClasses(headerFilePath)
pybindCode = generateBindingCode(headerFilePath, moduleName, matches)
writeToBindingCpp(pybindCode, moduleName)
if __name__ == "__main__":
main()