# -*- mode: python -*-

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

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

#
# 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',
        'write_concern_options.cpp'
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/bson/bson',
        '$BUILD_DIR/mongo/util/foundation',
        '$BUILD_DIR/mongo/db/index_names',
        '$BUILD_DIR/mongo/db/service_context',
    ],
)

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.CppUnitTest(
    target="dbmessage_test",
    source=[
        "dbmessage_test.cpp"
    ],
    LIBDEPS=[
        "common",
        "$BUILD_DIR/mongo/client/clientdriver",
    ],
)
env.CppUnitTest('record_id_test', 'record_id_test.cpp', LIBDEPS=[])

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

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/server_status_core',
        '$BUILD_DIR/mongo/db/concurrency/lock_manager',
        '$BUILD_DIR/mongo/db/stats/top',
        '$BUILD_DIR/mongo/util/fail_point',
        '$BUILD_DIR/mongo/util/net/network',
        '$BUILD_DIR/mongo/db/service_context',
        '$BUILD_DIR/mongo/util/progress_meter',
    ],
)

env.CppUnitTest(
    target='curop_test',
    source=[
        'curop_test.cpp',
    ],
    LIBDEPS=[
        'curop',
    ],
)

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

env.Library(
    target='mongohasher',
    source=[
        "hasher.cpp",
    ],
)

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

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

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

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

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/bson/bson",
        "$BUILD_DIR/mongo/util/foundation",
    ],
)

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/bson/bson',
        'common',
        'update_index_data',
    ],
)

# 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/bson/bson',
    ],
)

env.Library(
    target="server_options",
    source=[
        "server_options_helpers.cpp",
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/bson/bson',
        '$BUILD_DIR/mongo/util/cmdline_utils/cmdline_utils',
        # 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/base',
        '$BUILD_DIR/mongo/bson/bson',
        '$BUILD_DIR/mongo/db/repl/repl_coordinator_global',
        '$BUILD_DIR/mongo/db/service_context',
        '$BUILD_DIR/mongo/util/concurrency/synchronization',
        'range_arithmetic',
    ],
)

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


# 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",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/util/net/message_server_port",
        "$BUILD_DIR/mongo/util/net/miniwebserver",
        "$BUILD_DIR/mongo/util/signal_handlers",
    ],
)

env.Library(
    target="mongodwebserver",
    source=[
        "clientlistplugin.cpp",
        "restapi.cpp",
        "stats/snapshots_webplugins.cpp",
        "repl/replset_web_handler.cpp",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/db/coredb",
        "$BUILD_DIR/mongo/util/net/miniwebserver",
        "mongodandmongos",
        'repl/repl_coordinator_global',
    ],
)

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

env.Library(
    target='service_context',
    source=[
        'client.cpp',
        'client_basic.cpp',
        'service_context.cpp',
        'service_context_noop.cpp',
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/util/concurrency/spin_lock',
        '$BUILD_DIR/mongo/util/decorable',
        '$BUILD_DIR/mongo/util/net/hostandport',
    ],
)

env.Library(
    tagret='lasterror',
    source=[
        "lasterror.cpp",
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/util/foundation',
        'service_context',
    ],
)

coredbEnv = env.Clone()
coredbEnv.InjectThirdPartyIncludePaths(libraries=['snappy'])
coredbEnv.Library(
    target="coredb",
    source=[
        "audit.cpp",
        "commands.cpp",
        "commands/authentication_commands.cpp",
        "commands/connection_status.cpp",
        "commands/copydb_common.cpp",
        "commands/fail_point_cmd.cpp",
        "commands/find_and_modify_common.cpp",
        "commands/hashcmd.cpp",
        "commands/isself.cpp",
        "repl/isself.cpp",
        "commands/mr_common.cpp",
        "commands/rename_collection_common.cpp",
        "commands/server_status.cpp",
        "commands/parameters.cpp",
        "commands/user_management_commands_common.cpp",
        "commands/write_commands/write_commands_common.cpp",
        "pipeline/pipeline.cpp",
        "dbcommands_generic.cpp",
        "matcher/matcher.cpp",
        "pipeline/accumulator_add_to_set.cpp",
        "pipeline/accumulator_avg.cpp",
        "pipeline/accumulator_first.cpp",
        "pipeline/accumulator_last.cpp",
        "pipeline/accumulator_min_max.cpp",
        "pipeline/accumulator_push.cpp",
        "pipeline/accumulator_sum.cpp",
        "pipeline/dependencies.cpp",
        "pipeline/document_source.cpp",
        "pipeline/document_source_bson_array.cpp",
        "pipeline/document_source_command_shards.cpp",
        "pipeline/document_source_geo_near.cpp",
        "pipeline/document_source_group.cpp",
        "pipeline/document_source_limit.cpp",
        "pipeline/document_source_match.cpp",
        "pipeline/document_source_merge_cursors.cpp",
        "pipeline/document_source_out.cpp",
        "pipeline/document_source_project.cpp",
        "pipeline/document_source_redact.cpp",
        "pipeline/document_source_skip.cpp",
        "pipeline/document_source_sort.cpp",
        "pipeline/document_source_unwind.cpp",
        "pipeline/expression.cpp",
        "stats/timer_stats.cpp",
    ],
    LIBDEPS=[
        '$BUILD_DIR/mongo/client/clientdriver',
        '$BUILD_DIR/mongo/logger/parse_log_component_settings',
        '$BUILD_DIR/mongo/scripting/scripting_common',
        '$BUILD_DIR/mongo/util/cmdline_utils/cmdline_utils',
        '$BUILD_DIR/mongo/util/foundation',
        '$BUILD_DIR/mongo/util/processinfo',
        '$BUILD_DIR/third_party/shim_snappy',
        'auth/serverauth',
        'commands/server_status_core',
        'common',
        'exec/working_set',
        'index/key_generator',
        'index_names',
        'log_process_details',
        'matcher/expressions',
        'matcher/expressions_geo',
        'matcher/expressions_text',
        'pipeline/document_value',
        'server_options',
        'server_parameters',
        'startup_warnings_common',
        'stats/counters',
    ],
)


# mongod files - also files used in tools. present in dbtests, but not in mongos and not in client
# libs.
serverOnlyFiles = [
    "background.cpp",
    "catalog/apply_ops.cpp",
    "catalog/capped_utils.cpp",
    "catalog/coll_mod.cpp",
    "catalog/collection.cpp",
    "catalog/collection_compact.cpp",
    "catalog/collection_info_cache.cpp",
    "catalog/create_collection.cpp",
    "catalog/cursor_manager.cpp",
    "catalog/database.cpp",
    "catalog/database_holder.cpp",
    "catalog/document_validation.cpp",
    "catalog/drop_collection.cpp",
    "catalog/drop_database.cpp",
    "catalog/drop_indexes.cpp",
    "catalog/index_catalog.cpp",
    "catalog/index_catalog_entry.cpp",
    "catalog/index_create.cpp",
    "catalog/rename_collection.cpp",
    "clientcursor.cpp",
    "cloner.cpp",
    "commands/apply_ops.cpp",
    "commands/cleanup_orphaned_cmd.cpp",
    "commands/clone.cpp",
    "commands/clone_collection.cpp",
    "commands/collection_to_capped.cpp",
    "commands/compact.cpp",
    "commands/copydb.cpp",
    "commands/copydb_start_commands.cpp",
    "commands/count.cpp",
    "commands/create_indexes.cpp",
    "commands/current_op.cpp",
    "commands/cursor_responses.cpp",
    "commands/dbhash.cpp",
    "commands/distinct.cpp",
    "commands/drop_indexes.cpp",
    "commands/explain_cmd.cpp",
    "commands/find_and_modify.cpp",
    "commands/find_cmd.cpp",
    "commands/fsync.cpp",
    "commands/geo_near_cmd.cpp",
    "commands/get_last_error.cpp",
    "commands/getmore_cmd.cpp",
    "commands/group.cpp",
    "commands/index_filter_commands.cpp",
    "commands/kill_op.cpp",
    "commands/list_collections.cpp",
    "commands/list_databases.cpp",
    "commands/list_indexes.cpp",
    "commands/merge_chunks_cmd.cpp",
    "commands/mr.cpp",
    "commands/oplog_note.cpp",
    "commands/parallel_collection_scan.cpp",
    "commands/pipeline_command.cpp",
    "commands/plan_cache_commands.cpp",
    "commands/rename_collection.cpp",
    "commands/repair_cursor.cpp",
    "commands/test_commands.cpp",
    "commands/top_command.cpp",
    "commands/touch.cpp",
    "commands/user_management_commands.cpp",
    "commands/validate.cpp",
    "commands/write_commands/batch_executor.cpp",
    "commands/write_commands/write_commands.cpp",
    "commands/writeback_compatibility_shim.cpp",
    "db_raii.cpp",
    "dbcommands.cpp",
    "dbdirectclient.cpp",
    "dbeval.cpp",
    "dbhelpers.cpp",
    "driverHelpers.cpp",
    "geo/haystack.cpp",
    "index/2d_access_method.cpp",
    "index/btree_access_method.cpp",
    "index/fts_access_method.cpp",
    "index/hash_access_method.cpp",
    "index/haystack_access_method.cpp",
    "index/index_access_method.cpp",
    "index/s2_access_method.cpp",
    "index_builder.cpp",
    "index_legacy.cpp",
    "index_rebuilder.cpp",
    "instance.cpp",
    "introspect.cpp",
    "matcher/expression_where.cpp",
    "op_observer.cpp",
    "operation_context_impl.cpp",
    "ops/delete.cpp",
    "ops/insert.cpp",
    "ops/parsed_delete.cpp",
    "ops/parsed_update.cpp",
    "ops/update.cpp",
    "ops/update_lifecycle_impl.cpp",
    "ops/update_result.cpp",
    "pipeline/document_source_cursor.cpp",
    "pipeline/pipeline_d.cpp",
    "prefetch.cpp",
    "range_deleter_db_env.cpp",
    "range_deleter_service.cpp",
    "repair_database.cpp",
    "repl/bgsync.cpp",
    "repl/initial_sync.cpp",
    "repl/master_slave.cpp",
    "repl/minvalid.cpp",
    "repl/oplog.cpp",
    "repl/oplogreader.cpp",
    "repl/replication_coordinator_external_state_impl.cpp",
    "repl/replication_info.cpp",
    "repl/resync.cpp",
    "repl/rs_initialsync.cpp",
    "repl/rs_rollback.cpp",
    "repl/rs_sync.cpp",
    "repl/sync.cpp",
    "repl/sync_source_feedback.cpp",
    "repl/sync_tail.cpp",
    "service_context_d.cpp",
    "stats/fill_locker_info.cpp",
    "stats/lock_server_status_section.cpp",
    "stats/range_deleter_server_status.cpp",
    "stats/snapshots.cpp",
    "storage/storage_init.cpp",
    "storage_options.cpp",
    "ttl.cpp",
    "write_concern.cpp",
]

if has_option('use-cpu-profiler'):
    serverOnlyFiles.append('commands/cpuprofile.cpp')
    # If we are building with our internal gperftools, add the necessary include path.
    #
    # NOTE: This is pretty bad because it will spam this include path into many files that
    # don't require it, but because of the way things are currently set up, it is not easy to
    # scope it more narrowly. Better would be if the commands were a library, and could be
    # conditionally made to depend on this file, as a library and then we could easily scope
    # just to this file.
    env.InjectThirdPartyIncludePaths('tcmalloc')

serveronlyEnv = env.Clone()
serveronlyEnv.InjectThirdPartyIncludePaths(libraries=['snappy'])
serveronlyLibdeps = [
    "$BUILD_DIR/mongo/client/parallel",
    "$BUILD_DIR/mongo/s/batch_write_types",
    "$BUILD_DIR/mongo/s/catalog/legacy/catalog_manager_legacy",
    "$BUILD_DIR/mongo/s/client/sharding_connection_hook",
    "$BUILD_DIR/mongo/s/coreshard",
    "$BUILD_DIR/mongo/s/metadata",
    "$BUILD_DIR/mongo/s/serveronly",
    "$BUILD_DIR/mongo/scripting/scripting_server",
    "$BUILD_DIR/mongo/util/elapsed_tracker",
    "$BUILD_DIR/mongo/db/storage/mmap_v1/file_allocator",
    "$BUILD_DIR/third_party/shim_snappy",
    "auth/authmongod",
    "catalog/collection_options",
    "catalog/index_key_validate",
    "common",
    "concurrency/lock_manager",
    "concurrency/write_conflict_exception",
    "curop",
    "exec/exec",
    "exec/working_set",
    "fts/ftsmongod",
    "global_timestamp",
    "index/index_descriptor",
    "ops/update_driver",
    "query/query",
    "range_deleter",
    "repl/network_interface_impl",
    "repl/repl_coordinator_global",
    "repl/repl_coordinator_impl",
    "repl/repl_settings",
    "repl/replication_executor",
    "repl/replication_executor",
    "repl/replset_commands",
    "repl/rslog",
    "repl/topology_coordinator_impl",
    "startup_warnings_mongod",
    "stats/counters",
    "stats/top",
    "storage/devnull/storage_devnull",
    "storage/in_memory/storage_in_memory",
    "storage/mmap_v1/mmap",
    "storage/mmap_v1/storage_mmapv1",
    "storage/storage_engine_lock_file",
    "storage/storage_engine_metadata",
    "update_index_data",
]

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

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

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

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