Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined symbols for architecture xxx while calling a method defined in extension of a class in a Swift dynamic framework #767

Closed
imWildCat opened this issue Feb 23, 2022 · 4 comments

Comments

@imWildCat
Copy link

imWildCat commented Feb 23, 2022

Development Environment

  • Operating System: macOS 12.2.1 (21D62)
  • Xcode: Version 13.2.1 (13C100)
  • Bazel: 5.0.0

I built a demo: https://github.com/imWildCat/MinimalBazelFrameworkDemo/blob/swift-extension-bug/MinimalBazelFrameworkDemo/ViewController.swift#L23-L29

Here are the snippets from the demo:

The method cannot be found is defined in a Swift extension for a class

public extension DemoManager {
    func demoExtensionMethod() {
        print("demoExtensionMethod")
    }
}

BUILD of main app

load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application")
load(
    "@build_bazel_rules_swift//swift:swift.bzl",
    "swift_library",
)
load("//build-system/ios-utils:plist_fragment.bzl", "plist_fragment")

swift_library(
    name = "MinimalBazelFrameworkDemoAppLib",
    srcs = glob(["**/*.swift"]),
    data = [
        "Base.lproj/Main.storyboard",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "//DemoSwiftFramework:DemoSwiftFrameworkSource",
    ],
)

plist_fragment(
    name = "MinimalBazelFrameworkDemoAppPlist",
    extension = "plist",
    template =
        """
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>MinimalBazelFrameworkDemoApp</string>
    <key>CFBundleIdentifier</key>
    <string>com.demo.MinimalBazelFrameworkDemoApp</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>MinimalBazelFrameworkDemoApp</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0.0</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UIApplicationSceneManifest</key>
    <dict>
      <key>UIApplicationSupportsMultipleScenes</key>
      <false/>
      <key>UISceneConfigurations</key>
      <dict>
        <key>UIWindowSceneSessionRoleApplication</key>
        <array>
          <dict>
            <key>UISceneConfigurationName</key>
            <string>Default Configuration</string>
            <key>UISceneDelegateClassName</key>
            <string>MinimalBazelFrameworkDemo_MinimalBazelFrameworkDemoAppLib.SceneDelegate</string>
          </dict>
        </array>
      </dict>
    </dict>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
      <string>arm64</string>
    </array>
    <key>UIStatusBarTintParameters</key>
    <dict>
      <key>UINavigationBar</key>
      <dict>
        <key>Style</key>
        <string>UIBarStyleDefault</string>
        <key>Translucent</key>
        <false/>
      </dict>
    </dict>
    <key>UISupportedInterfaceOrientations</key>
    <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationLandscapeLeft</string>
      <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationPortraitUpsideDown</string>
      <string>UIInterfaceOrientationLandscapeLeft</string>
      <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    """,
)

ios_application(
    name = "MinimalBazelFrameworkDemoApp",
    bundle_id = "com.demo.MinimalBazelFrameworkDemoApp",
    families = [
        "iphone",
        "ipad",
    ],
    frameworks = [
        "//DemoSwiftFramework",
    ],
    infoplists = [":MinimalBazelFrameworkDemoAppPlist"],
    launch_storyboard = "Base.lproj/LaunchScreen.storyboard",
    minimum_os_version = "14.0",
    deps = [
        ":MinimalBazelFrameworkDemoAppLib",
    ],
)

BUILD of the dynamic framework

load("@build_bazel_rules_apple//apple:ios.bzl", "ios_framework")
load(
    "@build_bazel_rules_swift//swift:swift.bzl",
    "swift_library",
)
load("//build-system/ios-utils:plist_fragment.bzl", "plist_fragment")

swift_library(
    name = "DemoSwiftFrameworkSource",
    srcs = glob([
        "*.swift",
    ]),
    module_name = "DemoSwiftFramework",
    visibility = ["//visibility:public"],
    deps = [],
)

plist_fragment(
    name = "DemoSwiftFrameworkPlist",
    extension = "plist",
    template =
        """
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>DemoSwiftFramework</string>
    <key>CFBundleIdentifier</key>
    <string>com.example.DemoSwiftFramework</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>DemoSwiftFramework</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0.0</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>CFBundlePackageType</key>
    <string>FRWK</string>
        """,
)

ios_framework(
    name = "DemoSwiftFramework",
    bundle_id = "com.example.DemoSwiftFramework",
    families = [
        "iphone",
        "ipad",
    ],
    infoplists = [":DemoSwiftFrameworkPlist"],
    minimum_os_version = "14.0",
    visibility = ["//visibility:public"],
    deps = [":DemoSwiftFrameworkSource"],
)
@imWildCat
Copy link
Author

While debugging, the extension source code was actually included:

image

@imWildCat
Copy link
Author

After some research, by setting alwayslink = True, this issue can be fixed: imWildCat/MinimalBazelFrameworkDemo@59f4afc

I will close this issue tomorrow in case that this solution is a workaround.

@keith
Copy link
Member

keith commented Feb 23, 2022

Thanks for the repro! This bug is tracked here bazelbuild/rules_apple#332 since it's actually the fault of ios_framework

@keith keith closed this as completed Feb 23, 2022
@imWildCat
Copy link
Author

Cool, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants