Skip to content

Commit

Permalink
[bind.py] bind.py
Browse files Browse the repository at this point in the history
  • Loading branch information
diivm committed Aug 15, 2021
1 parent 31fd0b6 commit 8e4b6cd
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions clang_bind/bind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from pathlib import Path

from clang_bind.cmake_frontend import CMakeFileAPI


class Bind:
"""Class to bind cpp modules.
:param source_dir: Source dir of cpp library.
:type source_dir: str
:param build_dir: CMake build dir of cpp library, containing .cmake dir or compile_commands.json
:type build_dir: str
:param output_dir: Output dir
:type output_dir: str
:param output_module_name: Module name in python
:type output_module_name: str
:param cpp_modules: List of cpp modules to bind, defaults to []: bind all.
:type cpp_modules: list, optional
:param allow_inclusions_from_other_modules: Allow inclusions from other modules, which are not specified in cpp_modules, defaults to True
:type allow_inclusions_from_other_modules: bool, optional
:param use_compilation_db: Use compile_commands.json instead of CMake file API, defaults to False
:type use_compilation_db: bool, optional
"""

def __init__(
self,
source_dir,
build_dir,
output_dir,
output_module_name,
cpp_modules=[],
allow_inclusions_from_other_modules=True,
use_compilation_db=False,
):
self.source_dir = source_dir
self.build_dir = build_dir
self.output_dir = output_dir
# self.output_module_name = output_module_name # TODO
self.cpp_modules = cpp_modules
self.allow_inclusions_from_other_modules = allow_inclusions_from_other_modules
self.use_compilation_db = use_compilation_db

if self.use_compilation_db:
raise NotImplementedError() # TODO

self.inclusion_sources = []
self.binding_db = {}

self._set_binding_db()
self._set_inclusion_sources()

def _set_binding_db(self):
"""Sets binding_db variable."""
if not self.cpp_modules:
self.cpp_modules = CMakeFileAPI(self.build_dir).get_library_targets()

for module in self.cpp_modules:
sources = CMakeFileAPI(self.build_dir).get_sources(module)
cpp_sources = list(filter(lambda x: x.endswith(".cpp"), sources))
self.binding_db[module] = [
{
"source_path": Path(self.source_dir, cpp_source),
"output_path": Path(self.output_dir, cpp_source),
}
for cpp_source in cpp_sources
]

def _set_inclusion_sources(self):
"""Sets inclusion_sources variable."""
if self.allow_inclusions_from_other_modules:
cpp_modules_to_search = CMakeFileAPI(self.build_dir).get_library_targets()
else:
cpp_modules_to_search = self.cpp_modules

for module in cpp_modules_to_search:
sources = CMakeFileAPI(self.build_dir).get_sources(module)
cpp_sources = list(filter(lambda x: x.endswith(".cpp"), sources))
self.inclusion_sources += list(set(sources) - set(cpp_sources))

0 comments on commit 8e4b6cd

Please sign in to comment.