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

Import("env", "build", "install")

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)
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("ct*.yaml")
for yaml_file in yaml_files:
    base_name = yaml_file.stem
    generated_files.extend([
        f"#interfaces/clib/include/cantera_clib/{base_name}.h",
        f"#interfaces/clib/src/{base_name}.cpp",
    ])
    install("$inst_incdir/../cantera_clib",
            f"#interfaces/clib/include/cantera_clib/{base_name}.h")

install(env.RecursiveInstall, "$inst_incdir/../cantera_clib",
        "#interfaces/clib/include/cantera_clib")

# Run sourcegen without installation
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])
env.Depends(sourcegen, env.Glob("#interfaces/sourcegen/src/sourcegen/*.py"))
env.Depends(sourcegen, env.Glob("#interfaces/sourcegen/src/sourcegen/clib/*.*"))

build(tags)
build(sourcegen)

Return('sourcegen')
