bazelbuild / rules_apple

Bazel rules to build apps for Apple platforms.
Apache License 2.0
511 stars 269 forks source link

`ios_framework` export headers from dependencies #1201

Open robert-go opened 3 years ago

robert-go commented 3 years ago

Hi I'm trying to build a ios_framework. It use OpencvFramework. But I don't know how to export headers from Opencv to reuse it My BUILD file

load("@build_bazel_rules_apple//apple:ios.bzl", "ios_framework")

ios_framework(
    name = "IrisTracker",
    hdrs = [
        "IrisTracker.h",
    ] + glob([
        "OpencvFramework/opencv2.framework/Versions/A/Headers/**/*.h*",
    ]),
    infoplists = ["Info.plist"],
    bundle_id = "com.minhdv.IrisTracker",
    families = ["iphone", "ipad"],
    minimum_os_version = "10.0",
    deps = [
        ":IrisTrackerLibrary",
        "@ios_opencv//:OpencvFramework",
    ],
)

objc_library(
    name = "IrisTrackerLibrary",
    srcs = [
        "IrisTracker.mm",
    ],
    hdrs = [
        "IrisTracker.h",
    ],
    copts = ["-std=c++17"],
    data = [
        "//mediapipe/graphs/iris_tracking:iris_tracking_gpu.binarypb",
        "//mediapipe/modules/face_detection:face_detection_short_range.tflite",
        "//mediapipe/modules/face_landmark:face_landmark.tflite",
        "//mediapipe/modules/iris_landmark:iris_landmark.tflite",
    ],
    visibility = ["//mediapipe:__subpackages__"],
    deps = [
        "//mediapipe/objc:mediapipe_framework_ios",
    ] + select({
        "//mediapipe:ios_i386": [],
        "//mediapipe:ios_x86_64": [],
        "//conditions:default": [
            "//mediapipe/graphs/iris_tracking:iris_tracking_gpu_deps",
            "//mediapipe/framework/formats:landmark_cc_proto",
        ],
    }),
)
keith commented 3 years ago

Ideally whatever headers you need from opencv would be coming in automatically through your deps from the @ios_opencv//:OpencvFramework dependency. If you need to re-export headers for some reason they would have to have a separate rule in their BUILD file to give you a filegroup or something with them. I imagine your glob of OpencvFramework/opencv2.framework/Versions/A/Headers/**/*.h* is invalid in this BUILD file if those files are actually coming from the @ios_opencv workspace and therefore that matches nothing

robert-go commented 3 years ago

@keith thank you so much for your reply here is the BUILD file of opencv How can I re-export headers ?

# Description:
#   OpenCV libraries for video/image processing on iOS

licenses(["notice"])  # BSD license

exports_files(["LICENSE"])

load(
    "@build_bazel_rules_apple//apple:apple.bzl",
    "apple_static_framework_import",
)

apple_static_framework_import(
    name = "OpencvFramework",
    framework_imports = glob(["opencv2.framework/**"]),
    visibility = ["//visibility:public"],
)

objc_library(
    name = "opencv_objc_lib",
    deps = [":OpencvFramework"],
)

cc_library(
    name = "opencv",
    hdrs = glob([
        "opencv2.framework/Versions/A/Headers/**/*.h*",
    ]),
    copts = [
        "-std=c++11",
        "-x objective-c++",
    ],
    include_prefix = "opencv2",
    linkopts = [
        "-framework AssetsLibrary",
        "-framework CoreFoundation",
        "-framework CoreGraphics",
        "-framework CoreMedia",
        "-framework Accelerate",
        "-framework CoreImage",
        "-framework AVFoundation",
        "-framework CoreVideo",
        "-framework QuartzCore",
    ],
    strip_include_prefix = "opencv2.framework/Versions/A/Headers",
    visibility = ["//visibility:public"],
    deps = [":opencv_objc_lib"],
)
keith commented 3 years ago

In this case I was saying you could do something like:

filegroup(
    name = "headers",
    srcs = glob([
        "opencv2.framework/Versions/A/Headers/**/*.h*",
    ]),
)

And then consume the new headers rule from wherever. Note that it is pretty non-standard to have to manually export headers like this, normally they're inherited through deps instead