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

Fix import_middleman in Bazel 7 #873

Merged
merged 7 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/preflight_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set -e
# - XCODE_VERSION: The version of Xcode to use.

# GitHub runners are hitting 'module not found pkg_resources' required by prepare_sim.py
pip3 install setuptools --break-system-packages
pip3 install setuptools==69.5.1 --break-system-packages
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the other changes in this PR. Fixes this error happening on CI (see repro):

/Users/runner/work/rules_ios/rules_ios/tools/tests/prepare_sim.py:9: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  from pkg_resources import packaging
Traceback (most recent call last):
  File "/Users/runner/work/rules_ios/rules_ios/tools/tests/prepare_sim.py", line 9, in <module>
    from pkg_resources import packaging
ImportError: cannot import name 'packaging' from 'pkg_resources' (/opt/homebrew/lib/python3.12/site-packages/pkg_resources/__init__.py)

(found a thread online where someone was suggesting stick to this version to fix it)


# If flag --no-bzlmod is passed, writes a user.bazelrc file to disable Bzlmod.
if [[ "$*" == *--no-bzlmod* ]]; then
Expand Down
1 change: 1 addition & 0 deletions rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ bzl_library(
visibility = ["//visibility:public"],
deps = [
":features",
"//rules:utils.bzl",
"//rules/internal:objc_provider_utils",
"@build_bazel_rules_apple//apple",
],
Expand Down
51 changes: 47 additions & 4 deletions rules/import_middleman.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
load("@build_bazel_rules_apple//apple/internal:providers.bzl", "AppleFrameworkImportInfo", "new_appleframeworkimportinfo")
load("//rules/internal:objc_provider_utils.bzl", "objc_provider_utils")
load("//rules:utils.bzl", "is_bazel_7")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
load("@build_bazel_rules_apple//apple/internal:bundling_support.bzl", "bundling_support")
load("@build_bazel_rules_apple//apple/internal:providers.bzl", "AppleFrameworkImportInfo", "new_appleframeworkimportinfo")

_FindImportsAspectInfo = provider(fields = {
"imported_library_file": "",
Expand Down Expand Up @@ -159,6 +161,15 @@ def _deduplicate_test_deps(test_deps, deps):
return filtered

def _file_collector_rule_impl(ctx):
cc_toolchain = find_cpp_toolchain(ctx)
cc_features = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
language = "objc",
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)

linker_deps = _merge_linked_inputs(ctx.attr.deps)
test_linker_deps = _merge_linked_inputs(ctx.attr.test_deps)
input_static_frameworks = _deduplicate_test_deps(test_linker_deps[0], linker_deps[0])
Expand Down Expand Up @@ -255,8 +266,31 @@ def _file_collector_rule_impl(ctx):
)

# Create the CcInfo provider, linking information from this is used in Bazel 7+.
dep_cc_infos = [dep[CcInfo] for dep in ctx.attr.deps if CcInfo in dep]
cc_info = cc_common.merge_cc_infos(cc_infos = dep_cc_infos)
cc_info = None
if is_bazel_7:
cc_info = CcInfo(
linking_context = cc_common.create_linking_context(
linker_inputs = depset([
cc_common.create_linker_input(
owner = ctx.label,
user_link_flags = compat_link_opt if len(all_replaced_frameworks) else [],
libraries = depset([
cc_common.create_library_to_link(
actions = ctx.actions,
cc_toolchain = cc_toolchain,
feature_configuration = cc_features,
static_library = static_library,
alwayslink = False,
)
for static_library in replaced_static_framework.replaced.values()
]),
),
]),
),
)
else:
dep_cc_infos = [dep[CcInfo] for dep in ctx.attr.deps if CcInfo in dep]
cc_info = cc_common.merge_cc_infos(cc_infos = dep_cc_infos)

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

import_middleman = rule(
implementation = _file_collector_rule_impl,
fragments = ["apple"],
fragments = ["apple", "cpp"],
toolchains = use_cpp_toolchain(),
attrs = {
"deps": attr.label_list(aspects = [find_imports]),
"test_deps": attr.label_list(aspects = [find_imports], allow_empty = True),
"update_in_place": attr.label(executable = True, default = Label("//tools/m1_utils:update_in_place"), cfg = "exec"),
"_cc_toolchain": attr.label(
providers = [cc_common.CcToolchainInfo],
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
doc = """\
The C++ toolchain from which linking flags and other tools needed by the Swift
toolchain (such as `clang`) will be retrieved.
""",
),
},
doc = """
This rule adds the ability to update the Mach-o header on imported
Expand Down