load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbcode_macros//build_defs:cython_library.bzl", "CYTHON_DEFAULT_APPLE_SDKS", "CYTHON_DEFAULT_PLATFORMS")
load("@fbsource//tools/build_defs/dirsync:fb_dirsync_cpp_library.bzl", "fb_dirsync_cpp_library")
load("@fbsource//tools/build_defs/dirsync:fb_dirsync_cython_library.bzl", "fb_dirsync_cython_library")
load("@fbsource//tools/build_defs/dirsync:fb_dirsync_rule.bzl", "fb_dirsync_rule")
load("../defs.bzl", "folly_xplat_library")

oncall("fbcode_entropy_wardens_folly")

fb_dirsync_cpp_library(
    name = "gil_aware_manual_executor",
    srcs = ["GILAwareManualExecutor.cpp"],
    headers = ["GILAwareManualExecutor.h"],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    deps = [
        ":weak",
        "//folly:scope_guard",
    ],
    exported_deps = [
        "//folly/executors:drivable_executor",
        "//folly/executors:sequenced_executor",
    ],
)

fb_dirsync_cython_library(
    name = "range",
    headers = ["range.pxd"],
    cpp_deps = [
        "//folly:range",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
    deps = [
        ":folly",
    ],
)

fb_dirsync_cpp_library(
    name = "async_generator_lib",
    headers = [
        "async_generator.h",
    ],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    exported_deps = [
        "//folly:portability",
        "//folly/coro:async_generator",
        "//folly/coro:task",
    ],
)

fb_dirsync_cpp_library(
    name = "proactor_executor",
    srcs = [
        "ProactorExecutor.cpp",
    ],
    headers = [
        "ProactorExecutor.h",
    ],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    exported_deps = [
        ":asyncio_executor",
        "//folly/concurrency:concurrent_hash_map",
        "//folly/portability:windows",
    ],
)

fb_dirsync_cython_library(
    name = "executor",
    srcs = ["executor.pyx"],
    headers = [
        "executor.pxd",
        "executor_detail.pxd",
    ],
    header_namespace = "folly/python",
    cpp_deps = [
        "//folly/portability:unistd",
        ":asyncio_executor",
        ":executor_lib",
        ":proactor_executor",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
    deps = [
        ":folly",
    ],
)

fb_dirsync_cpp_library(
    name = "futures_lib",
    headers = [
        "futures.h",
    ],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    exported_deps = [
        ":asyncio_executor",
        ":executor_lib",
        ":weak",
        "//folly:executor",
        "//folly/futures:core",
    ],
)

fb_dirsync_cpp_library(
    name = "executor_lib",
    srcs = [
        "executor.cpp",
    ],
    headers = [
        "executor.h",
    ],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    compiler_flags = select({
        "DEFAULT": [],
        "ovr_config//os:windows": [
            "-DFOLLY_PYTHON_WIN_SHAREDLIB",
        ],
    }),
    platforms = CYTHON_DEFAULT_PLATFORMS,
    preferred_linkage = select({
        "DEFAULT": None,
        "ovr_config//os:windows": "shared",
    }),
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    deps = [
        ":import",
    ],
    exported_deps = [
        ":asyncio_executor",
        ":weak",
        "//folly:executor",
    ],
)

fb_dirsync_cython_library(
    name = "coro",
    headers = ["coro.pxd"],
    cpp_deps = [
        "//folly:cancellation_token",
        ":coro_lib",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
    deps = [
        ":executor",
    ],
)

fb_dirsync_cpp_library(
    name = "asyncio_executor",
    srcs = [
        "AsyncioExecutor.cpp",
    ],
    headers = [
        "AsyncioExecutor.h",
    ],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    exported_deps = [
        ":weak",
        "//folly:chrono",
        "//folly:exception_string",
        "//folly:function",
        "//folly/executors:drivable_executor",
        "//folly/executors:sequenced_executor",
        "//folly/io/async:async_base",
        "//folly/portability:gflags",
    ],
)

fb_dirsync_cython_library(
    name = "folly",
    headers = ["__init__.pxd"],
    cpp_deps = [
        "//folly/futures:core",
        "//folly:exception_wrapper",
        "//folly:executor",
        "//folly:portability",
        "//folly:try",
        "//folly:unit",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
)

fb_dirsync_cython_library(
    name = "fibers",
    srcs = ["fiber_manager.pyx"],
    headers = [
        "fiber_manager.pxd",
        "fibers.pxd",
    ],
    cpp_deps = [
        "//folly/fibers:core",
        "//folly/fibers:executor_loop_controller",
        "//folly/fibers:loop_controller",
        ":fibers_lib",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
    deps = [
        ":folly",
        ":futures",
    ],
)

fb_dirsync_cython_library(
    name = "async_generator",
    headers = ["async_generator.pxd"],
    cpp_deps = [
        ":async_generator_lib",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
    deps = [
        ":coro",
        ":optional",
    ],
)

fb_dirsync_cpp_library(
    name = "import",
    headers = [
        "import.h",
    ],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    exported_deps = [
        "fbsource//third-party/fmt:fmt",
        ":error",
        "//folly:likely",
        "//folly:portability",
    ],
)

fb_dirsync_cython_library(
    name = "memory",
    headers = ["memory.pxd"],
    cpp_deps = [
        "//folly:memory",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
)

fb_dirsync_cython_library(
    name = "function",
    headers = ["function.pxd"],
    cpp_deps = [
        "//folly:function",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
    deps = [
        ":folly",
    ],
)

fb_dirsync_cpp_library(
    name = "iobuf_ext",
    srcs = ["iobuf_ext.cpp"],
    headers = ["iobuf_ext.h"],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    deps = [":weak"],
    exported_deps = [
        "fbsource//third-party/python:python",
        "//folly:executor",
        "//folly/io:iobuf",
    ],
)

fb_dirsync_cpp_library(
    name = "iobuf_lib",
    srcs = ["iobuf.cpp"],
    exported_headers = {
        "folly/python/iobuf.h": "iobuf.h",
        # @fb-only[end= ]: "folly/python/iobuf_api.h": ":iobuf__iobuf_api.h",
    },
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    # TODO(T36778537): Cython-generated `*_api.h` headers aren't modular.
    modular_headers = False,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    target_compatible_with = ["fbcode//opensource/macros:broken-in-oss"],
    deps = [
        ":import",
    ],
    exported_deps = [
        "fbsource//third-party/python:python",
        "//folly/io:iobuf",
    ],
)

fb_dirsync_cython_library(
    name = "iobuf",
    srcs = ["iobuf.pyx"],
    headers = [
        "iobuf.pxd",
    ],
    header_namespace = "folly/python",
    api = ["iobuf"],
    cpp_deps = [
        "//folly/io:iobuf",
        ":iobuf_ext",
        ":iobuf_lib",
    ],
    package = "folly",
    tests = [
        "//folly/python/test:iobuf",
    ],
    types = ["iobuf.pyi"],
    visibility = ["PUBLIC"],
    deps = [
        ":executor",
        ":folly",
        ":futures",
    ],
)

fb_dirsync_cpp_library(
    name = "coro_lib",
    headers = [
        "coro.h",
    ],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    exported_deps = [
        ":asyncio_executor",
        ":executor_lib",
        ":weak",
        "//folly:cancellation_token",
        "//folly:executor",
        "//folly:portability",
        "//folly/coro:task",
    ],
)

fb_dirsync_cpp_library(
    name = "fibers_lib",
    srcs = ["fibers.cpp"],
    headers = ["fibers.h"],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    compiler_flags = select({
        "DEFAULT": [],
        "ovr_config//os:windows": [
            "-DFOLLY_PYTHON_WIN_SHAREDLIB",
        ],
    }),
    platforms = CYTHON_DEFAULT_PLATFORMS,
    preferred_linkage = select({
        "DEFAULT": None,
        "ovr_config//os:windows": "shared",
    }),
    target_compatible_with = ["fbcode//opensource/macros:broken-in-oss"],
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    deps = [
        ":import",
    ],
    exported_deps = [
        ":weak",
        "//folly/fibers:core",
    ],
)

fb_dirsync_cython_library(
    name = "cast",
    headers = ["cast.pxd"],
    cpp_deps = [
        "//folly/lang:cast",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
)

fb_dirsync_cython_library(
    name = "expected",
    headers = ["expected.pxd"],
    cpp_deps = ["//folly:expected"],
    package = "folly",
    visibility = ["PUBLIC"],
    deps = [
        ":folly",
    ],
)

fb_dirsync_cython_library(
    name = "futures",
    headers = ["futures.pxd"],
    cpp_deps = [
        ":futures_lib",
    ],
    package = "folly",
    visibility = ["PUBLIC"],
    deps = [
        ":executor",
    ],
)

fb_dirsync_cpp_library(
    name = "error",
    srcs = ["error.cpp"],
    headers = ["error.h"],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    platforms = CYTHON_DEFAULT_PLATFORMS,
    use_raw_headers = True,
    xplat_impl = folly_xplat_library,
    deps = [
        ":weak",
        "//folly:conv",
        "//folly:scope_guard",
    ],
    exported_deps = [
        "//folly:range",
    ],
)

fb_dirsync_cython_library(
    name = "optional",
    headers = ["optional.pxd"],
    cpp_deps = ["//folly:optional"],
    package = "folly",
    visibility = ["PUBLIC"],
    deps = [
        ":folly",  # So we don't squash the "folly" package
    ],
)

fb_dirsync_cython_library(
    name = "fbstring",
    headers = ["fbstring.pxd"],
    cpp_deps = [
        "//folly:fbstring",
    ],
    package = "folly",
    deps = [
        ":folly",
    ],
)

fb_dirsync_cython_library(
    name = "request_context",
    srcs = ["request_context.pyx"],
    headers = [
        "request_context.h",
        "request_context.pxd",
    ],
    header_namespace = "folly/python",
    compatible_with = [
        "ovr_config//os:linux",
        "ovr_config//os:macos",
        "ovr_config//os:windows",
    ],
    cpp_compiler_flags = select({
        "DEFAULT": ["-DFOLLY_PYTHON_META_BUILD"],
        # Conda is not Meta Python
        "ovr_config//distro:conda": [],
    }),
    cpp_deps = [
        "//folly/io/async:request_context",
        ":weak",
    ],
    package = "folly",
    types = ["request_context.pyi"],
    deps = [
        ":folly",
    ],
)

fb_dirsync_cython_library(
    name = "build_mode",
    srcs = [
        "build_mode.pyx",
    ],
    fbcode_only = True,
    package = "folly",
    types = [
        "build_mode.pyi",
    ],
    deps = [
        ":folly",
    ],
)

fb_dirsync_rule(
    name = "scoped_gil_release",
    headers = [
        "ScopedGILRelease.h",
    ],
    fbcode_only = True,
    fbcode_rule = cpp_library,
    visibility = ["PUBLIC"],
    exported_deps = [
        ":weak",
    ],
)

fb_dirsync_cpp_library(
    # @autodeps-skip
    name = "weak",
    headers = [
        "Weak.h",
    ],
    apple_sdks = CYTHON_DEFAULT_APPLE_SDKS,
    exported_linker_flags = select({
        "DEFAULT": [],
        # These make it so linking libpython is optional
        "ovr_config//os:macos": [
            "-Xlinker",
            "-undefined",
            "-Xlinker",
            "dynamic_lookup",
        ],
    }),
    platforms = CYTHON_DEFAULT_PLATFORMS,
    tests = [
        "fbcode//folly/python/test:weak-test",
        "fbcode//folly/python/test:weak-test-linked",
    ],
    use_raw_headers = True,
    visibility = ["PUBLIC"],
    xplat_impl = folly_xplat_library,
    exported_deps = [
        "fbsource//third-party/python:python-headers",
    ] + select({
        "DEFAULT": [],
        "ovr_config//os:windows": [
            "fbsource//third-party/python:python",  # @manual
        ],
    }),
)
