from os.path import join as pjoin, relpath
from buildutils import *

Import('env', 'install', 'buildSample')
localenv = env.Clone()

# (program name, [source files])
samples = [("ctlib", ["ctlib.f"]),
           ("demo", ["demo.f"]),
           ("isentropic", ["isentropic.f"])]

ftn_demo = localenv.SharedObject('demo_ftnlib.cpp',
                                 CPPPATH=['#include', localenv['boost_inc_dir'],
                                    localenv['extra_inc_dirs']])
for program_name, fortran_sources in samples:
    buildSample(localenv.Program, program_name,
                fortran_sources + ftn_demo,
                CPPPATH=['#build/src/fortran', '#include'],
                LIBS=env['cantera_libs']+['cantera_fortran']+env['cxx_stdlib'],
                LIBPATH=[env['sundials_libdir'], localenv['blas_lapack_dir'],
                         env['extra_lib_dirs'], env["hdf_libdir"], '#build/lib'],
                LINK='$FORTRAN_LINK')

# Generate SConstruct file to be installed
linkflags = ["-g", localenv["thread_flags"]]

flag_excludes = ["$(", "/TP", "$)", "/nologo"]
incdirs = [localenv["ct_incroot"]]
libdirs = [localenv["ct_libdir"]]
if localenv["package_build"]:
    # Remove sysroot flags in templated output files. This only applies to the
    # conda package for now.
    # Users should compile against their local SDKs, which should be backwards
    # compatible with the SDK used for building.
    flag_excludes.extend(["-isysroot", "-mmacosx", "-march", "-mtune",
                          "-fdebug-prefix-map"])
else:
    linkflags.append(f"-Wl,-rpath,{localenv['ct_shlibdir']}")

    incdirs.extend([localenv["sundials_include"], localenv["boost_inc_dir"]])
    incdirs.append(localenv["hdf_include"])
    incdirs.extend(localenv["extra_inc_dirs"])
    incdirs = list(set(incdirs))

    libdirs.extend([localenv["sundials_libdir"], localenv["blas_lapack_dir"]])
    libdirs.append(localenv["hdf_libdir"])
    libdirs.extend(localenv["extra_lib_dirs"])
    libdirs = list(set(libdirs))

libs = ["cantera_fortran"] + localenv["cantera_libs"] + localenv["cxx_stdlib"]

cc_flags = compiler_flag_list(localenv["CCFLAGS"] + localenv["CXXFLAGS"],
                              flag_excludes)
localenv["tmpl_compiler_flags"] = repr(cc_flags)
localenv['tmpl_cantera_incdirs'] = repr([x for x in incdirs if x])
localenv['tmpl_cantera_libs'] = repr(libs)
localenv['tmpl_cantera_libdirs'] = repr([x for x in libdirs if x])
localenv['tmpl_cantera_linkflags'] = repr([x for x in linkflags if x])
localenv['tmpl_cantera_frameworks'] = repr(localenv['FRAMEWORKS'])

if localenv["package_build"]:
    # We do not want to specify the conda compilers from the build environment,
    # because those won't be installed on the user's system.
    if localenv["OS"] == "Darwin":
        localenv["F77"] = "gfortran"
        localenv["CC"] = "clang"
    elif env["OS"] == "Windows":
        # compilation needs the Visual Studio Command Prompt
        localenv["F77"] = "ifx"
        localenv["CC"] = "cl"
    else:
        localenv["F77"] = "gfortran"
        localenv["CC"] = "gcc"

sconstruct = localenv.SubstFile('SConstruct', 'SConstruct.in')
install("$inst_sampledir/f77", sconstruct)
