Skip to content

Commit

Permalink
Merge pull request #58 from Chaste/4-rename-module-cpp
Browse files Browse the repository at this point in the history
Naming consistency
  • Loading branch information
kwabenantim authored Sep 26, 2024
2 parents d727ddc + ce6c51e commit d0777f6
Show file tree
Hide file tree
Showing 25 changed files with 108 additions and 118 deletions.
23 changes: 17 additions & 6 deletions cppwg/input/class_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ def update_from_ns(self, source_ns: "namespace_t") -> None: # noqa: F821

self.decls.append(class_decl)

# Update the class source file if not already set
if not self.source_file_full_path:
self.source_file_full_path = self.decls[0].location.file_name
self.source_file = os.path.basename(self.source_file_full_path)

# Update the base class declarations
self.base_decls = [
base.related_class for decl in self.decls for base in decl.bases
Expand All @@ -229,13 +234,19 @@ def update_from_source(self, source_file_paths: List[str]) -> None:
if self.excluded:
return

# Map class to a source file, assuming the file name is the class name
for file_path in source_file_paths:
file_name = os.path.basename(file_path)
if self.name == os.path.splitext(file_name)[0]:
self.source_file_full_path = file_path
if self.source_file is None:
# Attempt to map class to a source file
if self.source_file_full_path:
self.source_file = os.path.basename(self.source_file_full_path)
else:
for file_path in source_file_paths:
file_name = os.path.basename(file_path)
# Match file name if set
if self.source_file == file_name:
self.source_file_full_path = file_path
# Match class name, assuming the file name is the class name
elif self.name == os.path.splitext(file_name)[0]:
self.source_file = file_name
self.source_file_full_path = file_path

# Extract template args from the source file
self.extract_templates_from_source()
Expand Down
4 changes: 2 additions & 2 deletions cppwg/input/cpp_type_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def __init__(self, name: str, type_config: Optional[Dict[str, Any]] = None):
super().__init__(name)

self.module_info: Optional["ModuleInfo"] = None # noqa: F821
self.source_file_full_path: Optional[str] = None
self.source_file: Optional[str] = None
self.source_file_full_path: str = ""
self.source_file: str = ""
self.name_override: Optional[str] = None
self.template_signature: Optional[str] = None
self.template_params: Optional[List[str]] = None
Expand Down
4 changes: 3 additions & 1 deletion cppwg/input/module_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def compare(a: CppClassInfo, b: CppClassInfo) -> int:
cache[(b, a)] = 0
return 0

self.class_info_collection.sort(key=lambda x: x.name)
self.class_info_collection.sort(
key=lambda x: (os.path.dirname(x.source_file_full_path), x.name)
)

i = 0
n = len(self.class_info_collection)
Expand Down
3 changes: 3 additions & 0 deletions cppwg/input/package_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def collect_source_headers(self, restricted_paths: List[str]) -> None:
logger.error(f"No header files found in source root: {self.source_root}")
raise FileNotFoundError()

# Sort by filename
self.source_hpp_files.sort(key=lambda x: os.path.basename(x))

def update_from_source(self) -> None:
"""
Update modules with information from the source headers.
Expand Down
4 changes: 4 additions & 0 deletions cppwg/parsers/package_info_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ def parse(self) -> PackageInfo:
class_info.module_info = module_info
module_info.class_info_collection.append(class_info)

module_info.class_info_collection.sort(key=lambda x: x.name)

# Parse the free function data and create free function info objects.
# Note: if module_config["use_all_free_functions"] == True, free function
# info objects will be added later after parsing the C++ source code.
Expand Down Expand Up @@ -243,6 +245,8 @@ def parse(self) -> PackageInfo:
free_function_info
)

module_info.free_function_info_collection.sort(key=lambda x: x.name)

# Parse the variable data
if not module_config["use_all_variables"]:
for raw_variable_info in module_config["variables"]:
Expand Down
2 changes: 1 addition & 1 deletion cppwg/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
CPPWG_ALL_STRING = "CPPWG_ALL"

CPPWG_EXT = "cppwg"
CPPWG_HEADER_COLLECTION_FILENAME = "wrapper_header_collection.hpp"
CPPWG_HEADER_COLLECTION_FILENAME = f"wrapper_header_collection.{CPPWG_EXT}.hpp"

CPPWG_TRUE_STRINGS = ["ON", "YES", "Y", "TRUE", "T"]
CPPWG_FALSE_STRINGS = ["OFF", "NO", "N", "FALSE", "F"]
Expand Down
77 changes: 32 additions & 45 deletions cppwg/writers/header_collection_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class CppHeaderCollectionWriter:
The package information
wrapper_root : str
The output directory for the generated wrapper code
hpp_collection_filepath : str
hpp_collection_file : str
The path to save the header collection file to
hpp_collection_string : str
hpp_collection : str
The output string that gets written to the header collection file
class_dict : Dict[str, CppClassInfo]
A dictionary of all class info objects
Expand All @@ -37,13 +37,13 @@ def __init__(
self,
package_info: PackageInfo,
wrapper_root: str,
hpp_collection_filepath: str,
hpp_collection_file: str,
):

self.package_info: PackageInfo = package_info
self.wrapper_root: str = wrapper_root
self.hpp_collection_filepath: str = hpp_collection_filepath
self.hpp_collection_string: str = ""
self.hpp_collection_file: str = hpp_collection_file
self.hpp_collection: str = ""

# For convenience, collect all class and free function info into dicts keyed by name
self.class_dict: Dict[str, CppClassInfo] = {}
Expand Down Expand Up @@ -73,24 +73,23 @@ def should_include_all(self) -> bool:
def write(self) -> None:
"""Generate the header file output string and write it to file."""
# Add the top prefix text
self.hpp_collection_string += self.package_info.prefix_text + "\n"
self.hpp_collection += self.package_info.prefix_text + "\n"

# Add opening header guard
self.hpp_collection_string += f"#ifndef {self.package_info.name}_HEADERS_HPP_\n"
self.hpp_collection_string += f"#define {self.package_info.name}_HEADERS_HPP_\n"
self.hpp_collection += f"#ifndef {self.package_info.name}_HEADERS_HPP_\n"
self.hpp_collection += f"#define {self.package_info.name}_HEADERS_HPP_\n"

self.hpp_collection_string += "\n// Includes\n"
self.hpp_collection += "\n// Includes\n"

included_files = set() # Keep track of included files to avoid duplicates
seen_files = set() # Keep track of included files to avoid duplicates

if self.should_include_all():
# Include all the headers
for hpp_filepath in self.package_info.source_hpp_files:
hpp_filename = os.path.basename(hpp_filepath)

if hpp_filename not in included_files:
self.hpp_collection_string += f'#include "{hpp_filename}"\n'
included_files.add(hpp_filename)
for filepath in self.package_info.source_hpp_files:
filename = os.path.basename(filepath)
if filename not in seen_files:
self.hpp_collection += f'#include "{filename}"\n'
seen_files.add(filename)

else:
# Include specific headers needed by classes
Expand All @@ -100,30 +99,20 @@ def write(self) -> None:
if class_info.excluded:
continue

hpp_filename = None

if class_info.source_file:
hpp_filename = class_info.source_file

elif class_info.source_file_full_path:
hpp_filename = os.path.basename(
class_info.source_file_full_path
)

if hpp_filename and hpp_filename not in included_files:
self.hpp_collection_string += f'#include "{hpp_filename}"\n'
included_files.add(hpp_filename)
filename = class_info.source_file
if filename and filename not in seen_files:
self.hpp_collection += f'#include "{filename}"\n'
seen_files.add(filename)

# Include specific headers needed by free functions
for free_function_info in module_info.free_function_info_collection:
if free_function_info.source_file_full_path:
hpp_filename = os.path.basename(
filename = os.path.basename(
free_function_info.source_file_full_path
)

if hpp_filename not in included_files:
self.hpp_collection_string += f'#include "{hpp_filename}"\n'
included_files.add(hpp_filename)
if filename not in seen_files:
self.hpp_collection += f'#include "{filename}"\n'
seen_files.add(filename)

# Add the template instantiations e.g. `template class Foo<2,2>;`
# and typdefs e.g. `typedef Foo<2,2> Foo_2_2;`
Expand All @@ -150,19 +139,17 @@ def write(self) -> None:
template_instantiations += f"template class {cpp_name};\n"
template_typedefs += f" typedef {cpp_name} {py_name};\n"

self.hpp_collection_string += "\n// Instantiate Template Classes\n"
self.hpp_collection_string += template_instantiations
self.hpp_collection += "\n// Instantiate Template Classes\n"
self.hpp_collection += template_instantiations

self.hpp_collection_string += "\n// Typedefs for nicer naming\n"
self.hpp_collection_string += "namespace cppwg\n{\n"
self.hpp_collection_string += template_typedefs
self.hpp_collection_string += "} // namespace cppwg\n"
self.hpp_collection += "\n// Typedefs for nicer naming\n"
self.hpp_collection += "namespace cppwg\n{\n"
self.hpp_collection += template_typedefs
self.hpp_collection += "} // namespace cppwg\n"

# Add closing header guard
self.hpp_collection_string += (
f"\n#endif // {self.package_info.name}_HEADERS_HPP_\n"
)
self.hpp_collection += f"\n#endif // {self.package_info.name}_HEADERS_HPP_\n"

# Write the header collection string to file
with open(self.hpp_collection_filepath, "w") as hpp_file:
hpp_file.write(self.hpp_collection_string)
with open(self.hpp_collection_file, "w") as hpp_file:
hpp_file.write(self.hpp_collection)
2 changes: 1 addition & 1 deletion cppwg/writers/method_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def generate_virtual_override_wrapper(self) -> str:
if i == 0:
arg_name_list.append(f"{arg.name}")
else:
arg_name_list.append(" "*12 + f"{arg.name}")
arg_name_list.append(" " * 12 + f"{arg.name}")

arg_string = ", ".join(arg_list) # e.g. "int a, bool b, double c"
arg_name_string = ",\n".join(arg_name_list) # e.g. "a,\n b,\n c"
Expand Down
6 changes: 4 additions & 2 deletions cppwg/writers/module_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def write_module_wrapper(self) -> None:

# Format module name as _packagename_modulename
full_module_name = (
"_" + self.module_info.package_info.name + "_" + self.module_info.name
f"_{self.module_info.package_info.name}_{self.module_info.name}"
)

# Create the pybind11 module
Expand Down Expand Up @@ -132,7 +132,9 @@ def write_module_wrapper(self) -> None:
if not os.path.isdir(module_dir):
os.makedirs(module_dir)

module_cpp_file = os.path.join(module_dir, self.module_info.name + ".main.cpp")
module_cpp_file = os.path.join(
module_dir, f"{full_module_name}.main.{CPPWG_EXT}.cpp"
)

with open(module_cpp_file, "w") as out_file:
out_file.write(cpp_string)
Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/geometry/Point_2.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "Point_2.cppwg.hpp"

Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/geometry/Point_3.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "Point_3.cppwg.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Do not modify this file directly.

#include <pybind11/pybind11.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"
#include "Point_2.cppwg.hpp"
#include "Point_3.cppwg.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Do not modify this file directly.

#include <pybind11/pybind11.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

namespace py = pybind11;

Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/mesh/AbstractMesh_2_2.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "AbstractMesh_2_2.cppwg.hpp"

Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/mesh/AbstractMesh_3_3.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "AbstractMesh_3_3.cppwg.hpp"

Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/mesh/ConcreteMesh_2.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "ConcreteMesh_2.cppwg.hpp"

Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/mesh/ConcreteMesh_3.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "ConcreteMesh_3.cppwg.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Do not modify this file directly.

#include <pybind11/pybind11.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"
#include "AbstractMesh_2_2.cppwg.hpp"
#include "AbstractMesh_3_3.cppwg.hpp"
#include "ConcreteMesh_2.cppwg.hpp"
Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/primitives/Cuboid.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "Cuboid.cppwg.hpp"

Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/primitives/Rectangle.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "Rectangle.cppwg.hpp"

Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/primitives/Shape_2.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "Shape_2.cppwg.hpp"

Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/wrapper/primitives/Shape_3.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"

#include "Shape_3.cppwg.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Do not modify this file directly.

#include <pybind11/pybind11.h>
#include "wrapper_header_collection.hpp"
#include "wrapper_header_collection.cppwg.hpp"
#include "Shape_2.cppwg.hpp"
#include "Shape_3.cppwg.hpp"
#include "Rectangle.cppwg.hpp"
Expand Down
Loading

0 comments on commit d0777f6

Please sign in to comment.