Skip to content

Commit 7568f0d

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

File tree

11 files changed

+449
-3
lines changed

11 files changed

+449
-3
lines changed

apple/internal/ios_rules.bzl

Lines changed: 246 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ load(
9797
"new_iosappclipbundleinfo",
9898
"new_iosapplicationbundleinfo",
9999
"new_iosextensionbundleinfo",
100+
"new_ioskernelextensionbundleinfo",
100101
"new_iosframeworkbundleinfo",
101102
"new_iosimessageapplicationbundleinfo",
102103
"new_iosimessageextensionbundleinfo",
@@ -2560,13 +2561,221 @@ def _ios_sticker_pack_extension_impl(ctx):
25602561
rule_label = label,
25612562
)
25622563

2564+
def _ios_kernel_extension_impl(ctx):
2565+
"""Implementation of ios_kernel_extension."""
2566+
rule_descriptor = rule_support.rule_descriptor(
2567+
platform_type = "ios",
2568+
product_type = apple_product_type.kernel_extension,
2569+
)
2570+
2571+
actions = ctx.actions
2572+
apple_mac_toolchain_info = ctx.attr._mac_toolchain[AppleMacToolsToolchainInfo]
2573+
apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
2574+
bundle_name, bundle_extension = bundling_support.bundle_full_name(
2575+
custom_bundle_name = ctx.attr.bundle_name,
2576+
label_name = ctx.label.name,
2577+
rule_descriptor = rule_descriptor,
2578+
)
2579+
bundle_id = bundling_support.bundle_full_id(
2580+
bundle_id = ctx.attr.bundle_id,
2581+
bundle_id_suffix = ctx.attr.bundle_id_suffix,
2582+
bundle_name = bundle_name,
2583+
suffix_default = ctx.attr._bundle_id_suffix_default,
2584+
shared_capabilities = ctx.attr.shared_capabilities,
2585+
)
2586+
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
2587+
executable_name = ctx.attr.executable_name
2588+
features = features_support.compute_enabled_features(
2589+
requested_features = ctx.features,
2590+
unsupported_features = ctx.disabled_features,
2591+
)
2592+
label = ctx.label
2593+
platform_prerequisites = platform_support.platform_prerequisites(
2594+
apple_fragment = ctx.fragments.apple,
2595+
apple_platform_info = platform_support.apple_platform_info_from_rule_ctx(ctx),
2596+
build_settings = apple_xplat_toolchain_info.build_settings,
2597+
config_vars = ctx.var,
2598+
cpp_fragment = ctx.fragments.cpp,
2599+
device_families = ctx.attr.families,
2600+
explicit_minimum_deployment_os = ctx.attr.minimum_deployment_os_version,
2601+
explicit_minimum_os = ctx.attr.minimum_os_version,
2602+
features = features,
2603+
objc_fragment = ctx.fragments.objc,
2604+
uses_swift = swift_support.uses_swift(ctx.attr.deps),
2605+
xcode_version_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig],
2606+
)
2607+
predeclared_outputs = ctx.outputs
2608+
provisioning_profile = ctx.file.provisioning_profile
2609+
resource_deps = ctx.attr.deps + ctx.attr.resources
2610+
top_level_infoplists = resources.collect(
2611+
attr = ctx.attr,
2612+
res_attrs = ["infoplists"],
2613+
)
2614+
top_level_resources = resources.collect(
2615+
attr = ctx.attr,
2616+
res_attrs = ["resources"],
2617+
)
2618+
2619+
entitlements = entitlements_support.process_entitlements(
2620+
actions = actions,
2621+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2622+
bundle_id = bundle_id,
2623+
entitlements_file = ctx.file.entitlements,
2624+
platform_prerequisites = platform_prerequisites,
2625+
product_type = rule_descriptor.product_type,
2626+
provisioning_profile = provisioning_profile,
2627+
rule_label = label,
2628+
validation_mode = ctx.attr.entitlements_validation,
2629+
)
2630+
# This was added for b/122473338, and should be removed eventually once symbol stripping is
2631+
# better-handled. It's redundant with an option added in the CROSSTOOL for the
2632+
# "kernel_extension" feature, but for now it's necessary to detect kext linking so
2633+
# CompilationSupport.java can apply the correct type of symbol stripping.
2634+
extra_linkopts = [
2635+
"-Wl,-kext",
2636+
]
2637+
2638+
link_result = linking_support.register_binary_linking_action(
2639+
ctx,
2640+
cc_toolchains = cc_toolchain_forwarder,
2641+
entitlements = entitlements.linking,
2642+
exported_symbols_lists = ctx.files.exported_symbols_lists,
2643+
extra_linkopts = extra_linkopts,
2644+
platform_prerequisites = platform_prerequisites,
2645+
rule_descriptor = rule_descriptor,
2646+
stamp = ctx.attr.stamp,
2647+
)
2648+
binary_artifact = link_result.binary
2649+
debug_outputs = linking_support.debug_outputs_by_architecture(link_result.outputs)
2650+
2651+
processor_partials = [
2652+
partials.apple_bundle_info_partial(
2653+
actions = actions,
2654+
bundle_extension = bundle_extension,
2655+
bundle_id = bundle_id,
2656+
bundle_name = bundle_name,
2657+
executable_name = executable_name,
2658+
entitlements = entitlements.bundle,
2659+
label_name = label.name,
2660+
platform_prerequisites = platform_prerequisites,
2661+
predeclared_outputs = predeclared_outputs,
2662+
product_type = rule_descriptor.product_type,
2663+
rule_descriptor = rule_descriptor,
2664+
),
2665+
partials.binary_partial(
2666+
actions = actions,
2667+
binary_artifact = binary_artifact,
2668+
bundle_name = bundle_name,
2669+
executable_name = executable_name,
2670+
label_name = label.name,
2671+
),
2672+
partials.codesigning_dossier_partial(
2673+
actions = actions,
2674+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2675+
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
2676+
bundle_extension = bundle_extension,
2677+
bundle_name = bundle_name,
2678+
entitlements = entitlements.codesigning,
2679+
label_name = label.name,
2680+
platform_prerequisites = platform_prerequisites,
2681+
predeclared_outputs = predeclared_outputs,
2682+
provisioning_profile = provisioning_profile,
2683+
rule_descriptor = rule_descriptor,
2684+
),
2685+
partials.debug_symbols_partial(
2686+
actions = actions,
2687+
bundle_extension = bundle_extension,
2688+
bundle_name = bundle_name,
2689+
debug_dependencies = ctx.attr.deps,
2690+
dsym_binaries = debug_outputs.dsym_binaries,
2691+
dsym_info_plist_template = apple_mac_toolchain_info.dsym_info_plist_template,
2692+
executable_name = executable_name,
2693+
label_name = label.name,
2694+
linkmaps = debug_outputs.linkmaps,
2695+
platform_prerequisites = platform_prerequisites,
2696+
plisttool = apple_mac_toolchain_info.plisttool,
2697+
rule_label = label,
2698+
version = ctx.attr.version,
2699+
),
2700+
partials.resources_partial(
2701+
actions = actions,
2702+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2703+
bundle_extension = bundle_extension,
2704+
bundle_id = bundle_id,
2705+
bundle_name = bundle_name,
2706+
executable_name = executable_name,
2707+
environment_plist = ctx.file._environment_plist,
2708+
launch_storyboard = None,
2709+
platform_prerequisites = platform_prerequisites,
2710+
resource_deps = resource_deps,
2711+
rule_descriptor = rule_descriptor,
2712+
rule_label = label,
2713+
top_level_infoplists = top_level_infoplists,
2714+
top_level_resources = top_level_resources,
2715+
version = ctx.attr.version,
2716+
),
2717+
partials.swift_dylibs_partial(
2718+
actions = actions,
2719+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2720+
binary_artifact = binary_artifact,
2721+
label_name = label.name,
2722+
platform_prerequisites = platform_prerequisites,
2723+
),
2724+
partials.apple_symbols_file_partial(
2725+
actions = actions,
2726+
binary_artifact = binary_artifact,
2727+
dependency_targets = [],
2728+
dsym_binaries = debug_outputs.dsym_binaries,
2729+
label_name = label.name,
2730+
include_symbols_in_bundle = False,
2731+
platform_prerequisites = platform_prerequisites,
2732+
),
2733+
]
2734+
2735+
if platform_prerequisites.platform.is_device:
2736+
processor_partials.append(
2737+
partials.provisioning_profile_partial(
2738+
actions = actions,
2739+
profile_artifact = provisioning_profile,
2740+
rule_label = label,
2741+
),
2742+
)
2743+
2744+
processor_result = processor.process(
2745+
actions = actions,
2746+
apple_mac_toolchain_info = apple_mac_toolchain_info,
2747+
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
2748+
bundle_extension = bundle_extension,
2749+
bundle_name = bundle_name,
2750+
entitlements = entitlements.codesigning,
2751+
features = features,
2752+
ipa_post_processor = ctx.executable.ipa_post_processor,
2753+
partials = processor_partials,
2754+
platform_prerequisites = platform_prerequisites,
2755+
predeclared_outputs = predeclared_outputs,
2756+
process_and_sign_template = apple_mac_toolchain_info.process_and_sign_template,
2757+
provisioning_profile = provisioning_profile,
2758+
rule_descriptor = rule_descriptor,
2759+
rule_label = label,
2760+
)
2761+
25632762
return [
25642763
DefaultInfo(
25652764
files = processor_result.output_files,
25662765
),
2567-
IosStickerPackExtensionBundleInfo(),
2568-
new_iosextensionbundleinfo(),
2569-
OutputGroupInfo(**processor_result.output_groups),
2766+
OutputGroupInfo(
2767+
**outputs.merge_output_groups(
2768+
link_result.output_groups,
2769+
processor_result.output_groups,
2770+
)
2771+
),
2772+
new_appleexecutablebinaryinfo(
2773+
binary = binary_artifact,
2774+
cc_info = link_result.cc_info,
2775+
),
2776+
new_ioskernelextensionbundleinfo(),
2777+
# TODO(b/228856372): Remove when downstream users are migrated off this provider.
2778+
link_result.debug_outputs_provider,
25702779
] + processor_result.providers
25712780

25722781
ios_application = rule_factory.create_apple_rule(
@@ -3199,3 +3408,37 @@ for the stickers should all be in Sticker Pack directories, so `*.stickerpack/*.
31993408
},
32003409
],
32013410
)
3411+
3412+
ios_kernel_extension = rule_factory.create_apple_rule(
3413+
cfg = transition_support.apple_rule_arm64_as_arm64e_transition,
3414+
doc = "Builds and bundles an iOS Kernel Extension.",
3415+
implementation = _ios_kernel_extension_impl,
3416+
predeclared_outputs = {"archive": "%{name}.zip"},
3417+
attrs = [
3418+
apple_support.platform_constraint_attrs(),
3419+
rule_attrs.binary_linking_attrs(
3420+
deps_cfg = transition_support.apple_platform_split_transition,
3421+
extra_deps_aspects = [
3422+
apple_resource_aspect,
3423+
framework_provider_aspect,
3424+
],
3425+
is_test_supporting_rule = False,
3426+
requires_legacy_cc_toolchain = True,
3427+
),
3428+
rule_attrs.common_bundle_attrs(
3429+
deps_cfg = transition_support.apple_platform_split_transition,
3430+
),
3431+
rule_attrs.common_tool_attrs(),
3432+
rule_attrs.device_family_attrs(
3433+
allowed_families = rule_attrs.defaults.allowed_families.ios,
3434+
is_mandatory = False,
3435+
),
3436+
rule_attrs.infoplist_attrs(),
3437+
rule_attrs.ipa_post_processor_attrs(),
3438+
rule_attrs.platform_attrs(
3439+
add_environment_plist = True,
3440+
platform_type = "ios",
3441+
),
3442+
rule_attrs.signing_attrs(),
3443+
],
3444+
)

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},

apple/ios.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ load(
2323
_ios_framework = "ios_framework",
2424
_ios_imessage_application = "ios_imessage_application",
2525
_ios_imessage_extension = "ios_imessage_extension",
26+
_ios_kernel_extension = "ios_kernel_extension",
2627
_ios_static_framework = "ios_static_framework",
2728
_ios_sticker_pack_extension = "ios_sticker_pack_extension",
2829
)
@@ -51,6 +52,7 @@ ios_framework = _ios_framework
5152
ios_imessage_application = _ios_imessage_application
5253
ios_sticker_pack_extension = _ios_sticker_pack_extension
5354
ios_imessage_extension = _ios_imessage_extension
55+
ios_kernel_extension = _ios_kernel_extension
5456
ios_static_framework = _ios_static_framework
5557

5658
_DEFAULT_TEST_RUNNER = str(Label("//apple/testing/default_runner:ios_default_runner"))

apple/ios.doc.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ load(
2626
_ios_framework = "ios_framework",
2727
_ios_imessage_application = "ios_imessage_application",
2828
_ios_imessage_extension = "ios_imessage_extension",
29+
_ios_kernel_extension = "ios_kernel_extension",
2930
_ios_static_framework = "ios_static_framework",
3031
_ios_sticker_pack_extension = "ios_sticker_pack_extension",
3132
)
@@ -57,6 +58,7 @@ ios_extension = _ios_extension
5758
ios_framework = _ios_framework
5859
ios_imessage_application = _ios_imessage_application
5960
ios_imessage_extension = _ios_imessage_extension
61+
ios_kernel_extension = _ios_kernel_extension
6062
ios_static_framework = _ios_static_framework
6163
ios_sticker_pack_extension = _ios_sticker_pack_extension
6264
ios_test_runner = _ios_test_runner

apple/providers.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ load(
5757
_IosFrameworkBundleInfo = "IosFrameworkBundleInfo",
5858
_IosImessageApplicationBundleInfo = "IosImessageApplicationBundleInfo",
5959
_IosImessageExtensionBundleInfo = "IosImessageExtensionBundleInfo",
60+
_IosKernelExtensionBundleInfo = "IosKernelExtensionBundleInfo",
6061
_IosStaticFrameworkBundleInfo = "IosStaticFrameworkBundleInfo",
6162
_IosXcTestBundleInfo = "IosXcTestBundleInfo",
6263
_MacosApplicationBundleInfo = "MacosApplicationBundleInfo",
@@ -109,6 +110,7 @@ IosExtensionBundleInfo = _IosExtensionBundleInfo
109110
IosFrameworkBundleInfo = _IosFrameworkBundleInfo
110111
IosImessageApplicationBundleInfo = _IosImessageApplicationBundleInfo
111112
IosImessageExtensionBundleInfo = _IosImessageExtensionBundleInfo
113+
IosKernelExtensionBundleInfo = _IosKernelExtensionBundleInfo
112114
IosStaticFrameworkBundleInfo = _IosStaticFrameworkBundleInfo
113115
IosXcTestBundleInfo = _IosXcTestBundleInfo
114116
MacosApplicationBundleInfo = _MacosApplicationBundleInfo

0 commit comments

Comments
 (0)