Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions bindgen/CMakeLists.j2
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
cmake_minimum_required( VERSION 3.16 )
cmake_minimum_required( VERSION 3.17 )
project( {{ name }}
LANGUAGES CXX )

# Required if the platform's VTK links against MPI (cmake needs MPI::MPI_C)
enable_language( C )

find_package( Python REQUIRED COMPONENTS Development Interpreter)
find_package( Python REQUIRED COMPONENTS Development Interpreter )
message( STATUS "Python lib: ${Python_LIBRARIES}" )

find_package( fmt REQUIRED )
find_package( MPI QUIET )
find_package( VTK REQUIRED
COMPONENTS
WrappingPythonCore
Expand All @@ -13,21 +19,23 @@ find_package( VTK REQUIRED
CommonExecutionModel
freetype
)
message(STATUS "VTK ${VTK_VERSION} found")
find_package( pybind11 REQUIRED )
find_package( OpenCASCADE REQUIRED )

include_directories( ${PROJECT_SOURCE_DIR} )
file( GLOB CPP_FILES ${PROJECT_SOURCE_DIR}/*.cpp )

add_library( {{ name }} MODULE ${CPP_FILES} )
target_link_libraries( {{ name }} PRIVATE
target_link_libraries( {{ name }} PRIVATE
${OpenCASCADE_LIBRARIES}
Python::Module
pybind11::pybind11
pybind11::pybind11
VTK::WrappingPythonCore
VTK::RenderingCore
VTK::CommonDataModel
VTK::CommonExecutionModel
fmt::fmt
)
set_target_properties( {{ name }}
PROPERTIES
Expand All @@ -38,7 +46,7 @@ set_target_properties( {{ name }}

if(WIN32)
target_compile_options( {{ name }} PRIVATE /bigobj )
target_compile_definitions( {{ name }} PRIVATE
target_compile_definitions( {{ name }} PRIVATE
"Standard_EXPORT="
"Standard_EXPORTEXTERN="
"Standard_EXPORTEXTERNC=extern \"C\""
Expand Down
36 changes: 33 additions & 3 deletions bindgen/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ class ArgInfo:

arg_name: str
arg_type: str
arg_defualt: str
arg_default: str
arg_py_name: str = field(init=False)

def __post_init__(self):
Expand Down Expand Up @@ -648,7 +648,34 @@ def _underlying_type(
)

if decl.semantic_parent.kind != CursorKind.TRANSLATION_UNIT:
rv = "typename " + rv
# Add class prefix if it is missing from rv (e.g. spelling gives bare name)
parent = decl.semantic_parent.displayname or decl.semantic_parent.spelling
if parent and '::' not in rv:
rv = parent + '::' + rv
# Strip misplaced typename from middle (e.g. X<T>::typename X<T>::Y)
rv = rv.replace('::typename ', '::')
if not rv.startswith("typename "):
rv = "typename " + rv

# handle nested POD typedefs (e.g. function pointer typedefs inside a class)
elif typ.kind == TypeKind.TYPEDEF and typ.is_pod():
decl = typ.get_declaration()
if decl and decl.semantic_parent.kind not in (
CursorKind.TRANSLATION_UNIT, CursorKind.NAMESPACE
):
parent = decl.semantic_parent.displayname or decl.semantic_parent.spelling
if parent and '::' not in rv:
rv = parent + '::' + rv

# handle nested ENUMs and RECORDs (structs/classes defined inside a class)
elif typ.kind in (TypeKind.ENUM, TypeKind.RECORD):
decl = typ.get_declaration()
if decl and decl.semantic_parent.kind not in (
CursorKind.TRANSLATION_UNIT, CursorKind.NAMESPACE
):
parent = decl.semantic_parent.displayname or decl.semantic_parent.spelling
if parent and '::' not in rv:
rv = parent + '::' + rv

# additional postprocessing related to templates
if typ.kind == TypeKind.UNEXPOSED:
Expand Down Expand Up @@ -691,7 +718,10 @@ def _default_value(self, cur: Cursor) -> Optional[str]:

# handle default initalization of complex types
if "{ }" == rv:
rv = f"{get_named_type(cur.type).spelling}{rv}"
type_name = get_named_type(cur.type).spelling
if type_name.startswith("const "):
type_name = type_name[len("const "):]
rv = f"{type_name}{rv}"

return rv

Expand Down
54 changes: 39 additions & 15 deletions bindgen/macros.j2
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
{% for arg in f.args %}{{ arg.arg_type }}{{ "," if not loop.last }}{% endfor %}
{%- endmacro -%}

{%- macro argtypes_for_template(template, f) -%}
{% for arg in f.args %}{{- arg_type_with_template_params(template, arg.arg_type) -}}{{ "," if not loop.last }}{% endfor %}
{%- endmacro -%}

{%- macro argtypes_names(f) -%}
{% for arg in f.args %}{{ arg.arg_type }} {{ arg.arg_name }}{{ "," if not loop.last }}{% endfor %}
{%- endmacro -%}
Expand All @@ -41,7 +45,7 @@
{% for arg in f.args if is_byref(arg.arg_type) %}{{ arg.arg_type[:-1] }} {{ arg.arg_name }};
{% endfor %}
{% for arg in f.args if is_byref_smart_ptr(arg.arg_type) -%}
{{ arg.arg_type[:-1] }} {{ arg.name }}_ptr; {{ arg.name }}_ptr = &{{arg.name}};
{{ arg.arg_type[:-1] }} {{ arg.arg_name }}_ptr; {{ arg.arg_name }}_ptr = &{{arg.arg_name}};
{% endfor %}
{%- endmacro -%}

Expand All @@ -53,30 +57,42 @@
{% for arg in f.args if is_byref(arg.arg_type) %}{{ arg.arg_name }}{{ "," if not loop.last }}{% endfor %}
{%- endmacro -%}

{%- macro default_value(arg_type, arg_default) -%}
{%- if arg_default in ('NULL', '0', 'nullptr') and '*' in arg_type -%}
py::none()
{%- else -%}
static_cast<{{arg_type}}>({{arg_default}})
{%- endif -%}
{%- endmacro -%}

{%- macro argnames(f) -%}
{% for arg in f.args %} {{ "," if loop.first }} py::arg("{{arg.arg_py_name if arg.arg_py_name!='' else 'arg'}}"){% if d %}=static_cast<{{arg.arg_type}}>({{arg.arg_default}}){% endif %}{{ "," if not loop.last }}{% endfor %}
{% for arg in f.args %} {{ "," if loop.first }} py::arg("{{arg.arg_py_name if arg.arg_py_name!='' else 'arg'}}"){% if arg.arg_default %}={{ default_value(arg.arg_type, arg.arg_default) }}{% endif %}{{ "," if not loop.last }}{% endfor %}
{%- endmacro -%}

{%- macro argnames_template(template, f) -%}
{%- for arg in f.args -%} {{ "," if loop.first }}
{%- set ns = namespace(t=arg.arg_type) -%}
{%- if arg.arg_type.startswith(template.name) -%}
{% set ns.t = template.name + '<' + template.type_params|map('get',1)|join(', ') + '>' + arg.arg_type.split(template.name)[-1] %}
{%- endif -%}
py::arg("{{arg.arg_py_name if arg.arg_py_name != '' else 'arg'}}"){% if arg.arg_default %}=static_cast<{{arg_type_with_template_params(template, arg.arg_type)}}>({{arg.arg_default}}){% endif %}{{ ", " if not loop.last }}
{%- for arg in f.args -%}
{{ "," if loop.first }} py::arg("{{arg.arg_py_name if arg.arg_py_name != '' else 'arg'}}")
{%- if arg.arg_default -%}
={{ default_value(arg_type_with_template_params(template, arg.arg_type), arg.arg_default) }}
{%- endif -%}
{{ ", " if not loop.last }}
{%- endfor -%}
{%- endmacro -%}

{%- macro arg_type_with_template_params(template, t) -%}
{%- set ns = namespace(t=t) -%}
{%- if t.startswith(template.name+'::') -%}
{% set ns.t = 'typename ' + template.name + '<' + template.type_params|map('get',1)|join(', ') + '>' + t.split(template.name)[-1] %}
{%- set targs = '<' + template.type_params|map('get',1)|join(', ') + '>' -%}
{%- set qualified = template.name + targs -%}
{%- if ns.t.startswith(template.name+'::') -%}
{%- set ns.t = 'typename ' + qualified + ns.t[template.name|length:] -%}
{%- elif template.name+'<' in ns.t and '::' in ns.t.split(template.name, 1)[1] and 'typename' not in ns.t -%}
{%- set ns.t = ns.t.replace(template.name+'<', 'typename '+template.name+'<', 1) -%}
{%- endif -%}
{{ ns.t }}
{%- endmacro -%}

{%- macro argnames_not_byref(f) -%}
{% for arg in f.args if not is_byref(arg.arg_type) %} {{ "," if loop.first }} py::arg("{{arg.arg_py_name if arg.arg_py_name != '' else 'arg'}}"){% if arg.arg_default %}=static_cast<{{arg.arg_type}}>({{arg.arg_default}}){% endif %}{{ "," if not loop.last }}{% endfor %}
{% for arg in f.args if not is_byref(arg.arg_type) %} {{ "," if loop.first }} py::arg("{{arg.arg_py_name if arg.arg_py_name != '' else 'arg'}}"){% if arg.arg_default %}={{ default_value(arg.arg_type, arg.arg_default) }}{% endif %}{{ "," if not loop.last }}{% endfor %}
{%- endmacro -%}

{%- macro handle_results_ptr_byref(f) -%}
Expand Down Expand Up @@ -106,7 +122,14 @@
{%- endmacro -%}

{%- macro template_return_type(cls,f) -%}
{% if f.return_type.startswith(cls.name+'::') %}typename {{cls.name}}{{ template_args(cls) }}::{{ f.return_type.split('::')[1]}}{% else %}{{f.return_type}}{% endif %}
{%- set rt = f.return_type -%}
{%- if rt.startswith(cls.name+'::') -%}
typename {{cls.name}}{{ template_args(cls) }}::{{ rt.split('::')[1] }}
{%- elif cls.name+'<' in rt and '::' in rt.split(cls.name, 1)[1] and 'typename' not in rt -%}
{{ rt.replace(cls.name+'<', 'typename '+cls.name+'<', 1) }}
{%- else -%}
{{ rt }}
{%- endif -%}
{%- endmacro -%}

{%- macro template_method_pointer(cls,f) -%}
Expand Down Expand Up @@ -139,7 +162,7 @@

{% for super in c.superclasses %}
{% set p = all_classes.get(super,None) %}{% if p %}
{% for m in p.methods %}{% if m.pure_virtual and m.name not in c.methods_dict and not m.full_name in methods %}
{% for m in p.methods %}{% if m.pure_virtual and not m.full_name in methods %}
{{ prototype(m) }} override { {{pybind_overload(p,m)}} };
{% do methods.append(m.full_name) %}
{% endif %}{% endfor %}
Expand All @@ -154,7 +177,7 @@

{% for super in c.superclasses %}
{% set p = all_classes.get(super,None) %}{% if p %}
{% for m in p.protected_virtual_methods %}{% if m.name not in c.protected_virtual_methods_dict and not m.full_name in methods %}
{% for m in p.protected_virtual_methods %}{% if not m.full_name in methods %}
{{ prototype(m) }} override { {{pybind_overload(p,m)}} };
{% do methods.append(m.full_name) %}
{% endif %}{% endfor %}
Expand All @@ -164,11 +187,12 @@
// private pure virtual
{% for m in c.private_virtual_methods %}
{{ prototype(m) }} override { {{pybind_overload(c,m)}} };
{% do methods.append(m.full_name) %}
{% endfor %}

{% for super in c.superclasses %}
{% set p = all_classes.get(super,None) %}{% if p %}
{% for m in p.private_virtual_methods %}{% if m.name not in c.private_virtual_methods_dict and not m.full_name in methods %}
{% for m in p.private_virtual_methods %}{% if not m.full_name in methods %}
{{ prototype(m) }} override { {{pybind_overload(p,m)}} };
{% do methods.append(m.full_name) %}
{% endif %}{% endfor %}
Expand Down
4 changes: 2 additions & 2 deletions bindgen/template_templates.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "macros.j2" import cls_name,pointer,super,argtypes,argnames,method_pointer,
{% from "macros.j2" import cls_name,pointer,super,argtypes_for_template,argnames,method_pointer,
static_method_pointer,function_pointer,template_args_typename,template_args,
template_method_pointer,template_static_method_pointer, template_pointer, argnames_template %}
#pragma once
Expand Down Expand Up @@ -45,7 +45,7 @@ void register_template_{{t.name}}(py::object &m, const char *name){
static_cast<py::class_<{{t.name}}{{template_args(t)}} {{template_pointer(t)}} {{super(t,all_classes,all_typedefs)}}>>(m.attr(name))
{% for i, con in enumerate(t.constructors) %}
{% if not i in module_settings['Templates'].get(t.name,{}).get('exclude_constructors',[]) %}
.def(py::init< {{ argtypes(con) }} >() {{argnames_template(t, con)}} )
.def(py::init< {{ argtypes_for_template(t, con) }} >() {{argnames_template(t, con)}} )
{% endif %}
{% endfor %}
{% for m in t.methods if not references_inner(t.name,m)%}
Expand Down
12 changes: 10 additions & 2 deletions bindgen/translation_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ def parse_tu(
src = src[1:]

dummy_code = (
f"{parsing_header}\n{platform_parsing_header}\n{
tu_parsing_header}\n{src}"
f"{parsing_header}\n{platform_parsing_header}\n{tu_parsing_header}\n{src}"
)

# On FreeBSD, libclang's internal path guessing picks wrong system headers.
# Disable it and use -isystem with the paths from ocp.toml [FreeBSD] includes.
if target_platform == "FreeBSD":
system_dirs = {inc.rstrip('/') for inc in platform_includes}
clean_args = [a for a in args if not (a.startswith('-I') and a[2:].rstrip('/') in system_dirs)]
isystem_args = [flag for inc in platform_includes for flag in ("-isystem", inc)]
args = ["-nostdinc", "-nostdinc++"] + isystem_args + clean_args

tr_unit = ix.parse(
"dummy.cxx",
args,
Expand Down
Loading
Loading