from buildutils import *

Import('env', 'build', 'install', 'libraryTargets')

def defaultSetup(env, subdir, extensions):
    env.Append(CCFLAGS=env['pch_flags'])
    return mglob(env, subdir, *extensions)

def applicationSetup(env, subdir, extensions):
    # Add #define variables unique to application.cpp
    escaped_datadir = '\\"' + env['ct_datadir'].replace('\\', '\\\\') + '\\"'
    env.Append(CPPDEFINES={'CANTERA_DATA': escaped_datadir})
    return defaultSetup(env, subdir, extensions)

def globalSetup(env, subdir, extensions):
    # Add #define variables unique to global.cpp
    env.Append(CPPDEFINES={'GIT_COMMIT': '\\"{0}\\"'.format(env['git_commit'])})
    return defaultSetup(env, subdir, extensions)

def baseSetup(env, subdir, extensions):
    # All files in base except for application.cpp
    return [f for f in defaultSetup(env, subdir, extensions)
            if f.name != 'application.cpp' and f.name != 'global.cpp']

# (subdir, (file extensions), (extra setup(env)))
libs = [('base', ['cpp'], baseSetup),
        ('base', ['^application.cpp'], applicationSetup),
        ('base', ['^global.cpp'], globalSetup),
        ('thermo', ['cpp'], defaultSetup),
        ('tpx', ['cpp'], defaultSetup),
        ('equil', ['cpp','c'], defaultSetup),
        ('numerics', ['cpp'], defaultSetup),
        ('kinetics', ['cpp'], defaultSetup),
        ('transport', ['cpp'], defaultSetup),
        ('oneD', ['cpp'], defaultSetup),
        ('zeroD', ['cpp'], defaultSetup),
        ('clib', ['cpp'], defaultSetup),
        ]

localenv = env.Clone()
localenv.Prepend(CPPPATH=[Dir('#include'), Dir('#include/cantera/ext'), Dir('.')])
localenv.Append(CCFLAGS=env['warning_flags'])

if env['CC'] == 'cl' and env['debug']:
    env['use_pch'] = False # PCH doesn't work with per-file PDB

if env['use_pch']:
    if env['CC'] == 'cl':
        env.Command('#build/src/pch/system.h', '#src/pch/system.h',
                    Copy('$TARGET', '$SOURCE'))
        localenv['PCH'], pchobj = localenv.PCH('pch/system.cpp')
        pch = localenv['PCH']
        libraryTargets.append(pchobj)
        localenv['PCHSTOP'] = 'pch/system.h'
    else:
        localenv['precompiled_header'] = File('pch/system.h')
        pch = localenv.GchSh('#src/pch/system.h.gch',
                             localenv['precompiled_header'])
else:
    removeFile('src/pch/system.h.gch')
    localenv['pch_flags'] = []
    pch = None

for subdir, extensions, setup in libs:
    env2 = localenv.Clone()
    source = setup(env2, subdir, extensions)
    objects = env2.SharedObject(source)
    env2.Depends(objects, env2['config_h_target'])
    if pch:
        env2.Requires(objects, pch)
    libraryTargets.extend(objects)


# build the Cantera static library
lib = build(localenv.StaticLibrary('../lib/cantera', libraryTargets,
                                   SPAWN=getSpawn(localenv)))
localenv.Depends(lib, localenv['config_h_target'])
install('$inst_libdir', lib)
env['cantera_staticlib'] = lib

if (localenv['system_sundials'] == 'y'):
    localenv.Append(LIBS=localenv['sundials_libs'],
                    LIBPATH=localenv['sundials_libdir'])
if localenv['blas_lapack_libs']:
    localenv.Append(LIBS=localenv['blas_lapack_libs'],
                    LIBPATH=localenv['blas_lapack_dir'])

if localenv['system_yamlcpp']:
    localenv.Append(LIBS=['yaml-cpp'])

# Build the Cantera shared library
if localenv['layout'] != 'debian':
    if localenv['renamed_shared_libraries']:
        sharedName = '../lib/cantera_shared'
    else:
        sharedName = '../lib/cantera'

    if localenv['versioned_shared_library']:
        lib = build(localenv.SharedLibrary(sharedName, libraryTargets,
                                           SPAWN=getSpawn(localenv),
                                           SHLIBVERSION=localenv['cantera_pure_version']))
        install(localenv.InstallVersionedLib, '$inst_libdir', lib)
    else:
        lib = build(localenv.SharedLibrary(sharedName, libraryTargets,
                                           SPAWN=getSpawn(localenv)))
        install('$inst_libdir', lib)
    env['cantera_shlib'] = lib
    localenv.Depends(lib, localenv['config_h_target'])
