flatbuffer.gni 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright 2016 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. # Compile a flatbuffer.
  5. #
  6. # flatc_out_dir (optional)
  7. # Specifies the path suffix that output files are generated under. This
  8. # path will be appended to root_gen_dir.
  9. #
  10. # Targets that depend on the flatbuffer target will be able to include
  11. # the resulting FlatBuffers header with an include like:
  12. # #include "dir/for/my_flatbuffer/buffer_generated.h"
  13. # If undefined, this defaults to matchign the input directory for each
  14. # .fbs file (you should almost always use the default mode).
  15. #
  16. # flatc_include_dirs (optional)
  17. # Specifies the directories which FlatBuffers compiler uses to find
  18. # included .fbs files in. Almost always should be empty.
  19. #
  20. # The list always has an implicit first item corresponding to the root of
  21. # the source tree. This enables including .fbs files by absolute path.
  22. #
  23. # The compiler will try the directories in the order given, and if all
  24. # fail it will try to load relative to the directory of the schema file
  25. # being parsed.
  26. #
  27. # mutable (optional)
  28. # Boolean to compile flatbuffers with the "--gen-mutable" argument, which
  29. # generates non-const accessors for mutating FlatBuffers in-place.
  30. #
  31. # deps (optional)
  32. # Additional dependencies.
  33. #
  34. # Parameters for compiling the generated code:
  35. #
  36. # defines (optional)
  37. # Defines to supply to the source set that compiles the generated source
  38. # code.
  39. #
  40. # extra_configs (optional)
  41. # A list of config labels that will be appended to the configs applying
  42. # to the source set.
  43. #
  44. # testonly (optional)
  45. # Boolean to indicate whether the generated source sets should be labeled
  46. # as testonly.
  47. #
  48. # Example:
  49. # flatbuffer("mylib") {
  50. # sources = [
  51. # "foo.fbs",
  52. # ]
  53. # }
  54. import("//build/compiled_action.gni")
  55. template("flatbuffer") {
  56. assert(defined(invoker.sources), "Need sources for flatbuffers_library")
  57. action_name = "${target_name}_gen"
  58. source_set_name = target_name
  59. compiled_action_foreach(action_name) {
  60. visibility = [ ":$source_set_name" ]
  61. tool = "//third_party/flatbuffers:flatc"
  62. sources = invoker.sources
  63. deps = []
  64. if (defined(invoker.flatc_out_dir)) {
  65. out_dir = "$root_gen_dir/" + invoker.flatc_out_dir
  66. } else {
  67. out_dir = "{{source_gen_dir}}"
  68. }
  69. outputs = [ "$out_dir/{{source_name_part}}_generated.h" ]
  70. args = [
  71. "-c",
  72. "--keep-prefix",
  73. "-o",
  74. "$out_dir",
  75. "-I",
  76. rebase_path("//", root_build_dir),
  77. ]
  78. if (defined(invoker.flatc_include_dirs)) {
  79. foreach(include_dir, invoker.flatc_include_dirs) {
  80. args += [
  81. "-I",
  82. rebase_path(include_dir, root_build_dir),
  83. ]
  84. }
  85. }
  86. if (defined(invoker.mutable) && invoker.mutable) {
  87. args += [ "--gen-mutable" ]
  88. }
  89. if (defined(invoker.args)) {
  90. args += invoker.args
  91. }
  92. args += [ "{{source}}" ]
  93. # The deps may have steps that have to run before running flatc.
  94. if (defined(invoker.deps)) {
  95. deps += invoker.deps
  96. }
  97. }
  98. source_set(target_name) {
  99. forward_variables_from(invoker,
  100. [
  101. "visibility",
  102. "defines",
  103. ])
  104. sources = get_target_outputs(":$action_name")
  105. if (defined(invoker.extra_configs)) {
  106. configs += invoker.extra_configs
  107. }
  108. if (defined(invoker.testonly)) {
  109. testonly = invoker.testonly
  110. }
  111. public_configs = [ "//third_party/flatbuffers:flatbuffers_config" ]
  112. public_deps = [
  113. # The generated headers reference headers within FlatBuffers, so
  114. # dependencies must be able to find those headers too.
  115. ":$action_name",
  116. "//third_party/flatbuffers",
  117. ]
  118. # This will link any libraries in the deps (the use of invoker.deps in the
  119. # action won't link it).
  120. if (defined(invoker.deps)) {
  121. deps += invoker.deps
  122. }
  123. # Same for public_deps.
  124. if (defined(invoker.public_deps)) {
  125. public_deps += invoker.public_deps
  126. }
  127. }
  128. }