|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from clang_bind.cmake_frontend import CMakeFileAPI |
| 4 | + |
| 5 | + |
| 6 | +class Bind: |
| 7 | + """Class to bind cpp modules. |
| 8 | +
|
| 9 | + :param source_dir: Source dir of cpp library. |
| 10 | + :type source_dir: str |
| 11 | + :param build_dir: CMake build dir of cpp library, containing .cmake dir or compile_commands.json |
| 12 | + :type build_dir: str |
| 13 | + :param output_dir: Output dir |
| 14 | + :type output_dir: str |
| 15 | + :param output_module_name: Module name in python |
| 16 | + :type output_module_name: str |
| 17 | + :param cpp_modules: List of cpp modules to bind, defaults to []: bind all. |
| 18 | + :type cpp_modules: list, optional |
| 19 | + :param allow_inclusions_from_other_modules: Allow inclusions from other modules, which are not specified in cpp_modules, defaults to True |
| 20 | + :type allow_inclusions_from_other_modules: bool, optional |
| 21 | + :param use_compilation_db: Use compile_commands.json instead of CMake file API, defaults to False |
| 22 | + :type use_compilation_db: bool, optional |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + source_dir, |
| 28 | + build_dir, |
| 29 | + output_dir, |
| 30 | + output_module_name, |
| 31 | + cpp_modules=[], |
| 32 | + allow_inclusions_from_other_modules=True, |
| 33 | + use_compilation_db=False, |
| 34 | + ): |
| 35 | + self.source_dir = source_dir |
| 36 | + self.build_dir = build_dir |
| 37 | + self.output_dir = output_dir |
| 38 | + # self.output_module_name = output_module_name # TODO |
| 39 | + self.cpp_modules = cpp_modules |
| 40 | + self.allow_inclusions_from_other_modules = allow_inclusions_from_other_modules |
| 41 | + self.use_compilation_db = use_compilation_db |
| 42 | + |
| 43 | + if self.use_compilation_db: |
| 44 | + raise NotImplementedError() # TODO |
| 45 | + |
| 46 | + self.inclusion_sources = [] |
| 47 | + self.binding_db = {} |
| 48 | + |
| 49 | + self._set_binding_db() |
| 50 | + self._set_inclusion_sources() |
| 51 | + |
| 52 | + def _set_binding_db(self): |
| 53 | + """Sets binding_db variable.""" |
| 54 | + if not self.cpp_modules: |
| 55 | + self.cpp_modules = CMakeFileAPI(self.build_dir).get_library_targets() |
| 56 | + |
| 57 | + for module in self.cpp_modules: |
| 58 | + sources = CMakeFileAPI(self.build_dir).get_sources(module) |
| 59 | + cpp_sources = list(filter(lambda x: x.endswith(".cpp"), sources)) |
| 60 | + self.binding_db[module] = [ |
| 61 | + { |
| 62 | + "source_path": Path(self.source_dir, cpp_source), |
| 63 | + "output_path": Path(self.output_dir, cpp_source), |
| 64 | + } |
| 65 | + for cpp_source in cpp_sources |
| 66 | + ] |
| 67 | + |
| 68 | + def _set_inclusion_sources(self): |
| 69 | + """Sets inclusion_sources variable.""" |
| 70 | + if self.allow_inclusions_from_other_modules: |
| 71 | + cpp_modules_to_search = CMakeFileAPI(self.build_dir).get_library_targets() |
| 72 | + else: |
| 73 | + cpp_modules_to_search = self.cpp_modules |
| 74 | + |
| 75 | + for module in cpp_modules_to_search: |
| 76 | + sources = CMakeFileAPI(self.build_dir).get_sources(module) |
| 77 | + cpp_sources = list(filter(lambda x: x.endswith(".cpp"), sources)) |
| 78 | + self.inclusion_sources += list(set(sources) - set(cpp_sources)) |
0 commit comments