# -*- mode: python; -*-

import libdeps

Import("env")
Import("get_option")

env = env.Clone()


if get_option('install-mode') == 'hygienic':
    env.AutoInstall(
        'share/doc/mongo_embedded',
        source=[
            '#/LICENSE-Community.txt',
            '#/distsrc/THIRD-PARTY-NOTICES',
            '../LICENSE-Embedded.txt',
        ],
        INSTALL_ALIAS=[
            'embedded-dev',
        ],
    )

mongoEmbeddedEnv = env.Clone()
mongoEmbeddedEnv.AppendUnique(
    CPPDEFINES=[
        'MONGO_EMBEDDED_COMPILING',
    ],
)

if get_option('link-model') == 'static':
    mongoEmbeddedEnv.AppendUnique(
        CPPDEFINES=[
            'MONGO_EMBEDDED_STATIC',
        ],
    )
elif get_option('link-model') == 'dynamic-sdk':
    mongoEmbeddedEnv['LIBDEPS_SHLIBEMITTER'] = libdeps.make_libdeps_emitter(
        'SharedArchive',
        libdeps.dependency_visibility_honored
    )

mongoEmbeddedEnv.AppendUnique(
    SHLINKFLAGS=[
        '$MONGO_EXPORT_FILE_SHLINKFLAGS',
    ],
)

if mongoEmbeddedEnv.TargetOSIs('darwin'):
    # The current version and compatibility are the *minor* ABI
    # version metadata. If you extend (but do not break) the ABI, you
    # should increment current_version but leave compatibiity_version
    # at 1. If you break ABI, you should bump the entire library from
    # v1 to v2, in which case you would then reset both
    # current_version and compatibility_version to 1. You should never
    # need to set compatibility_version to anything but 1.
    mongoEmbeddedEnv.AppendUnique(
        SHLINKFLAGS=[
            '-Wl,-current_version,1',
            '-Wl,-compatibility_version,1',
        ],
    )


mongoEmbeddedTargets = mongoEmbeddedEnv.Library(
    target='mongo_embedded',
    source=[
        'mongo_embedded.cpp',
    ],
    LIBDEPS_PRIVATE=[
        '$BUILD_DIR/mongo/db/service_context',
        '$BUILD_DIR/mongo/rpc/protocol',
        '$BUILD_DIR/mongo/transport/transport_layer_mock',
        '$BUILD_DIR/mongo/embedded/embedded',
    ],
    INSTALL_ALIAS=[
        'embedded-dev',
    ],
)

if get_option('install-mode') == 'hygienic':
    env.AutoInstall(
        'include/mongo_embedded/v1/mongo_embedded',
        source=['mongo_embedded.h'],
        INSTALL_ALIAS=[
            'embedded-dev',
        ],
    )

yamlEnv = env.Clone()
yamlEnv.InjectThirdPartyIncludePaths(libraries=['yaml'])

if get_option('link-model') != 'dynamic-sdk':
    mongoEmbeddedTest = yamlEnv.Program(
        target='mongo_embedded_test',
        source=[
            'mongo_embedded_test.cpp',
        ],
        LIBDEPS=[
            '$BUILD_DIR/mongo/base',
            '$BUILD_DIR/mongo/db/commands/test_commands_enabled',
            '$BUILD_DIR/mongo/db/server_options_core',
            '$BUILD_DIR/mongo/rpc/protocol',
            '$BUILD_DIR/mongo/unittest/unittest',
            '$BUILD_DIR/mongo/util/net/network',
            '$BUILD_DIR/mongo/util/options_parser/options_parser',
            'mongo_embedded',
        ],
        INSTALL_ALIAS=[
            'embedded-test',
        ],
    )

    env.RegisterUnitTest(mongoEmbeddedTest[0])

# Frameworkization craziness begins here. Honestly, we should do this
# better in the future in some re-usable way, but we need to get this
# thing out the door, so here goes.

# First, we only do this in hygienic mode for the mobile targets,
# which are darwin but not macOS. For all others, we abort here. Maybe
# this should be a build flag? Since we aren't doing this for macOS,
# we can also ignore all the framework version nonsense.
if get_option('link-model') != 'dynamic-sdk' or get_option('install-mode') != 'hygienic' or not env.TargetOSIs('darwin') or env.TargetOSIs('macOS'):
    Return()

frameworkDir = env.Dir('$INSTALL_DIR/Frameworks/mongo_embedded.framework')
env.Alias('install-embedded-dev', frameworkDir)

resourceDir = frameworkDir
if env.TargetOSIs('macOS'):
    resourceDir = resourceDir.Dir('Resources')

env.Install(
    target=resourceDir,
    source=env.File(
        name=[
            'LICENSE-Community.txt',
            'LICENSE-Embedded.txt',
            'THIRD-PARTY-NOTICES',
        ],
        directory=env.Dir('$INSTALL_DIR/share/doc/mongo_embedded'),
    ),
)

env.Install(
    target=frameworkDir.Dir('Headers'),
    source=env.File('mongo_embedded.h')
)

env.InstallAs(
    target=frameworkDir.File('Modules/module.modulemap'),
    source="mongo_embedded.modulemap"
)

mongoEmbeddedPlist = env.Substfile(
    target="Info.plist",
    source='../Info.plist.in',
    SUBST_DICT=[
        ('@CFBundleExecutable@', 'mongo_embedded'),
        ('@CFBundleIdentifier@', 'org.mongodb.mongo-embedded'),
        ('@CFBundleVersion@', env['PLIST_MONGO_BUNDLE_VERSION']),
        ('@CFBundleShortVersionString@', env['PLIST_MONGO_BUNDLE_VERSION']),
        ('@MinimumOSVersion@', env['PLIST_MINIMUM_OS_VERSION'])
    ]
)

env.Install(
    target=resourceDir,
    source=mongoEmbeddedPlist,
)

mongoEmbeddedFwLib = env.InstallAs(
    target=frameworkDir.File('mongo_embedded'),
    source=mongoEmbeddedTargets[0],
)

env.AddPostAction(
    files=mongoEmbeddedFwLib,
    action=[
        "install_name_tool -delete_rpath @loader_path/../lib $TARGET",
        "install_name_tool -id @rpath/mongo_embedded.framework/mongo_embedded $TARGET",
    ]
)

mongoEmbeddedDSYM = getattr(mongoEmbeddedTargets[0].attributes, "separate_debug_file", None)
if mongoEmbeddedDSYM:
    frameworkDSYMDir = '$INSTALL_DIR/Frameworks/mongo_embedded.framework.dSYM'
    env.Alias('install-embedded-dev', frameworkDSYMDir)

    env.InstallAs(
        target=frameworkDSYMDir,
        source=mongoEmbeddedDSYM,
    )

mongoEmbeddedBCSymbolMap = getattr(mongoEmbeddedTargets[0].attributes, "bcsymbolmap_file", None)
if mongoEmbeddedBCSymbolMap:
    env.Install(
        target=frameworkDir.Dir('BCSymbolMaps'),
        source=mongoEmbeddedBCSymbolMap,
    )
