From 36da6cd3644e6d72f8856da8c02f5988740e978c Mon Sep 17 00:00:00 2001 From: Jerry Marino Date: Tue, 12 Sep 2023 20:27:22 -0700 Subject: [PATCH] [Bazel 6.x.x] sdk_dylib at _end_ of the linker invocation Apple's > Xcode 4.x linkers have problems when SDK dylibs are first - Bazel 6.0 broke this due to refactoring. see changes in ( https://github.com/bazelbuild/bazel/blob/283ed362e6ccceb047553c2517a0331afd02db90/tools/osx/crosstool/cc_toolchain_config.bzl#L261 ) So this PR, is adding sdk_dylib to _end_ of the linker invocation to make this work as it did --- rules/internal/framework_middleman.bzl | 22 +++++++++++++++++++ .../test-imports-app/BUILD.GoogleMobileAds | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/rules/internal/framework_middleman.bzl b/rules/internal/framework_middleman.bzl index db2aec5e0..7f8fd6f58 100644 --- a/rules/internal/framework_middleman.bzl +++ b/rules/internal/framework_middleman.bzl @@ -170,6 +170,16 @@ def _dedupe_key(key, avoid_libraries, objc_provider_fields, check_name = False): updated_library.append(f) objc_provider_fields[key] = depset(updated_library) +def _get_lib_name(name): + """return a lib name for a name dropping prefix/suffix""" + if name.startswith("lib"): + name = name[3:] + if name.endswith(".dylib"): + name = name[:-6] + elif name.endswith(".so"): + name = name[:-3] + return name + def _dep_middleman(ctx): objc_providers = [] cc_providers = [] @@ -225,6 +235,18 @@ def _dep_middleman(ctx): _dedupe_key("imported_library", avoid_libraries, objc_provider_fields, check_name = True) _dedupe_key("static_framework_file", avoid_libraries, objc_provider_fields, check_name = True) + if "sdk_dylib" in objc_provider_fields: + # Put sdk_dylib at _end_ of the linker invocation. Apple's linkers have + # problems when SDK dylibs are first in the list, starting with Bazel + # 6.0 this is backwards by default + objc_provider_fields["linkopt"] = depset( + [], + transitive = [ + objc_provider_fields.get("linkopt", depset([])), + depset(["-l" + _get_lib_name(lib) for lib in objc_provider_fields.pop("sdk_dylib").to_list()]), + ], + ) + objc_provider = apple_common.new_objc_provider(**objc_provider_fields) cc_info_provider = cc_common.merge_cc_infos(direct_cc_infos = [], cc_infos = cc_providers) providers = [ diff --git a/tests/ios/unit-test/test-imports-app/BUILD.GoogleMobileAds b/tests/ios/unit-test/test-imports-app/BUILD.GoogleMobileAds index a35947486..d353ed9aa 100644 --- a/tests/ios/unit-test/test-imports-app/BUILD.GoogleMobileAds +++ b/tests/ios/unit-test/test-imports-app/BUILD.GoogleMobileAds @@ -10,7 +10,7 @@ apple_static_framework_import( allow_empty = False, ), sdk_dylibs = [ - "libsqlite3", + "libsqlite3.dylib", "libz", ], sdk_frameworks = [