# -*- mode: python; -*-

Import([
    'env',
    'get_option',
    'has_option',
    'usemozjs',
    'version_parts',
    'version_extra',
])

env = env.Clone()

env.Library(
    target='benchrun',
    source=[
        'bench.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/client/clientdriver_minimal',
        '$BUILD_DIR/mongo/db/logical_session_id',
        '$BUILD_DIR/mongo/scripting/bson_template_evaluator',
    ],
    LIBDEPS_PRIVATE=[
        '$BUILD_DIR/third_party/shim_pcrecpp',
    ],
)

generateJSErrorCodes = env.Command(
    target=['error_codes.js'],
    source=[
        '$BUILD_DIR/mongo/base/generate_error_codes.py',
        '$BUILD_DIR/mongo/base/error_codes.yml',
        'error_codes.tpl.js',
    ],
    action=['$PYTHON ${SOURCES[0]} ${SOURCES[1]} ${SOURCES[2]} ${TARGETS[0]}'])
env.Alias('generated-sources', generateJSErrorCodes)

# Files added here need to be added in scripting/engine.cpp and buildscripts/vcxproj.header as well.
js_header = env.JSHeader(
    target="mongojs.cpp",
    source=[
        "assert.js",
        "bulk_api.js",
        "check_log.js",
        "collection.js",
        "crud_api.js",
        "db.js",
        "error_codes.js",
        "explainable.js",
        "explain_query.js",
        "mongo.js",
        "query.js",
        "session.js",
        "types.js",
        "utils_auth.js",
        "utils.js",
        "utils_sh.js",
    ]
)

env.Alias('generated-sources', js_header)

# Files added here need to be added in shell/shell_utils.cpp as well.
env.JSHeader(
    target="mongo-server.cpp",
    source=[
        "servers.js",
        "shardingtest.js",
        "servers_misc.js",
        "replsettest.js",
        "data_consistency_checker.js",
        "bridge.js",
        "keyvault.js",
        "feature_compatibility_version.js",
    ],
)

env.Alias('generated-sources', "mongo-server.cpp")

env.Library(
    target='mongojs',
    source=[
        'mongojs.cpp',
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/base",
    ],
)

env.Library(
    target='shell_options_storage',
    source=[
        'shell_options_storage.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
    ],
    LIBDEPS_PRIVATE=[
        '$BUILD_DIR/mongo/client/connection_string',
        '$BUILD_DIR/mongo/util/cmdline_utils/cmdline_utils',
    ],
)

env.Library(
    target='shell_options_register',
    source=[
        'shell_options.idl',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        'shell_options_storage',
    ],
    LIBDEPS_PRIVATE=[
        '$BUILD_DIR/mongo/util/options_parser/options_parser',
    ],
)

env.Library(
    target='shell_utils',
    source=[
        'mongo-server.cpp',
        'shell_options.cpp',
        'shell_utils.cpp',
        'shell_utils_extended.cpp',
        'shell_utils_launcher.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base/environment_buffer',
        '$BUILD_DIR/mongo/client/clientdriver_network',
        '$BUILD_DIR/mongo/db/mongohasher',
        '$BUILD_DIR/mongo/db/traffic_reader',
        '$BUILD_DIR/mongo/scripting/scripting',
        '$BUILD_DIR/mongo/transport/message_compressor',
        '$BUILD_DIR/mongo/util/password',
        'benchrun',
        'shell_options_storage',
    ]
)

if get_option('ssl') == 'on':
    kmsEnv = env.Clone()

    kmsEnv.InjectThirdParty(libraries=['kms-message'])

    kmsEnv.Library(
        target="kms",
        source=[
            "kms.cpp",
            "kms_aws.cpp",
            "kms_azure.cpp",
            "kms_gcp.cpp",
            "kms_local.cpp",
            "kms_network.cpp",
            "kms.idl",
        ],
        LIBDEPS=[
            '$BUILD_DIR/mongo/base/secure_allocator',
        ],
        LIBDEPS_PRIVATE=[
            '$BUILD_DIR/mongo/base',
            '$BUILD_DIR/mongo/crypto/aead_encryption',
            '$BUILD_DIR/mongo/db/commands/test_commands_enabled',
            '$BUILD_DIR/mongo/util/net/network',
            '$BUILD_DIR/mongo/util/net/ssl_manager',
            '$BUILD_DIR/mongo/util/net/ssl_options',
            '$BUILD_DIR/third_party/shim_kms_message',
        ],
    )

    env.Library(
        target="kms_shell",
        source=[
            "kms_shell.cpp",
        ],
        LIBDEPS_PRIVATE=[
            'kms',
            'shell_utils',
        ],
    )

    scriptingEnv = env.Clone()
    scriptingEnv.InjectMozJS()

    scriptingEnv.Library(
        target="encrypted_dbclient",
        source=[
            "encrypted_dbclient_base.cpp",
            "fle_shell_options.idl",
        ],
        LIBDEPS_PRIVATE=[
            '$BUILD_DIR/mongo/client/clientdriver_minimal',
            '$BUILD_DIR/mongo/crypto/aead_encryption',
            '$BUILD_DIR/mongo/crypto/symmetric_crypto',
            '$BUILD_DIR/mongo/scripting/scripting',
            '$BUILD_DIR/mongo/shell/shell_options_register',
            '$BUILD_DIR/third_party/shim_mozjs',
            'kms',
        ],
    )

env.Library(
    target="linenoise_utf8",
    source=[
        "linenoise_utf8.cpp",
    ],
)

env.Library(
    target="linenoise",
    source=[
        "linenoise.cpp",
        "mk_wcwidth.cpp"
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
    ],
    LIBDEPS_PRIVATE=[
        "linenoise_utf8",
    ],
)

if not has_option('noshell') and usemozjs:

    env.Library(
        target="mongo_initializers",
        source=[
            "mongodbcr.cpp",
            "shell_options_init.cpp",
        ],
        LIBDEPS_PRIVATE=[
            # NOTE: If you need to add a static or mongo initializer to mongo startup,
            # please add that library here, as a private library dependency.
            "$BUILD_DIR/mongo/client/clientdriver_network",
            "$BUILD_DIR/mongo/client/connection_string",
            "$BUILD_DIR/mongo/db/catalog/index_key_validate",
            "$BUILD_DIR/mongo/db/logical_session_id_helpers",
            "$BUILD_DIR/mongo/db/mongohasher",
            "$BUILD_DIR/mongo/db/pipeline/change_stream_invalidation_info",
            "$BUILD_DIR/mongo/db/query/command_request_response",
            "$BUILD_DIR/mongo/db/query/query_request",
            "$BUILD_DIR/mongo/db/server_options_core",
            "$BUILD_DIR/mongo/db/storage/duplicate_key_error_info",
            "$BUILD_DIR/mongo/db/traffic_reader",
            "$BUILD_DIR/mongo/db/views/resolved_view",
            "$BUILD_DIR/mongo/executor/network_interface_factory",
            "$BUILD_DIR/mongo/executor/network_interface_thread_pool",
            "$BUILD_DIR/mongo/executor/thread_pool_task_executor",
            "$BUILD_DIR/mongo/rpc/protocol",
            "$BUILD_DIR/mongo/scripting/scripting",
            "$BUILD_DIR/mongo/transport/message_compressor",
            "$BUILD_DIR/mongo/transport/message_compressor_options_client",
            "$BUILD_DIR/mongo/transport/transport_layer_manager",
            "$BUILD_DIR/mongo/util/net/http_client_impl",
            "$BUILD_DIR/mongo/util/net/network",
            "$BUILD_DIR/mongo/util/net/ssl_options_client" if get_option('ssl') == 'on' else '',
            "$BUILD_DIR/mongo/util/options_parser/options_parser_init",
            "$BUILD_DIR/mongo/util/password",
            "$BUILD_DIR/mongo/util/processinfo",
            "$BUILD_DIR/mongo/util/signal_handlers",
            "$BUILD_DIR/mongo/util/version_impl",
            "$BUILD_DIR/third_party/shim_pcrecpp",
            "benchrun",
            "encrypted_dbclient" if get_option('ssl') == 'on' else '',
            "kms_shell" if get_option('ssl') == 'on' else '',
            "mongojs",
            "shell_options_register",
            "shell_utils",
        ],
        LIBDEPS=[
            # NOTE: This list must remain empty. Please only add to LIBDEPS_PRIVATE
        ],

    )

    env.Library(
        target="mongo_main",
        source=[
            "mongo_main.cpp",
        ],
        LIBDEPS_PRIVATE=[
            # NOTE: Do not add new libdeps (public or private) here unless
            # required by the linker to satisfy symbol dependencies from
            # the files listed above in `sources`. If you need to add a
            # library to inject a static or mongo initializer to mongo,
            # please add that library as a private libdep of
            # mongo_initializers.
            "$BUILD_DIR/mongo/transport/transport_layer",
            "$BUILD_DIR/mongo/util/net/ssl_manager",
            "$BUILD_DIR/mongo/util/signal_handlers",
            "$BUILD_DIR/third_party/shim_pcrecpp",
            "linenoise",
            "mongo_initializers",
            "shell_utils",
        ],
        LIBDEPS=[
            # NOTE: This list must remain empty. Please only add to LIBDEPS_PRIVATE
        ],
    )

    shellEnv = env.Clone()
    if env.TargetOSIs('windows'):
        shellEnv.Append(LIBS=["winmm"])

        generatedMongoManifest = shellEnv.Substfile(
                'mongo.manifest.in',
                SUBST_DICT=[
                    ('@mongo_version_major@', version_parts[0]),
                    ('@mongo_version_minor@', version_parts[1]),
                    ('@mongo_version_patch@', version_parts[2]),
                    ('@mongo_version_extra@', version_parts[3]),
                    ('@mongo_version_extra_str@', version_extra),
                ])
        shellEnv.Alias('generated-sources', generatedMongoManifest)
        shellEnv.Depends("mongo.res", generatedMongoManifest)

    mongo_shell = shellEnv.Program(
        target="mongo",
        source=[
            "mongo.cpp",
        ] + env.WindowsResourceFile("mongo.rc"),
        LIBDEPS=[
            # NOTE: Do not add new libdeps (public or private) here unless
            # required by the linker to satisfy symbol dependencies from
            # the files listed above in `sources`. If you need to add a
            # library to inject a static or mongo initializer to mongo,
            # please add that library as a private libdep of
            # mongo_initializers.
            '$BUILD_DIR/mongo/base',
            'mongo_main',
        ],
        AIB_COMPONENT="mongo",
        AIB_COMPONENTS_EXTRA=[
            "core",
            "default",
            "devcore",
            "dist",
            "dist-test",
            "shell",
            "integration-tests",
        ],
    )


env.CppUnitTest(
    target='shell_test',
    source=[
        'kms_test.cpp' if get_option('ssl') == 'on' else [],
        'shell_options_test.cpp',
        'shell_utils_test.cpp'
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/util/signal_handlers',
        'kms' if get_option('ssl') == 'on' else [],
        'shell_options_register',
        'shell_utils',
    ],
)
