Skip to content

Commit 67656bb

Browse files
ilhanrajaIlhan Raja
authored andcommitted
feat: Add support for ios_kernel_extension() bazel rule
1 parent 585ba4c commit 67656bb

File tree

15 files changed

+506
-1
lines changed

15 files changed

+506
-1
lines changed

MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ bazel_dep(
3131
non_module_deps = use_extension("//apple:extensions.bzl", "non_module_deps")
3232
use_repo(non_module_deps, "xctestrunner")
3333

34+
macos_iokit_headers = use_extension("//apple:extensions.bzl", "macos_iokit_headers")
35+
use_repo(macos_iokit_headers, "macos_iokit_headers")
36+
3437
provisioning_profile_repository = use_extension("//apple:apple.bzl", "provisioning_profile_repository_extension")
3538
use_repo(provisioning_profile_repository, "local_provisioning_profiles")
3639

apple/extensions.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Definitions for bzlmod module extensions."""
1616

1717
load("@bazel_features//:features.bzl", "bazel_features")
18+
load("//apple/internal:macosx_iokit.bzl", "macosx_iokit")
1819
load("//apple/internal:repositories.bzl", "apple_rules_dependencies")
1920

2021
def _non_module_deps_impl(module_ctx):
@@ -29,3 +30,8 @@ def _non_module_deps_impl(module_ctx):
2930
)
3031

3132
non_module_deps = module_extension(implementation = _non_module_deps_impl)
33+
34+
def _macos_iokit_headers_impl(module_ctx):
35+
macosx_iokit(name = "macos_iokit_headers")
36+
37+
macos_iokit_headers = module_extension(implementation = _macos_iokit_headers_impl)

apple/internal/ios_rules.bzl

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ load(
100100
"new_iosframeworkbundleinfo",
101101
"new_iosimessageapplicationbundleinfo",
102102
"new_iosimessageextensionbundleinfo",
103+
"new_ioskernelextensionbundleinfo",
103104
"new_iosstaticframeworkbundleinfo",
104105
)
105106
load(
@@ -2569,6 +2570,225 @@ def _ios_sticker_pack_extension_impl(ctx):
25692570
OutputGroupInfo(**processor_result.output_groups),
25702571
] + processor_result.providers
25712572

2573+
def _ios_kernel_extension_impl(ctx):
2574+
"""Implementation of ios_kernel_extension."""
2575+
rule_descriptor = rule_support.rule_descriptor(
2576+
platform_type = "ios",
2577+
product_type = apple_product_type.kernel_extension,
2578+
)
2579+
2580+
actions = ctx.actions
2581+
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
2582+
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
2583+
bundle_name, bundle_extension = bundling_support.bundle_full_name(
2584+
custom_bundle_name = ctx.attr.bundle_name,
2585+
label_name = ctx.label.name,
2586+
rule_descriptor = rule_descriptor,
2587+
)
2588+
bundle_id = bundling_support.bundle_full_id(
2589+
bundle_id = ctx.attr.bundle_id,
2590+
bundle_id_suffix = ctx.attr.bundle_id_suffix,
2591+
bundle_name = bundle_name,
2592+
suffix_default = ctx.attr._bundle_id_suffix_default,
2593+
shared_capabilities = ctx.attr.shared_capabilities,
2594+
)
2595+
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
2596+
executable_name = ctx.attr.executable_name
2597+
features = features_support.compute_enabled_features(
2598+
requested_features = ctx.features,
2599+
unsupported_features = ctx.disabled_features,
2600+
)
2601+
label = ctx.label
2602+
platform_prerequisites = platform_support.platform_prerequisites(
2603+
apple_fragment = ctx.fragments.apple,
2604+
apple_platform_info = platform_support.apple_platform_info_from_rule_ctx(ctx),
2605+
build_settings = apple_xplat_toolchain_info.build_settings,
2606+
config_vars = ctx.var,
2607+
cpp_fragment = ctx.fragments.cpp,
2608+
device_families = ctx.attr.families,
2609+
explicit_minimum_deployment_os = ctx.attr.minimum_deployment_os_version,
2610+
explicit_minimum_os = ctx.attr.minimum_os_version,
2611+
features = features,
2612+
objc_fragment = ctx.fragments.objc,
2613+
uses_swift = swift_support.uses_swift(ctx.attr.deps),
2614+
xcode_version_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig],
2615+
)
2616+
predeclared_outputs = ctx.outputs
2617+
provisioning_profile = ctx.file.provisioning_profile
2618+
resource_deps = ctx.attr.deps + ctx.attr.resources
2619+
top_level_infoplists = resources.collect(
2620+
attr = ctx.attr,
2621+
res_attrs = ["infoplists"],
2622+
)
2623+
top_level_resources = resources.collect(
2624+
attr = ctx.attr,
2625+
res_attrs = ["resources"],
2626+
)
2627+
2628+
entitlements = entitlements_support.process_entitlements(
2629+
actions = actions,
2630+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2631+
bundle_id = bundle_id,
2632+
entitlements_file = ctx.file.entitlements,
2633+
platform_prerequisites = platform_prerequisites,
2634+
product_type = rule_descriptor.product_type,
2635+
provisioning_profile = provisioning_profile,
2636+
rule_label = label,
2637+
validation_mode = ctx.attr.entitlements_validation,
2638+
)
2639+
2640+
# This was added for b/122473338, and should be removed eventually once symbol stripping is
2641+
# better-handled. It's redundant with an option added in the CROSSTOOL for the
2642+
# "kernel_extension" feature, but for now it's necessary to detect kext linking so
2643+
# CompilationSupport.java can apply the correct type of symbol stripping.
2644+
extra_linkopts = [
2645+
"-Wl,-kext",
2646+
]
2647+
2648+
link_result = linking_support.register_binary_linking_action(
2649+
ctx,
2650+
cc_toolchains = cc_toolchain_forwarder,
2651+
entitlements = entitlements.linking,
2652+
exported_symbols_lists = ctx.files.exported_symbols_lists,
2653+
extra_linkopts = extra_linkopts,
2654+
extra_requested_features = ["kernel_extension"],
2655+
platform_prerequisites = platform_prerequisites,
2656+
rule_descriptor = rule_descriptor,
2657+
stamp = ctx.attr.stamp,
2658+
)
2659+
binary_artifact = link_result.binary
2660+
debug_outputs = linking_support.debug_outputs_by_architecture(link_result.outputs)
2661+
2662+
processor_partials = [
2663+
partials.apple_bundle_info_partial(
2664+
actions = actions,
2665+
bundle_extension = bundle_extension,
2666+
bundle_id = bundle_id,
2667+
bundle_name = bundle_name,
2668+
executable_name = executable_name,
2669+
entitlements = entitlements.bundle,
2670+
label_name = label.name,
2671+
platform_prerequisites = platform_prerequisites,
2672+
predeclared_outputs = predeclared_outputs,
2673+
product_type = rule_descriptor.product_type,
2674+
rule_descriptor = rule_descriptor,
2675+
),
2676+
partials.binary_partial(
2677+
actions = actions,
2678+
binary_artifact = binary_artifact,
2679+
bundle_name = bundle_name,
2680+
executable_name = executable_name,
2681+
label_name = label.name,
2682+
),
2683+
partials.codesigning_dossier_partial(
2684+
actions = actions,
2685+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2686+
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
2687+
bundle_extension = bundle_extension,
2688+
bundle_name = bundle_name,
2689+
entitlements = entitlements.codesigning,
2690+
label_name = label.name,
2691+
platform_prerequisites = platform_prerequisites,
2692+
predeclared_outputs = predeclared_outputs,
2693+
provisioning_profile = provisioning_profile,
2694+
rule_descriptor = rule_descriptor,
2695+
),
2696+
partials.debug_symbols_partial(
2697+
actions = actions,
2698+
bundle_extension = bundle_extension,
2699+
bundle_name = bundle_name,
2700+
debug_dependencies = ctx.attr.deps,
2701+
dsym_binaries = debug_outputs.dsym_binaries,
2702+
dsym_info_plist_template = apple_mac_toolchain_info.dsym_info_plist_template,
2703+
executable_name = executable_name,
2704+
label_name = label.name,
2705+
linkmaps = debug_outputs.linkmaps,
2706+
platform_prerequisites = platform_prerequisites,
2707+
plisttool = apple_mac_toolchain_info.plisttool,
2708+
rule_label = label,
2709+
version = ctx.attr.version,
2710+
),
2711+
partials.resources_partial(
2712+
actions = actions,
2713+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2714+
bundle_extension = bundle_extension,
2715+
bundle_id = bundle_id,
2716+
bundle_name = bundle_name,
2717+
executable_name = executable_name,
2718+
environment_plist = ctx.file._environment_plist,
2719+
launch_storyboard = None,
2720+
platform_prerequisites = platform_prerequisites,
2721+
resource_deps = resource_deps,
2722+
rule_descriptor = rule_descriptor,
2723+
rule_label = label,
2724+
top_level_infoplists = top_level_infoplists,
2725+
top_level_resources = top_level_resources,
2726+
version = ctx.attr.version,
2727+
),
2728+
partials.swift_dylibs_partial(
2729+
actions = actions,
2730+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2731+
binary_artifact = binary_artifact,
2732+
label_name = label.name,
2733+
platform_prerequisites = platform_prerequisites,
2734+
),
2735+
partials.apple_symbols_file_partial(
2736+
actions = actions,
2737+
binary_artifact = binary_artifact,
2738+
dependency_targets = [],
2739+
dsym_binaries = debug_outputs.dsym_binaries,
2740+
label_name = label.name,
2741+
include_symbols_in_bundle = False,
2742+
platform_prerequisites = platform_prerequisites,
2743+
),
2744+
]
2745+
2746+
if platform_prerequisites.platform.is_device:
2747+
processor_partials.append(
2748+
partials.provisioning_profile_partial(
2749+
actions = actions,
2750+
profile_artifact = provisioning_profile,
2751+
rule_label = label,
2752+
),
2753+
)
2754+
2755+
processor_result = processor.process(
2756+
actions = actions,
2757+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2758+
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
2759+
bundle_extension = bundle_extension,
2760+
bundle_name = bundle_name,
2761+
entitlements = entitlements.codesigning,
2762+
features = features,
2763+
ipa_post_processor = ctx.executable.ipa_post_processor,
2764+
partials = processor_partials,
2765+
platform_prerequisites = platform_prerequisites,
2766+
predeclared_outputs = predeclared_outputs,
2767+
process_and_sign_template = apple_mac_toolchain_info.process_and_sign_template,
2768+
provisioning_profile = provisioning_profile,
2769+
rule_descriptor = rule_descriptor,
2770+
rule_label = label,
2771+
)
2772+
2773+
return [
2774+
DefaultInfo(
2775+
files = processor_result.output_files,
2776+
),
2777+
OutputGroupInfo(
2778+
**outputs.merge_output_groups(
2779+
link_result.output_groups,
2780+
processor_result.output_groups,
2781+
)
2782+
),
2783+
new_appleexecutablebinaryinfo(
2784+
binary = binary_artifact,
2785+
cc_info = link_result.cc_info,
2786+
),
2787+
new_ioskernelextensionbundleinfo(),
2788+
# TODO(b/228856372): Remove when downstream users are migrated off this provider.
2789+
link_result.debug_outputs_provider,
2790+
] + processor_result.providers
2791+
25722792
ios_application = rule_factory.create_apple_rule(
25732793
doc = "Builds and bundles an iOS Application.",
25742794
implementation = _ios_application_impl,
@@ -3199,3 +3419,37 @@ for the stickers should all be in Sticker Pack directories, so `*.stickerpack/*.
31993419
},
32003420
],
32013421
)
3422+
3423+
ios_kernel_extension = rule_factory.create_apple_rule(
3424+
cfg = transition_support.apple_rule_arm64_as_arm64e_transition,
3425+
doc = "Builds and bundles an iOS Kernel Extension.",
3426+
implementation = _ios_kernel_extension_impl,
3427+
predeclared_outputs = {"archive": "%{name}.zip"},
3428+
attrs = [
3429+
apple_support.platform_constraint_attrs(),
3430+
rule_attrs.binary_linking_attrs(
3431+
deps_cfg = transition_support.apple_platform_split_transition,
3432+
extra_deps_aspects = [
3433+
apple_resource_aspect,
3434+
framework_provider_aspect,
3435+
],
3436+
is_test_supporting_rule = False,
3437+
requires_legacy_cc_toolchain = True,
3438+
),
3439+
rule_attrs.common_bundle_attrs(
3440+
deps_cfg = transition_support.apple_platform_split_transition,
3441+
),
3442+
rule_attrs.common_tool_attrs(),
3443+
rule_attrs.device_family_attrs(
3444+
allowed_families = rule_attrs.defaults.allowed_families.ios,
3445+
is_mandatory = False,
3446+
),
3447+
rule_attrs.infoplist_attrs(),
3448+
rule_attrs.ipa_post_processor_attrs(),
3449+
rule_attrs.platform_attrs(
3450+
add_environment_plist = True,
3451+
platform_type = "ios",
3452+
),
3453+
rule_attrs.signing_attrs(),
3454+
],
3455+
)

apple/internal/macosx_iokit.bzl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def _macosx_iokit_impl(repository_ctx):
2+
# Run xcrun to get the macOS SDK path
3+
result = repository_ctx.execute(["xcrun", "--show-sdk-path", "--sdk", "macosx"])
4+
if result.return_code != 0:
5+
fail("Failed to get macOS SDK path: " + result.stderr)
6+
sdk_path = result.stdout.strip()
7+
8+
# We want to expose System/Library/Frameworks/Kernel.framework/Headers
9+
headers_src = sdk_path + "/System/Library/Frameworks/Kernel.framework/Headers"
10+
11+
# Create a symlink in the repository
12+
repository_ctx.symlink(headers_src, "Headers")
13+
14+
# Create BUILD file
15+
repository_ctx.file(
16+
"BUILD",
17+
"""
18+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
19+
20+
cc_library(
21+
name = "iokit_headers",
22+
hdrs = select({
23+
"@platforms//os:macos": [],
24+
"//conditions:default": glob(["Headers/**"]),
25+
}),
26+
includes = select({
27+
"@platforms//os:macos": [],
28+
"//conditions:default": ["Headers"],
29+
}),
30+
visibility = ["//visibility:public"],
31+
)
32+
""",
33+
)
34+
35+
macosx_iokit = repository_rule(
36+
implementation = _macosx_iokit_impl,
37+
environ = ["XCODE_VERSION_OVERRIDE"],
38+
)

apple/internal/providers.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,20 @@ that requirement.
662662
init = _make_banned_init(provider_name = "IosImessageExtensionBundleInfo"),
663663
)
664664

665+
IosKernelExtensionBundleInfo, new_ioskernelextensionbundleinfo = provider(
666+
doc = """
667+
Denotes that a target is an iOS kernel extension.
668+
669+
This provider does not contain any fields of its own at this time but is used as
670+
a "marker" to indicate that a target is specifically an iOS kernel extension
671+
bundle (and not some other Apple bundle). Rule authors who wish to require that
672+
a dependency is an iOS kernel extension should use this provider to describe that
673+
requirement.
674+
""",
675+
fields = {},
676+
init = _make_banned_init(provider_name = "IosKernelExtensionBundleInfo"),
677+
)
678+
665679
IosXcTestBundleInfo, new_iosxctestbundleinfo = provider(
666680
doc = """
667681
Denotes a target that is an iOS .xctest bundle.

apple/internal/rule_support.bzl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ _DEFAULT_MACOS_BUNDLE_LOCATIONS = _describe_bundle_locations(
141141
contents_relative_resources = "Resources",
142142
)
143143

144+
# Kernel extensions on iOS are not bundled in the same way as other Apple artifacts.
145+
_DEFAULT_IOS_KERNEL_EXTENSION_BUNDLE_LOCATIONS = _describe_bundle_locations(
146+
bundle_relative_contents = "",
147+
contents_relative_binary = "",
148+
contents_relative_resources = "",
149+
)
150+
144151
# Descriptors for all possible platform/product type combinations.
145152
# TODO(b/248317958): Migrate rpaths to args on the linking_support methods.
146153
_RULE_TYPE_DESCRIPTORS = {
@@ -222,6 +229,16 @@ _RULE_TYPE_DESCRIPTORS = {
222229
"@loader_path/Frameworks",
223230
],
224231
),
232+
# ios_kernel_extension
233+
apple_product_type.kernel_extension: _describe_rule_type(
234+
allowed_device_families = ["iphone", "ipad"],
235+
binary_infoplist = False,
236+
bundle_extension = ".kext",
237+
bundle_locations = _DEFAULT_IOS_KERNEL_EXTENSION_BUNDLE_LOCATIONS,
238+
bundle_package_type = bundle_package_type.kernel_extension,
239+
product_type = apple_product_type.kernel_extension,
240+
requires_signing_for_device = False,
241+
),
225242
# ios_imessage_application
226243
apple_product_type.messages_application: _describe_rule_type(
227244
additional_infoplist_values = {"LSApplicationLaunchProhibited": True},

0 commit comments

Comments
 (0)