Skip to content

Commit

Permalink
[sourcegen] Add main CLib header
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Jan 1, 2025
1 parent 189741e commit f9e69bf
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
11 changes: 6 additions & 5 deletions interfaces/sourcegen/sourcegen/_TagFileParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ def xml_compounds(kind: str, names: list[str]) -> dict[str,str]:

# Handle exceptions for unknown/undocumented classes
unknown = set(bases) - set(class_names)
if unknown:
if "', '".join(unknown):
unknown = "', '".join(unknown)
_logger.warning("Class(es) in configuration file are missing "
f"from tag file: {unknown!r}")
_logger.critical("Class(es) in configuration file are missing "
f"from tag file: {unknown!r}")
exit(1)

# Parse content of classes that are specified by the configuration file
class_names = set(bases) & set(class_names)
Expand Down Expand Up @@ -243,8 +244,8 @@ def tag_lookup(tag_info: TagInfo) -> TagDetails:
_logger.error(f"No XML matches found for {tag_info.qualified_name!r}")
return TagDetails()
if len(matches) != 1:
_logger.error(f"Inconclusive XML matches found for {tag_info.qualified_name!r}")
return TagDetails()
_logger.warning(f"Inconclusive XML matches found for {tag_info.qualified_name!r}")
matches = matches[:1]

def cleanup(entry: str) -> str:
# Remove stray XML markup
Expand Down
28 changes: 28 additions & 0 deletions interfaces/sourcegen/sourcegen/_data/ct_auto.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Configuration for code generation.
# Implements portion of replacement for CLib "ct"

# 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.

cabinet:
prefix: ct3
base: ""
functions:
# - name: getCanteraError(int buflen, char* buf)
# - name: setLogWriter(void* logger)
# - name: setLogCallback(LogCallback writer);
- name: addCanteraDirectory
implements: addDirectory
- name: getDataDirectories
- name: findInputFile
# - name: getCanteraVersion
- name: getGitCommit
implements: gitCommit
- name: suppress_deprecation_warnings
- name: make_deprecation_warnings_fatal
- name: suppress_warnings
- name: warnings_suppressed
- name: make_warnings_fatal
- name: suppress_thermo_warnings
# - name: clearStorage();
# - name: resetStorage();
14 changes: 8 additions & 6 deletions interfaces/sourcegen/sourcegen/clib/_CLibSourceGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _ret_crosswalk(
def _prop_crosswalk(self, par_list: list[Param]) -> list[Param]:
"""Crosswalk for argument type."""
if not par_list:
return [], []
return []
params = []
for par in par_list:
what = par.p_type
Expand Down Expand Up @@ -297,7 +297,7 @@ def _scaffold_body(self, c_func: CFunc, recipe: Recipe) -> tuple[str, set[str]]:
template = loader.from_string(self._templates["clib-method"])

else:
_logger.critical(f"Method not implemented: {cxx_type!r}.")
_logger.critical(f"{recipe.what!r} not implemented: {c_func.name!r}.")
exit(1)

return template.render(**args), bases
Expand Down Expand Up @@ -361,14 +361,15 @@ def merge_params(implements, cxx_func: CFunc) -> tuple[list[Param], int]:
recipe.what = "constructor"
elif "void" not in cxx_func.ret_type and cxx_arglen == 0:
recipe.what = "getter"
elif "void" in cxx_func.ret_type and cxx_arglen == 1:
elif "void" in cxx_func.ret_type and cxx_arglen == 1 and cxx_func.base:
p_type = cxx_func.arglist[0].p_type
if "*" in p_type and not p_type.startswith("const"):
recipe.what = "getter" # getter assigns to existing array
else:
recipe.what = "setter"
elif any(recipe.implements.startswith(base)
for base in [recipe.base] + recipe.parents + recipe.derived):
elif cxx_func.base and \
any(recipe.implements.startswith(base)
for base in [recipe.base] + recipe.parents + recipe.derived):
recipe.what = "method"
else:
recipe.what = "function"
Expand Down Expand Up @@ -452,7 +453,8 @@ def _write_implementation(self, header: HeaderFile) -> None:

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

template = loader.from_string(self._templates["clib-source-file"])
output = template.render(
Expand Down
1 change: 1 addition & 0 deletions interfaces/sourcegen/sourcegen/clib/_Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Config:
"""Provides configuration info for the CLibSourceGenerator class"""

ret_type_crosswalk = {
"bool": "int",
"void": "int",
"int": "int",
"size_t": "int",
Expand Down
24 changes: 16 additions & 8 deletions interfaces/sourcegen/sourcegen/clib/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ clib-param: |-
clib-comment: |-
{{ briefdescription }}
{% for par in params -%}
{% for par in params %}
{{ par }}
{% endfor -%}
{% if returns -%}
{% endfor %}
{% if returns %}
{{ '@returns' + 13*' ' + returns }}
{% endif -%}
{{ ' ' }}
{% if implements -%}
{% endif %}
{% if params or returns %}
{% endif %}
{% if implements %}
{{ '@implements ' + implements }}
{% endif -%}
{% if relates -%}
{% endif %}
{% if relates %}
{{ '@relates ' + ', '.join(relates) }}
{% endif %}
Expand Down Expand Up @@ -58,6 +60,7 @@ clib-header-file: |-
{{ headers | indent(4) }}
{% if base %}
{% if no_constructor %}
/**
* Return handle to parent of {{ base }} object (if applicable).
Expand All @@ -70,6 +73,7 @@ clib-header-file: |-
*/
int {{ prefix }}_cabinetSize();
{% endif %}
#ifdef __cplusplus
}
#endif
Expand Down Expand Up @@ -251,8 +255,10 @@ clib-source-file: |-
using namespace Cantera;
{% if base %}
typedef Cabinet<{{ base }}> {{ base }}Cabinet3;
template<> {{ base }}Cabinet3* {{ base }}Cabinet3::s_storage = 0;
{% endif %}
{% if other %}
{% for entry in other %}
Expand All @@ -264,6 +270,7 @@ clib-source-file: |-
{{ implementations | indent(4) }}
{% if base %}
{% if no_constructor %}
int {{ prefix }}_parentHandle(int handle)
{
Expand All @@ -286,4 +293,5 @@ clib-source-file: |-
}
}
{% endif %}
} // extern "C"

0 comments on commit f9e69bf

Please sign in to comment.