|
| 1 | +# Copyright 2023 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +A rule for providing support for order files during build. |
| 17 | +""" |
| 18 | + |
| 19 | +load("@rules_cc//cc/common:cc_common.bzl", "cc_common") |
| 20 | +load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") |
| 21 | + |
| 22 | +visibility([ |
| 23 | + "//apple/...", |
| 24 | + "//test/...", |
| 25 | +]) |
| 26 | + |
| 27 | +def _concatenate_files(*, actions, files, name): |
| 28 | + """Concatenates multiple files together. |
| 29 | +
|
| 30 | + Args: |
| 31 | + actions: The ctx.actions associated with the rule. |
| 32 | + files: The files that should be concatenated. Order will be preserved. |
| 33 | + name: The ctx.attr.name associated with the rule. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + A declared file that contains the concatenated output. |
| 37 | + """ |
| 38 | + |
| 39 | + out_file = actions.declare_file("%s_concat.order" % name) |
| 40 | + |
| 41 | + actions.run_shell( |
| 42 | + inputs = files, |
| 43 | + outputs = [out_file], |
| 44 | + progress_message = "Concatenating order files into %s" % out_file.short_path, |
| 45 | + arguments = [out_file.path] + [f.path for f in files], |
| 46 | + command = "awk '1' ${@:2} > \"$1\"", |
| 47 | + mnemonic = "OrderFileConcatenation", |
| 48 | + ) |
| 49 | + |
| 50 | + return out_file |
| 51 | + |
| 52 | +def _dedup_file(*, actions, file, name): |
| 53 | + """Removes duplicate lines from a file. |
| 54 | +
|
| 55 | + Args: |
| 56 | + actions: The ctx.actions associated with the rule. |
| 57 | + file: The file that should have its duplicate lines removed. Order will be preserved. |
| 58 | + name: The ctx.attr.name associated with the rule. |
| 59 | +
|
| 60 | + Returns: |
| 61 | + A declared file that contains the deduplicated output. |
| 62 | + """ |
| 63 | + |
| 64 | + out_file = actions.declare_file("%s_dedup.order" % name) |
| 65 | + |
| 66 | + actions.run_shell( |
| 67 | + inputs = [file], |
| 68 | + outputs = [out_file], |
| 69 | + progress_message = "Deduping order files into %s" % out_file.short_path, |
| 70 | + arguments = [file.path, out_file.path], |
| 71 | + command = "awk '!x[$0]++' \"$1\" > \"$2\"", |
| 72 | + mnemonic = "OrderFileDeduplication", |
| 73 | + ) |
| 74 | + |
| 75 | + return out_file |
| 76 | + |
| 77 | +def _link_order_file(*, ctx, order_file, stats): |
| 78 | + """Returns a provider that will inject an order file during linking of the iOS application. |
| 79 | +
|
| 80 | + Args: |
| 81 | + ctx: The context associated with the order_file rule. |
| 82 | + order_file: The final order file to be used during linking. |
| 83 | + stats: A boolean indicating whether to log stats about how the linker used the order file. |
| 84 | + """ |
| 85 | + |
| 86 | + linkopts = [ |
| 87 | + "-Wl,-order_file,%s" % order_file.path, |
| 88 | + ] |
| 89 | + if stats: |
| 90 | + linkopts.append("-Wl,-order_file_statistics") |
| 91 | + |
| 92 | + linker_input = cc_common.create_linker_input( |
| 93 | + owner = ctx.label, |
| 94 | + user_link_flags = depset(linkopts), |
| 95 | + additional_inputs = depset([order_file]), |
| 96 | + ) |
| 97 | + |
| 98 | + return CcInfo( |
| 99 | + linking_context = cc_common.create_linking_context( |
| 100 | + linker_inputs = depset([linker_input]), |
| 101 | + ), |
| 102 | + ) |
| 103 | + |
| 104 | +def _order_file_impl(ctx): |
| 105 | + """Prepares a list of order files for inclusion into an iOS Application. |
| 106 | +
|
| 107 | + Order files optimize the order of symbols in the binary, thus improving performance of the |
| 108 | + application. This method will concatenate multiple order files together, remove duplicate lines |
| 109 | + and prepare the linker commands necessary to apply the order files to the binary. |
| 110 | +
|
| 111 | + Full details on the contents of order files are available at |
| 112 | + https://developer.apple.com/documentation/xcode/build-settings-reference#Order-File |
| 113 | + With additional details on how to generate an order file at |
| 114 | + https://developer.apple.com/library/archive/documentation/Performance/Conceptual/CodeFootprint/Articles/ImprovingLocality.html |
| 115 | +
|
| 116 | + Args: |
| 117 | + ctx: The ctx associated with the rule. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + An array of Info objects for consumption by later stages of build. |
| 121 | + """ |
| 122 | + |
| 123 | + if ctx.var["COMPILATION_MODE"] != "opt": |
| 124 | + # Apple's guidance: Generally you should not specify an order file in Debug or Development |
| 125 | + # configurations, as this will make the linked binary less readable to the debugger. |
| 126 | + # Use them only in Release or Deployment configurations. |
| 127 | + return [CcInfo()] |
| 128 | + |
| 129 | + concatenated_order_file = _concatenate_files( |
| 130 | + name = ctx.attr.name, |
| 131 | + actions = ctx.actions, |
| 132 | + files = ctx.files.deps, |
| 133 | + ) |
| 134 | + deduped_order_file = _dedup_file( |
| 135 | + name = ctx.attr.name, |
| 136 | + actions = ctx.actions, |
| 137 | + file = concatenated_order_file, |
| 138 | + ) |
| 139 | + |
| 140 | + linker_cc_info = _link_order_file( |
| 141 | + ctx = ctx, |
| 142 | + order_file = deduped_order_file, |
| 143 | + stats = ctx.attr.stats, |
| 144 | + ) |
| 145 | + |
| 146 | + return [ |
| 147 | + linker_cc_info, |
| 148 | + ] |
| 149 | + |
| 150 | +# This (apple_)order_file rule will inject order files into your application dependencies. |
| 151 | +# |
| 152 | +# See additional documentation in `_order_file_impl` above. |
| 153 | +# |
| 154 | +# Use like any other dependency in your BUILD. For example: |
| 155 | +# |
| 156 | +# apple_order_file( |
| 157 | +# name = "app_order_file" |
| 158 | +# deps = [ |
| 159 | +# "my_file.order", |
| 160 | +# "my_second_order_file.order", |
| 161 | +# ] |
| 162 | +# ) |
| 163 | +# |
| 164 | +# ios_application( |
| 165 | +# name = "app", |
| 166 | +# deps = [":app_order_file"], |
| 167 | +# ) |
| 168 | +# |
| 169 | +order_file = rule( |
| 170 | + implementation = _order_file_impl, |
| 171 | + attrs = { |
| 172 | + "deps": attr.label_list( |
| 173 | + allow_files = True, |
| 174 | + mandatory = True, |
| 175 | + doc = "The raw text order files to be used in the iOS application.", |
| 176 | + ), |
| 177 | + "stats": attr.bool( |
| 178 | + default = False, |
| 179 | + doc = "Indicate whether to log stats about how the linker used the order file.", |
| 180 | + ), |
| 181 | + }, |
| 182 | + doc = """\ |
| 183 | +Injects the provided `.order` files into Apple link lines, concatenating and deduplicating them before supplying the appropriate linker flags. |
| 184 | +The rule short-circuits in non-optimized compilations because generating order files is intended for release/deployment builds where they improve runtime locality. |
| 185 | +
|
| 186 | +Example: |
| 187 | +
|
| 188 | +```starlark |
| 189 | +apple_order_file( |
| 190 | + name = "app_order_file", |
| 191 | + deps = [ |
| 192 | + "my_file.order", |
| 193 | + "my_second_order_file.order", |
| 194 | + ], |
| 195 | +) |
| 196 | +
|
| 197 | +ios_application( |
| 198 | + name = "app", |
| 199 | + deps = [":app_order_file"], |
| 200 | +) |
| 201 | +``` |
| 202 | +
|
| 203 | +Set `stats = True` if you want the linker to emit information about how it used the order file. |
| 204 | +""", |
| 205 | +) |
0 commit comments