1# Copyright 2024 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import("//build/config/zip.gni") 6 7if (is_ios) { 8 import("//build/config/ios/ios_sdk_overrides.gni") 9} 10 11# Compile a xib or storyboard file and add it to a bundle_data so that it is 12# available at runtime in the bundle. 13# 14# Arguments 15# 16# source: 17# string, path of the xib or storyboard to compile. 18# 19# extension: 20# string, extension of the generated file or bundle. 21# 22# bundle_files: 23# list of string, name of the files in the generated bundle; 24# if empty, the output is expected to be a single file. 25# 26# Forwards all variables to the bundle_data target. 27template("bundle_data_ib_file") { 28 assert(defined(invoker.source), "source needs to be defined for $target_name") 29 assert(defined(invoker.extension), 30 "extension needs to be defined for $target_name") 31 assert(defined(invoker.bundle_files), 32 "bundle_files needs to be defined for $target_name") 33 34 _target_name = target_name 35 _compile_target = 36 target_name + "_compile_" + get_path_info(invoker.source, "extension") 37 38 _output_path = "$target_gen_dir/$_target_name/" 39 _output_name = get_path_info(invoker.source, "name") + ".${invoker.extension}" 40 41 if (is_ios) { 42 _deployment_target = ios_deployment_target 43 _target_devices = [ 44 "iphone", 45 "ipad", 46 ] 47 } else { 48 assert(false, "Unsupported platform: " + current_os) 49 } 50 51 action(_compile_target) { 52 forward_variables_from(invoker, 53 "*", 54 [ 55 "source", 56 "bundle_files", 57 ]) 58 59 script = "//build/config/apple/compile_ib_files.py" 60 args = [ 61 "--input", 62 rebase_path(invoker.source, root_build_dir), 63 "--output", 64 rebase_path("$_output_path/$_output_name", root_build_dir), 65 "--minimum-deployment-target", 66 _deployment_target, 67 "--auto-activate-custom-fonts", 68 ] 69 70 foreach(target_device, _target_devices) { 71 args += [ 72 "--target-device", 73 target_device, 74 ] 75 } 76 77 sources = [ invoker.source ] 78 if (invoker.bundle_files == []) { 79 outputs = [ "$_output_path/$_output_name" ] 80 } else { 81 outputs = [] 82 foreach(_bundle_file, invoker.bundle_files) { 83 outputs += [ "$_output_path/$_output_name/$_bundle_file" ] 84 } 85 } 86 } 87 88 bundle_data(_target_name) { 89 forward_variables_from(invoker, "*", [ "source" ]) 90 91 if (!defined(public_deps)) { 92 public_deps = [] 93 } 94 public_deps += [ ":$_compile_target" ] 95 96 sources = get_target_outputs(":$_compile_target") 97 if (invoker.bundle_files == []) { 98 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ] 99 } else { 100 outputs = 101 [ "{{bundle_resources_dir}}/$_output_name/{{source_file_part}}" ] 102 } 103 } 104} 105 106# Compile a xib file and add it to a bundle_data so that it is available at 107# runtime in the bundle. 108# 109# Arguments 110# 111# source: 112# string, path of the xib or storyboard to compile. 113# 114# Forwards all variables to the bundle_data target. 115template("bundle_data_xib_file") { 116 assert(defined(invoker.source), "source needs to be defined for $target_name") 117 118 _extension = get_path_info(invoker.source, "extension") 119 assert(_extension == "xib", 120 "source must have the .xib extension for $target_name") 121 122 bundle_data_ib_file(target_name) { 123 forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY) 124 forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) 125 126 extension = "nib" 127 bundle_files = [] 128 } 129} 130 131# Compile a storyboard file and add it to a bundle_data so that it is available 132# at runtime in the bundle. 133# 134# Arguments 135# 136# source: 137# string, path of the xib or storyboard to compile. 138# 139# bundle_files 140# list of strings, name of the individual files in the generated bundle 141# 142# Forwards all variables to the bundle_data target. 143template("bundle_data_storyboard_file") { 144 assert(defined(invoker.source), "source needs to be defined for $target_name") 145 146 assert(defined(invoker.bundle_files) && invoker.bundle_files != [], 147 "bundle_files needs to be defined for $target_name") 148 149 _extension = get_path_info(invoker.source, "extension") 150 assert(_extension == "storyboard", 151 "source must have the .storyboard extension for $target_name") 152 153 bundle_data_ib_file(target_name) { 154 forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY) 155 forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) 156 157 extension = "storyboardc" 158 } 159} 160 161# Compile a strings file and add it to a bundle_data so that it is available 162# at runtime in the bundle. 163# 164# Arguments 165# 166# source: 167# string, path of the strings file to compile. 168# 169# output: 170# string, path of the compiled file in the final bundle. 171# 172# Forwards all variables to the bundle_data target. 173template("bundle_data_strings") { 174 assert(defined(invoker.source), "source needs to be defined for $target_name") 175 assert(defined(invoker.output), "output needs to be defined for $target_name") 176 177 _source_extension = get_path_info(invoker.source, "extension") 178 assert(_source_extension == "strings", 179 "source must be a .strings for $target_name") 180 181 _target_name = target_name 182 _convert_target = target_name + "_compile_strings" 183 184 convert_plist(_convert_target) { 185 visibility = [ ":$_target_name" ] 186 source = invoker.source 187 output = 188 "$target_gen_dir/$_target_name/" + get_path_info(invoker.source, "file") 189 format = "binary1" 190 } 191 192 bundle_data(_target_name) { 193 forward_variables_from(invoker, 194 "*", 195 [ 196 "source", 197 "output", 198 ]) 199 200 if (!defined(public_deps)) { 201 public_deps = [] 202 } 203 public_deps += [ ":$_convert_target" ] 204 205 sources = get_target_outputs(":$_convert_target") 206 207 outputs = [ invoker.output ] 208 } 209} 210 211# This template declares a bundle_data target that reference an assets 212# catalog so that is is compiled to the asset catalog of the generated 213# bundle. 214# 215# The target will ensure that only the files explicitly listed will be 216# compiled into the final application (i.e. it allow listing some of 217# the assets catalog content conditionally). 218# 219# The target requires that the files are located in a .xcassets bundle 220# in the repository (or generated via a script). This ensures that the 221# assets catalog is correctly visible in Xcode (though as usual, using 222# Xcode to make change to the .xcassets bundle will not be reflected in 223# the final build unless the target is updated in the gn configuration). 224# 225# Arguments 226# 227# sources: 228# required, list of strings, path to the files contained in the 229# .xcassets bundle; this may contains a sub-set of the files on 230# disk if some assets are only compiled conditionally 231# 232# catalog: 233# required, string, path to the .xcassets bundle; all path in 234# sources must be relative to this path or the compilation will 235# fail 236# 237# Example 238# 239# bundle_data_xcassets("assets") { 240# catalog = "Assets.xcassets" 241# sources = [ 242# "Assets.xcassets/Color.colorset/Contents.json", 243# "Assets.xcassets/Contents.json", 244# ] 245# if (includes_images) { 246# sources += [ 247# "Assets.xcassets/Image.imageset/Contents.json", 248# "Assets.xcassets/Image.imageset/Image.svg", 249# ] 250# } 251# } 252template("bundle_data_xcassets") { 253 assert(defined(invoker.sources), "sources must be defined for $target_name") 254 assert(defined(invoker.catalog), "catalog must be defined for $target_name") 255 256 _target_name = target_name 257 _target_zip = target_name + "_zip" 258 259 zip(_target_zip) { 260 _catalog_name = get_path_info(invoker.catalog, "file") 261 _catalog_path = get_path_info(invoker.catalog, "dir") 262 263 inputs = invoker.sources 264 output = "$target_out_dir/$target_name/$_catalog_name" 265 base_dir = _catalog_path 266 } 267 268 bundle_data(_target_name) { 269 forward_variables_from(invoker, 270 "*", 271 [ 272 "sources", 273 "deps", 274 "public_deps", 275 ]) 276 277 public_deps = [ ":$_target_zip" ] 278 sources = get_target_outputs(":$_target_zip") 279 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ] 280 } 281} 282