json_data_generator.gni 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2022 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. # Generates cross-language JSON data from json5 files. Use this generator
  5. # if you want to share JSON data between different languages. The generator
  6. # will render your designate template file with the JSON content.
  7. #
  8. # Variables:
  9. # sources: (mandatory)
  10. # The json data source files.
  11. #
  12. # output_dir: (optional)
  13. # The output directory for the generated files. By default it will be the
  14. # $target_gen_dir, which is the directory where the gn task is invoked.
  15. #
  16. # templates: (mandatory)
  17. # List of Jinja template files to be generated. The generated file name
  18. # will be the template file name without the '.jinja' suffix, e.g. template
  19. # 'abc.cc.jinja' will generate 'abc.cc'.
  20. # template_helper: (optional)
  21. # A python file to provide custom globals and filters for Jinja templates.
  22. # Jinja template only supports very limited python expressions, to provide
  23. # custom globals/filter specific to your source JSON files, you can pass a
  24. # additional python path to this parameter, the python file you pass must
  25. # include 2 functions: `get_custom_globals(model)` and
  26. # `get_custom_filters(model)`, the parsed JSON model will be passed to your
  27. # custom functions.
  28. #
  29. # Example:
  30. # json_data_generator("my_json_data") {
  31. # sources = [ "my_json_data.json5" ]
  32. # templates = [
  33. # "my_json_data.cc.jinja",
  34. # "my_json_data.js.jinja",
  35. # ]
  36. # output_dir = "$root_gen_dir/my_dir"
  37. # }
  38. #
  39. # See README.md for more information.
  40. template("json_data_generator") {
  41. script_file = "//tools/json_data_generator/main.py"
  42. common_inputs = [ "//tools/json_data_generator/generator.py" ]
  43. action(target_name) {
  44. script = script_file
  45. forward_variables_from(invoker,
  46. [
  47. "deps",
  48. "sources",
  49. ])
  50. assert(defined(sources), "Source json files must be defined.")
  51. assert(defined(invoker.templates), "Template files must be defined.")
  52. inputs = common_inputs + invoker.templates
  53. output_dir = invoker.output_dir
  54. if (!defined(invoker.output_dir)) {
  55. output_dir = target_gen_dir
  56. }
  57. outputs = []
  58. foreach(t, get_path_info(invoker.templates, "name")) {
  59. outputs += [ "$output_dir/$t" ]
  60. }
  61. args =
  62. [ "--templates" ] + rebase_path(invoker.templates, root_build_dir) + [
  63. "--out-dir",
  64. rebase_path(output_dir, root_build_dir),
  65. "--sources",
  66. ] + rebase_path(sources, root_build_dir)
  67. if (defined(invoker.template_helper)) {
  68. args += [
  69. "--template-helper",
  70. rebase_path(invoker.template_helper, root_build_dir),
  71. ]
  72. }
  73. }
  74. }