json_schema_api.gni 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. # Copyright 2014 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. # This file contains templates for generating static libraries based on the
  5. # corresponding output of the schema compiler tools. The output can be either
  6. # the generated C++ types (generated_types template), the bundled extension
  7. # function registration (json_schema_api template with
  8. # bundle_registration = true), or the bundled JSON strings of the APIs
  9. # (json_schema_api template with bundle = true). The generated library target
  10. # has implicit hard dependencies on all schema files listed by the invoker and
  11. # is itself a hard dependency.
  12. #
  13. # Common variables that can be used in all templates are:
  14. # - sources [required] A list of schema files used to generate the C++ types.
  15. #
  16. # - root_namespace [required]
  17. # A Python string substituion pattern used to generate the C++
  18. # namespace for each API. Use %(namespace)s to replace with the API
  19. # namespace, like "toplevel::%(namespace)s_api".
  20. #
  21. # schema_include_rules [optional]
  22. # A list of paths to include when searching for referenced objects,
  23. # with the namespace separated by a :.
  24. # Example:
  25. # [ '/foo/bar:Foo::Bar::%(namespace)s' ]
  26. #
  27. # - configs [optional]
  28. # Extra gn configs to apply to the compile step.
  29. #
  30. # - deps [optional]
  31. # If any deps are specified they will be inherited by the static library
  32. # target.
  33. #
  34. # - visibility [optional]
  35. # A specific visibility to apply for the generated static library. If
  36. # omitted, visibility will be inherited from the invoker.
  37. # NOTE: Common variables here for when multiple templates use them.
  38. compiler_root = "//tools/json_schema_compiler"
  39. compiler_script = "$compiler_root/compiler.py"
  40. compiler_sources = [
  41. "$compiler_root/cc_generator.py",
  42. "$compiler_root/code.py",
  43. "$compiler_root/compiler.py",
  44. "$compiler_root/cpp_bundle_generator.py",
  45. "$compiler_root/cpp_generator.py",
  46. "$compiler_root/cpp_type_generator.py",
  47. "$compiler_root/cpp_util.py",
  48. "$compiler_root/h_generator.py",
  49. "$compiler_root/idl_schema.py",
  50. "$compiler_root/model.py",
  51. "$compiler_root/util_cc_helper.py",
  52. ]
  53. # Outputs the bundle of generated JSON strings for each API.
  54. #
  55. # Template-specific variables (in addition to the common ones described above):
  56. #
  57. # bundle_name [required]
  58. # A string to prepend to generated bundle class names, so that multiple
  59. # bundle rules can be used without conflicting. Only used with one of
  60. # the cpp-bundle generators.
  61. #
  62. # root [optional]
  63. # base directory of the source json file(s)
  64. # defaults to "//"
  65. #
  66. # target_prefix [optional]
  67. # subdir below root_gen_dir that is the base directory for the generated
  68. # output files, defaults to empty string (no subdir)
  69. template("generated_json_strings") {
  70. assert(defined(invoker.sources),
  71. "\"sources\" must be defined for the $target_name template.")
  72. assert(defined(invoker.root_namespace),
  73. "\"root_namespace\" must be defined for the $target_name template.")
  74. assert(defined(invoker.bundle_name),
  75. "\"bundle_name\" must be defined for bundles")
  76. assert(!defined(invoker.root) || defined(invoker.target_prefix),
  77. "\"target_prefix\" is required when \"root\" is specified")
  78. schema_include_rules = ""
  79. if (defined(invoker.schema_include_rules)) {
  80. schema_include_rules = invoker.schema_include_rules
  81. }
  82. include_dir = root_gen_dir
  83. destdir = rebase_path(root_gen_dir, root_build_dir)
  84. if (defined(invoker.target_prefix)) {
  85. destdir += "/${invoker.target_prefix}"
  86. include_dir += "/${invoker.target_prefix}"
  87. }
  88. generated_config_name = target_name + "_generated_config"
  89. config(generated_config_name) {
  90. include_dirs = [ include_dir ]
  91. }
  92. root_namespace = invoker.root_namespace
  93. if (defined(invoker.root)) {
  94. root_folder = invoker.root
  95. } else {
  96. root_folder = "//"
  97. }
  98. # Save the target_name, since other targets (like the action() and
  99. # action_foreach() below) need to reference them, but would have their own
  100. # target_name variable.
  101. root_target_name = target_name
  102. bundle_generator_schema_name = target_name + "_bundle_generator_schema"
  103. action(bundle_generator_schema_name) {
  104. visibility = [ ":$root_target_name" ]
  105. script = compiler_script
  106. inputs = compiler_sources + invoker.sources
  107. outputs = [
  108. "$target_gen_dir/generated_schemas.cc",
  109. "$target_gen_dir/generated_schemas.h",
  110. ]
  111. args = [
  112. "--root=" + rebase_path(root_folder, root_build_dir),
  113. "--destdir=$destdir",
  114. "--namespace=$root_namespace",
  115. "--bundle-name=" + invoker.bundle_name,
  116. "--generator=cpp-bundle-schema",
  117. "--include-rules=$schema_include_rules",
  118. ] + rebase_path(invoker.sources, root_build_dir)
  119. }
  120. # Compute the contents of the library/source set.
  121. lib_sources = get_target_outputs(":$bundle_generator_schema_name")
  122. lib_deps = [ ":$bundle_generator_schema_name" ]
  123. lib_public_deps = [
  124. # For base::StringPiece.
  125. "//base",
  126. ]
  127. lib_extra_configs = []
  128. if (defined(invoker.configs)) {
  129. lib_extra_configs += invoker.configs
  130. }
  131. if (defined(invoker.deps)) {
  132. lib_deps += invoker.deps
  133. }
  134. static_library(target_name) {
  135. sources = lib_sources
  136. deps = lib_deps
  137. public_deps = lib_public_deps
  138. configs += lib_extra_configs
  139. public_configs = [ ":$generated_config_name" ]
  140. if (defined(invoker.visibility)) {
  141. visibility = invoker.visibility
  142. }
  143. }
  144. }
  145. # Outputs the bundle of extension function registrations.
  146. #
  147. # Template-specific variables (in addition to the common ones described above):
  148. #
  149. # bundle_name [required]
  150. # A string to prepend to generated bundle class names, so that multiple
  151. # bundle rules can be used without conflicting. Only used with one of
  152. # the cpp-bundle generators.
  153. #
  154. # impl_dir [required if bundle_registration = true, otherwise unused]
  155. # The path containing C++ implementations of API functions. This path is
  156. # used as the root path when looking for {schema}/{schema}_api.h headers
  157. # when generating API registration bundles. Such headers, if found, are
  158. # automatically included by the generated code.
  159. #
  160. # root [optional]
  161. # base directory of the source json file(s)
  162. # defaults to "//"
  163. #
  164. # target_prefix [optional]
  165. # subdir below root_gen_dir that is the base directory for the generated
  166. # output files, defaults to empty string (no subdir)
  167. template("function_registration") {
  168. assert(defined(invoker.sources),
  169. "\"sources\" must be defined for the $target_name template.")
  170. assert(defined(invoker.root_namespace),
  171. "\"root_namespace\" must be defined for the $target_name template.")
  172. assert(defined(invoker.bundle_name),
  173. "\"bundle_name\" must be defined for bundle registrations")
  174. assert(defined(invoker.impl_dir),
  175. "\"impl_dir\" must be defined for the $target_name template.")
  176. assert(!defined(invoker.root) || defined(invoker.target_prefix),
  177. "\"target_prefix\" is required when \"root\" is specified")
  178. schema_include_rules = ""
  179. if (defined(invoker.schema_include_rules)) {
  180. schema_include_rules = invoker.schema_include_rules
  181. }
  182. include_dir = root_gen_dir
  183. destdir = rebase_path(root_gen_dir, root_build_dir)
  184. if (defined(invoker.target_prefix)) {
  185. destdir += "/${invoker.target_prefix}"
  186. include_dir += "/${invoker.target_prefix}"
  187. }
  188. generated_config_name = target_name + "_generated_config"
  189. config(generated_config_name) {
  190. include_dirs = [ include_dir ]
  191. }
  192. root_namespace = invoker.root_namespace
  193. # Save the target_name, since other targets (like the action() and
  194. # action_foreach() below) need to reference them, but would have their own
  195. # target_name variable.
  196. root_target_name = target_name
  197. if (defined(invoker.root)) {
  198. root_folder = invoker.root
  199. } else {
  200. root_folder = "//"
  201. }
  202. # Child directory inside the generated file tree.
  203. gen_child_dir = get_path_info(invoker.impl_dir + "/", "gen_dir")
  204. bundle_generator_registration_name =
  205. target_name + "_bundle_generator_registration"
  206. action(bundle_generator_registration_name) {
  207. visibility = [ ":$root_target_name" ]
  208. script = compiler_script
  209. inputs = compiler_sources + invoker.sources
  210. outputs = [
  211. "$gen_child_dir/generated_api_registration.cc",
  212. "$gen_child_dir/generated_api_registration.h",
  213. ]
  214. args = [
  215. "--root=" + rebase_path(root_folder, root_build_dir),
  216. "--destdir=$destdir",
  217. "--namespace=$root_namespace",
  218. "--bundle-name=" + invoker.bundle_name,
  219. "--generator=cpp-bundle-registration",
  220. "--impl-dir=" + rebase_path(invoker.impl_dir, root_folder),
  221. "--include-rules=$schema_include_rules",
  222. ] + rebase_path(invoker.sources, root_build_dir)
  223. }
  224. # Compute the contents of the library/source set.
  225. lib_sources = get_target_outputs(":$bundle_generator_registration_name")
  226. lib_deps = [ ":$bundle_generator_registration_name" ]
  227. lib_extra_configs = []
  228. if (defined(invoker.configs)) {
  229. lib_extra_configs += invoker.configs
  230. }
  231. if (defined(invoker.deps)) {
  232. lib_deps += invoker.deps
  233. }
  234. static_library(target_name) {
  235. sources = lib_sources
  236. deps = lib_deps
  237. public_deps = []
  238. configs += lib_extra_configs
  239. public_configs = [ ":$generated_config_name" ]
  240. if (defined(invoker.visibility)) {
  241. visibility = invoker.visibility
  242. }
  243. }
  244. }
  245. # Generates the C++ types for the given APIs.
  246. #
  247. # root [optional]
  248. # base directory of the source json file(s)
  249. # defaults to "//"
  250. #
  251. # target_prefix [optional]
  252. # subdir below root_gen_dir that is the base directory for the generated
  253. # output files, defaults to empty string (no subdir)
  254. template("generated_types") {
  255. assert(defined(invoker.sources),
  256. "\"sources\" must be defined for the $target_name template.")
  257. assert(defined(invoker.root_namespace),
  258. "\"root_namespace\" must be defined for the $target_name template.")
  259. assert(!defined(invoker.root) || defined(invoker.target_prefix),
  260. "\"target_prefix\" is required when \"root\" is specified")
  261. schema_include_rules = ""
  262. if (defined(invoker.schema_include_rules)) {
  263. schema_include_rules = invoker.schema_include_rules
  264. }
  265. include_dir = root_gen_dir
  266. destdir = rebase_path(root_gen_dir, root_build_dir)
  267. if (defined(invoker.target_prefix)) {
  268. destdir += "/${invoker.target_prefix}"
  269. include_dir += "/${invoker.target_prefix}"
  270. }
  271. generated_config_name = target_name + "_generated_config"
  272. config(generated_config_name) {
  273. include_dirs = [ include_dir ]
  274. }
  275. if (defined(invoker.root)) {
  276. root_folder = invoker.root
  277. } else {
  278. root_folder = "//"
  279. }
  280. root_namespace = invoker.root_namespace
  281. # Save the target_name, since other targets (like the action() and
  282. # action_foreach() below) need to reference them, but would have their own
  283. # target_name variable.
  284. root_target_name = target_name
  285. schema_generator_name = target_name + "_schema_generator"
  286. action_foreach(schema_generator_name) {
  287. visibility = [ ":$root_target_name" ]
  288. script = compiler_script
  289. sources = invoker.sources
  290. inputs = compiler_sources
  291. outputs = [
  292. "$target_gen_dir/{{source_name_part}}.cc",
  293. "$target_gen_dir/{{source_name_part}}.h",
  294. ]
  295. args = [
  296. "{{source}}",
  297. "--root=" + rebase_path(root_folder, root_build_dir),
  298. "--destdir=$destdir",
  299. "--namespace=$root_namespace",
  300. "--generator=cpp",
  301. "--include-rules=$schema_include_rules",
  302. ]
  303. }
  304. # Compute the contents of the library/source set.
  305. lib_sources = get_target_outputs(":$schema_generator_name")
  306. lib_public_deps = [ ":$schema_generator_name" ]
  307. lib_deps = [
  308. "//base",
  309. "//tools/json_schema_compiler:generated_api_util",
  310. ]
  311. lib_extra_configs = []
  312. if (defined(invoker.configs)) {
  313. lib_extra_configs += invoker.configs
  314. }
  315. if (defined(invoker.deps)) {
  316. lib_deps += invoker.deps
  317. }
  318. static_library(target_name) {
  319. sources = lib_sources
  320. deps = lib_deps
  321. public_deps = lib_public_deps
  322. configs += lib_extra_configs
  323. public_configs = [ ":$generated_config_name" ]
  324. if (defined(invoker.visibility)) {
  325. visibility = invoker.visibility
  326. }
  327. }
  328. }