load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbsource//xplat/folly:defs.bzl", "folly_xplat_library")
load("@fbsource//xplat/pfh/triage_InfrastructureSupermoduleOptou:DEFS.bzl", "triage_InfrastructureSupermoduleOptou")

oncall("fbcode_entropy_wardens_folly")

# xplat build rules

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "barrier",
    srcs = [
        "Barrier.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    force_static = False,
    raw_headers = [
        "Barrier.h",
    ],
    deps = [
        "//third-party/glog:glog",
        "//xplat/folly:scope_guard",
        "//xplat/folly/lang:new",
    ],
    exported_deps = ["//xplat/folly/futures:core"],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "cleanup",
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "Cleanup.h",
    ],
    deps = [
        ":core",
        "//third-party/glog:glog",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "core",
    srcs = [
        "Future.cpp",
        "HeapTimekeeper.cpp",
        "Promise.cpp",
        "ThreadWheelTimekeeper.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    force_static = False,
    labels = [
        # This library should be used via :futures. Depslint
        # doesn't support the concept of target-reexports (nor do we know the
        # best design for such a feature), so we just exclude this target from
        # being added.
        "depslint_never_add",
    ],
    raw_headers = [
        "Future.h",
        "Future-inl.h",
        "Future-pre.h",
        "HeapTimekeeper.h",
        "Promise.h",
        "Promise-inl.h",
        "Retrying.h",
        "ThreadWheelTimekeeper.h",
        "WTCallback.h",
    ],
    deps = [
        "fbsource//xplat/folly/lang:safe_assert",
        "//xplat/folly:likely",
        "//xplat/folly:singleton",
        "//xplat/folly/system:thread_name",
    ],
    exported_deps = [
        "fbsource//xplat/folly/lang:switch",
        "fbsource//xplat/folly/portability:gflags",
        "fbsource//xplat/folly/synchronization:distributed_mutex",
        "fbsource//xplat/folly/synchronization:relaxed_atomic",
        "fbsource//xplat/folly/synchronization:saturating_semaphore",
        ":portability",
        "//xplat/folly:chrono",
        "//xplat/folly:optional",
        "//xplat/folly:portability",
        "//xplat/folly:random",
        "//xplat/folly:scope_guard",
        "//xplat/folly:traits",
        "//xplat/folly:try",
        "//xplat/folly:unit",
        "//xplat/folly:utility",
        "//xplat/folly/container:intrusive_heap",
        "//xplat/folly/detail:async_trace",
        "//xplat/folly/executors:drivable_executor",
        "//xplat/folly/executors:executor_with_priority",
        "//xplat/folly/executors:global_executor",
        "//xplat/folly/executors:inline_executor",
        "//xplat/folly/executors:queued_immediate_executor",
        "//xplat/folly/executors:timed_drivable_executor",
        "//xplat/folly/experimental/coro:traits",
        "//xplat/folly/fibers:core",
        "//xplat/folly/functional:invoke",
        "//xplat/folly/futures/detail:core",
        "//xplat/folly/futures/detail:types",
        "//xplat/folly/io/async:async_base",
        "//xplat/folly/lang:exception",
        "//xplat/folly/lang:pretty",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "future_splitter",
    feature = triage_InfrastructureSupermoduleOptou,
    force_static = False,
    raw_headers = [
        "FutureSplitter.h",
    ],
    exported_deps = [
        ":core",
        ":shared_promise",
        "//xplat/folly/lang:exception",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "futures",
    feature = triage_InfrastructureSupermoduleOptou,
    deps = [
        ":barrier",
        ":core",
        ":future_splitter",
        ":shared_promise",
        "//xplat/folly/executors:inline_executor",
        "//xplat/folly/executors:manual_executor",
        "//xplat/folly/executors:queued_immediate_executor",
        "//xplat/folly/executors:scheduled_executor",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "manual_timekeeper",
    srcs = [
        "ManualTimekeeper.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "ManualTimekeeper.h",
    ],
    deps = [
        ":core",
        "//xplat/folly:synchronized",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "portability",
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "Portability.h",
    ],
    deps = [
        "//xplat/folly:portability",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "shared_promise",
    srcs = [
        "SharedPromise.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    force_static = False,
    raw_headers = [
        "SharedPromise.h",
        "SharedPromise-inl.h",
    ],
    exported_deps = [
        ":core",
        "//xplat/folly:portability",
        "//xplat/folly/executors:inline_executor",
        "//xplat/folly/lang:exception",
    ],
)

# fbcode build rules

fbcode_target(
    _kind = cpp_library,
    name = "barrier",
    srcs = ["Barrier.cpp"],
    headers = ["Barrier.h"],
    deps = [
        "//folly:scope_guard",
        "//folly/lang:new",
    ],
    exported_deps = [
        ":core",
    ],
    external_deps = [
        "glog",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "core",
    srcs = [
        "Future.cpp",
        "HeapTimekeeper.cpp",
        "Promise.cpp",
        "ThreadWheelTimekeeper.cpp",
    ],
    headers = [
        "Future.h",
        "Future-inl.h",
        "Future-pre.h",
        "HeapTimekeeper.h",
        "Promise.h",
        "Promise-inl.h",
        "Retrying.h",
        "ThreadWheelTimekeeper.h",
        "WTCallback.h",
    ],
    deps = [
        "//folly:likely",
        "//folly:singleton",
        "//folly/container:intrusive_heap",
        "//folly/lang:safe_assert",
        "//folly/portability:gflags",
        "//folly/synchronization:distributed_mutex",
        "//folly/synchronization:relaxed_atomic",
        "//folly/synchronization:saturating_semaphore",
        "//folly/synchronization:wait_options",
        "//folly/system:thread_name",
    ],
    exported_deps = [
        ":portability",
        "//folly:chrono",
        "//folly:optional",
        "//folly:portability",
        "//folly:random",
        "//folly:scope_guard",
        "//folly:traits",
        "//folly:try",
        "//folly:unit",
        "//folly:utility",
        "//folly/container:foreach",
        "//folly/coro:traits",
        "//folly/detail:async_trace",
        "//folly/executors:drivable_executor",
        "//folly/executors:executor_with_priority",
        "//folly/executors:global_executor",
        "//folly/executors:inline_executor",
        "//folly/executors:queued_immediate_executor",
        "//folly/executors:timed_drivable_executor",
        "//folly/fibers:core",
        "//folly/functional:invoke",
        "//folly/futures/detail:core",
        "//folly/futures/detail:types",
        "//folly/io/async:async_base",
        "//folly/lang:exception",
        "//folly/lang:pretty",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "future_splitter",
    headers = ["FutureSplitter.h"],
    exported_deps = [
        ":core",
        ":shared_promise",
        "//folly/lang:exception",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "futures",
    exported_deps = [
        ":barrier",  # @manual
        ":core",  # @manual
        ":future_splitter",  # @manual
        ":shared_promise",  # @manual
        "//folly/executors:inline_executor",  # @manual
        "//folly/executors:manual_executor",  # @manual
        "//folly/executors:queued_immediate_executor",  # @manual
        "//folly/executors:scheduled_executor",  # @manual
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "manual_timekeeper",
    srcs = ["ManualTimekeeper.cpp"],
    headers = ["ManualTimekeeper.h"],
    deps = [
        "//folly/synchronization:atomic_util",
    ],
    exported_deps = [
        "//folly:synchronized",
        "//folly/futures:core",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "portability",
    headers = ["Portability.h"],
    exported_deps = [
        "//folly:portability",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "shared_promise",
    srcs = ["SharedPromise.cpp"],
    headers = [
        "SharedPromise.h",
        "SharedPromise-inl.h",
    ],
    exported_deps = [
        ":core",
        "//folly:portability",
        "//folly/executors:inline_executor",
        "//folly/lang:exception",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "cleanup",
    headers = ["Cleanup.h"],
    exported_deps = [
        "//folly/futures:core",
    ],
    exported_external_deps = [
        "glog",
    ],
)
