bazelbuild / rules_swift

Bazel rules to build Swift on Apple and Linux platforms
Apache License 2.0
309 stars 133 forks source link

`linkopts` from `cc_library` not propagated through `swift_library` #432

Open keith opened 4 years ago

keith commented 4 years ago

When you have targets like these:

load("@build_bazel_rules_apple//apple:ios.bzl", "ios_unit_test")
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")

cc_library(
    name = "cclib",
    srcs = ["ccfoo.cc"],
    linkopts = ["-some-flag-that-should-break-linking"],
    tags = ["swift_module"],
)

swift_library(
    name = "swiftlib",
    srcs = ["swiftfoo.swift"],
    deps = ["cclib"],
)

ios_unit_test(
    name = "test",
    minimum_os_version = "10.0",
    deps = ["swiftlib"],
)

The invalid linkopts from cc_library are ignored.

Sample project: linkopts.zip

Run bazel build test which should fail to link.

keith commented 4 years ago

After a bit more investigation the problem is that apple_binary only takes linkopts from the ObjcProvider into account:

https://github.com/bazelbuild/bazel/blob/220a1c8414d92cf59c469591c19274d384109f0b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcVariablesExtension.java#L222

and objc_library collects the linkopts from its transitive cc_library dependents: https://github.com/bazelbuild/bazel/blob/220a1c8414d92cf59c469591c19274d384109f0b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcCommon.java#L382-L406

but swift_library does not. Ideally apple_binary would collect the linkopts from all its transitive CcInfos. Alternatively swift_library could be changed to propagate cc_library's linkopts with the ObjcProvider it propagates.