# -*- mode: python -*-

import libdeps

Import("env use_system_version_of_library usemozjs get_option")
Import("wiredtiger")
Import("mobile_se")

boostSuffix = "-1.60.0"
snappySuffix = '-1.1.3'
zlibSuffix = '-1.2.11'
pcreSuffix = "-8.42"
mozjsSuffix = '-45'
yamlSuffix = '-0.6.2'
icuSuffix = '-57.1'
gperftoolsSuffix = '-2.5'
lz4Suffix = '-r131'
timelibSuffix = '-2018.01alpha1'
tomcryptSuffix = '-1.18.1'
benchmarkSuffix = '-1.3.0'
sqliteSuffix = '-amalgamation-3190300'
libarchiveSuffix = '-3.4.0'

thirdPartyIncludePathList = [
    ('percona_incl', '#/src/third_party/install/include'),
    ('libarchive', '#/src/third_party/libarchive' + libarchiveSuffix),
    ('fmt', '#src/third_party/fmt/dist/include'),
    ('s2', '#/src/third_party/s2'),
    ('timelib', '#/src/third_party/timelib' + timelibSuffix),
]

if not use_system_version_of_library('tcmalloc'):
    thirdPartyIncludePathList.append(
        ('gperftools', '#/src/third_party/gperftools' + gperftoolsSuffix + '/src'))

if not use_system_version_of_library('pcre'):
    thirdPartyIncludePathList.append(
        ('pcre', '#/src/third_party/pcre' + pcreSuffix))

if not use_system_version_of_library('boost'):
    thirdPartyIncludePathList.append(
        ('boost', '#/src/third_party/boost' + boostSuffix))

if not use_system_version_of_library('snappy'):
    thirdPartyIncludePathList.append(
        ('snappy', '#/src/third_party/snappy' + snappySuffix))

if not use_system_version_of_library('lz4'):
    thirdPartyIncludePathList.append(
        ('lz4', '#/src/third_party/lz4' + lz4Suffix))

# Valgrind is a header only include as valgrind.h includes everything we need
if not use_system_version_of_library('valgrind'):
    thirdPartyIncludePathList.append(
        ('valgrind', '#/src/third_party/valgrind-3.11.0/include'))

if not use_system_version_of_library('zlib'):
    thirdPartyIncludePathList.append(
        ('zlib', '#/src/third_party/zlib' + zlibSuffix))

if not use_system_version_of_library('sqlite'):
    thirdPartyIncludePathList.append(
        ('sqlite', '#/src/third_party/sqlite' + sqliteSuffix + '/sqlite'))

if not use_system_version_of_library('google-benchmark'):
    thirdPartyIncludePathList.append(
        ('benchmark', '#/src/third_party/benchmark' + benchmarkSuffix + '/benchmark/include'))

# TODO: figure out if we want to offer system versions of mozjs.  Mozilla
# hasn't offered a source tarball since 24, but in theory they could.
#
#if not use_system_version_of_library('mozjs'):
if True:
    thirdPartyIncludePathList.append(
        ('mozjs', ['#/src/third_party/mozjs' + mozjsSuffix + '/include',
                   '#/src/third_party/mozjs' + mozjsSuffix + '/mongo_sources',
                   '#/src/third_party/mozjs' + mozjsSuffix + '/platform/' + env["TARGET_ARCH"] + "/" + env["TARGET_OS"] + "/include",
        ]))

if "tom" in env["MONGO_CRYPTO"]:
    thirdPartyIncludePathList.append(
        ('tomcrypt', ['#/src/third_party/tomcrypt' + tomcryptSuffix + '/src/headers',
    ]))


if not use_system_version_of_library('stemmer'):
    thirdPartyIncludePathList.append(
        ('stemmer', '#/src/third_party/libstemmer_c/include'))

# Note that the wiredtiger.h header is generated, so
# we want to look for it in the build directory not
# the source directory.
if wiredtiger and not use_system_version_of_library('wiredtiger'):
    thirdPartyIncludePathList.append(
        ('wiredtiger', '$BUILD_DIR/third_party/wiredtiger'))

if not use_system_version_of_library('yaml'):
    thirdPartyIncludePathList.append(
        ('yaml', '#/src/third_party/yaml-cpp' + yamlSuffix + '/include'))

if not use_system_version_of_library('asio'):
    thirdPartyIncludePathList.append(
        ('asio', '#/src/third_party/asio-master/asio/include'))

if not use_system_version_of_library('intel_decimal128'):
    thirdPartyIncludePathList.append(
        ('intel_decimal128', '#/src/third_party/IntelRDFPMathLib20U1/LIBRARY'))

if not use_system_version_of_library('icu'):
    thirdPartyIncludePathList.append(
        ('icu', '#/src/third_party/icu4c' + icuSuffix + '/source/common'))
    thirdPartyIncludePathList.append(
        ('icu', '#/src/third_party/icu4c' + icuSuffix + '/source/i18n'))

def injectAllThirdPartyIncludePaths(thisEnv):
    thisEnv.PrependUnique(CPPPATH=[entry[1] for entry in thirdPartyIncludePathList])

def injectThirdPartyIncludePaths(thisEnv, libraries):
    thisEnv.PrependUnique(CPPPATH=[
        entry[1] for entry in thirdPartyIncludePathList if entry[0] in libraries])

env.AddMethod(injectAllThirdPartyIncludePaths, 'InjectAllThirdPartyIncludePaths')
env.AddMethod(injectThirdPartyIncludePaths, 'InjectThirdPartyIncludePaths')

env = env.Clone()

murmurEnv = env.Clone()
murmurEnv.InjectThirdPartyIncludePaths(libraries=['fmt'])
murmurEnv.SConscript('murmurhash3/SConscript', exports={ 'env' : murmurEnv })


s2Env = env.Clone()
s2Env.InjectThirdPartyIncludePaths(libraries=['s2', 'boost', 'fmt'])
s2Env.InjectMongoIncludePaths()
s2Env.SConscript('s2/SConscript', exports={'env' : s2Env})


if use_system_version_of_library("fmt"):
    fmtEnv = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_FMT_SYSLIBDEP'],
        ])
else:
    fmtEnv = env.Clone()
    fmtEnv.InjectThirdPartyIncludePaths(libraries=['fmt'])
    fmtEnv.InjectMongoIncludePaths()
    fmtEnv.SConscript('fmt/SConscript', exports={'env' : fmtEnv})
    fmtEnv = fmtEnv.Clone(
        LIBDEPS_INTERFACE=[
            'fmt/fmt',
        ])

fmtEnv.Library(
    target="shim_fmt",
    source=[
        'shim_fmt.cpp',
    ])


if use_system_version_of_library("pcre"):
    pcreEnv = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_PCRE_SYSLIBDEP'],
            env['LIBDEPS_PCRECPP_SYSLIBDEP'],
        ])
else:
    pcreEnv = env.Clone()
    pcreEnv.InjectThirdPartyIncludePaths(libraries=['pcre'])
    pcreEnv.SConscript('pcre' + pcreSuffix + '/SConscript', exports={ 'env' : pcreEnv })
    pcreEnv = pcreEnv.Clone(
        LIBDEPS_INTERFACE=[
            'pcre' + pcreSuffix + '/pcrecpp',
        ])

pcreEnv.Library(
    target="shim_pcrecpp",
    source=[
        'shim_pcrecpp.cc',
    ])


boostEnv = env
if use_system_version_of_library("boost"):
    # On windows, we don't need the syslibdeps because autolib will select the right libraries
    # for us automatically.
    if not env.TargetOSIs('windows'):
        boostEnv = env.Clone(
            SYSLIBDEPS=[
                env['LIBDEPS_BOOST_PROGRAM_OPTIONS_SYSLIBDEP'],
                env['LIBDEPS_BOOST_FILESYSTEM_SYSLIBDEP'],
                env['LIBDEPS_BOOST_SYSTEM_SYSLIBDEP'],
                env['LIBDEPS_BOOST_IOSTREAMS_SYSLIBDEP'],
            ])
else:
    boostDirectory = 'boost' + boostSuffix
    boostEnv = env.Clone()
    boostEnv.InjectThirdPartyIncludePaths(libraries=['boost'])
    boostEnv.SConscript(boostDirectory + '/SConscript', exports={ 'env' : boostEnv })
    boostEnv = boostEnv.Clone(
        LIBDEPS_INTERFACE=[
            boostDirectory + '/boost_program_options',
            boostDirectory + '/boost_filesystem',
            boostDirectory + '/boost_system',
            boostDirectory + '/boost_iostreams',
        ])

boostEnv.Library(
    target="shim_boost",
    source=[
        'shim_boost.cpp',
    ])


if use_system_version_of_library("snappy"):
    snappyEnv = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_SNAPPY_SYSLIBDEP'],
        ])
else:
    snappyEnv = env.Clone()
    snappyEnv.InjectThirdPartyIncludePaths(libraries=['snappy'])
    snappyEnv.InjectMongoIncludePaths()
    snappyEnv.SConscript('snappy' + snappySuffix + '/SConscript', exports={ 'env' : snappyEnv })
    snappyEnv = snappyEnv.Clone(
        LIBDEPS_INTERFACE=[
            'snappy' + snappySuffix + '/snappy',
        ])

snappyEnv.Library(
    target="shim_snappy",
    source=[
        'shim_snappy.cpp',
    ])


if use_system_version_of_library("lz4"):
    lz4Env = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_LZ4_SYSLIBDEP'],
        ])
else:
    lz4Env = env.Clone()
    lz4Env.InjectThirdPartyIncludePaths(libraries=['lz4'])
    lz4Env.InjectMongoIncludePaths()
    lz4Env.SConscript('lz4' + lz4Suffix + '/SConscript', exports={ 'env' : lz4Env })
    lz4Env = lz4Env.Clone(
        LIBDEPS=[
            'lz4' + lz4Suffix + '/lz4'
        ])

lz4Env.Library(
    target="shim_lz4",
    source=[
        'shim_lz4.cpp',
    ])


if use_system_version_of_library("zlib"):
    zlibEnv = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_ZLIB_SYSLIBDEP'],
        ])
else:
    zlibEnv = env.Clone()
    zlibEnv.InjectThirdPartyIncludePaths(libraries=['zlib'])
    zlibEnv.SConscript('zlib' + zlibSuffix + '/SConscript', exports={ 'env' : zlibEnv })
    zlibEnv = zlibEnv.Clone(
        LIBDEPS_INTERFACE=[
            'zlib' + zlibSuffix + '/zlib',
        ])

zlibEnv.Library(
    target="shim_zlib",
    source=[
        'shim_zlib.cpp',
    ])

if use_system_version_of_library("google-benchmark"):
    benchmarkEnv = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_BENCHMARK_SYSLIBDEP'],
        ])
else:
    benchmarkEnv = env.Clone()
    benchmarkEnv.InjectThirdPartyIncludePaths(libraries=['benchmark'])
    benchmarkEnv.SConscript(
        'benchmark' + benchmarkSuffix + '/SConscript',
        exports={ 'env' : benchmarkEnv })
    benchmarkEnv = benchmarkEnv.Clone(
        LIBDEPS_INTERFACE=[
            'benchmark' + benchmarkSuffix + '/benchmark',
        ])

benchmarkEnv.Library(
    target="shim_benchmark",
    source=[
        'shim_benchmark.cpp',
    ])

if usemozjs:
    mozjsEnv = env.Clone()
    mozjsEnv.SConscript('mozjs' + mozjsSuffix + '/SConscript', exports={'env' : mozjsEnv })
    mozjsEnv = mozjsEnv.Clone(
        LIBDEPS_INTERFACE=[
            'mozjs' + mozjsSuffix + '/mozjs',
            'shim_zlib',
        ])

    mozjsEnv.Library(
        target="shim_mozjs",
        source=[
            'shim_mozjs.cpp',
        ])

if "tom" in env["MONGO_CRYPTO"]:
    tomcryptEnv = env.Clone()
    tomcryptEnv.SConscript('tomcrypt' + tomcryptSuffix + '/SConscript', exports={'env' : tomcryptEnv })
    tomcryptEnv = tomcryptEnv.Clone(
        LIBDEPS_INTERFACE=[
            'tomcrypt' + tomcryptSuffix + '/tomcrypt',
        ])

    tomcryptEnv.Library(
        target="shim_tomcrypt",
        source=[
            'shim_tomcrypt.cpp',
        ])

gperftoolsEnv = env
if (gperftoolsEnv['MONGO_ALLOCATOR'] == "tcmalloc"):
    if use_system_version_of_library("tcmalloc"):
        gperftoolsEnv = env.Clone(
            SYSLIBDEPS=[
                env['LIBDEPS_TCMALLOC_SYSLIBDEP'],
            ])
    else:
        gperftoolsEnv = env.Clone()
        gperftoolsEnv.InjectThirdPartyIncludePaths(libraries=['gperftools'])
        gperftoolsEnv.SConscript('gperftools' + gperftoolsSuffix + '/SConscript', exports={ 'env' : gperftoolsEnv })
        gperftoolsEnv = gperftoolsEnv.Clone(
            LIBDEPS_INTERFACE=[
                'gperftools' + gperftoolsSuffix + '/tcmalloc_minimal',
            ])

gperftoolsEnv.Library(
    target="shim_allocator",
    source=[
        "shim_allocator.cpp",
    ])


if use_system_version_of_library("stemmer"):
    stemmerEnv = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_STEMMER_SYSLIBDEP'],
        ])
else:
    stemmerEnv = env.Clone()
    stemmerEnv.InjectThirdPartyIncludePaths(libraries=['stemmer'])
    stemmerEnv.SConscript('libstemmer_c/SConscript', exports={ 'env' : stemmerEnv })
    stemmerEnv = stemmerEnv.Clone(
        LIBDEPS_INTERFACE=[
             'libstemmer_c/stemmer',
        ])

stemmerEnv.Library(
    target="shim_stemmer",
    source=[
        'shim_stemmer.cpp'
    ])


if use_system_version_of_library("yaml"):
    yamlEnv = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_YAML_SYSLIBDEP'],
        ])
else:
    yamlEnv = env.Clone()
    yamlEnv.InjectThirdPartyIncludePaths(libraries=['yaml', 'boost'])
    yamlEnv.SConscript('yaml-cpp' + yamlSuffix + '/SConscript', exports={ 'env' : yamlEnv })
    yamlEnv = yamlEnv.Clone(
        LIBDEPS_INTERFACE=[
            'yaml-cpp' + yamlSuffix + '/yaml',
        ])

yamlEnv.Library(
    target="shim_yaml",
    source=[
        'shim_yaml.cpp',
    ])

timelibEnv = env.Clone();
timelibEnv.InjectThirdPartyIncludePaths(libraries=['timelib'])
timelibEnv.SConscript('timelib' + timelibSuffix + '/SConscript', exports={ 'env' : timelibEnv })
timelibEnv = timelibEnv.Clone(
    LIBDEPS_INTERFACE=[
        'timelib' + timelibSuffix + '/timelib',
    ])

timelibEnv.Library(
    target='shim_timelib',
    source=[
        'shim_timelib.cpp',
    ])

if wiredtiger:
    if use_system_version_of_library("wiredtiger"):
        wiredtigerEnv = env.Clone(
            SYSLIBDEPS=[
                env['LIBDEPS_WIREDTIGER_SYSLIBDEP'],
            ])
    else:
        wiredtigerEnv = env.Clone()
        wiredtigerEnv.InjectThirdPartyIncludePaths(libraries=['wiredtiger'])
        wiredtigerEnv.SConscript('wiredtiger/SConscript', exports={ 'env' : wiredtigerEnv })
        wiredtigerEnv = wiredtigerEnv.Clone(
            LIBDEPS_INTERFACE=[
                 'wiredtiger/wiredtiger',
            ])

    wiredtigerEnv.Library(
        target="shim_wiredtiger",
        source=[
            'shim_wiredtiger.cpp'
        ])

if use_system_version_of_library("sqlite"):
    sqliteEnv = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_SQLITE_SYSLIBDEP']
        ])
else:
    sqliteEnv = env.Clone()
    sqliteEnv.InjectThirdPartyIncludePaths(libraries=['sqlite'])
    sqliteEnv.SConscript('sqlite' + sqliteSuffix + '/SConscript', exports={ 'env' : sqliteEnv })
    sqliteEnv = sqliteEnv.Clone(
        LIBDEPS_INTERFACE=[
            'sqlite' + sqliteSuffix + '/sqlite3',
        ])

sqliteEnv.Library(
    target='shim_sqlite',
    source=[
        'shim_sqlite.cpp',
    ])

if use_system_version_of_library("asio"):
    # Normally, we would request LIBDEPS_ASIO_SYSLIBDEP here, but on most systems, the system asio
    # will be header only so there is no library required. In the rare case where one is, it can be
    # injected via LIBS= on the command line.
    asioEnv = env.Clone()
else:
    asioEnv = env.Clone()
    asioEnv.InjectThirdPartyIncludePaths(libraries=['asio'])
    asioEnv.SConscript('asio-master/SConscript', exports={ 'env' : asioEnv })
    asioEnv = asioEnv.Clone(
        LIBDEPS_INTERFACE=[
            'asio-master/asio',
        ])

asioEnv.Library(
    target="shim_asio",
    source=[
        'shim_asio.cpp'
    ])

if use_system_version_of_library("intel_decimal128"):
    intelDecimal128Env = env.Clone(
	SYSLIBDEPS=[
	    env['LIBDEPS_INTEL_DECIMAL128_SYSLIBDEP'],
	])
else:
    intelDecimal128Env = env.Clone()
    intelDecimal128Env.InjectThirdPartyIncludePaths(libraries=['intel_decimal128'])
    intelDecimal128Env.SConscript('IntelRDFPMathLib20U1/SConscript', exports={ 'env' : intelDecimal128Env })
    intelDecimal128Env = intelDecimal128Env.Clone(
	LIBDEPS_INTERFACE=[
	    'IntelRDFPMathLib20U1/intel_decimal128',
	])

intelDecimal128Env.Library(
    target="shim_intel_decimal128",
    source=[
	'shim_intel_decimal128.cpp'
    ])

if use_system_version_of_library("icu"):
    icuEnv = env.Clone(
        SYSLIBDEPS=[
            env['LIBDEPS_ICUDATA_SYSLIBDEP'],
            env['LIBDEPS_ICUI18N_SYSLIBDEP'],
            env['LIBDEPS_ICUUC_SYSLIBDEP'],
        ])
else:
    icuEnv = env.Clone()
    icuEnv.InjectThirdPartyIncludePaths(libraries=['icu'])
    icuEnv.SConscript('icu4c' + icuSuffix + '/source/SConscript', exports={ 'env' : icuEnv })
    icuEnv = icuEnv.Clone(
        LIBDEPS_INTERFACE=[
            'icu4c' + icuSuffix + '/source/icu_i18n',
        ])

icuEnv.Library(
    target='shim_icu',
    source=[
        'shim_icu.cpp',
    ])

libarchiveEnv = env.Clone()
libarchiveEnv.InjectThirdPartyIncludePaths(libraries=['libarchive'])
libarchiveEnv.SConscript('libarchive' + libarchiveSuffix + '/libarchive/SConscript', exports={ 'env' : libarchiveEnv})
libarchiveEnv = libarchiveEnv.Clone(
    LIBDEPS_INTERFACE=[
        'libarchive' + libarchiveSuffix + '/libarchive/libarchive',
    ])

libarchiveEnv.Library(
    target='shim_libarchive',
    source=[
        'shim_libarchive.cpp',
    ])

