-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add split toolchain support for platforms to the rules_apple apple_st…
- Loading branch information
Showing
9 changed files
with
450 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# Copyright 2022 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
A rule for handling the cc_toolchains and their constraints for a potential "fat" Mach-O binary. | ||
""" | ||
|
||
load("@build_bazel_rules_apple//apple:providers.bzl", "ApplePlatformInfo") | ||
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain") | ||
|
||
def _target_os_from_rule_ctx(ctx): | ||
"""Returns a `String` representing the selected Apple OS.""" | ||
ios_constraint = ctx.attr._ios_constraint[platform_common.ConstraintValueInfo] | ||
macos_constraint = ctx.attr._macos_constraint[platform_common.ConstraintValueInfo] | ||
tvos_constraint = ctx.attr._tvos_constraint[platform_common.ConstraintValueInfo] | ||
watchos_constraint = ctx.attr._watchos_constraint[platform_common.ConstraintValueInfo] | ||
|
||
if ctx.target_platform_has_constraint(ios_constraint): | ||
return str(apple_common.platform_type.ios) | ||
elif ctx.target_platform_has_constraint(macos_constraint): | ||
return str(apple_common.platform_type.macos) | ||
elif ctx.target_platform_has_constraint(tvos_constraint): | ||
return str(apple_common.platform_type.tvos) | ||
elif ctx.target_platform_has_constraint(watchos_constraint): | ||
return str(apple_common.platform_type.watchos) | ||
fail("ERROR: A valid Apple platform constraint could not be found from the resolved toolchain.") | ||
|
||
def _target_arch_from_rule_ctx(ctx): | ||
"""Returns a `String` representing the selected target architecture or cpu type.""" | ||
arm64_constraint = ctx.attr._arm64_constraint[platform_common.ConstraintValueInfo] | ||
arm64e_constraint = ctx.attr._arm64e_constraint[platform_common.ConstraintValueInfo] | ||
arm64_32_constraint = ctx.attr._arm64_32_constraint[platform_common.ConstraintValueInfo] | ||
armv7_constraint = ctx.attr._armv7_constraint[platform_common.ConstraintValueInfo] | ||
armv7k_constraint = ctx.attr._armv7k_constraint[platform_common.ConstraintValueInfo] | ||
x86_64_constraint = ctx.attr._x86_64_constraint[platform_common.ConstraintValueInfo] | ||
i386_constraint = ctx.attr._i386_constraint[platform_common.ConstraintValueInfo] | ||
|
||
if ctx.target_platform_has_constraint(arm64_constraint): | ||
return "arm64" | ||
elif ctx.target_platform_has_constraint(arm64e_constraint): | ||
return "arm64e" | ||
elif ctx.target_platform_has_constraint(arm64_32_constraint): | ||
return "arm64_32" | ||
elif ctx.target_platform_has_constraint(armv7_constraint): | ||
return "armv7" | ||
elif ctx.target_platform_has_constraint(armv7k_constraint): | ||
return "armv7k" | ||
elif ctx.target_platform_has_constraint(x86_64_constraint): | ||
return "x86_64" | ||
elif ctx.target_platform_has_constraint(i386_constraint): | ||
return "i386" | ||
fail("ERROR: A valid Apple cpu constraint could not be found from the resolved toolchain.") | ||
|
||
def _target_environment_from_rule_ctx(ctx): | ||
"""Returns a `String` representing the selected environment (e.g. "device", "simulator").""" | ||
device_constraint = ctx.attr._apple_device_constraint[platform_common.ConstraintValueInfo] | ||
simulator_constraint = ctx.attr._apple_simulator_constraint[platform_common.ConstraintValueInfo] | ||
|
||
if ctx.target_platform_has_constraint(device_constraint): | ||
return "device" | ||
elif ctx.target_platform_has_constraint(simulator_constraint): | ||
return "simulator" | ||
fail("ERROR: A valid Apple environment (device, simulator) constraint could not be found from" + | ||
" the resolved toolchain.") | ||
|
||
def _cc_toolchain_forwarder_impl(ctx): | ||
return [ | ||
find_cpp_toolchain(ctx), | ||
ApplePlatformInfo( | ||
target_os = _target_os_from_rule_ctx(ctx), | ||
target_arch = _target_arch_from_rule_ctx(ctx), | ||
target_environment = _target_environment_from_rule_ctx(ctx), | ||
), | ||
] | ||
|
||
cc_toolchain_forwarder = rule( | ||
implementation = _cc_toolchain_forwarder_impl, | ||
attrs = { | ||
# Legacy style toolchain assignment. | ||
"_cc_toolchain": attr.label( | ||
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), | ||
), | ||
# The full list of possible constraints to be resolved are assigned here. | ||
"_ios_constraint": attr.label( | ||
default = Label("@platforms//os:ios"), | ||
), | ||
"_macos_constraint": attr.label( | ||
default = Label("@platforms//os:macos"), | ||
), | ||
"_tvos_constraint": attr.label( | ||
default = Label("@platforms//os:tvos"), | ||
), | ||
"_watchos_constraint": attr.label( | ||
default = Label("@platforms//os:watchos"), | ||
), | ||
"_arm64_constraint": attr.label( | ||
default = Label("@platforms//cpu:arm64"), | ||
), | ||
"_arm64e_constraint": attr.label( | ||
default = Label("@platforms//cpu:arm64e"), | ||
), | ||
"_arm64_32_constraint": attr.label( | ||
default = Label("@platforms//cpu:arm64_32"), | ||
), | ||
"_armv7_constraint": attr.label( | ||
default = Label("@platforms//cpu:armv7"), | ||
), | ||
"_armv7k_constraint": attr.label( | ||
default = Label("@platforms//cpu:armv7k"), | ||
), | ||
"_x86_64_constraint": attr.label( | ||
default = Label("@platforms//cpu:x86_64"), | ||
), | ||
"_i386_constraint": attr.label( | ||
default = Label("@platforms//cpu:i386"), | ||
), | ||
"_apple_device_constraint": attr.label( | ||
default = Label("@build_bazel_apple_support//constraints:device"), | ||
), | ||
"_apple_simulator_constraint": attr.label( | ||
default = Label("@build_bazel_apple_support//constraints:simulator"), | ||
), | ||
}, | ||
doc = """ | ||
Shared rule that returns CcToolchainInfo, plus a rules_apple defined provider based on querying | ||
ctx.target_platform_has_constraint(...) that covers all Apple cpu, platform, environment constraints | ||
for the purposes of understanding what constraints the results of each Apple split transition | ||
resolve to from the perspective of any bundling and binary rules that generate "fat" Apple binaries. | ||
""", | ||
provides = [cc_common.CcToolchainInfo, ApplePlatformInfo], | ||
# Anticipated "new" toolchain assignment. | ||
toolchains = use_cpp_toolchain(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.