bazelbuild / rules_apple

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

cc_info_dylibs_partial packages non `.dylib` files too. #2516

Closed hiroyuki-komatsu closed 2 months ago

hiroyuki-komatsu commented 2 months ago

Since 66de359836e375a070a6edde29c22cc5754c5e90, .dylib files are packaged into the target .app file. However it seems other unnecessary files with CcInfo are also packaged.

For example, Frameworks/Myfwk.framework/Myfwk are also packaged as Frameworks/Myfwk.

Steps to reproduce the issue

bazel build test/starlark_tests/targets_under_test/macos:app_with_imported_dynamic_xcframework
unzip -l bazel-bin/test/starlark_tests/targets_under_test/macos/app_with_imported_dynamic_xcframework.zip

Actual behavior

Frameworks/generated_dynamic_macos_xcframework is packaged in addition to Frameworks/generated_dynamic_macos_xcframework.framework/generated_dynamic_macos_xcframework.

Archive:  bazel-bin/test/starlark_tests/targets_under_test/macos/app_with_imported_dynamic_xcframework.zip
  Length     Name
---------    ----
        0    app_with_imported_dynamic_xcframework.app/
   248896    app_with_imported_dynamic_xcframework.app/Contents/Frameworks/generated_dynamic_macos_xcframework
        0    app_with_imported_dynamic_xcframework.app/Contents/Frameworks/generated_dynamic_macos_xcframework.framework/Resources/
      692    app_with_imported_dynamic_xcframework.app/Contents/Frameworks/generated_dynamic_macos_xcframework.framework/Resources/Info.plist
     2442    app_with_imported_dynamic_xcframework.app/Contents/Frameworks/generated_dynamic_macos_xcframework.framework/_CodeSignature/CodeResources
    68704    app_with_imported_dynamic_xcframework.app/Contents/Frameworks/generated_dynamic_macos_xcframework.framework/generated_dynamic_macos_xcframework
      617    app_with_imported_dynamic_xcframework.app/Contents/Info.plist
    51600    app_with_imported_dynamic_xcframework.app/Contents/MacOS/app_with_imported_dynamic_xcframework
        8    app_with_imported_dynamic_xcframework.app/Contents/PkgInfo
      656    app_with_imported_dynamic_xcframework.app/Contents/Resources/codesign_v_fmwks_output.txt
     3094    app_with_imported_dynamic_xcframework.app/Contents/_CodeSignature/CodeResources
---------    -------
   376709    11 files

Expected behavior

  Length     Name
---------    ----
        0    app_with_imported_dynamic_xcframework.app/
        0    app_with_imported_dynamic_xcframework.app/Contents/Frameworks/generated_dynamic_macos_xcframework.framework/Resources/
      692    app_with_imported_dynamic_xcframework.app/Contents/Frameworks/generated_dynamic_macos_xcframework.framework/Resources/Info.plist
     2442    app_with_imported_dynamic_xcframework.app/Contents/Frameworks/generated_dynamic_macos_xcframework.framework/_CodeSignature/CodeResources
    68704    app_with_imported_dynamic_xcframework.app/Contents/Frameworks/generated_dynamic_macos_xcframework.framework/generated_dynamic_macos_xcframework
      617    app_with_imported_dynamic_xcframework.app/Contents/Info.plist
    51600    app_with_imported_dynamic_xcframework.app/Contents/MacOS/app_with_imported_dynamic_xcframework
        8    app_with_imported_dynamic_xcframework.app/Contents/PkgInfo
      656    app_with_imported_dynamic_xcframework.app/Contents/Resources/codesign_v_fmwks_output.txt
     2736    app_with_imported_dynamic_xcframework.app/Contents/_CodeSignature/CodeResources
---------    -------
   127455    10 files

Possible solution

This issue can be addressed by making cc_info_dylibs_partial handle only .dylib files.

https://github.com/bazelbuild/rules_apple/blob/master/apple/internal/partials/cc_info_dylibs.bzl#L35

--- a/apple/internal/partials/cc_info_dylibs.bzl
+++ b/apple/internal/partials/cc_info_dylibs.bzl
@@ -32,7 +32,7 @@ def _cc_info_dylibs_partial_impl(
         cc_info = target[CcInfo]
         for linker_input in cc_info.linking_context.linker_inputs.to_list():
             for library in linker_input.libraries:
-                if library.dynamic_library:
+                if library.dynamic_library and library.dynamic_library.extension == "dylib":
                     bundle_files.append(
                         (processor.location.framework, None, depset([library.dynamic_library])),
                     )

Additional contexts

The current version does not work with frameworks of universal binary (e.g. QtCore). Because:

Here's an actual example