include_defs("//ReactCommon/DEFS")

CXX_LIBRARY_COMPILER_FLAGS = [
    "-std=c++14",
    "-Wall",
]

if THIS_IS_FBOBJC:
  inherited_buck_flags = STATIC_LIBRARY_IOS_FLAGS
  CXX_LIBRARY_COMPILER_FLAGS += inherited_buck_flags.get_flag_value('compiler_flags')

rn_xplat_cxx_library(
    name = "module",
    header_namespace = "",
    exported_headers = subdir_glob(
        [
            ("", "CxxModule.h"),
            ("", "JsArgumentHelpers.h"),
            ("", "JsArgumentHelpers-inl.h"),
        ],
        prefix = "cxxreact",
    ),
    compiler_flags = CXX_LIBRARY_COMPILER_FLAGS,
    force_static = True,
    visibility = [
        "PUBLIC",
    ],
    deps = [
        "//xplat/folly:molly",
    ],
)

rn_xplat_cxx_library(
    name = "jsbigstring",
    srcs = [
        "JSBigString.cpp",
    ],
    header_namespace = "",
    exported_headers = subdir_glob(
        [("", "JSBigString.h")],
        prefix = "cxxreact",
    ),
    compiler_flags = CXX_LIBRARY_COMPILER_FLAGS + [
        "-fexceptions",
        "-frtti",
    ],
    force_static = True,
    visibility = [
        "PUBLIC",
    ],
    deps = [
        "//xplat/folly:molly",
    ],
)

rn_xplat_cxx_library(
    name = "samplemodule",
    srcs = ["SampleCxxModule.cpp"],
    header_namespace = "",
    exported_headers = ["SampleCxxModule.h"],
    compiler_flags = CXX_LIBRARY_COMPILER_FLAGS + [
        "-fno-omit-frame-pointer",
        "-fexceptions",
    ],
    soname = "libxplat_react_module_samplemodule.so",
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":module",
        "//xplat/folly:molly",
    ],
)

CXXREACT_PUBLIC_HEADERS = [
    "CxxNativeModule.h",
    "Instance.h",
    "JSBundleType.h",
    "JSExecutor.h",
    "JSCExecutor.h",
    "JSCNativeModules.h",
    "JSIndexedRAMBundle.h",
    "JSModulesUnbundle.h",
    "MessageQueueThread.h",
    "MethodCall.h",
    "ModuleRegistry.h",
    "NativeModule.h",
    "NativeToJsBridge.h",
    "Platform.h",
    "RecoverableError.h",
    "SharedProxyCxxModule.h",
    "SystraceSection.h",
]

rn_xplat_cxx_library(
    name = "bridge",
    srcs = glob(
        ["*.cpp"],
        excludes = [
            "JSBigString.cpp",
            "SampleCxxModule.cpp",
        ],
    ),
    headers = glob(
        ["*.h"],
        excludes = CXXREACT_PUBLIC_HEADERS,
    ),
    header_namespace = "",
    exported_headers = dict([
        (
            "cxxreact/%s" % header,
            header,
        )
        for header in CXXREACT_PUBLIC_HEADERS
    ]),
    compiler_flags = CXX_LIBRARY_COMPILER_FLAGS + [
        "-fexceptions",
        "-frtti",
    ],
    fbandroid_preprocessor_flags = [
        "-DWITH_JSC_EXTRA_TRACING=1",
        "-DWITH_JSC_MEMORY_PRESSURE=1",
        "-DWITH_FB_MEMORY_PROFILING=1",
    ],
    fbandroid_visibility = [
        # TL;DR: If you depend on this target directly, you're gonna have a
        # Bad Time(TM).
        #
        # `facebook::react::JSCExecutor::initOnJSVMThread` (in `:bridge`) does
        # some platform-dependant setup. Exactly what setup to do is
        # determined by some static functors, defined in `Platform.h`, which
        # are initially `nullptr`. On Android, these functors are properly
        # assigned as part of `react/jni`'s `JNI_OnLoad`.  By depending directly
        # on the bridge, we can mess up the SO initialisation order, causing
        # `initOnJSVMThread` to be called before the platform-specific hooks
        # have been properly initialised. Bad Times(TM).
        #     -- @ashokmenon (2017/01/03)
        react_native_target("jni/react/jni:jni"),
        react_native_xplat_target("cxxreact/..."),
    ],
    fbobjc_frameworks = [
        "$SDKROOT/System/Library/Frameworks/JavaScriptCore.framework",
    ],
    fbobjc_inherited_buck_flags = STATIC_LIBRARY_IOS_FLAGS,
    fbobjc_preprocessor_flags = DEBUG_PREPROCESSOR_FLAGS,
    fbobjc_visibility = ["PUBLIC"],
    force_static = True,
    preprocessor_flags = [
        "-DLOG_TAG=\"ReactNative\"",
        "-DWITH_FBSYSTRACE=1",
    ] + INSPECTOR_FLAGS,
    tests = [
        react_native_xplat_target("cxxreact/tests:tests"),
    ],
    deps = [
        ":module",
        ":jsbigstring",
        "//xplat/fbsystrace:fbsystrace",
        "//xplat/folly:molly",
        react_native_xplat_target("jschelpers:jschelpers"),
        react_native_xplat_target("microprofiler:microprofiler"),
    ] + JSC_DEPS,
)
