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