blob: 0c5e2a7f153fa3da3d7a489f4f15118897ec6ef8 [file] [log] [blame]
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Compile a flatbuffer.
#
# sources
# Specifies the sources to compile.
#
# flatc_out_dir (optional)
# Specifies the path suffix that output files are generated under. This
# path will be appended to root_gen_dir.
#
# Targets that depend on the proto target will be able to include the
# resulting flatbuffers header with an include like:
# #include "dir/for/my_flatbuffer/buffer_generated.h"
# If undefined, this defaults to matching the input directory for each
# .fbs file (you should almost always use the default mode).
#
# deps (optional)
# Additional non flatbuffer dependencies that needs to be build before this one.
#
# public_deps (optional)
# Additional flatbuffer dependencies.
#
# Parameters for compiling the generated code:
#
# defines (optional)
# Defines to supply to the source set that compiles the generated source
# code.
#
# extra_configs (optional)
# A list of config labels that will be appended to the configs applying
# to the source set.
#
# testonly (optional)
# Boolean to indicate whether the generated source sets should be labeled
# as testonly.
#
# Example:
# flatbuffer("mylib") {
# sources = [
# "foo.fbs",
# ]
# }
import("//build/compiled_action.gni")
template("flatbuffer") {
assert(defined(invoker.sources), "Need sources for flatbuffers_library")
action_name = "${target_name}_gen"
config_name = "${target_name}_config"
source_set_name = target_name
compiled_action_foreach(action_name) {
visibility = [ ":$source_set_name" ]
tool = "//third_party/flatbuffers:flatc"
sources = invoker.sources
if (defined(invoker.flatc_out_dir)) {
out_dir = "$root_gen_dir/" + invoker.flatc_out_dir
} else {
out_dir = "$target_gen_dir/snapshot"
}
outputs = [
"$out_dir/{{source_name_part}}_generated.h",
]
args = [
"-I",
rebase_path("//"),
"-c",
"-o",
rebase_path("$out_dir", root_build_dir),
"--gen-mutable",
"{{source}}",
]
deps = []
# The deps may have steps that have to run before running flatc.
if (defined(invoker.deps)) {
deps += invoker.deps
}
if (defined(invoker.public_deps)) {
deps += invoker.public_deps
}
}
config(config_name) {
include_dirs = get_path_info(get_target_outputs(":$action_name"), "dir")
}
source_set(target_name) {
forward_variables_from(invoker,
[
"visibility",
"defines",
])
sources = get_target_outputs(":$action_name")
if (defined(invoker.extra_configs)) {
configs += invoker.extra_configs
}
if (defined(invoker.testonly)) {
testonly = invoker.testonly
}
public_configs = [
"//third_party/flatbuffers:flatbuffers_config",
":$config_name",
]
public_deps = [
# The generated headers reference headers within flatbuffers, so
# dependencies must be able to find those headers too.
"//third_party/flatbuffers",
]
deps = [
":$action_name",
]
# This will link any libraries in the public_deps (the use of
# invoker.public_deps in the action won't link it).
if (defined(invoker.public_deps)) {
public_deps += invoker.public_deps
}
}
}