Skip to content

Commit 839cf60

Browse files
authored
Fix import_middleman in Bazel 7 (#873)
Can be considered a follow up to #850 to fix `import_middleman` in Bazel 7 while keeping it backwards compatible with Bazel 6.
1 parent 70e2438 commit 839cf60

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

.github/workflows/preflight_env.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set -e
88
# - XCODE_VERSION: The version of Xcode to use.
99

1010
# GitHub runners are hitting 'module not found pkg_resources' required by prepare_sim.py
11-
pip3 install setuptools --break-system-packages
11+
pip3 install setuptools==69.5.1 --break-system-packages
1212

1313
# If flag --no-bzlmod is passed, writes a user.bazelrc file to disable Bzlmod.
1414
if [[ "$*" == *--no-bzlmod* ]]; then

rules/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ bzl_library(
104104
visibility = ["//visibility:public"],
105105
deps = [
106106
":features",
107+
"//rules:utils.bzl",
107108
"//rules/internal:objc_provider_utils",
108109
"@build_bazel_rules_apple//apple",
109110
],

rules/import_middleman.bzl

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
load("@build_bazel_rules_apple//apple/internal:providers.bzl", "AppleFrameworkImportInfo", "new_appleframeworkimportinfo")
21
load("//rules/internal:objc_provider_utils.bzl", "objc_provider_utils")
2+
load("//rules:utils.bzl", "is_bazel_7")
3+
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
34
load("@build_bazel_rules_apple//apple/internal:bundling_support.bzl", "bundling_support")
5+
load("@build_bazel_rules_apple//apple/internal:providers.bzl", "AppleFrameworkImportInfo", "new_appleframeworkimportinfo")
46

57
_FindImportsAspectInfo = provider(fields = {
68
"imported_library_file": "",
@@ -159,6 +161,15 @@ def _deduplicate_test_deps(test_deps, deps):
159161
return filtered
160162

161163
def _file_collector_rule_impl(ctx):
164+
cc_toolchain = find_cpp_toolchain(ctx)
165+
cc_features = cc_common.configure_features(
166+
ctx = ctx,
167+
cc_toolchain = cc_toolchain,
168+
language = "objc",
169+
requested_features = ctx.features,
170+
unsupported_features = ctx.disabled_features,
171+
)
172+
162173
linker_deps = _merge_linked_inputs(ctx.attr.deps)
163174
test_linker_deps = _merge_linked_inputs(ctx.attr.test_deps)
164175
input_static_frameworks = _deduplicate_test_deps(test_linker_deps[0], linker_deps[0])
@@ -255,8 +266,31 @@ def _file_collector_rule_impl(ctx):
255266
)
256267

257268
# Create the CcInfo provider, linking information from this is used in Bazel 7+.
258-
dep_cc_infos = [dep[CcInfo] for dep in ctx.attr.deps if CcInfo in dep]
259-
cc_info = cc_common.merge_cc_infos(cc_infos = dep_cc_infos)
269+
cc_info = None
270+
if is_bazel_7:
271+
cc_info = CcInfo(
272+
linking_context = cc_common.create_linking_context(
273+
linker_inputs = depset([
274+
cc_common.create_linker_input(
275+
owner = ctx.label,
276+
user_link_flags = compat_link_opt if len(all_replaced_frameworks) else [],
277+
libraries = depset([
278+
cc_common.create_library_to_link(
279+
actions = ctx.actions,
280+
cc_toolchain = cc_toolchain,
281+
feature_configuration = cc_features,
282+
static_library = static_library,
283+
alwayslink = False,
284+
)
285+
for static_library in replaced_static_framework.replaced.values()
286+
]),
287+
),
288+
]),
289+
),
290+
)
291+
else:
292+
dep_cc_infos = [dep[CcInfo] for dep in ctx.attr.deps if CcInfo in dep]
293+
cc_info = cc_common.merge_cc_infos(cc_infos = dep_cc_infos)
260294

261295
return [
262296
DefaultInfo(files = depset(dynamic_framework_dirs + replaced_frameworks)),
@@ -266,11 +300,20 @@ def _file_collector_rule_impl(ctx):
266300

267301
import_middleman = rule(
268302
implementation = _file_collector_rule_impl,
269-
fragments = ["apple"],
303+
fragments = ["apple", "cpp"],
304+
toolchains = use_cpp_toolchain(),
270305
attrs = {
271306
"deps": attr.label_list(aspects = [find_imports]),
272307
"test_deps": attr.label_list(aspects = [find_imports], allow_empty = True),
273308
"update_in_place": attr.label(executable = True, default = Label("//tools/m1_utils:update_in_place"), cfg = "exec"),
309+
"_cc_toolchain": attr.label(
310+
providers = [cc_common.CcToolchainInfo],
311+
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
312+
doc = """\
313+
The C++ toolchain from which linking flags and other tools needed by the Swift
314+
toolchain (such as `clang`) will be retrieved.
315+
""",
316+
),
274317
},
275318
doc = """
276319
This rule adds the ability to update the Mach-o header on imported

0 commit comments

Comments
 (0)