-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binding flow #28
Draft
diivm
wants to merge
9
commits into
PointCloudLibrary:master
Choose a base branch
from
diivm:devel/bind
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Binding flow #28
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb04e7f
[cmake_frontend.py] add get_library_targets; add some documentation
diivm 31fd0b6
[cmake_frontend.py] change parameter optionality; ret type of functions
diivm 6a5933d
[bind.py] bind.py
diivm 64bea42
[cmake_frontend.py] change parameter optionality; return type
diivm 279fa3c
[parse.py] add support for inclusion sources
diivm 29c9b0b
fixup! [bind.py] bind.py
diivm 2502c9c
fixup! [bind.py] bind.py
diivm 7112bbc
fixup! [bind.py] bind.py
diivm 7bdcbcd
[init_bindings.sh] add file
diivm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
from pathlib import Path | ||
|
||
from clang_bind.cmake_frontend import CMakeFileAPI, CompilationDatabase | ||
from clang_bind.parse import Parse | ||
|
||
|
||
class Bind: | ||
"""Class to bind C++ targets. | ||
|
||
: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_targets: List of C++ targets to bind, defaults to []: bind all. | ||
:type cpp_targets: list, optional | ||
:param allow_inclusions_from_other_targets: Allow inclusions from other targets, which are not specified in cpp_targets, defaults to True | ||
:type allow_inclusions_from_other_targets: bool, optional | ||
""" | ||
|
||
def __init__( | ||
self, | ||
source_dir, | ||
build_dir, | ||
output_dir, | ||
output_module_name, | ||
cpp_targets=[], | ||
allow_inclusions_from_other_targets=True, | ||
): | ||
all_cpp_targets = CMakeFileAPI(build_dir).get_library_targets() | ||
if not cpp_targets: | ||
cpp_targets = all_cpp_targets # bind all C++ targets | ||
|
||
all_inclusion_sources = [] | ||
for target in all_cpp_targets: # for all C++ targets, populate the variable | ||
sources = CMakeFileAPI(build_dir).get_sources(target) # target's sources | ||
cpp_sources = list( | ||
filter(lambda source: source.endswith(".cpp"), sources) | ||
) # sources ending with .cpp | ||
all_inclusion_sources += list( | ||
set(sources) - set(cpp_sources) | ||
) # other sources like .h and .hpp files | ||
|
||
self.binding_db = {} # binding database | ||
for target in cpp_targets: | ||
sources = CMakeFileAPI(build_dir).get_sources(target) # target's sources | ||
cpp_sources = list( | ||
filter(lambda source: source.endswith(".cpp"), sources) | ||
) # sources ending with .cpp | ||
inclusion_sources = ( | ||
all_inclusion_sources | ||
if allow_inclusions_from_other_targets | ||
else list(set(sources) - set(cpp_sources)) | ||
) # other sources like .h and .hpp files | ||
|
||
self.binding_db[target] = { | ||
"source_dir": source_dir, # source dir containing C++ targets | ||
"output_dir": output_dir, # output dir | ||
"inclusion_sources": inclusion_sources, # inclusions for the target | ||
"files": [ # list of files' information | ||
{ | ||
"source": cpp_source, | ||
"compiler_arguments": CompilationDatabase( | ||
build_dir | ||
).get_compilation_arguments(cpp_source), | ||
} | ||
for cpp_source in cpp_sources | ||
], | ||
} | ||
|
||
def _parse(self): | ||
"""For all input files, get the parsed tree and update the db.""" | ||
for target in self.binding_db.values(): | ||
source_dir = target.get("source_dir") | ||
inclusion_sources = [ | ||
str(Path(source_dir, inclusion_source)) | ||
for inclusion_source in target.get("inclusion_sources") | ||
] # string full paths of inclusion sources | ||
|
||
for file in target.get("files"): | ||
parsed_tree = Parse( | ||
Path(source_dir, file.get("source")), | ||
inclusion_sources, | ||
file.get("compiler_arguments"), | ||
).get_tree() | ||
|
||
file.update({"parsed_tree": parsed_tree}) # update db | ||
|
||
# Debugging: | ||
# | ||
# - To print the trees: | ||
# parsed_tree.show() | ||
# | ||
# - To save the JSONs: | ||
# import json | ||
# json_output_path = Path( | ||
# target.get("output_dir"), | ||
# "parsed", | ||
# file.get("source").replace(".cpp", ".json"), | ||
# ) | ||
# json_output_path.parent.mkdir(parents=True, exist_ok=True) | ||
# with open(json_output_path, 'w') as f: | ||
# json.dump(parsed_tree.to_dict(), f, indent=4) | ||
|
||
def bind(self): | ||
"""Function to bind the input files.""" | ||
self._parse() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about STATIC build?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't see it in
pcl
, so didn't get to test how it is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PCL_SHARED_LIBS
can be used to choose between shared and static library for pclThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please provide a reference link or example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmake -DPCL_SHARED_LIBS:BOOL=FALSE
or similar