# -*- mode: python -*-

Import([
    "get_option",
    "env",
    ])

env = env.Clone()
env.InjectThirdParty(libraries=['zlib'])

env['CCFLAGS_WERROR'] = []

def removeIfPresent(lst, item):
    try:
        lst.remove(item)
    except ValueError:
        pass

for to_remove in ['-Wall', '-W', '/W3', '-Wsign-compare']:
    removeIfPresent(env['CCFLAGS'], to_remove)

# See what -D's show up in make.  The AB_CD one might change, but we're little
# endian only for now so I think it's sane
env.Prepend(CPPDEFINES=[
        'IMPL_MFBT',
        'JS_USE_CUSTOM_ALLOCATOR',
        'STATIC_JS_API=1',
        'U_NO_DEFAULT_INCLUDE_UTF_HEADERS=1',
        ])

if get_option('spider-monkey-dbg') == "on":
    env.Prepend(CPPDEFINES=[
            'DEBUG',
            'JS_DEBUG',
            'JS_GC_ZEAL',
            ])


env.Append(
    FORCEINCLUDES=[
        'js-confdefs.h'
    ],
)

if env.TargetOSIs('windows'):
    env.Append(
        CCFLAGS=[
            # 'declaration' : no matching operator delete found; memory will not be freed if
            # initialization throws an exception
            '/wd4291',

            # name" : the inline specifier cannot be used when a friend declaration refers to a
            # specialization of a function template
            '/wd4396',

            # nonstandard extension used : zero-sized array in struct/union
            '/wd4200',

            # 'identifier' : no suitable definition provided for explicit template instantiation
            # request
            '/wd4661',

            # 'operation' : unsafe mix of type 'type' and type 'type' in operation
            '/wd4805',

            # 'reinterpret_cast': conversion from 'type' to 'type' of greater size
            '/wd4312',

            # 'operator': unsafe use of type 'type' in operation
            '/wd4804',
        ],
        CXXFLAGS=[
            # TODO(SERVER-59417): Override any potentially applied
            # C++20 switch to unconditionally build in C++17 mode, at
            # least until MozJS is upgraded to an ESR that can be
            # compiled in C++20 mode.
            '/std:c++17',
        ],
    )
else:
    env.Append(
        CXXFLAGS=[
            '-Wno-non-virtual-dtor',
            '-Wno-invalid-offsetof',
            '-Wno-sign-compare',
            # TODO(SERVER-59417): Override any potentially applied
            # C++20 switch to unconditionally build in C++17 mode, at
            # least until MozJS is upgraded to an ESR that can be
            # compiled in C++20 mode.
            '-std=c++17',
        ],
    )

# js/src, js/public and mfbt are the only required sources right now, that
# could change in the future
#
# Also:
# We pre-generate configs for platforms and just check them in.  Running
# mozilla's config requires a relatively huge portion of their tree.
env.Prepend(CPPPATH=[
    'extract/js/src',
    'extract/mfbt',
    'extract/intl/icu/source/common',
    'include',
    'mongo_sources',
    'platform/' + env["TARGET_ARCH"] + "/" + env["TARGET_OS"] + "/build",
    'platform/' + env["TARGET_ARCH"] + "/" + env["TARGET_OS"] + "/include",
])

sources = [
    "mongo_sources/mongoErrorReportToString.cpp",
    "mongo_sources/freeOpToJSContext.cpp",
    "extract/js/src/builtin/RegExp.cpp",
    "extract/js/src/frontend/Parser.cpp",
    "extract/js/src/gc/StoreBuffer.cpp",
    "extract/js/src/jsarray.cpp",
    "extract/js/src/jsmath.cpp",
    "extract/js/src/mfbt/Unified_cpp_mfbt0.cpp",
    "extract/js/src/perf/pm_stub.cpp",
    "extract/js/src/util/DoubleToString.cpp",
    "extract/js/src/vm/Interpreter.cpp",
    "extract/js/src/vm/JSAtom.cpp",
    "extract/mfbt/Compression.cpp",
    "extract/mfbt/double-conversion/double-conversion/strtod.cc",
    "extract/mfbt/lz4.c",
    "extract/mozglue/misc/Printf.cpp",
    "extract/mozglue/misc/TimeStamp.cpp",
    "extract/mozglue/misc/StackWalk.cpp",
]

if env.TargetOSIs('windows'):
    sources.extend([
    "extract/mozglue/misc/ConditionVariable_windows.cpp",
    "extract/mozglue/misc/Mutex_windows.cpp",
    "extract/mozglue/misc/TimeStamp_windows.cpp",
    ])
else:
    sources.extend([
    "extract/mozglue/misc/ConditionVariable_posix.cpp",
    "extract/mozglue/misc/Mutex_posix.cpp",
    "extract/mozglue/misc/TimeStamp_posix.cpp",
    ])



sources.append( [ "extract/modules/fdlibm/{}".format(f) for f in [
        'e_acos.cpp',
        'e_acosh.cpp',
        'e_asin.cpp',
        'e_atan2.cpp',
        'e_atanh.cpp',
        'e_cosh.cpp',
        'e_exp.cpp',
        'e_hypot.cpp',
        'e_log.cpp',
        'e_log10.cpp',
        'e_log2.cpp',
        'e_pow.cpp',
        'e_sinh.cpp',
        'e_sqrt.cpp',
        'k_exp.cpp',
        's_asinh.cpp',
        's_atan.cpp',
        's_cbrt.cpp',
        's_ceil.cpp',
        's_ceilf.cpp',
        's_copysign.cpp',
        's_expm1.cpp',
        's_fabs.cpp',
        's_floor.cpp',
        's_floorf.cpp',
        's_log1p.cpp',
        's_nearbyint.cpp',
        's_rint.cpp',
        's_rintf.cpp',
        's_scalbn.cpp',
        's_tanh.cpp',
        's_trunc.cpp',
        's_truncf.cpp',
]])


if env['TARGET_ARCH'] == 'x86_64':
    sources.extend([
        "extract/js/src/jit/x86-shared/Disassembler-x86-shared.cpp",
    ])

if env.TargetOSIs('windows'):
    env.Prepend(CPPDEFINES=[
        ("_CRT_RAND_S", "1")
    ])

if env['TARGET_ARCH'] == 'x86_64':
    env.Prepend(CPPDEFINES=[
        ("WASM_HUGE_MEMORY", "1")
    ])

sources.extend(Glob('platform/' + env["TARGET_ARCH"] + "/" + env["TARGET_OS"] + "/build/*.cpp")),

# All of those unified sources come in from configure.  The files don't
# actually build individually anymore.
env.Library(
    target="mozjs",
    source=sources,
    LIBDEPS_TAGS=[
        # Depends on allocation symbols defined elsewhere
        'illegal_cyclic_or_unresolved_dependencies_allowlisted',
    ],
)
