• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    args = [
90      rebase_path(get_path_info(_sources[0], "dir"), root_build_dir),
91      rebase_path(_output_dir, root_build_dir),
92    ]
93  }
94
95  bundle_data(_data_target_name) {
96    forward_variables_from(invoker,
97                           "*",
98                           [
99                             "deps",
100                             "outputs",
101                             "public_deps",
102                             "sources",
103                           ])
104
105    sources = _outputs
106    outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
107    public_deps = [ ":$_copy_target_name" ]
108  }
109}
110
111# Those templates are specialisation of the asset_catalog template for known
112# types of asset catalog types (imageset, launchimage, appiconset).
113#
114# Arguments
115#
116#     sources:
117#       required, list of strings, paths to the file contained in the
118#       asset catalog directory; this must contain the Contents.json file
119#       and all the image referenced by it (not enforced by the template).
120#
121template("appiconset") {
122  asset_catalog(target_name) {
123    forward_variables_from(invoker, "*", [ "asset_type" ])
124    asset_type = "appiconset"
125  }
126}
127template("colorset") {
128  asset_catalog(target_name) {
129    forward_variables_from(invoker, "*", [ "asset_type" ])
130    asset_type = "colorset"
131  }
132}
133template("imageset") {
134  asset_catalog(target_name) {
135    forward_variables_from(invoker, "*", [ "asset_type" ])
136    asset_type = "imageset"
137  }
138}
139template("launchimage") {
140  asset_catalog(target_name) {
141    forward_variables_from(invoker, "*", [ "asset_type" ])
142    asset_type = "launchimage"
143  }
144}
145template("symbolset") {
146  asset_catalog(target_name) {
147    forward_variables_from(invoker, "*", [ "asset_type" ])
148    asset_type = "symbolset"
149  }
150}
151