Skip to content

Commit

Permalink
[sourcegen] Add includes
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Jan 1, 2025
1 parent b9b2bd2 commit 5e14309
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
17 changes: 12 additions & 5 deletions interfaces/sourcegen/sourcegen/clib/_CLibSourceGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def param(item: Param):
def _handle_crosswalk(self, what: str, crosswalk: dict, derived: list[str]) -> str:
"""Crosswalk for object handle."""
cabinet = None
classes = self._config.cabinets + derived
classes = list(self._config.includes.keys()) + derived
for base in classes:
ret_type = what.replace(f"<{base}>", "<T>")
if ret_type in crosswalk:
Expand Down Expand Up @@ -107,7 +107,7 @@ def _ret_crosswalk(
return returns, buffer

if "shared_ptr" in what:
# check for crosswalk with object from cabinets
# check for crosswalk with object from includes
handle = self._handle_crosswalk(
what, self._config.ret_type_crosswalk, derived)
returns = Param(
Expand Down Expand Up @@ -425,7 +425,8 @@ def _write_header(self, header: HeaderFile) -> None:

def _write_implementation(self, header: HeaderFile) -> None:
"""Parse header specification and generate implementation file."""
loader = Environment(loader=BaseLoader)
loader = Environment(loader=BaseLoader, trim_blocks=True, lstrip_blocks=True)

filename = header.output_name(suffix=".cpp", auto="3")
_logger.info(f" scaffolding {filename.name!r}")

Expand All @@ -438,11 +439,17 @@ def _write_implementation(self, header: HeaderFile) -> None:
implementations.append(
template.render(declaration=c_func.declaration(),body=body))
other |= bases
implementations = "\n\n".join(implementations)
str_utils = "copyString" in implementations

includes = []
for obj in [header.cabinet] + list(other):
includes += self._config.includes[obj]

template = loader.from_string(self._templates["clib-source-file"])
output = template.render(
name=filename.stem, source_entries=implementations,
base=header.cabinet, other=other)
name=filename.stem, implementations=implementations,
includes=includes, base=header.cabinet, other=other, str_utils=str_utils)

if self._out_dir:
out = Path(self._out_dir) / "src" / filename.name
Expand Down
6 changes: 3 additions & 3 deletions interfaces/sourcegen/sourcegen/clib/_Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class Config:
"const vector<shared_ptr<T>>&": 'int[]',
}

cabinets: list[str]
includes: dict[str,list[str]]

@classmethod
def from_parsed(cls, *, cabinets=None) -> 'Config':
def from_parsed(cls, *, includes=None) -> 'Config':
"""Ensure that configurations are correct."""
return cls(cabinets or [])
return cls(includes or {})
19 changes: 12 additions & 7 deletions interfaces/sourcegen/sourcegen/clib/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ ignore_files: []
ignore_funcs:
ctsoln_auto.yaml: [newInterface, adjacent]

# Cabinets holding objects
cabinets:
- Solution
- ThermoPhase
- Kinetics
- Transport
- Func1
# Cabinets with associated includes
includes:
Solution:
- cantera/base/Solution.h
ThermoPhase:
- cantera/thermo/ThermoFactory.h
Kinetics:
- cantera/kinetics/KineticsFactory.h
Transport:
- cantera/transport/TransportFactory.h
Func1:
- cantera/numerics/Func1Factory.h
23 changes: 16 additions & 7 deletions interfaces/sourcegen/sourcegen/clib/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,29 @@ clib-source-file: |-
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.
#include "../include/clib3_defs.h"
#include "../include/{{ name }}.h"
#include "cantera/clib_experimental/{{ name }}.h"
#include "cantera/clib_experimental/clib3_defs.h"
#include "../clib/Cabinet.h"
{% if str_utils %}
#include "cantera/base/stringUtils.h"
{% endif %}
{% for entry in includes %}
#include "{{ entry }}"
{% endfor %}
using namespace Cantera;
typedef Cabinet<{{ base }}> {{ base }}Cabinet;
template<> {{ base }}Cabinet* {{ base }}Cabinet::s_storage = 0;
{% if other %}
{% for entry in other -%}
{% for entry in other %}
typedef Cabinet<{{ entry }}> {{ entry }}Cabinet; // initialized elsewhere
{% endfor -%}
{% endfor %}
{% endif %}
extern "C" {
{% for entry in source_entries %}
{{ entry | indent(4) }}
{% endfor %}
{{ implementations | indent(4) }}
}

0 comments on commit 5e14309

Please sign in to comment.