Skip to content

Commit

Permalink
Add split toolchain support for platforms to the rules_apple apple_st…
Browse files Browse the repository at this point in the history
…atic_library. (#1533)

(cherry picked from commit 80ed692)
  • Loading branch information
keith authored Aug 10, 2023
1 parent b4c44b0 commit a12d1df
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 16 deletions.
15 changes: 15 additions & 0 deletions apple/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load(":cc_toolchain_forwarder.bzl", "cc_toolchain_forwarder")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -62,6 +63,7 @@ bzl_library(
"//apple/internal:rule_factory",
"//apple/internal:transition_support",
"@bazel_skylib//lib:dicts",
"@bazel_tools//tools/cpp:toolchain_utils",
],
)

Expand All @@ -73,6 +75,15 @@ bzl_library(
],
)

bzl_library(
name = "cc_toolchain_forwarder",
srcs = ["cc_toolchain_forwarder.bzl"],
deps = [
":providers",
"@bazel_tools//tools/cpp:toolchain_utils",
],
)

bzl_library(
name = "common",
srcs = ["common.bzl"],
Expand Down Expand Up @@ -203,6 +214,10 @@ bzl_library(
deps = ["//apple/internal:xcarchive"],
)

cc_toolchain_forwarder(
name = "default_cc_toolchain_forwarder",
)

# Consumed by bazel tests.
filegroup(
name = "for_bazel_tests",
Expand Down
25 changes: 22 additions & 3 deletions apple/apple_static_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,30 @@ load(
load(
"@build_bazel_rules_apple//apple:providers.bzl",
"AppleBinaryInfo",
"ApplePlatformInfo",
)
load(
"@bazel_skylib//lib:dicts.bzl",
"dicts",
)
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "use_cpp_toolchain")

def _apple_static_library_impl(ctx):
# Validation of the platform type and minimum version OS currently happen in
# `transition_support.apple_platform_transition`, either implicitly through native
# Most validation of the platform type and minimum version OS currently happens in
# `transition_support.apple_platform_split_transition`, either implicitly through native
# `dotted_version` or explicitly through `fail` on an unrecognized platform type value.

# Validate that the resolved platform matches the platform_type attr.
for toolchain_key, resolved_toolchain in ctx.split_attr._cc_toolchain_forwarder.items():
if resolved_toolchain[ApplePlatformInfo].target_os != ctx.attr.platform_type:
fail("""
ERROR: Unexpected resolved platform:
Expected Apple platform type of "{platform_type}", but that was not found in {toolchain_key}.
""".format(
platform_type = ctx.attr.platform_type,
toolchain_key = toolchain_key,
))

link_result = linking_support.register_static_library_linking_action(ctx = ctx)

files_to_build = [link_result.library]
Expand Down Expand Up @@ -71,6 +84,12 @@ apple_static_library = rule(
cfg = transition_support.apple_platform_split_transition,
),
{
"_cc_toolchain_forwarder": attr.label(
cfg = transition_support.apple_platform_split_transition,
providers = [cc_common.CcToolchainInfo, ApplePlatformInfo],
default =
"@build_bazel_rules_apple//apple:default_cc_toolchain_forwarder",
),
"additional_linker_inputs": attr.label_list(
# Flag required for compile_one_dependency
flags = ["DIRECT_COMPILE_TIME_INPUT"],
Expand Down Expand Up @@ -173,5 +192,5 @@ not in the top-level bundle.
"lipo_archive": "%{name}_lipo.a",
},
fragments = ["objc", "apple", "cpp"],
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
toolchains = use_cpp_toolchain(),
)
144 changes: 144 additions & 0 deletions apple/cc_toolchain_forwarder.bzl
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(),
)
15 changes: 15 additions & 0 deletions apple/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ provide debug info.
},
)

ApplePlatformInfo = provider(
doc = "Provides information for the currently selected Apple platforms.",
fields = {
"target_os": """
`String` representing the selected Apple OS.
""",
"target_arch": """
`String` representing the selected target architecture or cpu type.
""",
"target_environment": """
`String` representing the selected target environment (e.g. "device", "simulator").
""",
},
)

def merge_apple_framework_import_info(apple_framework_import_infos):
"""
Merges multiple `AppleFrameworkImportInfo` into one.
Expand Down
20 changes: 20 additions & 0 deletions doc/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ Propagated by framework and XCFramework import rules: `apple_dynamic_framework_i
| <a id="AppleFrameworkImportInfo-debug_info_binaries"></a>debug_info_binaries | Depset of Files that represent framework binaries and dSYM binaries that provide debug info. |


<a id="ApplePlatformInfo"></a>

## ApplePlatformInfo

<pre>
ApplePlatformInfo(<a href="#ApplePlatformInfo-target_os">target_os</a>, <a href="#ApplePlatformInfo-target_arch">target_arch</a>, <a href="#ApplePlatformInfo-target_environment">target_environment</a>)
</pre>

Provides information for the currently selected Apple platforms.

**FIELDS**


| Name | Description |
| :------------- | :------------- |
| <a id="ApplePlatformInfo-target_os"></a>target_os | <code>String</code> representing the selected Apple OS. |
| <a id="ApplePlatformInfo-target_arch"></a>target_arch | <code>String</code> representing the selected target architecture or cpu type. |
| <a id="ApplePlatformInfo-target_environment"></a>target_environment | <code>String</code> representing the selected target environment (e.g. "device", "simulator"). |


<a id="AppleProvisioningProfileInfo"></a>

## AppleProvisioningProfileInfo
Expand Down
Loading

0 comments on commit a12d1df

Please sign in to comment.