diff --git a/src/main/java/com/google/devtools/build/lib/packages/BuiltinRestriction.java b/src/main/java/com/google/devtools/build/lib/packages/BuiltinRestriction.java index f7d3d542b3b69c..4a150bd5f9839d 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/BuiltinRestriction.java +++ b/src/main/java/com/google/devtools/build/lib/packages/BuiltinRestriction.java @@ -49,6 +49,10 @@ public final class BuiltinRestriction { BuiltinRestriction.allowlistEntry("rules_android", ""), BuiltinRestriction.allowlistEntry("build_bazel_rules_android", ""), + // Apple rules + BuiltinRestriction.allowlistEntry("", "third_party/bazel_rules/rules_apple"), + BuiltinRestriction.allowlistEntry("rules_apple", ""), + // Cc rules BuiltinRestriction.allowlistEntry("", "third_party/bazel_rules/rules_cc"), BuiltinRestriction.allowlistEntry("rules_cc", ""), diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/objc/AppleCommonApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/objc/AppleCommonApi.java index f8b20760506916..b245254cbd0346 100644 --- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/objc/AppleCommonApi.java +++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/objc/AppleCommonApi.java @@ -16,9 +16,6 @@ import com.google.common.collect.ImmutableMap; import com.google.devtools.build.docgen.annot.DocCategory; -import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; -import com.google.devtools.build.lib.collect.nestedset.Depset; -import com.google.devtools.build.lib.starlarkbuildapi.FileApi; import com.google.devtools.build.lib.starlarkbuildapi.StarlarkRuleContextApi; import com.google.devtools.build.lib.starlarkbuildapi.apple.DottedVersionApi; import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi; @@ -26,15 +23,9 @@ import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcInfoApi; import com.google.devtools.build.lib.starlarkbuildapi.platform.ConstraintValueInfoApi; import net.starlark.java.annot.Param; -import net.starlark.java.annot.ParamType; import net.starlark.java.annot.StarlarkBuiltin; import net.starlark.java.annot.StarlarkMethod; -import net.starlark.java.eval.Dict; import net.starlark.java.eval.EvalException; -import net.starlark.java.eval.NoneType; -import net.starlark.java.eval.Sequence; -import net.starlark.java.eval.StarlarkInt; -import net.starlark.java.eval.StarlarkThread; import net.starlark.java.eval.StarlarkValue; /** Interface for a module with useful functions for creating apple-related rule implementations. */ @@ -121,50 +112,6 @@ default ProviderApi getXcodeVersionConfigConstructor() { throw new UnsupportedOperationException(); // just for docs } - @StarlarkMethod( - name = "AppleDynamicFramework", - doc = - "The constructor/key for the AppleDynamicFramework provider.

" - + "If a target propagates the AppleDynamicFramework provider, use this " - + "as the key with which to retrieve it. Example:
" - + "

\n"
-              + "dep = ctx.attr.deps[0]\n"
-              + "p = dep[apple_common.AppleDynamicFramework]\n"
-              + "
", - structField = true) - default ProviderApi getAppleDynamicFrameworkConstructor() { - throw new UnsupportedOperationException(); // just for docs - } - - @StarlarkMethod( - name = "AppleExecutableBinary", - doc = - "The constructor/key for the AppleExecutableBinary provider.

" - + "If a target propagates the AppleExecutableBinary provider," - + " use this as the key with which to retrieve it. Example:
" - + "

\n"
-              + "dep = ctx.attr.deps[0]\n"
-              + "p = dep[apple_common.AppleExecutableBinary]\n"
-              + "
", - structField = true) - default ProviderApi getAppleExecutableBinaryConstructor() { - throw new UnsupportedOperationException(); // just for docs - } - - @StarlarkMethod( - name = "AppleDebugOutputs", - doc = - "The constructor/key for the AppleDebugOutputs provider.

If a target" - + " propagates the AppleDebugOutputs provider, use this as the key with" - + " which to retrieve it. Example:

\n"
-              + "dep = ctx.attr.deps[0]\n"
-              + "p = dep[apple_common.AppleDebugOutputs]\n"
-              + "
", - structField = true) - default void getAppleDebugOutputsConstructor() { - throw new UnsupportedOperationException(); - } - @StarlarkMethod( name = "apple_host_system_env", doc = @@ -203,200 +150,6 @@ default ImmutableMap getTargetAppleEnvironment( throw new UnsupportedOperationException(); // just for docs } - @StarlarkMethod( - name = "new_dynamic_framework_provider", - doc = "Creates a new AppleDynamicFramework provider instance.", - parameters = { - @Param( - name = "binary", - allowedTypes = { - @ParamType(type = FileApi.class), - @ParamType(type = NoneType.class), - }, - named = true, - positional = false, - defaultValue = "None", - doc = "The dylib binary artifact of the dynamic framework."), - @Param( - name = "cc_info", - named = true, - positional = false, - doc = - "A CcInfo which contains information about the transitive dependencies " - + "linked into the binary."), - @Param( - name = "framework_dirs", - allowedTypes = { - @ParamType(type = Depset.class, generic1 = String.class), - @ParamType(type = NoneType.class), - }, - named = true, - positional = false, - defaultValue = "None", - doc = - "The framework path names used as link inputs in order to link against the dynamic " - + "framework."), - @Param( - name = "framework_files", - allowedTypes = { - @ParamType(type = Depset.class, generic1 = FileApi.class), - @ParamType(type = NoneType.class), - }, - named = true, - positional = false, - defaultValue = "None", - doc = - "The full set of artifacts that should be included as inputs to link against the " - + "dynamic framework") - }, - useStarlarkThread = true) - default Object newDynamicFrameworkProvider( - Object dylibBinary, - CcInfoApiT depsCcInfo, - Object dynamicFrameworkDirs, - Object dynamicFrameworkFiles, - StarlarkThread thread) - throws EvalException { - throw new UnsupportedOperationException(); // just for docs - } - - @StarlarkMethod( - name = "new_executable_binary_provider", - doc = "Creates a new AppleExecutableBinaryInfo provider instance.", - parameters = { - @Param( - name = "binary", - allowedTypes = { - @ParamType(type = FileApi.class), - @ParamType(type = NoneType.class), - }, - named = true, - positional = false, - defaultValue = "None", - doc = "The binary artifact of the executable."), - @Param( - name = "cc_info", - named = true, - positional = false, - doc = - "A CcInfo which contains information about the transitive dependencies " - + "linked into the binary."), - }, - useStarlarkThread = true) - default Object newExecutableBinaryProvider( - Object executableBinary, CcInfoApiT depsCcInfo, StarlarkThread thread) throws EvalException { - throw new UnsupportedOperationException(); // just for docs - } - - @StarlarkMethod( - name = "link_multi_arch_binary", - doc = - "Links a (potentially multi-architecture) binary targeting Apple platforms. This " - + "method comprises a bulk of the logic of the Starlark apple_binary " - + "rule in the rules_apple domain and exists to aid in the migration of its " - + "linking logic to Starlark in rules_apple.\n" - + "

This API is highly experimental and subject to change at any time. Do " - + "not depend on the stability of this function at this time.", - parameters = { - @Param(name = "ctx", named = true, positional = false, doc = "The Starlark rule context."), - @Param( - name = "avoid_deps", - allowedTypes = { - @ParamType(type = Sequence.class, generic1 = TransitiveInfoCollection.class), - @ParamType(type = NoneType.class), - }, - named = true, - positional = false, - defaultValue = "None", - doc = - "A list of Targets that are in the dependency graph of the binary but" - + " whose libraries should not be linked into the binary. This is the case for" - + " dependencies that will be found at runtime in another image, such as the" - + " bundle loader or any dynamic libraries/frameworks that will be loaded by" - + " this binary."), - @Param( - name = "extra_linkopts", - allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)}, - named = true, - positional = false, - defaultValue = "[]", - doc = "Extra linkopts to be passed to the linker action."), - @Param( - name = "extra_link_inputs", - allowedTypes = {@ParamType(type = Sequence.class, generic1 = FileApi.class)}, - named = true, - positional = false, - defaultValue = "[]", - doc = "Extra files to pass to the linker action."), - @Param( - name = "extra_requested_features", - allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)}, - named = true, - positional = false, - defaultValue = "[]", - doc = "Extra requested features to be passed to the linker action."), - @Param( - name = "extra_disabled_features", - allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)}, - named = true, - positional = false, - defaultValue = "[]", - doc = "Extra disabled features to be passed to the linker action."), - @Param( - name = "stamp", - named = true, - positional = false, - defaultValue = "-1", - doc = - "Whether to include build information in the linked binary. If 1, build " - + "information is always included. If 0, build information is always excluded. " - + "If -1 (the default), then the behavior is determined by the --[no]stamp " - + "flag. This should be set to 0 when generating the executable output for " - + "test rules."), - @Param( - name = "variables_extension", - positional = false, - named = true, - documented = false, - allowedTypes = { - @ParamType(type = Dict.class), - @ParamType(type = NoneType.class), - }, - defaultValue = "None"), - }, - useStarlarkThread = true) - // TODO(b/70937317): Iterate on, improve, and solidify this API. - default StructApi linkMultiArchBinary( - StarlarkRuleContextT starlarkRuleContext, - Object avoidDeps, // Sequence expected. - Sequence extraLinkopts, // expected. - Sequence extraLinkInputs, // expected. - Sequence extraRequestedFeatures, // expected. - Sequence extraDisabledFeatures, // expected. - StarlarkInt stamp, - Object variablesExtension, - StarlarkThread thread) { - throw new UnsupportedOperationException(); // just for docs - } - - @StarlarkMethod( - name = "link_multi_arch_static_library", - doc = - "Links a (potentially multi-architecture) static library targeting Apple platforms." - + " This method comprises a part of the Starlark apple_static_library" - + " rule logic, in the rules_apple domain and exists to aid in the migration of its" - + " linking logic to Starlark in rules_apple.\n" - + "

This API is highly experimental and subject to change at any time." - + " Do not depend on the stability of this function at this time.", - parameters = { - @Param(name = "ctx", named = true, positional = false, doc = "The Starlark rule context."), - }, - useStarlarkThread = true) - default StructApi linkMultiArchStaticLibrary( - StarlarkRuleContextT starlarkRuleContext, StarlarkThread thread) { - throw new UnsupportedOperationException(); // just for docs - } - @StarlarkMethod( name = "dotted_version", doc = "Creates a new DottedVersion instance.", diff --git a/src/main/starlark/builtins_bzl/common/objc/apple_common.bzl b/src/main/starlark/builtins_bzl/common/objc/apple_common.bzl index ab5012416a7940..c3f3fc541ebf8b 100644 --- a/src/main/starlark/builtins_bzl/common/objc/apple_common.bzl +++ b/src/main/starlark/builtins_bzl/common/objc/apple_common.bzl @@ -15,13 +15,14 @@ """Legacy apple_common module""" load(":common/objc/apple_env.bzl", "apple_host_system_env", "target_apple_env") -load(":common/objc/apple_platform.bzl", "PLATFORM", "PLATFORM_TYPE") +load(":common/objc/apple_platform.bzl", "PLATFORM", "PLATFORM_TYPE", "apple_platform") load(":common/objc/apple_toolchain.bzl", "apple_toolchain") -load(":common/objc/linking_support.bzl", "AppleDebugOutputsInfo", "AppleDynamicFrameworkInfo", "AppleExecutableBinaryInfo", "linking_support") +load(":common/objc/compilation_support.bzl", "compilation_support") load(":common/objc/objc_info.bzl", "ObjcInfo") load(":common/xcode/providers.bzl", "XcodeVersionInfo", "XcodeVersionPropertiesInfo") native_apple_common = _builtins.internal.apple_common +native_objc_internal = _builtins.internal.objc_internal apple_common = struct( apple_toolchain = lambda: apple_toolchain, @@ -30,15 +31,14 @@ apple_common = struct( XcodeProperties = XcodeVersionPropertiesInfo, XcodeVersionConfig = XcodeVersionInfo, Objc = ObjcInfo, - AppleDynamicFramework = AppleDynamicFrameworkInfo, - AppleExecutableBinary = AppleExecutableBinaryInfo, - AppleDebugOutputs = AppleDebugOutputsInfo, apple_host_system_env = apple_host_system_env, target_apple_env = target_apple_env, new_objc_provider = ObjcInfo, - new_dynamic_framework_provider = lambda **kwargs: AppleDynamicFrameworkInfo(**kwargs), - new_executable_binary_provider = lambda **kwargs: AppleExecutableBinaryInfo(**kwargs), - link_multi_arch_binary = linking_support.link_multi_arch_binary, - link_multi_arch_static_library = linking_support.link_multi_arch_static_library, dotted_version = lambda version: native_apple_common.dotted_version(version), + apple_platform = apple_platform, + compilation_support = compilation_support, + get_cpu = lambda config: native_objc_internal.get_cpu(config), + get_apple_config = lambda config: native_objc_internal.get_apple_config(config), + get_split_build_configs = lambda ctx: native_objc_internal.get_split_build_configs(ctx), + get_split_prerequisites = lambda ctx: native_objc_internal.get_split_prerequisites(ctx), ) diff --git a/src/main/starlark/builtins_bzl/common/objc/linking_support.bzl b/src/main/starlark/builtins_bzl/common/objc/linking_support.bzl deleted file mode 100644 index 996aea88e96821..00000000000000 --- a/src/main/starlark/builtins_bzl/common/objc/linking_support.bzl +++ /dev/null @@ -1,320 +0,0 @@ -# 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. - -"""apple_common.link_multi_arch_static_library Starlark implementation""" - -load(":common/cc/cc_common.bzl", "cc_common") -load(":common/cc/cc_info.bzl", "CcInfo") -load(":common/objc/compilation_support.bzl", "compilation_support") -load(":common/objc/multi_arch_binary_support.bzl", "get_split_target_triplet", "subtract_linking_contexts") -load(":common/objc/objc_info.bzl", "ObjcInfo") - -objc_internal = _builtins.internal.objc_internal - -AppleDynamicFrameworkInfo = provider( - doc = "Contains information about an Apple dynamic framework.", - fields = { - "framework_dirs": """\ -The framework path names used as link inputs in order to link against the -dynamic framework. -""", - "framework_files": """\ -The full set of artifacts that should be included as inputs to link against the -dynamic framework. -""", - "binary": "The dylib binary artifact of the dynamic framework.", - "cc_info": """\ -A `CcInfo` which contains information about the transitive dependencies linked -into the binary. -""", - }, -) - -AppleExecutableBinaryInfo = provider( - doc = """ -Contains the executable binary output that was built using -`link_multi_arch_binary` with the `executable` binary type. -""", - fields = { - "binary": """\ -The executable binary artifact output by `link_multi_arch_binary`. -""", - "cc_info": """\ -A `CcInfo` which contains information about the transitive dependencies linked -into the binary. -""", - }, -) - -AppleDebugOutputsInfo = provider( - """ -Holds debug outputs of an Apple binary rule. - -The only field is `output_map`, which is a dictionary of: - `{ arch: { "dsym_binary": File, "linkmap": File }` - -Where `arch` is any Apple architecture such as "arm64" or "armv7". -""", - fields = ["outputs_map"], -) - -def _link_multi_arch_static_library(ctx): - """Links a (potentially multi-architecture) static library targeting Apple platforms. - - Rule context is a required parameter due to usage of the cc_common.configure_features API. - - Args: - ctx: The Starlark rule context. - - Returns: - A Starlark struct containing the following attributes: - - output_groups: OutputGroupInfo provider from transitive CcInfo validation_artifacts. - - outputs: List of structs containing the following attributes: - - library: Artifact representing a linked static library. - - architecture: Linked static library architecture (e.g. 'arm64', 'x86_64'). - - platform: Linked static library target Apple platform (e.g. 'ios', 'macos'). - - environment: Linked static library environment (e.g. 'device', 'simulator'). - """ - - split_target_triplets = get_split_target_triplet(ctx) - - split_deps = ctx.split_attr.deps - split_avoid_deps = ctx.split_attr.avoid_deps - child_configs_and_toolchains = ctx.split_attr._child_configuration_dummy - - outputs = [] - - for split_transition_key, child_toolchain in child_configs_and_toolchains.items(): - cc_toolchain = child_toolchain[cc_common.CcToolchainInfo] - common_variables = compilation_support.build_common_variables( - ctx = ctx, - toolchain = cc_toolchain, - use_pch = True, - deps = split_deps[split_transition_key], - ) - - avoid_objc_providers = [] - avoid_cc_providers = [] - avoid_cc_linking_contexts = [] - - if len(split_avoid_deps.keys()): - for dep in split_avoid_deps[split_transition_key]: - if ObjcInfo in dep: - avoid_objc_providers.append(dep[ObjcInfo]) - if CcInfo in dep: - avoid_cc_providers.append(dep[CcInfo]) - avoid_cc_linking_contexts.append(dep[CcInfo].linking_context) - - name = ctx.label.name + "-" + cc_toolchain.target_gnu_system_name + "-fl" - - cc_linking_context = subtract_linking_contexts( - owner = ctx.label, - linking_contexts = common_variables.objc_linking_context.cc_linking_contexts, - avoid_dep_linking_contexts = avoid_cc_linking_contexts, - ) - linking_outputs = compilation_support.register_fully_link_action( - name = name, - common_variables = common_variables, - cc_linking_context = cc_linking_context, - ) - - output = { - "library": linking_outputs.library_to_link.static_library, - } - - if split_target_triplets != None: - target_triplet = split_target_triplets.get(split_transition_key) - output["platform"] = target_triplet.platform - output["architecture"] = target_triplet.architecture - output["environment"] = target_triplet.environment - - outputs.append(struct(**output)) - - header_tokens = [] - for _, deps in split_deps.items(): - for dep in deps: - if CcInfo in dep: - header_tokens.append(dep[CcInfo].compilation_context.validation_artifacts) - - output_groups = {"_validation": depset(transitive = header_tokens)} - - return struct( - outputs = outputs, - output_groups = OutputGroupInfo(**output_groups), - ) - -def _link_multi_arch_binary( - *, - ctx, - avoid_deps = [], - extra_linkopts = [], - extra_link_inputs = [], - extra_requested_features = [], - extra_disabled_features = [], - stamp = -1, - variables_extension = {}): - """Links a (potentially multi-architecture) binary targeting Apple platforms. - - This method comprises a bulk of the logic of the Starlark apple_binary - rule in the rules_apple domain and exists to aid in the migration of its - linking logic to Starlark in rules_apple. - - This API is **highly experimental** and subject to change at any time. Do - not depend on the stability of this function at this time. - """ - - split_target_triplets = get_split_target_triplet(ctx) - split_build_configs = objc_internal.get_split_build_configs(ctx) - split_deps = ctx.split_attr.deps - child_configs_and_toolchains = ctx.split_attr._child_configuration_dummy - - if split_deps and split_deps.keys() != child_configs_and_toolchains.keys(): - fail(("Split transition keys are different between 'deps' [%s] and " + - "'_child_configuration_dummy' [%s]") % ( - split_deps.keys(), - child_configs_and_toolchains.keys(), - )) - - avoid_cc_infos = [ - dep[AppleDynamicFrameworkInfo].cc_info - for dep in avoid_deps - if AppleDynamicFrameworkInfo in dep - ] - avoid_cc_infos.extend([ - dep[AppleExecutableBinaryInfo].cc_info - for dep in avoid_deps - if AppleExecutableBinaryInfo in dep - ]) - avoid_cc_infos.extend([dep[CcInfo] for dep in avoid_deps if CcInfo in dep]) - avoid_cc_linking_contexts = [dep.linking_context for dep in avoid_cc_infos] - - outputs = [] - cc_infos = [] - legacy_debug_outputs = {} - - cc_infos.extend(avoid_cc_infos) - - # $(location...) is only used in one test, and tokenize only affects linkopts in one target - additional_linker_inputs = getattr(ctx.attr, "additional_linker_inputs", []) - attr_linkopts = [ - ctx.expand_location(opt, targets = additional_linker_inputs) - for opt in getattr(ctx.attr, "linkopts", []) - ] - attr_linkopts = [token for opt in attr_linkopts for token in ctx.tokenize(opt)] - - for split_transition_key, child_toolchain in child_configs_and_toolchains.items(): - cc_toolchain = child_toolchain[cc_common.CcToolchainInfo] - deps = split_deps.get(split_transition_key, []) - target_triplet = split_target_triplets.get(split_transition_key) - - common_variables = compilation_support.build_common_variables( - ctx = ctx, - toolchain = cc_toolchain, - deps = deps, - extra_disabled_features = extra_disabled_features, - extra_enabled_features = extra_requested_features, - attr_linkopts = attr_linkopts, - ) - - cc_infos.append(CcInfo( - compilation_context = cc_common.merge_compilation_contexts( - compilation_contexts = - common_variables.objc_compilation_context.cc_compilation_contexts, - ), - linking_context = cc_common.merge_linking_contexts( - linking_contexts = common_variables.objc_linking_context.cc_linking_contexts, - ), - )) - - cc_linking_context = subtract_linking_contexts( - owner = ctx.label, - linking_contexts = common_variables.objc_linking_context.cc_linking_contexts + - avoid_cc_linking_contexts, - avoid_dep_linking_contexts = avoid_cc_linking_contexts, - ) - - child_config = split_build_configs.get(split_transition_key) - - additional_outputs = [] - extensions = {} - - dsym_binary = None - if ctx.fragments.cpp.apple_generate_dsym: - if ctx.fragments.cpp.objc_should_strip_binary: - suffix = "_bin_unstripped.dwarf" - else: - suffix = "_bin.dwarf" - dsym_binary = ctx.actions.declare_shareable_artifact( - ctx.label.package + "/" + ctx.label.name + suffix, - child_config.bin_dir, - ) - extensions["dsym_path"] = dsym_binary.path # dsym symbol file - additional_outputs.append(dsym_binary) - legacy_debug_outputs.setdefault(target_triplet.architecture, {})["dsym_binary"] = dsym_binary - - linkmap = None - if ctx.fragments.cpp.objc_generate_linkmap: - linkmap = ctx.actions.declare_shareable_artifact( - ctx.label.package + "/" + ctx.label.name + ".linkmap", - child_config.bin_dir, - ) - extensions["linkmap_exec_path"] = linkmap.path # linkmap file - additional_outputs.append(linkmap) - legacy_debug_outputs.setdefault(target_triplet.architecture, {})["linkmap"] = linkmap - - name = ctx.label.name + "_bin" - executable = compilation_support.register_configuration_specific_link_actions( - name = name, - common_variables = common_variables, - cc_linking_context = cc_linking_context, - build_config = child_config, - extra_link_args = extra_linkopts, - stamp = stamp, - user_variable_extensions = variables_extension | extensions, - additional_outputs = additional_outputs, - deps = deps, - extra_link_inputs = extra_link_inputs, - attr_linkopts = attr_linkopts, - ) - - output = { - "binary": executable, - "platform": target_triplet.platform, - "architecture": target_triplet.architecture, - "environment": target_triplet.environment, - "dsym_binary": dsym_binary, - "linkmap": linkmap, - } - - outputs.append(struct(**output)) - - header_tokens = [] - for _, deps in split_deps.items(): - for dep in deps: - if CcInfo in dep: - header_tokens.append(dep[CcInfo].compilation_context.validation_artifacts) - - output_groups = {"_validation": depset(transitive = header_tokens)} - - return struct( - cc_info = cc_common.merge_cc_infos(direct_cc_infos = cc_infos), - output_groups = output_groups, - outputs = outputs, - debug_outputs_provider = AppleDebugOutputsInfo(outputs_map = legacy_debug_outputs), - ) - -linking_support = struct( - link_multi_arch_static_library = _link_multi_arch_static_library, - link_multi_arch_binary = _link_multi_arch_binary, -) diff --git a/src/main/starlark/builtins_bzl/common/objc/multi_arch_binary_support.bzl b/src/main/starlark/builtins_bzl/common/objc/multi_arch_binary_support.bzl deleted file mode 100644 index d55743d2031dcf..00000000000000 --- a/src/main/starlark/builtins_bzl/common/objc/multi_arch_binary_support.bzl +++ /dev/null @@ -1,105 +0,0 @@ -# Copyright 2024 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License(**kwargs): 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(**kwargs): software -# distributed under the License is distributed on an "AS IS" BASIS(**kwargs): -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND(**kwargs): either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Support utility for creating multi-arch Apple binaries.""" - -load(":common/cc/cc_common.bzl", "cc_common") -load(":common/objc/apple_platform.bzl", "apple_platform") -load(":common/objc/compilation_support.bzl", "compilation_support") - -objc_internal = _builtins.internal.objc_internal -TargetTripletInfo = provider( - "Contains the the target triplet (architecture, platform, environment) for a given configuration.", - fields = { - "architecture": "string, the CPU as returned by AppleConfiguration.getSingleArchitecture()", - "platform": "apple_platform.PLATFORM_TPYE string as returned by apple_platform.get_target_platform()", - "environment": "string ('device', 'simulator' or 'macabi) as returned by apple_platform.get_target_environment", - }, -) - -def _build_avoid_library_set(avoid_dep_linking_contexts): - avoid_library_set = dict() - for linking_context in avoid_dep_linking_contexts: - for linker_input in linking_context.linker_inputs.to_list(): - for library_to_link in linker_input.libraries: - library_artifact = compilation_support.get_static_library_for_linking(library_to_link) - if library_artifact: - avoid_library_set[library_artifact.short_path] = True - return avoid_library_set - -def subtract_linking_contexts(owner, linking_contexts, avoid_dep_linking_contexts): - """Subtracts the libraries in avoid_dep_linking_contexts from linking_contexts. - - Args: - owner: The label of the target currently being analyzed. - linking_contexts: An iterable of CcLinkingContext objects. - avoid_dep_linking_contexts: An iterable of CcLinkingContext objects. - - Returns: - A CcLinkingContext object. - """ - libraries = [] - user_link_flags = [] - additional_inputs = [] - linkstamps = [] - avoid_library_set = _build_avoid_library_set(avoid_dep_linking_contexts) - for linking_context in linking_contexts: - for linker_input in linking_context.linker_inputs.to_list(): - for library_to_link in linker_input.libraries: - library_artifact = compilation_support.get_library_for_linking(library_to_link) - if library_artifact.short_path not in avoid_library_set: - libraries.append(library_to_link) - user_link_flags.extend(linker_input.user_link_flags) - additional_inputs.extend(linker_input.additional_inputs) - linkstamps.extend(linker_input.linkstamps) - linker_input = cc_common.create_linker_input( - owner = owner, - libraries = depset(libraries, order = "topological"), - user_link_flags = user_link_flags, - additional_inputs = depset(additional_inputs), - linkstamps = depset(linkstamps), - ) - return cc_common.create_linking_context( - linker_inputs = depset([linker_input]), - owner = owner, - ) - -def _get_target_triplet(config): - """Returns the target triplet (architecture, platform, environment) for a given configuration.""" - cpu_platform = apple_platform.for_target_cpu(objc_internal.get_cpu(config)) - apple_config = objc_internal.get_apple_config(config) - - return TargetTripletInfo( - architecture = apple_config.single_arch_cpu, - platform = apple_platform.get_target_platform(cpu_platform), - environment = apple_platform.get_target_environment(cpu_platform), - ) - -def get_split_target_triplet(ctx): - """Transforms a rule context's ctads to a Starlark Dict mapping transitions to target triplets. - - Args: - ctx: The Starlark rule context. - - Returns: - A Starlark Dict keyed by split transition keys with - their target triplet (architecture, platform, environment) as value. - """ - result = dict() - ctads = objc_internal.get_split_prerequisites(ctx) - for split_transition_key, config in ctads.items(): - if split_transition_key == None: - fail("unexpected empty key in split transition") - result[split_transition_key] = _get_target_triplet(config) - return result diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java index 0a3e0477fce1ff..66adb265b5371d 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java @@ -17,6 +17,7 @@ import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.truth.Truth.assertThat; import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.getFirstArtifactEndingWith; +import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuild; import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuiltins; import com.google.common.base.Joiner; @@ -76,8 +77,7 @@ public abstract class ObjcRuleTestCase extends BuildViewTestCase { private static final Provider.Key APPLE_EXECUTABLE_BINARY_PROVIDER_KEY = new StarlarkProvider.Key( - keyForBuiltins( - Label.parseCanonicalUnchecked("@_builtins//:common/objc/linking_support.bzl")), + keyForBuild(Label.parseCanonicalUnchecked("//test_starlark:apple_binary_starlark.bzl")), "AppleExecutableBinaryInfo"); @Before @@ -304,6 +304,15 @@ protected static void addAppleBinaryStarlarkRule(Scratch scratch) throws Excepti " '//command_line_option:grte_top',", " '//command_line_option:platforms',", "]", + "AppleDynamicFrameworkInfo = provider(", + " fields = ['framework_dirs', 'framework_files', 'binary', 'cc_info'],)", + "", + "AppleExecutableBinaryInfo = provider(", + " fields = ['binary', 'cc_info'],)", + "", + "AppleDebugOutputsInfo = provider(", + " fields = ['outputs_map'],)", + "", "def _command_line_options(*, environment_arch = None, platform_type, settings):", " cpu = ('darwin_' + environment_arch if platform_type == 'macos'", " else platform_type + '_' + environment_arch)", @@ -346,6 +355,223 @@ protected static void addAppleBinaryStarlarkRule(Scratch scratch) throws Excepti " inputs = _apple_platform_transition_inputs,", " outputs = _apple_rule_base_transition_outputs,", ")", + "TargetTripletInfo = provider(fields = ['architecture', 'platform', 'environment'],)", + "def _build_avoid_library_set(avoid_dep_linking_contexts):", + " avoid_library_set = dict()", + " for linking_context in avoid_dep_linking_contexts:", + " for linker_input in linking_context.linker_inputs.to_list():", + " for library_to_link in linker_input.libraries:", + " library_artifact =" + + " apple_common.compilation_support.get_static_library_for_linking(library_to_link)", + " if library_artifact:", + " avoid_library_set[library_artifact.short_path] = True", + " return avoid_library_set", + "", + "def subtract_linking_contexts(owner, linking_contexts, avoid_dep_linking_contexts):", + " libraries = []", + " user_link_flags = []", + " additional_inputs = []", + " linkstamps = []", + " avoid_library_set = _build_avoid_library_set(avoid_dep_linking_contexts)", + " for linking_context in linking_contexts:", + " for linker_input in linking_context.linker_inputs.to_list():", + " for library_to_link in linker_input.libraries:", + " library_artifact =" + + " apple_common.compilation_support.get_library_for_linking(library_to_link)", + " if library_artifact.short_path not in avoid_library_set:", + " libraries.append(library_to_link)", + " user_link_flags.extend(linker_input.user_link_flags)", + " additional_inputs.extend(linker_input.additional_inputs)", + " linkstamps.extend(linker_input.linkstamps)", + " linker_input = cc_common.create_linker_input(", + " owner = owner,", + " libraries = depset(libraries, order = 'topological'),", + " user_link_flags = user_link_flags,", + " additional_inputs = depset(additional_inputs),", + " linkstamps = depset(linkstamps),", + " )", + " return cc_common.create_linking_context(", + " linker_inputs = depset([linker_input]),", + " owner = owner,", + " )", + "", + "def _get_target_triplet(config):", + " cpu_platform =" + + " apple_common.apple_platform.for_target_cpu(apple_common.get_cpu(config))", + " apple_config = apple_common.get_apple_config(config)", + "", + " return TargetTripletInfo(", + " architecture = apple_config.single_arch_cpu,", + " platform = apple_common.apple_platform.get_target_platform(cpu_platform),", + " environment = apple_common.apple_platform.get_target_environment(cpu_platform),", + " )", + "", + "def get_split_target_triplet(ctx):", + " result = dict()", + " ctads = apple_common.get_split_prerequisites(ctx)", + " for split_transition_key, config in ctads.items():", + " if split_transition_key == None:", + " fail('unexpected empty key in split transition')", + " result[split_transition_key] = _get_target_triplet(config)", + " return result", + "", + "def _link_multi_arch_binary(", + " *,", + " ctx,", + " avoid_deps = [],", + " extra_linkopts = [],", + " extra_link_inputs = [],", + " extra_requested_features = [],", + " extra_disabled_features = [],", + " stamp = -1,", + " variables_extension = {}):", + "", + " split_target_triplets = get_split_target_triplet(ctx)", + " split_build_configs = apple_common.get_split_build_configs(ctx)", + " split_deps = ctx.split_attr.deps", + " child_configs_and_toolchains = ctx.split_attr._child_configuration_dummy", + "", + " if split_deps and split_deps.keys() != child_configs_and_toolchains.keys():", + " fail(('Split transition keys are different between deps [%s] and ' +", + " '_child_configuration_dummy [%s]') % (", + " split_deps.keys(),", + " child_configs_and_toolchains.keys(),", + " ))", + "", + " avoid_cc_infos = [", + " dep[AppleDynamicFrameworkInfo].cc_info", + " for dep in avoid_deps", + " if AppleDynamicFrameworkInfo in dep", + " ]", + " avoid_cc_infos.extend([", + " dep[AppleExecutableBinaryInfo].cc_info", + " for dep in avoid_deps", + " if AppleExecutableBinaryInfo in dep", + " ])", + " avoid_cc_infos.extend([dep[CcInfo] for dep in avoid_deps if CcInfo in dep])", + " avoid_cc_linking_contexts = [dep.linking_context for dep in avoid_cc_infos]", + "", + " outputs = []", + " cc_infos = []", + " legacy_debug_outputs = {}", + "", + " cc_infos.extend(avoid_cc_infos)", + "", + " additional_linker_inputs = getattr(ctx.attr, 'additional_linker_inputs', [])", + " attr_linkopts = [", + " ctx.expand_location(opt, targets = additional_linker_inputs)", + " for opt in getattr(ctx.attr, 'linkopts', [])", + " ]", + " attr_linkopts = [token for opt in attr_linkopts for token in ctx.tokenize(opt)]", + "", + " for split_transition_key, child_toolchain in child_configs_and_toolchains.items():", + " cc_toolchain = child_toolchain[cc_common.CcToolchainInfo]", + " deps = split_deps.get(split_transition_key, [])", + " target_triplet = split_target_triplets.get(split_transition_key)", + "", + " common_variables = apple_common.compilation_support.build_common_variables(", + " ctx = ctx,", + " toolchain = cc_toolchain,", + " deps = deps,", + " extra_disabled_features = extra_disabled_features,", + " extra_enabled_features = extra_requested_features,", + " attr_linkopts = attr_linkopts,", + " )", + "", + " cc_infos.append(CcInfo(", + " compilation_context = cc_common.merge_compilation_contexts(", + " compilation_contexts =", + " common_variables.objc_compilation_context.cc_compilation_contexts,", + " ),", + " linking_context = cc_common.merge_linking_contexts(", + " linking_contexts =" + + " common_variables.objc_linking_context.cc_linking_contexts,", + " ),", + " ))", + "", + " cc_linking_context = subtract_linking_contexts(", + " owner = ctx.label,", + " linking_contexts = common_variables.objc_linking_context.cc_linking_contexts" + + " +", + " avoid_cc_linking_contexts,", + " avoid_dep_linking_contexts = avoid_cc_linking_contexts,", + " )", + "", + " child_config = split_build_configs.get(split_transition_key)", + "", + " additional_outputs = []", + " extensions = {}", + "", + " dsym_binary = None", + " if ctx.fragments.cpp.apple_generate_dsym:", + " if ctx.fragments.cpp.objc_should_strip_binary:", + " suffix = '_bin_unstripped.dwarf'", + " else:", + " suffix = '_bin.dwarf'", + " dsym_binary = ctx.actions.declare_shareable_artifact(", + " ctx.label.package + '/' + ctx.label.name + suffix,", + " child_config.bin_dir,", + " )", + " extensions['dsym_path'] = dsym_binary.path # dsym symbol file", + " additional_outputs.append(dsym_binary)", + " legacy_debug_outputs.setdefault(target_triplet.architecture," + + " {})['dsym_binary'] = dsym_binary", + "", + " linkmap = None", + " if ctx.fragments.cpp.objc_generate_linkmap:", + " linkmap = ctx.actions.declare_shareable_artifact(", + " ctx.label.package + '/' + ctx.label.name + '.linkmap',", + " child_config.bin_dir,", + " )", + " extensions['linkmap_exec_path'] = linkmap.path # linkmap file", + " additional_outputs.append(linkmap)", + " legacy_debug_outputs.setdefault(target_triplet.architecture, {})['linkmap'] =" + + " linkmap", + "", + " name = ctx.label.name + '_bin'", + " executable =" + + " apple_common.compilation_support.register_configuration_specific_link_actions(", + " name = name,", + " common_variables = common_variables,", + " cc_linking_context = cc_linking_context,", + " build_config = child_config,", + " extra_link_args = extra_linkopts,", + " stamp = stamp,", + " user_variable_extensions = variables_extension | extensions,", + " additional_outputs = additional_outputs,", + " deps = deps,", + " extra_link_inputs = extra_link_inputs,", + " attr_linkopts = attr_linkopts,", + " )", + "", + " output = {", + " 'binary': executable,", + " 'platform': target_triplet.platform,", + " 'architecture': target_triplet.architecture,", + " 'environment': target_triplet.environment,", + " 'dsym_binary': dsym_binary,", + " 'linkmap': linkmap,", + " }", + "", + " outputs.append(struct(**output))", + "", + " header_tokens = []", + " for _, deps in split_deps.items():", + " for dep in deps:", + " if CcInfo in dep:", + " " + + " header_tokens.append(dep[CcInfo].compilation_context.validation_artifacts)", + "", + " output_groups = {'_validation': depset(transitive = header_tokens)}", + "", + " return struct(", + " cc_info = cc_common.merge_cc_infos(direct_cc_infos = cc_infos),", + " output_groups = output_groups,", + " outputs = outputs,", + " debug_outputs_provider = AppleDebugOutputsInfo(outputs_map =" + + " legacy_debug_outputs),", + " )", + "", "def apple_binary_starlark_impl(ctx):", " all_avoid_deps = list(ctx.attr.avoid_deps)", " binary_type = ctx.attr.binary_type", @@ -362,11 +588,11 @@ protected static void addAppleBinaryStarlarkRule(Scratch scratch) throws Excepti + " '@loader_path/Frameworks'])", " if ctx.attr.bundle_loader:", " bundle_loader = ctx.attr.bundle_loader", - " bundle_loader_file = bundle_loader[apple_common.AppleExecutableBinary].binary", + " bundle_loader_file = bundle_loader[AppleExecutableBinaryInfo].binary", " all_avoid_deps.append(bundle_loader)", " linkopts.extend(['-bundle_loader', bundle_loader_file.path])", " link_inputs.append(bundle_loader_file)", - " link_result = apple_common.link_multi_arch_binary(", + " link_result = _link_multi_arch_binary(", " ctx = ctx,", " avoid_deps = all_avoid_deps,", " extra_linkopts = linkopts,", @@ -408,7 +634,7 @@ protected static void addAppleBinaryStarlarkRule(Scratch scratch) throws Excepti " link_result.debug_outputs_provider,", " ]", " if binary_type == 'executable':", - " providers.append(apple_common.new_executable_binary_provider(", + " providers.append(AppleExecutableBinaryInfo(", " binary = processed_binary,", " cc_info = link_result.cc_info,", " ))", diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkTest.java b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkTest.java index baec12578d2d95..3bfb5bf555794f 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkTest.java @@ -16,7 +16,6 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuild; -import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuiltins; import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; @@ -46,8 +45,7 @@ public class ObjcStarlarkTest extends ObjcRuleTestCase { private static final Provider.Key APPLE_EXECUTABLE_BINARY_PROVIDER_KEY = new StarlarkProvider.Key( - keyForBuiltins( - Label.parseCanonicalUnchecked("@_builtins//:common/objc/linking_support.bzl")), + keyForBuild(Label.parseCanonicalUnchecked("//test_starlark:apple_binary_starlark.bzl")), "AppleExecutableBinaryInfo"); @Before