from buildutils import compiler_flag_list

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

# (program name, [source files])
samples = [("ctlib", ["ctlib.f"]),
           ("demo", ["f77_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 = [r"\$\(", "/TP", r"\$\)", "/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(["-mmacosx", "-march", "-mtune", "-fdebug-prefix-map",
                          "-isysroot", ".*/_build_env/"])
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 = sorted(set(incdirs))

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

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

cc_flags = compiler_flag_list(localenv["CCFLAGS"] + localenv["CXXFLAGS"],
                              env["CC"], 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']:
    # For package builds, use environment variables or rely on SCons to find the
    # default compilers
    localenv['tmpl_cc'] = "env['CC'] = os.environ.get('CC', env['CC'])"
    localenv['tmpl_f77'] = "env['F77'] = os.environ.get('F77', env['F77'])"
else:
    # Otherwise, use the same compilers that were used to build Cantera, with the
    # environment variables optionally overriding
    localenv['tmpl_cc'] = env.subst("env['cc'] = os.environ.get('cc', '$cc')")
    localenv['tmpl_f77'] = env.subst("env['F77'] = os.environ.get('F77', '$F77')")

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