load("@build_bazel_rules_android//android:rules.bzl", "android_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])  # Apache 2.0

exports_files(["LICENSE.txt"])

licenses(["notice"])

config_setting(
    name = "freebsd",
    values = {"cpu": "freebsd"},
)

config_setting(
    name = "windows",
    values = {"cpu": "x64_windows"},
)

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

# Public flatc library to compile flatbuffer files at runtime.
cc_library(
    name = "flatbuffers",
    hdrs = ["//:public_headers"],
    linkstatic = 1,
    strip_include_prefix = "/include",
    visibility = ["//visibility:public"],
    deps = ["//src:flatbuffers"],
)

# Public C++ headers for the Flatbuffers library.
filegroup(
    name = "public_headers",
    srcs = [
        "include/flatbuffers/base.h",
        "include/flatbuffers/code_generators.h",
        "include/flatbuffers/flatbuffers.h",
        "include/flatbuffers/flexbuffers.h",
        "include/flatbuffers/hash.h",
        "include/flatbuffers/idl.h",
        "include/flatbuffers/minireflect.h",
        "include/flatbuffers/reflection.h",
        "include/flatbuffers/reflection_generated.h",
        "include/flatbuffers/registry.h",
        "include/flatbuffers/stl_emulation.h",
        "include/flatbuffers/util.h",
    ],
    visibility = ["//:__subpackages__"],
)

# Public flatc compiler library.
cc_library(
    name = "flatc_library",
    linkstatic = 1,
    visibility = ["//visibility:public"],
    deps = [
        "@flatbuffers//src:flatc_library",
    ],
)

# Public flatc compiler.
cc_binary(
    name = "flatc",
    linkopts = select({
        ":freebsd": [
            "-lm",
        ],
        ":windows": [],
        "//conditions:default": [
            "-lm",
            "-ldl",
        ],
    }),
    visibility = ["//visibility:public"],
    deps = [
        "@flatbuffers//src:flatc",
    ],
)

filegroup(
    name = "flatc_headers",
    srcs = [
        "include/flatbuffers/flatc.h",
    ],
    visibility = ["//:__subpackages__"],
)

# Library used by flatbuffer_cc_library rules.
cc_library(
    name = "runtime_cc",
    hdrs = [
        "include/flatbuffers/base.h",
        "include/flatbuffers/flatbuffers.h",
        "include/flatbuffers/flexbuffers.h",
        "include/flatbuffers/stl_emulation.h",
        "include/flatbuffers/util.h",
    ],
    linkstatic = 1,
    strip_include_prefix = "/include",
    visibility = ["//visibility:public"],
)

filegroup(
    name = "runtime_py_srcs",
    srcs = [
        "python/flatbuffers/__init__.py",
        "python/flatbuffers/builder.py",
        "python/flatbuffers/compat.py",
        "python/flatbuffers/encode.py",
        "python/flatbuffers/number_types.py",
        "python/flatbuffers/packer.py",
        "python/flatbuffers/table.py",
        "python/flatbuffers/util.py",
    ],
)

py_library(
    name = "runtime_py",
    srcs = [":runtime_py_srcs"],
    visibility = ["//visibility:public"],
)

filegroup(
    name = "runtime_java_srcs",
    srcs = glob(["java/com/google/flatbuffers/**/*.java"]),
)

java_library(
    name = "runtime_java",
    srcs = [":runtime_java_srcs"],
    visibility = ["//visibility:public"],
)

android_library(
    name = "runtime_android",
    srcs = [":runtime_java_srcs"],
    visibility = ["//visibility:public"],
)
