# TensorFlow Lite for Objective-C

load("//tensorflow/lite:special_rules.bzl", "ios_visibility_whitelist", "tflite_ios_lab_runner")
load("//tensorflow/lite/ios:ios.bzl", "TFL_DEFAULT_TAGS", "TFL_DISABLED_SANITIZER_TAGS", "TFL_MINIMUM_OS_VERSION")
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application", "ios_unit_test")

package(
    default_visibility = ["//visibility:private"],
    licenses = ["notice"],  # Apache 2.0
)

SOURCES = glob([
    "sources/*.h",
    "sources/*.m",
    "sources/*.mm",
])

API_HEADERS = glob([
    "apis/*.h",
])

# Compiler flags for building regular non-test libraries.
RELEASE_COPTS = [
    # Enables language-specific warnings for Objective-C, Objective-C++, C, and C++.
    "-Wall",
    # Warns if functions, variables, and types marked with the deprecated attribute are being used.
    "-Wdeprecated-declarations",
    # Warns for errors in documentation.
    "-Wdocumentation",
    # Turns all warnings into errors.
    "-Werror",
    # Enables extra warning flags that are not enabled by -Wall.
    "-Wextra",
    # Warns if a global function is defined without a previous prototype declaration.
    "-Wmissing-prototypes",
    # From -Wextra. Disables warning when signed value is converted to unsigned value during comparison.
    "-Wno-sign-compare",
    # From -Wextra. Disables warning for unused parameters, which are common in delegate methods and block callbacks.
    "-Wno-unused-parameter",
    # Warns if a global or local variable or type declaration shadows another variable, parameter, type, class member, or instance variable.
    "-Wshadow",
    # Warns if a function is declared or defined without specifying the argument types. For a block with no args, use (void) instead of ().
    "-Wstrict-prototypes",
    # Warns if an @selector() expression is encountered with a method name that hasn't been defined yet.
    "-Wundeclared-selector",
    # Turn off warnings for headers not part of TensorFlow Lite Objective-C API.
    "--system-header-prefix=tensorflow/lite/c/",
]

# Compiler flags for building test libraries.
TEST_COPTS = RELEASE_COPTS + [
    # From -Wall. Disables warning when passing nil to a callee that requires a non-null argument.
    "-Wno-nonnull",
    # Disables warning when a global or local variable or type declaration shadows another.
    "-Wno-shadow",
]

objc_library(
    name = "TensorFlowLite",
    srcs = SOURCES,
    hdrs = API_HEADERS,
    copts = RELEASE_COPTS,
    tags = TFL_DEFAULT_TAGS,
    visibility = ios_visibility_whitelist(),
    deps = [
        "//tensorflow/lite/c:c_api",
        "//tensorflow/lite/delegates/coreml:coreml_delegate",
        "//tensorflow/lite/delegates/gpu:metal_delegate",
        "//tensorflow/lite/delegates/xnnpack:xnnpack_delegate",
    ],
    alwayslink = 1,
)

ios_unit_test(
    name = "Tests",
    size = "medium",
    minimum_os_version = TFL_MINIMUM_OS_VERSION,
    runner = tflite_ios_lab_runner("IOS_LATEST"),
    tags = TFL_DEFAULT_TAGS + TFL_DISABLED_SANITIZER_TAGS + [
        "nozapfhahn",  # TODO(b/145984659): Enable after solving tool failure.
    ],
    deps = [
        ":TestsLibrary",
    ],
)

objc_library(
    name = "TestsLibrary",
    testonly = 1,
    srcs = glob([
        "tests/*.m",
    ]),
    hdrs = glob([
        "apis/*.h",
        "sources/*.h",
        "tests/*.h",
    ]),
    copts = TEST_COPTS,
    data = [
        "//tensorflow/lite:testdata/add.bin",
        "//tensorflow/lite:testdata/add_quantized.bin",
    ],
    tags = TFL_DEFAULT_TAGS + ["builder_default_ios_x86_64"],
    deps = [
        ":TensorFlowLite",
    ],
)

ios_application(
    name = "TestApp",
    app_icons = glob(["apps/TestApp/TestApp/Assets.xcassets/AppIcon.appiconset/**"]),
    bundle_id = "com.tensorflow.lite.objc.TestApp",
    families = [
        "ipad",
        "iphone",
    ],
    infoplists = ["apps/TestApp/TestApp/Info.plist"],
    minimum_os_version = TFL_MINIMUM_OS_VERSION,
    sdk_frameworks = [
        "CoreGraphics",
    ],
    tags = TFL_DEFAULT_TAGS,
    deps = [
        ":TestAppLibrary",
    ],
)

objc_library(
    name = "TestAppLibrary",
    srcs = glob(["apps/TestApp/TestApp/*.m"]),
    hdrs = glob(["apps/TestApp/TestApp/*.h"]),
    data = glob(["apps/TestApp/TestApp/Base.lproj/*.storyboard"]) + [
        "//tensorflow/lite:testdata/add.bin",
        "//tensorflow/lite:testdata/add_quantized.bin",
        "//tensorflow/lite:testdata/multi_add.bin",
    ],
    includes = [
        "apis",
    ],
    module_name = "TestApp",
    tags = TFL_DEFAULT_TAGS + [
        "manual",
        "builder_default_ios_x86_64",
    ],
    deps = [
        ":TensorFlowLite",
    ],
)
