xref: /aosp_15_r20/external/cronet/build/config/ios/asset_catalog.gni (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2017 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/ios/ios_sdk.gni")
6
7# This template declares a bundle_data target that references an asset
8# catalog so that it is compiled to the asset catalog of the generated
9# bundle.
10#
11# The create_bundle target requires that all asset catalogs are part of an
12# .xcasset bundle. This requirement comes from actool that only receives
13# the path to the .xcasset bundle directory and not to the individual
14# assets directories.
15#
16# The requirement is a bit problematic as it prevents compiling only a
17# subset of the asset catakig that are contained in a .xcasset. This template
18# fixes that by instead copying the content of the asset catalog to temporary
19# .xcasset directory (below $root_out_dir) and defining a bundle_data
20# target that refers to those copies (this is efficient as the "copy" is
21# implemented by hardlinking if possible on macOS).
22#
23# Since the create_data target will only refer to the .xcasset directory
24# and additional "action" target that runs a dummy script is defined. It
25# does nothing but pretends to generate the .xcassets directory (while
26# it is really created as a side-effect of the "copy" step). This allows
27# to workaround the check in "gn" that all inputs below $root_out_dir have
28# to be outputs of another target with a public dependency path.
29#
30# This template also ensures that the file are only copied once when the
31# build targets multiple architectures at the same time (aka "fat build").
32#
33# Arguments
34#
35#     sources:
36#       required, list of strings, paths to the file contained in the
37#       asset catalog directory; this must contain the Contents.json file
38#       and all the image referenced by it (not enforced by the template).
39#
40#     asset_type:
41#       required, string, type of the asset catalog, that is the extension
42#       of the directory containing the images and the Contents.json file.
43#
44template("asset_catalog") {
45  assert(defined(invoker.sources) && invoker.sources != [],
46         "sources must be defined and not empty for $target_name")
47
48  assert(defined(invoker.asset_type) && invoker.asset_type != "",
49         "asset_type must be defined and not empty for $target_name")
50
51  _copy_target_name = target_name + "__copy"
52  _data_target_name = target_name
53
54  _sources = invoker.sources
55  _outputs = []
56
57  # The compilation of resources into Assets.car is enabled automatically
58  # by the "create_bundle" target if any of the "bundle_data" sources's
59  # path is in a .xcassets directory and matches one of the know asset
60  # catalog type.
61  _xcassets_dir = "$target_gen_dir/${target_name}.xcassets"
62  _output_dir = "$_xcassets_dir/" +
63                get_path_info(get_path_info(_sources[0], "dir"), "file")
64
65  foreach(_source, invoker.sources) {
66    _dir = get_path_info(_source, "dir")
67    _outputs += [ "$_output_dir/" + get_path_info(_source, "file") ]
68
69    assert(get_path_info(_dir, "extension") == invoker.asset_type,
70           "$_source dirname must have .${invoker.asset_type} extension")
71  }
72
73  action(_copy_target_name) {
74    # Forward "deps", "public_deps" and "testonly" in case some of the
75    # source files are generated.
76    forward_variables_from(invoker,
77                           [
78                             "deps",
79                             "public_deps",
80                             "testonly",
81                           ])
82
83    script = "//build/config/ios/hardlink.py"
84
85    visibility = [ ":$_data_target_name" ]
86    sources = _sources
87    outputs = _outputs + [ _xcassets_dir ]
88
89    _source = get_path_info(_sources[0], "dir")
90
91    args = [
92      "--output-dir",
93      rebase_path(_xcassets_dir, root_build_dir),
94      "--relative-to",
95      rebase_path(get_path_info(_source, "dir"), root_build_dir),
96      rebase_path(_source, root_build_dir),
97    ]
98  }
99
100  bundle_data(_data_target_name) {
101    forward_variables_from(invoker,
102                           "*",
103                           [
104                             "deps",
105                             "outputs",
106                             "public_deps",
107                             "sources",
108                           ])
109
110    sources = _outputs
111    outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
112    public_deps = [ ":$_copy_target_name" ]
113  }
114}
115
116# Those templates are specialisation of the asset_catalog template for known
117# types of asset catalog types (imageset, launchimage, appiconset).
118#
119# Arguments
120#
121#     sources:
122#       required, list of strings, paths to the file contained in the
123#       asset catalog directory; this must contain the Contents.json file
124#       and all the image referenced by it (not enforced by the template).
125#
126template("appiconset") {
127  asset_catalog(target_name) {
128    forward_variables_from(invoker, "*", [ "asset_type" ])
129    asset_type = "appiconset"
130  }
131}
132template("colorset") {
133  asset_catalog(target_name) {
134    forward_variables_from(invoker, "*", [ "asset_type" ])
135    asset_type = "colorset"
136  }
137}
138template("imageset") {
139  asset_catalog(target_name) {
140    forward_variables_from(invoker, "*", [ "asset_type" ])
141    asset_type = "imageset"
142  }
143}
144template("launchimage") {
145  asset_catalog(target_name) {
146    forward_variables_from(invoker, "*", [ "asset_type" ])
147    asset_type = "launchimage"
148  }
149}
150template("symbolset") {
151  asset_catalog(target_name) {
152    forward_variables_from(invoker, "*", [ "asset_type" ])
153    asset_type = "symbolset"
154  }
155}
156