From 243d4a798b0368384ecfb8030fd4a3b7afccff5a Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Fri, 30 May 2014 19:21:38 +0200 Subject: [PATCH] wrapper: add use_module - a module can use the converters used by another with the use_module('A') command. In which case : - the generate converter header for A will be included. - the header, at the top, now contains a simple list of all wrapped types, which is then included in the wrapped_types of the module for proper code generation. - simplify the code generation : just generate_code. - all arguments are analyzed from sys.argv at the import of the wrap_generator module. In any case, the xx_desc.py will be called from the corresponding cmake command, hence with the right arguments. - Added a dependencies in my_module_B of wrap_test to show how to make the dependencies in the cmake file, if needed. --- cmake/FindPythonWrapperMacro.cmake | 4 +- cmake/TRIQSConfig.cmake.in | 6 ++- pytriqs/gf/local/gf_desc.py | 3 +- pytriqs/lattice/lattice_tools_desc.py | 3 +- pytriqs/operators/operators2_desc.py | 3 +- pytriqs/parameters/parameters_desc.py | 6 +-- .../random_generator/random_generator_desc.py | 3 +- .../py_converter_wrapper.mako.hpp | 4 ++ pytriqs/wrap_generator/wrap_generator.py | 46 +++++++++++++++---- pytriqs/wrap_test/CMakeLists.txt | 3 ++ pytriqs/wrap_test/my_moduleB_desc.py | 4 +- pytriqs/wrap_test/my_module_desc.py | 3 +- pytriqs/wrap_test/test_g_desc.py | 3 +- 13 files changed, 64 insertions(+), 27 deletions(-) diff --git a/cmake/FindPythonWrapperMacro.cmake b/cmake/FindPythonWrapperMacro.cmake index c3e9e657..ee6f0809 100644 --- a/cmake/FindPythonWrapperMacro.cmake +++ b/cmake/FindPythonWrapperMacro.cmake @@ -22,7 +22,9 @@ macro (triqs_python_extension ModuleName) ${CMAKE_SOURCE_DIR}/pytriqs/wrap_generator/wrapper.mako.cpp ${wrap_name} ${CMAKE_SOURCE_DIR}/pytriqs/wrap_generator/py_converter_wrapper.mako.hpp - ${converter_name} ) + ${converter_name} + ${CMAKE_BINARY_DIR}/include/pytriqs/converters/ + ) set_property (GLOBAL APPEND PROPERTY TRIQS_PY_CONVERTERS_TARGETS "python_wrap_${ModuleName}") diff --git a/cmake/TRIQSConfig.cmake.in b/cmake/TRIQSConfig.cmake.in index 229bff06..8a657ee4 100644 --- a/cmake/TRIQSConfig.cmake.in +++ b/cmake/TRIQSConfig.cmake.in @@ -95,7 +95,11 @@ macro (triqs_python_extension ModuleName) @CMAKE_INSTALL_PREFIX@/share/triqs/wrap_generator/wrapper.mako.cpp ${wrap_name} @CMAKE_INSTALL_PREFIX@/share/triqs/wrap_generator/py_converter_wrapper.mako.hpp - ${converter_name} ) + ${converter_name} + # after this, list of paths for the header generated by the python wrapper generator + @CMAKE_INSTALL_PREFIX@/include/pytriqs/converters/ + ${CMAKE_BINARY_DIR}/include/pytriqs/converters/ + ) #set_property (GLOBAL APPEND PROPERTY TRIQS_PY_CONVERTERS_TARGETS "python_wrap_${ModuleName}") diff --git a/pytriqs/gf/local/gf_desc.py b/pytriqs/gf/local/gf_desc.py index f849a230..4c85c8ea 100644 --- a/pytriqs/gf/local/gf_desc.py +++ b/pytriqs/gf/local/gf_desc.py @@ -473,6 +473,5 @@ module.add_function(name = "make_gf_from_inverse_fourier", signature="gf_view py_type c_to_py_type = {'void' : 'None', 'int' : 'int', 'long' : 'int', 'double' : "float", "std::string" : "str"} @@ -461,7 +466,7 @@ class module_ : - functions : dict : string -> function_. Modules functions. Key is the python name. - include_list : a list of files to include for compilation """ - wrapped_types = {} + wrapped_types = [] def __init__(self, full_name, doc = '') : self.full_name = full_name @@ -474,12 +479,12 @@ class module_ : self.using =[] self.python_functions = {} self.hidden_python_functions = {} - + self.module_path_list = [] + def add_class(self, cls): if cls.py_type in self.classes : raise IndexError, "The class %s already exists"%cls.py_type self.classes[cls.py_type] = cls - self.wrapped_types[cls.c_type] = cls - self.wrapped_types[cls.c_type_absolute] = cls # we can call is by its name or its absolute name + self.wrapped_types += [cls.c_type, cls.c_type_absolute] # we can call is by its name or its absolute name def add_function(self, **kw): if "name" in kw : @@ -499,6 +504,30 @@ class module_ : else : self.hidden_python_functions[name or f.__name__] = python_function(name or f.__name__, f) + def use_module(self,modulename) : + """ From the name of the module : + - add the header file generated for this module to the include list + - read this file, extract the list of wrapped_types, and add it to + the wrapped_type list + """ + f = None + print "argv", sys.argv + for path in module_path_list : + hppfile = path + '/' + modulename + '.hpp' + if os.path.exists(hppfile) : + f = open(hppfile ,'r') + break + if not f : raise RuntimeError, "Can not find the module %s.\n ... module_path_list = %s"%(modulename, self.module_path_list) + + while f.readline().strip() != "// WrappedTypeList" : + pass + l = f.readline()[3:] # // strip "// " + self.wrapped_types += eval(l) + self.add_include(hppfile) + print "Loading triqs wrapped module %s"%modulename + print " ... found C++ header file %s"%hppfile + print " ... found wrapped types %s"%l + def add_include(self, *path) : self.include_list.extend(path) @@ -527,7 +556,7 @@ class module_ : for n,f in class_.hidden_python_function.items() : self.add_python_function(f,name = n, hidden=True) - def generate_code(self, mako_template, wrap_file) : + def generate_wrapper_code(self, mako_template, wrap_file) : self.prepare_for_generation() tpl = Template(filename=mako_template) rendered = tpl.render(module=self, regular_type_if_view_else_type= regular_type_if_view_else_type, is_type_a_view = is_type_a_view) @@ -541,7 +570,8 @@ class module_ : with open(wrap_file,'w') as f: f.write(rendered) - #def generate_code_and_header(self, arglist) : - # self.generate_code(mako_template = arglist.argv[0], wrap_file = arglist.argv[1]) - # self.generate_py_converter_header(mako_template = arglist.argv[2], wrap_file = arglist.argv[3]) + def generate_code(self) : + self.generate_wrapper_code(mako_template = wrapper_mako, wrap_file = wrapper_target) + self.generate_py_converter_header(mako_template = header_mako, wrap_file = header_target) + diff --git a/pytriqs/wrap_test/CMakeLists.txt b/pytriqs/wrap_test/CMakeLists.txt index 9444e69f..07caa349 100644 --- a/pytriqs/wrap_test/CMakeLists.txt +++ b/pytriqs/wrap_test/CMakeLists.txt @@ -13,6 +13,9 @@ triqs_python_extension(my_moduleB wrap_test) triqs_python_extension(test_g wrap_test) # ??triqs_set_rpath_for_target(my_module) +# the module B must be generated AFTER my_module since it uses some wrapped types... +add_dependencies(python_wrap_my_moduleB python_wrap_my_module) + include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ) install (FILES ${CMAKE_SOURCE_DIR}/pytriqs/__init__.py.template DESTINATION "include/pytriqs/gf/local" RENAME __init__.py) diff --git a/pytriqs/wrap_test/my_moduleB_desc.py b/pytriqs/wrap_test/my_moduleB_desc.py index 88473050..f5e16506 100644 --- a/pytriqs/wrap_test/my_moduleB_desc.py +++ b/pytriqs/wrap_test/my_moduleB_desc.py @@ -2,11 +2,11 @@ from wrap_generator import * # The module mod = module_(full_name = "pytriqs.wrap_test.my_moduleB", doc = " Doc of my_module ") +mod.use_module('my_module') mod.add_include("") mod.add_include("") mod.add_function (name = "print_a2", signature = "void(A a)", doc = "DOC of print_a") -mod.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2]) -mod.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4]) +mod.generate_code() diff --git a/pytriqs/wrap_test/my_module_desc.py b/pytriqs/wrap_test/my_module_desc.py index 0f67d4e7..e58ded27 100644 --- a/pytriqs/wrap_test/my_module_desc.py +++ b/pytriqs/wrap_test/my_module_desc.py @@ -103,6 +103,5 @@ def f1(x,y): module.add_python_function(f1) if __name__ == '__main__' : - module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2]) - module.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4]) + module.generate_code() diff --git a/pytriqs/wrap_test/test_g_desc.py b/pytriqs/wrap_test/test_g_desc.py index 599d66b6..23c20a36 100644 --- a/pytriqs/wrap_test/test_g_desc.py +++ b/pytriqs/wrap_test/test_g_desc.py @@ -13,6 +13,5 @@ module.add_function (name = "pass_sgf", signature = "void (gf_view