# -*- mode: python -*-

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

boostSuffix = "-1.60.0"
snappySuffix = '-1.1.3'
zlibSuffix = '-1.2.11'
pcreSuffix = "-8.41"
mozjsSuffix = '-45'
yamlSuffix = '-0.5.3'
icuSuffix = '-57.1'
gperftoolsSuffix = '-2.5'

thirdPartyIncludePathList = [
    ('s2', '#/src/third_party/s2'),
    ('tz', '#/src/third_party/tz'),
]

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))

# 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))

# 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 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.
# The wiredtiger_ext.h is a static file in the source tree
# In the system installs, wiredtiger.h and wiredtiger_ext.h are in the same directory
if wiredtiger and not use_system_version_of_library('wiredtiger'):
    thirdPartyIncludePathList.append(
        ('wiredtiger', '$BUILD_DIR/third_party/wiredtiger'))
    thirdPartyIncludePathList.append(
        ('wiredtiger_ext', '#/src/third_party/wiredtiger/src/include'))

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-asio-1-11-0/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')


murmurEnv = env.Clone()
murmurEnv.SConscript('murmurhash3/SConscript', exports={ 'env' : murmurEnv })


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


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=[
            '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_THREAD_SYSLIBDEP'],
                env['LIBDEPS_BOOST_SYSTEM_SYSLIBDEP'],
                env['LIBDEPS_BOOST_IOSTREAMS_SYSLIBDEP'],
                env['LIBDEPS_BOOST_CHRONO_SYSLIBDEP'],
                env['LIBDEPS_BOOST_REGEX_SYSLIBDEP'],
            ])
else:
    boostDirectory = 'boost' + boostSuffix
    boostEnv = env.Clone()
    boostEnv.InjectThirdPartyIncludePaths(libraries=['boost'])
    boostEnv.SConscript(boostDirectory + '/SConscript', exports={ 'env' : boostEnv })
    boostEnv = boostEnv.Clone(
        LIBDEPS=[
            boostDirectory + '/boost_program_options',
            boostDirectory + '/boost_filesystem',
            boostDirectory + '/boost_thread',
            boostDirectory + '/boost_system',
            boostDirectory + '/boost_chrono',
            boostDirectory + '/boost_iostreams',
            boostDirectory + '/boost_regex',
        ])

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=[
            'snappy' + snappySuffix + '/snappy',
        ])

snappyEnv.Library(
    target="shim_snappy",
    source=[
        'shim_snappy.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.InjectMongoIncludePaths()
    zlibEnv.SConscript('zlib' + zlibSuffix + '/SConscript', exports={ 'env' : zlibEnv })
    zlibEnv = zlibEnv.Clone(
        LIBDEPS=[
            'zlib' + zlibSuffix + '/zlib',
        ])

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

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

    mozjsEnv.Library(
        target="shim_mozjs",
        source=[
            'shim_mozjs.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.InjectMongoIncludePaths()
        gperftoolsEnv.SConscript('gperftools' + gperftoolsSuffix + '/SConscript', exports={ 'env' : gperftoolsEnv })
        gperftoolsEnv = gperftoolsEnv.Clone(
            LIBDEPS=[
                '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=[
            '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=[
            'yaml-cpp' + yamlSuffix + '/yaml',
        ])

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


tzEnv = env.Clone()
if env.TargetOSIs('solaris'):
    tzEnv.InjectThirdPartyIncludePaths(libraries=['tz'])
    tzEnv.SConscript('tz/SConscript', exports={ 'env' : tzEnv })
    tzEnv = tzEnv.Clone(
        LIBDEPS=[
            'tz/tz',
        ])

tzEnv.Library(
    target='shim_tz',
    source=[
        'shim_tz.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=[
                'wiredtiger/wiredtiger',
            ])

    wiredtigerEnv.Library(
        target="shim_wiredtiger",
        source=[
            'shim_wiredtiger.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-asio-1-11-0/SConscript', exports={ 'env' : asioEnv })
    asioEnv = asioEnv.Clone(
        LIBDEPS=[
            'asio-asio-1-11-0/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=[
	    '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=[
            'icu4c' + icuSuffix + '/source/icu_i18n',
        ])

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