|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2022 The Bazel Authors. All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# Common Bash functions to test Apple rules in Bazel. |
| 18 | +# |
| 19 | + |
| 20 | +function make_starlark_apple_binary_rule_in() { |
| 21 | + local dir="$1"; shift |
| 22 | + |
| 23 | + # All of the attributes below, except for `stamp`, are required as part of the |
| 24 | + # implied contract of `apple_common.link_multi_arch_binary` since it asks for |
| 25 | + # attributes directly from the rule context. As these requirements are changed |
| 26 | + # from implied attributes to function arguments, they can be removed. |
| 27 | + cat >> "${dir}/starlark_apple_binary.bzl" <<EOF |
| 28 | +def _starlark_apple_binary_impl(ctx): |
| 29 | + link_result = apple_common.link_multi_arch_binary( |
| 30 | + ctx = ctx, |
| 31 | + stamp = ctx.attr.stamp, |
| 32 | + ) |
| 33 | + processed_binary = ctx.actions.declare_file( |
| 34 | + '{}_lipobin'.format(ctx.label.name) |
| 35 | + ) |
| 36 | + lipo_inputs = [output.binary for output in link_result.outputs] |
| 37 | + if len(lipo_inputs) > 1: |
| 38 | + apple_env = {} |
| 39 | + xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] |
| 40 | + apple_env.update(apple_common.apple_host_system_env(xcode_config)) |
| 41 | + apple_env.update( |
| 42 | + apple_common.target_apple_env( |
| 43 | + xcode_config, |
| 44 | + ctx.fragments.apple.single_arch_platform, |
| 45 | + ), |
| 46 | + ) |
| 47 | + args = ctx.actions.args() |
| 48 | + args.add('-create') |
| 49 | + args.add_all(lipo_inputs) |
| 50 | + args.add('-output', processed_binary) |
| 51 | + ctx.actions.run( |
| 52 | + arguments = [args], |
| 53 | + env = apple_env, |
| 54 | + executable = '/usr/bin/lipo', |
| 55 | + execution_requirements = xcode_config.execution_info(), |
| 56 | + inputs = lipo_inputs, |
| 57 | + outputs = [processed_binary], |
| 58 | + ) |
| 59 | + else: |
| 60 | + ctx.actions.symlink( |
| 61 | + target_file = lipo_inputs[0], |
| 62 | + output = processed_binary, |
| 63 | + ) |
| 64 | + return [ |
| 65 | + DefaultInfo(files = depset([processed_binary])), |
| 66 | + OutputGroupInfo(**link_result.output_groups), |
| 67 | + link_result.debug_outputs_provider, |
| 68 | + ] |
| 69 | +
|
| 70 | +starlark_apple_binary = rule( |
| 71 | + attrs = { |
| 72 | + "_child_configuration_dummy": attr.label( |
| 73 | + cfg = apple_common.multi_arch_split, |
| 74 | + default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), |
| 75 | + ), |
| 76 | + "_xcode_config": attr.label( |
| 77 | + default = configuration_field( |
| 78 | + fragment = "apple", |
| 79 | + name = "xcode_config_label", |
| 80 | + ), |
| 81 | + ), |
| 82 | + "_xcrunwrapper": attr.label( |
| 83 | + cfg = "exec", |
| 84 | + default = Label("@bazel_tools//tools/objc:xcrunwrapper"), |
| 85 | + executable = True, |
| 86 | + ), |
| 87 | + "binary_type": attr.string(default = "executable"), |
| 88 | + "bundle_loader": attr.label(), |
| 89 | + "deps": attr.label_list( |
| 90 | + cfg = apple_common.multi_arch_split, |
| 91 | + ), |
| 92 | + "dylibs": attr.label_list(), |
| 93 | + "linkopts": attr.string_list(), |
| 94 | + "minimum_os_version": attr.string(), |
| 95 | + "platform_type": attr.string(), |
| 96 | + "stamp": attr.int(default = -1, values = [-1, 0, 1]), |
| 97 | + }, |
| 98 | + fragments = ["apple", "objc", "cpp"], |
| 99 | + implementation = _starlark_apple_binary_impl, |
| 100 | + outputs = { |
| 101 | + # Provided for compatibility with apple_binary tests only. |
| 102 | + "lipobin": "%{name}_lipobin", |
| 103 | + }, |
| 104 | +) |
| 105 | +EOF |
| 106 | +} |
| 107 | + |
| 108 | +function make_starlark_apple_static_library_rule_in() { |
| 109 | + local dir="$1"; shift |
| 110 | + |
| 111 | + # All of the attributes below are required as part of the implied contract of |
| 112 | + # `apple_common.link_multi_arch_static_library` since it asks for attributes |
| 113 | + # directly from the rule context. As these requirements are changed from |
| 114 | + # implied attributes to function arguments, they can be removed. |
| 115 | + cat >> "${dir}/starlark_apple_static_library.bzl" <<EOF |
| 116 | +def _starlark_apple_static_library_impl(ctx): |
| 117 | + if not hasattr(apple_common.platform_type, ctx.attr.platform_type): |
| 118 | + fail('Unsupported platform type \"{}\"'.format(ctx.attr.platform_type)) |
| 119 | + link_result = apple_common.link_multi_arch_static_library(ctx = ctx) |
| 120 | + processed_library = ctx.actions.declare_file( |
| 121 | + '{}_lipo.a'.format(ctx.label.name) |
| 122 | + ) |
| 123 | + files_to_build = [processed_library] |
| 124 | + runfiles = ctx.runfiles( |
| 125 | + files = files_to_build, |
| 126 | + collect_default = True, |
| 127 | + collect_data = True, |
| 128 | + ) |
| 129 | + lipo_inputs = [output.library for output in link_result.outputs] |
| 130 | + if len(lipo_inputs) > 1: |
| 131 | + apple_env = {} |
| 132 | + xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] |
| 133 | + apple_env.update(apple_common.apple_host_system_env(xcode_config)) |
| 134 | + apple_env.update( |
| 135 | + apple_common.target_apple_env( |
| 136 | + xcode_config, |
| 137 | + ctx.fragments.apple.single_arch_platform, |
| 138 | + ), |
| 139 | + ) |
| 140 | + args = ctx.actions.args() |
| 141 | + args.add('-create') |
| 142 | + args.add_all(lipo_inputs) |
| 143 | + args.add('-output', processed_library) |
| 144 | + ctx.actions.run( |
| 145 | + arguments = [args], |
| 146 | + env = apple_env, |
| 147 | + executable = '/usr/bin/lipo', |
| 148 | + execution_requirements = xcode_config.execution_info(), |
| 149 | + inputs = lipo_inputs, |
| 150 | + outputs = [processed_library], |
| 151 | + ) |
| 152 | + else: |
| 153 | + ctx.actions.symlink( |
| 154 | + target_file = lipo_inputs[0], |
| 155 | + output = processed_library, |
| 156 | + ) |
| 157 | + providers = [ |
| 158 | + DefaultInfo(files = depset(files_to_build), runfiles = runfiles), |
| 159 | + link_result.objc, |
| 160 | + link_result.output_groups, |
| 161 | + ] |
| 162 | + return providers |
| 163 | +
|
| 164 | +starlark_apple_static_library = rule( |
| 165 | + _starlark_apple_static_library_impl, |
| 166 | + attrs = { |
| 167 | + '_child_configuration_dummy': attr.label( |
| 168 | + cfg = apple_common.multi_arch_split, |
| 169 | + default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), |
| 170 | + ), |
| 171 | + '_xcode_config': attr.label( |
| 172 | + default = configuration_field( |
| 173 | + fragment = "apple", |
| 174 | + name = "xcode_config_label", |
| 175 | + ), |
| 176 | + ), |
| 177 | + '_xcrunwrapper': attr.label( |
| 178 | + executable = True, |
| 179 | + cfg = 'exec', |
| 180 | + default = Label("@bazel_tools//tools/objc:xcrunwrapper"), |
| 181 | + ), |
| 182 | + 'additional_linker_inputs': attr.label_list( |
| 183 | + allow_files = True, |
| 184 | + ), |
| 185 | + 'avoid_deps': attr.label_list( |
| 186 | + cfg = apple_common.multi_arch_split, |
| 187 | + default = [], |
| 188 | + ), |
| 189 | + 'deps': attr.label_list( |
| 190 | + cfg = apple_common.multi_arch_split, |
| 191 | + ), |
| 192 | + 'linkopts': attr.string_list(), |
| 193 | + 'platform_type': attr.string(), |
| 194 | + 'minimum_os_version': attr.string(), |
| 195 | + }, |
| 196 | + outputs = { |
| 197 | + 'lipo_archive': '%{name}_lipo.a', |
| 198 | + }, |
| 199 | + cfg = apple_common.apple_crosstool_transition, |
| 200 | + fragments = ['apple', 'objc', 'cpp',], |
| 201 | +) |
| 202 | +EOF |
| 203 | +} |
0 commit comments