chromium/third_party/material_design_icons/BUILD.gn

# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

assert(target_os == "ios")

import("//build/config/ios/ios_sdk.gni")

# This template declares a bundle_data target that references an asset
# catalog so that it is compiled to the asset catalog of the generated
# bundle.
#
# The create_bundle target requires that all asset catalogs are part of an
# .xcasset bundle. This requirement comes from actool that only receives
# the path to the .xcasset bundle directory and not to the individual
# assets directories.
#
# The requirement is a bit problematic as it prevents compiling only a
# subset of the asset catalog that are contained in a .xcasset. This template
# fixes that by instead copying the content of the asset catalog to temporary
# .xcasset directory (below $root_out_dir) and defining a bundle_data
# target that refers to those copies (this is efficient as the "copy" is
# implemented by hardlinking if possible on macOS).
#
# Since the create_data target will only refer to the .xcasset directory
# and additional "action" target that runs a dummy script is defined. It
# does nothing but pretends to generate the .xcassets directory (while
# it is really created as a side-effect of the "copy" step). This allows
# to workaround the check in "gn" that all inputs below $root_out_dir have
# to be outputs of another target with a public dependency path.
#
# This template also ensures that the file are only copied once when the
# build targets multiple architectures at the same time (aka "fat build").
#
# Arguments
#
#     sources:
#       required, list of strings, paths to the file contained in the
#       asset catalog directory; this must contain the Contents.json file
#       and all the image referenced by it (not enforced by the template).
#
template("material_icon") {
  assert(defined(invoker.sources) && invoker.sources != [],
         "sources must be defined and not empty for $target_name")

  _copy_target_name = target_name + "__copy"
  _data_target_name = target_name

  _sources = invoker.sources
  _outputs = []

  # The compilation of resources into Assets.car is enabled automatically
  # by the "create_bundle" target if any of the "bundle_data" sources's
  # path is in a .xcassets directory and matches one of the know asset
  # catalog type.
  _xcassets_dir = "$target_gen_dir/${target_name}.xcassets"
  _output_dir = "$_xcassets_dir/" +
                get_path_info(get_path_info(_sources[0], "dir"), "file")

  foreach(_source, invoker.sources) {
    _dir = get_path_info(_source, "dir")
    _outputs += [ "$_output_dir/" + get_path_info(_source, "file") ]
  }

  action(_copy_target_name) {
    # Forward "deps", "public_deps" and "testonly" in case some of the
    # source files are generated.
    forward_variables_from(invoker,
                           [
                             "deps",
                             "public_deps",
                             "testonly",
                           ])

    script = "//build/config/ios/hardlink.py"

    visibility = [ ":$_data_target_name" ]
    sources = _sources
    outputs = _outputs + [ _xcassets_dir ]

    _source = get_path_info(_sources[0], "dir")

    args = [
      "--output-dir",
      rebase_path(_xcassets_dir, root_build_dir),
      "--relative-to",
      rebase_path(get_path_info(_source, "dir"), root_build_dir),
      rebase_path(_source, root_build_dir),
    ]
  }

  bundle_data(_data_target_name) {
    forward_variables_from(invoker,
                           "*",
                           [
                             "deps",
                             "outputs",
                             "public_deps",
                             "sources",
                           ])

    sources = _outputs
    outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
    public_deps = [ ":$_copy_target_name" ]
  }
}

# List all resources used by Chrome on iOS (including those used by the private
# downstream repository).  Not all resources are listed as there are thousands
# of resources in material_design_icons repository.
#
# The image sets are described via strings following the $category/$image_name
# pattern and do not correspond to real paths.
_image_sets = [
  "action/ic_account_circle",
  "action/ic_account_circle_48pt",
  "action/ic_credit_card",
  "action/ic_delete",
  "action/ic_done",
  "action/ic_feedback",
  "action/ic_help",
  "action/ic_info",
  "action/ic_lock_18pt",
  "action/ic_open_in_browser",
  "action/ic_print",
  "action/ic_report_problem",
  "action/ic_search",
  "action/ic_settings_white",
  "action/ic_touch_app",
  "communication/ic_comment",
  "communication/ic_email",
  "communication/ic_message",
  "content/ic_link",
  "content/ic_send",
  "content/ic_undo",
  "editor/ic_mode_edit",
  "file/ic_file_download",
  "hardware/ic_desktop_windows",
  "hardware/ic_desktop_windows_white",
  "hardware/ic_keyboard",
  "hardware/ic_keyboard_arrow_down",
  "hardware/ic_keyboard_arrow_right",
  "hardware/ic_keyboard_arrow_up",
  "hardware/ic_mouse",
  "image/ic_photo_camera",
  "image/ic_photo_library",
  "maps/ic_place",
  "navigation/ic_arrow_back",
  "navigation/ic_arrow_forward_white",
  "navigation/ic_check",
  "navigation/ic_chevron_left",
  "navigation/ic_chevron_left_white_36pt",
  "navigation/ic_chevron_right",
  "navigation/ic_close",
  "navigation/ic_close_white",
  "navigation/ic_fullscreen",
  "navigation/ic_fullscreen_exit",
  "navigation/ic_menu_white",
  "navigation/ic_more_vert",
  "navigation/ic_refresh",
  "navigation/ic_refresh_white",
  "toggle/ic_check_box_outline_blank_white",
  "toggle/ic_check_box_white",
  "toggle/ic_radio_button_checked_white",
  "toggle/ic_radio_button_unchecked_white",
]

# Define all the imagesets using the description from _image_sets variable.
# All imagesets are assumed to be universal and to include @1x, @2x and @3x
# version of the images.
foreach(_image_set, _image_sets) {
  _category = get_path_info(_image_set, "dir")
  _image_name = get_path_info(_image_set, "file")
  material_icon(_image_name) {
    _imageset_dir = "src/$_category/ios/$_image_name.imageset"
    sources = [
      "$_imageset_dir/${_image_name}.png",
      "$_imageset_dir/${_image_name}_2x.png",
      "$_imageset_dir/${_image_name}_3x.png",
      "$_imageset_dir/Contents.json",
    ]
  }
}