"""SConscript file for generated CLib."""
from pathlib import Path
from buildutils import multi_glob, logger

Import("env", "build")

vars = {
    "@INCLUDE@": "",
    "@ENABLED_SECTIONS@": "",
    "@GENERATE_HTML@": "NO",
    # Ignore warnings during tag file generation
    # (they are turned on for `scons doxygen`)
    "@WARNINGS@": "NO",
    "@WARN_IF_DOC_ERROR@": "NO",
    "@WARN_IF_INCOMPLETE_DOC@": "NO",
}
env.Substfile(
    "#build/doc/Doxyfile_xml", "#doc/doxygen/Doxyfile.in", SUBST_DICT=vars)
logger.info("Generating Doxygen tagfile.")
tags = env.Command(
        "#build/doc/Cantera.tag", "#build/doc/Doxyfile_xml", "doxygen $SOURCE")
cxx_files = list(env.Glob("#doc/doxygen/*") +
                 multi_glob(env, "#include/cantera", "h") +
                 multi_glob(env, "#include/cantera/*", "h") +
                 multi_glob(env, "#src/cantera/*", "h", "cpp"))
env.Depends(tags, cxx_files)

env.Alias("doxygen_tags", tags)

# Generated file names can be anticipated
generated_files = []
auto_path = Path(Dir("#interfaces/sourcegen/src/sourcegen/headers").abspath)
yaml_files = auto_path.glob("*_auto.yaml")
for yaml_file in yaml_files:
    base_name = yaml_file.stem.replace("_auto", "")
    generated_files.extend([
        f"#interfaces/clib/include/cantera_clib/{base_name}.h",
        f"#interfaces/clib/src/{base_name}.cpp",
    ])

# Run sourcegen without installation
logger.info("Running sourcegen to generate CLib files.")
sourcegen = env.Command(
    generated_files,
    tags,
    ("$python_cmd -m interfaces.sourcegen.src.sourcegen "
     f"--api=clib --output={Path(Dir('#interfaces/clib').abspath)}")
)
env.Depends(sourcegen, [File(ff) for ff in yaml_files])

build(tags)
build(sourcegen)

Return('sourcegen')
