From d509fa91089f7c47775f9d25472db0e19be8a4b2 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 6 Jan 2021 01:48:28 +0100 Subject: [PATCH] gen_cid add gen_yml.py and gen_cc.ysl2 --- gen/gen_cid/c_2_ast.py | 4 +- gen/gen_cid/cid_2_yml.py | 1 + .../gen_pybind.ysl2 => gen_cid/gen_cc.ysl2} | 5 +- gen/gen_cid/gen_yml.py | 58 +++++++++++++++++++ 4 files changed, 63 insertions(+), 5 deletions(-) rename gen/{examples/synth_shed/gen_pybind.ysl2 => gen_cid/gen_cc.ysl2} (90%) create mode 100755 gen/gen_cid/gen_yml.py diff --git a/gen/gen_cid/c_2_ast.py b/gen/gen_cid/c_2_ast.py index 0fd104d..3a18896 100644 --- a/gen/gen_cid/c_2_ast.py +++ b/gen/gen_cid/c_2_ast.py @@ -6,11 +6,11 @@ from clang.cindex import CursorKind class C2AST: def __init__(self, library_file): if not clang.cindex.Config.loaded: - print("Using libclang from: %s", library_file) + # print("Using libclang from: %s", library_file) clang.cindex.Config.set_library_file(library_file) import sys sys.setrecursionlimit(10000) - print("max recursion limit set to:", sys.getrecursionlimit()) + # print("max recursion limit set to:", sys.getrecursionlimit()) def parse(self, filename, content=None, follow_includes=False): index = clang.cindex.Index.create() diff --git a/gen/gen_cid/cid_2_yml.py b/gen/gen_cid/cid_2_yml.py index 0d4d708..83b0355 100644 --- a/gen/gen_cid/cid_2_yml.py +++ b/gen/gen_cid/cid_2_yml.py @@ -15,6 +15,7 @@ def generate_yml(cid, module_name): def _grammar_header(): grammar = """ +decl module @name; decl method @name; decl struct @name; diff --git a/gen/examples/synth_shed/gen_pybind.ysl2 b/gen/gen_cid/gen_cc.ysl2 similarity index 90% rename from gen/examples/synth_shed/gen_pybind.ysl2 rename to gen/gen_cid/gen_cc.ysl2 index 1333d78..ccbaae6 100644 --- a/gen/examples/synth_shed/gen_pybind.ysl2 +++ b/gen/gen_cid/gen_cc.ysl2 @@ -1,7 +1,7 @@ include yslt.yml2 tstylesheet { - template "pEp" { + template "module" { document("py_module.cc", "text") { || #include @@ -13,7 +13,7 @@ tstylesheet { using namespace std; namespace py = pybind11; - PYBIND11_MODULE(synth_shed, m) { + PYBIND11_MODULE(«@name», m) { `` apply "method" @@ -24,7 +24,6 @@ tstylesheet { } || } - document("gen_pybind.marker", "text") > "" } template "method" { diff --git a/gen/gen_cid/gen_yml.py b/gen/gen_cid/gen_yml.py new file mode 100755 index 0000000..5cee982 --- /dev/null +++ b/gen/gen_cid/gen_yml.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import sys +import json +import gen_cid +from gen_cid import utils + + +def print_indent(*args, indent=0): + print('\t' * indent, *args) + +def main(): + cfg_file = sys.argv[1] + print("reading cfg file:", cfg_file) + data = [] + try: + with open(cfg_file) as json_data_file: + data = json.load(json_data_file) + except IOError: + print("cfg file not readble:", cfg_file) + exit(-1) + + + pymodule_name = data["module_name"] + libclang_path = data["libclang_path"] + + header_filename = data["header_filename"] + vars = data["variables"] + funcs = data["functions"] + + + print_indent("CONFIG:") + print_indent("module_name:", pymodule_name, indent=1) + + print_indent("libclang_path:", libclang_path, indent=1) + + print_indent("header_filename:", header_filename, indent=1) + + print_indent("variables:", indent=1) + for var in vars: + print_indent(var, indent=2) + + print_indent("functions:", indent=1) + for func in funcs: + print_indent(func, indent=2) + + + cidtools = gen_cid.CIDTools(libclang_path, header_filename, pymodule_name) + header = cidtools.extract(vars, funcs, debug_ast=False, debug_cid=False, debug_yml=False) + + print_indent("OUTPUT:") + print_indent(header["yml"]) + utils.write_string(header["yml"], "./" + "py_module" + ".yml2", ) + + +if __name__ == "__main__": + main()