# -*- mode: python -*-

Import("env")
Import("has_option")
Import("wiredtiger")

env.SConscript(
    dirs=[
        'auth',
        'bson',
        'catalog',
        'commands',
        'concurrency',
        'exec',
        'fts',
        'ftdc',
        'geo',
        'index',
        'matcher',
        'ops',
        'pipeline',
        'query',
        'repl',
        's',
        'sorter',
        'stats',
        'storage',
        'views',
    ],
)

#
# The db/'common' lib has the abstractions that are shared by components of the
# server. Ideally, many of the object in 'coredb' should be moved here when their dependencies
# get resolved.
#

env.Library(
    target= 'common',
    source= [
        'field_ref.cpp',
        'field_ref_set.cpp',
        'field_parser.cpp',
        'keypattern.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/db/index_names',
        '$BUILD_DIR/mongo/db/write_concern_options',
    ]
)

env.CppUnitTest(
    target= 'field_ref_test',
    source= 'field_ref_test.cpp',
    LIBDEPS=[
        'common',
    ],
)

env.CppUnitTest(
    target= 'field_ref_set_test',
    source = 'field_ref_set_test.cpp',
    LIBDEPS=[
        'common',
    ],
)

env.CppUnitTest(
    target= 'field_parser_test',
    source= 'field_parser_test.cpp',
    LIBDEPS=[
        'common',
    ],
)

env.CppUnitTest(
    target= 'hasher_test',
    source= [
        'hasher_test.cpp',
    ],
    LIBDEPS=[
        'common',
        '$BUILD_DIR/mongo/db/mongohasher',
    ],
)

env.CppUnitTest(
    target= 'keypattern_test',
    source= 'keypattern_test.cpp',
    LIBDEPS=[
        'common'
    ],
)

env.Library(
    target="dbmessage",
    source=[
        "dbmessage.cpp",
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/transport/transport_layer_common',
        '$BUILD_DIR/mongo/util/net/network',
    ]
)

env.CppUnitTest(
    target="dbmessage_test",
    source=[
        "dbmessage_test.cpp"
    ],
    LIBDEPS=[
        "dbmessage",
    ],
)
env.CppUnitTest('record_id_test', 'record_id_test.cpp', LIBDEPS=[])

env.Library(
    target='startup_warnings_common',
    source=[
        'startup_warnings_common.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/util/net/network',
    ]
)

env.Library(
    target='startup_warnings_mongod',
    source=[
        'startup_warnings_mongod.cpp',
    ],
    LIBDEPS=[
        'startup_warnings_common',
        '$BUILD_DIR/mongo/util/processinfo',
    ]
)

env.CppUnitTest(
    target='startup_warnings_mongod_test',
    source=[
        'startup_warnings_mongod_test.cpp',
    ],
    LIBDEPS=[
        'startup_warnings_mongod',
    ],
)

env.Library(
    target='curop',
    source=[
        'curop.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/bson/mutable/mutable_bson',
        '$BUILD_DIR/mongo/db/commands',
        '$BUILD_DIR/mongo/db/concurrency/lock_manager',
        '$BUILD_DIR/mongo/db/service_context',
        '$BUILD_DIR/mongo/db/query/command_request_response',
        '$BUILD_DIR/mongo/rpc/client_metadata',
        '$BUILD_DIR/mongo/util/fail_point',
        '$BUILD_DIR/mongo/util/net/network',
        '$BUILD_DIR/mongo/util/progress_meter',
    ],
)

env.CppUnitTest(
    target='operation_context_test',
    source=[
        'operation_context_test.cpp',
    ],
    LIBDEPS=[
        'service_context',
        '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init',
        '$BUILD_DIR/mongo/db/service_context_noop_init',
        '$BUILD_DIR/mongo/unittest/concurrency',
        '$BUILD_DIR/mongo/util/clock_source_mock',
    ],
)

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

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

# Range arithmetic library, used by both mongod and mongos
env.Library(
    target='range_arithmetic',
    source=[
        'range_arithmetic.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
    ],
)

env.CppUnitTest(
    target='range_arithmetic_test',
    source=[
        'range_arithmetic_test.cpp',
    ],
    LIBDEPS=[
        'range_arithmetic',
    ],
)

env.Library(
    target='global_timestamp',
    source=[
        'global_timestamp.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/db/service_context'
    ]
)

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

env.CppUnitTest(
    target='namespace_string_test',
    source=[
        'namespace_string_test.cpp',
    ],
    LIBDEPS=[
        'namespace_string',
    ],
)

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

env.CppUnitTest(
    target="server_parameters_test",
    source=[
        "server_parameters_test.cpp",
    ],
    LIBDEPS=[
        "server_parameters",
        '$BUILD_DIR/mongo/db/service_context',
    ],
)

env.Library(
    target='update_index_data',
    source=[
        'update_index_data.cpp'
    ],
    LIBDEPS=[
        'common',
    ],
)

env.CppUnitTest(
    target='update_index_data_test',
    source=[
        'update_index_data_test.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        'common',
        'update_index_data',
    ],
)

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

env.CppUnitTest(
    target='collection_index_usage_tracker_test',
    source=[
        'collection_index_usage_tracker_test.cpp',
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/util/clock_source_mock",
        'collection_index_usage_tracker',
    ],
)

# This library exists because some libraries, such as our networking library, need access to server
# options, but not to the helpers to set them from the command line.  libserver_options_core.a just
# has the structure for storing the server options, while libserver_options.a has the code to set
# them via the command line.
env.Library(
    target="server_options_core",
    source=[
        "server_options.cpp",
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
    ],
)

env.Library(
    target="server_options",
    source=[
        "server_options_helpers.cpp",
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/util/cmdline_utils/cmdline_utils',
        '$BUILD_DIR/mongo/transport/message_compressor',
        # The dependency on network is a temporary crutch that should go away once the
        # networking library has separate options
        '$BUILD_DIR/mongo/util/net/network',
        '$BUILD_DIR/mongo/util/options_parser/options_parser',
        'server_options_core',
        'server_parameters',
    ],
)

env.CppUnitTest(
    target='server_options_test',
    source=[
        'server_options_test.cpp',
    ],
    LIBDEPS=[
        'server_options',
    ],
)

env.Library(
    target='range_deleter',
    source=[
        'range_deleter.cpp',
        'range_deleter_mock_env.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/db/common',
        '$BUILD_DIR/mongo/db/repl/repl_coordinator_global',
        '$BUILD_DIR/mongo/db/service_context',
        'range_arithmetic',
    ],
)

env.CppUnitTest(
    target='range_deleter_test',
    source=[
        'range_deleter_test.cpp',
    ],
    LIBDEPS=[
        'commands_test_crutch',
        'common',
        'range_deleter',
        '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init',
        '$BUILD_DIR/mongo/db/repl/replmocks',
        '$BUILD_DIR/mongo/db/service_context_noop_init',
    ],
)

# This library is linked into mongos and mongod only, not into the shell or any tools.
env.Library(
    target="mongodandmongos",
    source=[
        "dbwebserver.cpp",
        "initialize_server_global_state.cpp",
        "server_extra_log_context.cpp",
        "server_options_init.cpp",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/client/clientdriver",
        "$BUILD_DIR/mongo/db/auth/authservercommon",
        "$BUILD_DIR/mongo/logger/max_log_size",
        "$BUILD_DIR/mongo/rpc/command_reply",
        "$BUILD_DIR/mongo/rpc/command_request",
        "$BUILD_DIR/mongo/rpc/metadata",
        "$BUILD_DIR/mongo/util/net/miniwebserver",
        "$BUILD_DIR/mongo/util/processinfo",
        "$BUILD_DIR/mongo/util/signal_handlers",
        "auth/authorization_manager_global",
    ],
    LIBDEPS_TAGS=[
        # Depends on Command::execCommand, which is not uniquely defined
        'incomplete',
    ],
)

env.Library(
    target="mongodwebserver",
    source=[
        "clientlistplugin.cpp",
        "restapi.cpp",
        "stats/snapshots_webplugins.cpp",
        "repl/repl_set_web_handler.cpp",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/db/curop",
        "$BUILD_DIR/mongo/db/dbdirectclient",
        "$BUILD_DIR/mongo/util/net/miniwebserver",
        "mongodandmongos",
        'repl/repl_coordinator_global',
    ],
    LIBDEPS_TAGS=[
        # Depends on symbols from serverOnlyFiles
        'incomplete',
    ],
)

# mongod options
env.Library(
    target="mongod_options",
    source=[
        "mongod_options.cpp",
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/db/serveronly',
        '$BUILD_DIR/mongo/util/options_parser/options_parser_init',
        'repl/replica_set_messages',
        'server_options',
    ],
)

env.Library(
    target="write_concern_options",
    source=[
        "write_concern_options.cpp",
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/bson/util/bson_extract',
    ],
)

env.CppUnitTest(
    target= 'write_concern_options_test',
    source= 'write_concern_options_test.cpp',
    LIBDEPS=[
        'write_concern_options',
    ],
)

env.Library(
    target='service_context',
    source=[
        'client.cpp',
        'operation_context.cpp',
        'service_context.cpp',
        'service_context_noop.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/util/clock_sources',
        '$BUILD_DIR/mongo/util/concurrency/spin_lock',
        '$BUILD_DIR/mongo/util/decorable',
        '$BUILD_DIR/mongo/util/fail_point',
        '$BUILD_DIR/mongo/util/net/hostandport',
        '$BUILD_DIR/mongo/transport/transport_layer_common',
        '$BUILD_DIR/mongo/transport/transport_layer_manager',
    ],
)

env.Library(
    target='service_context_noop_init',
    source=[
        'service_context_noop_init.cpp'
    ],
    LIBDEPS=[
        'service_context',
    ],
)

env.Library(
    target='lasterror',
    source=[
        "lasterror.cpp",
    ],
    LIBDEPS=[
        'service_context',
    ],
)

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

env.Library(
    target="commands",
    source=[
        'commands.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/bson/mutable/mutable_bson',
        '$BUILD_DIR/mongo/db/auth/authcore',
        '$BUILD_DIR/mongo/db/wire_version',
        '$BUILD_DIR/mongo/rpc/command_status',
        '$BUILD_DIR/mongo/rpc/rpc',
        '$BUILD_DIR/mongo/s/write_ops/batch_write_types',
        'audit',
        'auth/authorization_manager_global',
        'commands/server_status_core',
        'commands/test_commands_enabled',
        'service_context',
    ],
    LIBDEPS_TAGS=[
        # Dependencies on Command::registerError and
        # Command::execCommand, which are not uniquely defined.
        'incomplete',
    ],
)

env.Library(
    target="commands_test_crutch",
    source=[
        "commands_test_crutch.cpp",
    ],
    LIBDEPS=[
        "commands",
    ],
)

env.CppUnitTest(
    target="commands_test",
    source=[
        "commands_test.cpp",
    ],
    LIBDEPS=[
        "commands",
        "commands_test_crutch",
        "service_context_noop_init",
        "auth/authorization_manager_mock_init",
        "repl/replmocks",
    ],
)

# TODO: This library should probably be folded into catalog/catalog,
# with which it is currently circular.
env.Library(
    target='db_raii',
    source=[
        'db_raii.cpp',
    ],
    LIBDEPS=[
        'catalog/catalog',
        'stats/top',
        'views/views',
    ],
)

env.Library(
    target='run_commands',
    source=[
        'run_commands.cpp',
    ],
    LIBDEPS=[
        "commands",
        "curop",
    ],
    LIBDEPS_TAGS=[
        # Dependency on Command::execCommand, which is not uniquely
        # defined.
        'incomplete',
    ],
)

env.Library(
    target='dbdirectclient',
    source=[
        'dbdirectclient.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/client/clientdriver',
        'curop',
        'lasterror',
    ],
    LIBDEPS_TAGS=[
        # Depends on assembleResponse, defined in instance.cpp below.
        'incomplete',
    ],
)

env.Library(
    target='dbhelpers',
    source=[
        'dbhelpers.cpp',
    ],
    LIBDEPS=[
        'db_raii',
        'index/index_access_methods',
        'ops/write_ops',
    ],
)

# mongod files - also files used in tools. present in dbtests, but not in mongos and not in client
# libs.
serverOnlyFiles = [
    "background.cpp",
    "clientcursor.cpp",
    "cloner.cpp",
    "curop_metrics.cpp",
    "index_builder.cpp",
    "index_legacy.cpp",
    "index_rebuilder.cpp",
    "instance.cpp",
    "introspect.cpp",
    "op_observer_impl.cpp",
    "operation_context_impl.cpp",
    "prefetch.cpp",
    "range_deleter_db_env.cpp",
    "range_deleter_service.cpp",
    "read_concern.cpp",
    "repair_database.cpp",
    "s/sharding_connection_hook_for_mongod.cpp",
    "service_context_d.cpp",
    "storage/storage_init.cpp",
    "ttl.cpp",
    "write_concern.cpp",
]

serveronlyLibdeps = [
    "$BUILD_DIR/mongo/db/bson/dotted_path_support",
    "$BUILD_DIR/mongo/executor/network_interface_factory",
    "$BUILD_DIR/mongo/s/catalog/sharding_catalog_client_impl",
    "$BUILD_DIR/mongo/s/catalog/sharding_catalog_manager_impl",
    "$BUILD_DIR/mongo/s/client/sharding_connection_hook",
    "$BUILD_DIR/mongo/s/coreshard",
    "$BUILD_DIR/mongo/s/shard_id",
    "$BUILD_DIR/mongo/s/serveronly",
    "$BUILD_DIR/mongo/scripting/scripting_server",
    "$BUILD_DIR/mongo/util/clock_sources",
    "$BUILD_DIR/mongo/util/elapsed_tracker",
    "$BUILD_DIR/mongo/util/net/network",
    "$BUILD_DIR/mongo/util/net/ssl_manager_status",
    "$BUILD_DIR/mongo/db/storage/mmap_v1/file_allocator",
    "$BUILD_DIR/third_party/shim_snappy",
    '$BUILD_DIR/mongo/db/ttl_collection_cache',
    "auth/authmongod",
    "catalog/catalog",
    "catalog/collection_options",
    "catalog/document_validation",
    "catalog/index_key_validate",
    "commands/killcursors_common",
    "commands/dcommands",
    "collection_index_usage_tracker",
    "common",
    "concurrency/lock_manager",
    "concurrency/write_conflict_exception",
    "curop",
    "dbdirectclient",
    "dbhelpers",
    "db_raii",
    "exec/exec",
    "exec/working_set",
    "fts/ftsmongod",
    "ftdc/ftdc_mongod",
    "global_timestamp",
    "index/index_descriptor",
    "index/index_access_methods",
    "matcher/expressions_mongod_only",
    "ops/write_ops",
    "ops/update_driver",
    "ops/write_ops_parsers",
    "pipeline/aggregation",
    "pipeline/serveronly",
    "query/query",
    "range_deleter",
    "repl/bgsync",
    "repl/initial_sync_common",
    "repl/oplog_buffer_collection",
    "repl/oplog_buffer_blocking_queue",
    "repl/oplog_buffer_proxy",
    "repl/repl_coordinator_global",
    "repl/repl_coordinator_impl",
    "repl/repl_settings",
    "repl/rs_rollback",
    "repl/rslog",
    "repl/serveronly",
    "repl/storage_interface",
    "repl/sync_tail",
    "repl/topology_coordinator_impl",
    "run_commands",
    "s/commands",
    "s/metadata",
    "s/sharding",
    "startup_warnings_mongod",
    "stats/counters",
    "stats/serveronly",
    "stats/top",
    "storage/devnull/storage_devnull",
    "storage/ephemeral_for_test/storage_ephemeral_for_test",
    "storage/mmap_v1/mmap",
    "storage/mmap_v1/storage_mmapv1",
    "storage/storage_engine_lock_file",
    "storage/storage_engine_metadata",
    "storage/storage_options",
    "update_index_data",
    "views/views_mongod",
]

if wiredtiger:
    serveronlyLibdeps.append('storage/wiredtiger/storage_wiredtiger')
    serveronlyLibdeps.append('$BUILD_DIR/third_party/shim_wiredtiger')

env.Library(
    target="serveronly",
    source=serverOnlyFiles,
    LIBDEPS=serveronlyLibdeps,
)

env.Library(
    target= 'op_observer_noop',
    source= [
        'op_observer_noop.cpp',
    ],
    LIBDEPS= [
    ],
)

env.Library(
    target= 'service_context_d_test_fixture',
    source= [
        'service_context_d_test_fixture.cpp',
    ],
    LIBDEPS= [
        'op_observer_noop',
        '$BUILD_DIR/mongo/db/serveronly',
        '$BUILD_DIR/mongo/db/storage/storage_options',
        '$BUILD_DIR/mongo/unittest/unittest',
    ],
)

env.Library(
    target='log_process_details',
    source=[
        'log_process_details.cpp',
    ],
    LIBDEPS=[
        'repl/repl_coordinator_global',
        'repl/repl_coordinator_interface',
        'repl/replica_set_messages',
        'server_options',
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/util/net/network',
    ],
)

env.Library(
    target='conn_pool_options',
    source=[
        'conn_pool_options.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/client/clientdriver',
        '$BUILD_DIR/mongo/db/server_parameters',
        '$BUILD_DIR/mongo/s/client/sharding_client',
    ],
)

env.Library(
    target='ttl_collection_cache',
    source=[
        'ttl_collection_cache.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/base',
        '$BUILD_DIR/mongo/db/service_context',
    ],
)

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

asioEnv = env.Clone()
asioEnv.InjectThirdPartyIncludePaths('asio')

asioEnv.CppIntegrationTest(
    target='nesting_depth_test',
    source=[
        'nesting_depth_test.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/executor/network_interface_asio_fixture',
    ],
)

