bazelbuild / rules_apple

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

Unable to build XCFramework with swift library that has a dependency to an Objc Library #2547

Open prasadpamidi opened 1 month ago

prasadpamidi commented 1 month ago

Hi,

I'm currently exploring the use of Bazel to build and distribute our internal SDK but running into an issue where the apple_xcframework action fails while i try to build a swift_library due to missing reference to an Objc library that the swift_library depends on.. Also apple_xcframework works fine when i try to build an objc_library target directly instead of swift_library. I'm curious if this is not supported or if i am doing this wrong. Also this doesn't happen if add the swift_library to ios_application target and try to build the application target.. I did try to explore different options via copts and linkopts to provide the directory to look for the ObjcLib framework/lib during build step, but none worked.

Here's the error:

bazel-out/darwin_x86_64-opt-exec-ST-4b127f4e9eac/bin/external/rules_swift~/tools/worker/worker swiftc @bazel-out/ios_sim_arm64-fastbuild-ios-sim_arm64-min16.0-applebin_ios-ST-1a614b7cee85/bin/SwiftLib.swiftmodule-0.params)
# Configuration: e78661a1df9c1b862432476a17f727734c4389d9eb2b54e457242104cf4bfca4
# Execution platform: @@platforms//host:host
error: verify-emitted-module-interface command failed with exit code 1 (use -v to see invocation)
bazel-out/ios_sim_arm64-fastbuild-ios-sim_arm64-min16.0-applebin_ios-ST-1a614b7cee85/bin/SwiftLib.swiftinterface:4:8: error: no such module 'ObjcLib'
import ObjcLib
       ^
bazel-out/ios_sim_arm64-fastbuild-ios-sim_arm64-min16.0-applebin_ios-ST-1a614b7cee85/bin/SwiftLib.swiftinterface:1:1: error: failed to verify module interface of 'SwiftLib' due to the errors above; the textual interface may be broken by project issues or a compiler bug
// swift-interface-format-version: 1.0
^
error: fatalError

Here's the BUILD file.

load("@rules_cc//cc:defs.bzl", "objc_library")
load("@build_bazel_rules_apple//apple:apple.bzl", "apple_xcframework")
load("@rules_swift//swift:swift.bzl", "swift_library")

filegroup(
    name = "ObjcLibHeaders",
    srcs = [
        "ObjcLib/Classes/Util.h",
    ],
)

objc_library(
    name = "ObjcLib",
    srcs = glob([
        "ObjcLib/Classes/*.h",
        "ObjcLib/Classes/*.m",
    ]),
    hdrs = [":ObjcLibHeaders"],
    includes = [
        "ObjcLib/Classes",
    ],
    module_name = "ObjcLib",
    enable_modules = True,
    visibility = ["//visibility:public"],
)

swift_library(
    name = "SwiftLib",
    srcs = glob(["SwiftLib/Classes/**/*.swift"]),
    module_name = "SwiftLib",
    tags = ["manual"],
    visibility = ["//visibility:public"],
    deps = [
        ":ObjcLib",
    ],
)

apple_xcframework(
    name = "SwiftLibXCFramework",
    deps = [
        ":SwiftLib",
    ],
    infoplists = ["SwiftLib/Info.plist"],
    ios = {
        "device": ["arm64"],
        "simulator": [
            "x86_64",
            "arm64",
        ],
    },
    minimum_os_versions = {
        "ios": "16.0",
    },
    bundle_id = "com.bazel.ios.sample",
    bundle_name = "SwiftLib",
    visibility = ["//visibility:public"],
)

Here's the project to try https://github.com/prasadpamidi/bazel-ios-sample

Am I missing something?

prasadpamidi commented 1 month ago

Not sure whether if this is an issue with apple_xcframework or swift_library rule in rules_swift repo https://github.com/bazelbuild/rules_swift/issues/1314