Dynamic framework throws undefined symbol error in `compilation_mode opt`
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 72
Description
### Description of the bug:
We have a dynamic framework that has an open class and we have a static library that inherits the open class from Framework and depends on the underlying swift_library. It looks something like this
Foo Framework
open class Foo {
public var name: String?
public init() { }
public convenience init(name: String) {
self.init()
self.name = name
}
private func someFunction() {
print("Hello world")
}
}
Bar Library
import Foo
public final class Bar: Foo {
public convenience init(name: String) {
self.init(name: name)
}
}
When I run Bazel build for release configuration `--compilation_mode=opt` , it throws undefined symbol error for the private function from dynamic framework private func someFunction()
Note: It works fine in debug mode `--compilation_mode=dbg` .
### Which category does this issue belong to?
Core
### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
To reproduce clone this repository - https://github.com/sanju-naik/Bazel-Example.git and check out the `dynamic-framework-issues` branch.
Running this command - bazel build --config=Release --ios_multi_cpus=sim_arm64 BazelDemo , throws the error.
### Error
```
(cd /private/var/tmp/_bazel_sanju/e107e611dd65132537122ef1dabd8e89/execroot/__main__ && \
exec env - \
APPLE_SDK_PLATFORM=iPhoneSimulator \
APPLE_SDK_VERSION_OVERRIDE=16.4 \
PATH=/Users/sanju/Library/Caches/bazelisk/downloads/bazelbuild/bazel-6.3.2-darwin-arm64/bin:/opt/homebrew/opt/openjdk/bin:/Users/sanju/.rbenv/shims:/Users/sanju/.rbenv/bin:/opt/homebrew/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin \
XCODE_VERSION_OVERRIDE=14.3.0.14E222b \
ZERO_AR_DATE=1 \
external/local_config_cc/wrapped_clang @bazel-out/ios-sim_arm64-min12.0-applebin_ios-ios_sim_arm64-opt-ST-acb08275552e/bin/BazelDemo_bin-2.params)
# Configuration: b94fcb8744f284f7d0ecde69e1d097360d802dc23000b86c0429a22d42c495f9
# Execution platform: @local_config_platform//:host
Undefined symbols for architecture arm64:
"_$s3FooAAC12someFunction33_EDFAF2F1CEBC6FC422A5A3D516B36930LLyyF", referenced from:
_$s3BarAACN in libBar.a(Bar.swift.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error in child process '/usr/bin/xcrun'. 1
Target //:BazelDemo failed to build
INFO: Elapsed time: 0.359s, Critical Path: 0.22s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully
```
### Which operating system are you running Bazel on?
mac OS
### What is the output of `bazel info release`?
release 6.3.2
### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.
NA
### What's the output of `git remote get-url origin; git rev-parse master; git rev-parse HEAD` ?
```text
NA
```
### Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.
NA
### Have you found anything relevant by searching the web?
No
### Any other information, logs, or outputs that you want to share?
Looks like Bazel is over-stripping symbols in `opt` mode. From the repro example I have linked above, it complains about missing this symbol `s3FooAAC12someFunction33_EDFAF2F1CEBC6FC422A5A3D516B36930LLyyF`.
When I build the same App in `dbg` mode or build it using plain Xcode it also contains another symbol for the same method `s3FooAAC12someFunction33_EDFAF2F1CEBC6FC422A5A3D516B36930LLyyFTq` which Bazel is dropping in opt mode and seems like that might be causing this issue.
Contributor guide
Research direction
Clone the linked Bazel-Example repository and check out the dynamic-framework-issues branch. Run `bazel build --config=Release --ios_multi_cpus=sim_arm64 BazelDemo`, then compare the opt and dbg builds and the reported Swift symbols. Done means the opt-mode build links successfully without the undefined symbol error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- build-system, mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100