| # Copyright 2019 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. |
| |
| import("$zx/public/gn/fidl/fidlc.gni") |
| import("$zx/public/gn/subtarget_aliases.gni") |
| |
| # This is the $banjo_support module for C bindings. See banjo_library() for |
| # details. This file should not normally be imported by other code. |
| |
| # This tells banjo_library() to invoke banjo_c_library(). |
| banjo_support_templates = [ |
| { |
| import = "$zx/public/gn/banjo/c.gni" |
| target = "banjo_c_library" |
| }, |
| ] |
| |
| # Provide C bindings for banjo_library(). **Do not use directly!** |
| # |
| # This is never used directly, but only indirectly by banjo_library(). |
| # See there for details. |
| template("banjo_c_library") { |
| not_needed(invoker, "*") |
| |
| banjo_target = target_name |
| |
| # The dots in the Banjo library namespace are resolved to directories. |
| banjo_library_path = string_replace(banjo_target, ".", "/") |
| library_target = "$banjo_target.c" |
| gen_target = "_banjo_c_library.generate.$library_target" |
| gen_dir = get_label_info(":$gen_target($default_toolchain)", |
| "target_gen_dir") + "/$library_target" |
| gen_sources = [ "$gen_dir/include/${banjo_library_path}.h" ] |
| |
| fidlc(gen_target) { |
| visibility = [ ":*" ] |
| forward_variables_from(invoker, [ "testonly" ]) |
| |
| tool = "$zx/tools/banjo:compiler" |
| |
| deps = [ |
| ":$banjo_target", |
| ] |
| outputs = gen_sources |
| args = [ |
| "--backend", |
| "c", |
| "--output", |
| rebase_path(outputs[0], root_build_dir), |
| ] |
| } |
| |
| if (current_toolchain != default_toolchain) { |
| gen_deps = [ ":$gen_target($default_toolchain)" ] |
| |
| config("_banjo_c_library.config.$library_target") { |
| visibility = [ |
| ":$library_target.headers", |
| ":$library_target.static", |
| ] |
| forward_variables_from(invoker, [ "testonly" ]) |
| |
| # Skip adding generated headers if this is a dummy target. |
| if (invoker.target_type != "banjo_dummy") { |
| include_dirs = [ "$gen_dir/include" ] |
| } |
| } |
| |
| library(library_target) { |
| forward_variables_from(invoker, |
| [ |
| "visibility", |
| "testonly", |
| ]) |
| |
| # The generated sources come from the generation step, so depend on it. |
| deps = gen_deps |
| sources = gen_sources |
| |
| # Users of the bindings library need the generated headers. |
| public_configs = [ ":_banjo_c_library.config.$library_target" ] |
| |
| # The `.headers` subtarget of a library() only depends on its |
| # public_deps. But all users of the headers need to depend on the |
| # generation step too, so put it in public_deps as well. |
| public_deps = gen_deps |
| |
| # The generated headers of a dependent banjo_library() will #include the |
| # generated headers for its dependencies' bindings libraries, so those |
| # headers are needed in public_deps. The generated bindings code may |
| # call into its dependencies' bindings code, so the libraries |
| # themselves are needed in deps too. |
| foreach(dep, invoker.banjo_deps) { |
| deps += [ "$dep.c" ] |
| public_deps += [ "$dep.c.headers" ] |
| } |
| |
| # Generated headers have #include <ddk/driver.h>. |
| public_deps += [ "$zx/system/ulib/ddk:headers" ] |
| } |
| |
| # Things normally depend on "banjo/foo:c" rather than "banjo/foo:foo.c". |
| subtarget_aliases(target_name) { |
| forward_variables_from(invoker, |
| [ |
| "visibility", |
| "testonly", |
| ]) |
| outputs = [ |
| "c", |
| "c.headers", |
| "c.static", |
| ] |
| } |
| } |
| } |