rules_swift icon indicating copy to clipboard operation
rules_swift copied to clipboard

Documentation to use bridging headers

Open fwouts opened this issue 5 years ago • 2 comments

Hi, I'm trying to build a fairly complex app, which has a mix of Swift and Objective-C.

The Swift code relies heavily on a bridging header, which itself imports a number of headers. Here's a snippet of App-Bridging-Header.h:

#import "Constants.h"
#import "CJAMacros.h"
... (many more headers)

Each of the imported headers lives in a different directory. Xcode makes that work (I'm not sure how), but I'm not sure how to do this with Bazel.

How can this header, and all its dependencies, be declared with Bazel? How can they be used from a swift_library target?

Thank you 🙃

fwouts avatar Mar 19 '20 11:03 fwouts

same for me

fnc12 avatar May 05 '23 17:05 fnc12

I wasn't able to find official documentation that covers this, but this is what I've found that works:

  1. Set the module_name on your objc_library targets (or rely on the default value as described in the objc_library documentation)
  2. List the objc_library targets as deps of your swift_library
  3. Use import [obj_module_name] in your .swift code

For example:

objc_library(
    name = "foo_lib",
    srcs = ["Bar.m"],
    hdrs = ["Bar.h"],
    module_name = "FooLib",
)

swift_library(
    name = "baz_lib",
    srcs = ["Baz.swift"],
    deps = [":foo_lib"],
)
// Baz.swift
import FooLib

let bar = Bar()

kersson avatar Jul 09 '23 18:07 kersson