| # Copyright 2018 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/host_tool_action.gni") |
| |
| template("banjo_library") { |
| assert(defined(invoker.sources), |
| "banjo_library(\"$target_name\") must set `sources`") |
| |
| # The main target is an action that generates the headers. |
| # Its public_deps includes a config() target that makes them visible. |
| # banjo_library() targets use public_deps for dependencies between headers. |
| banjo_target = target_name |
| config_target = "${banjo_target}._config" |
| |
| # The generated headers are the same in every toolchain, so generate only |
| # once, in $default_toolchain. |
| gen_dir = get_label_info(":$banjo_target($default_toolchain)", |
| "target_gen_dir") + "/$banjo_target" |
| |
| config(config_target) { |
| visibility = [ ":$banjo_target" ] |
| forward_variables_from(invoker, [ "testonly" ]) |
| include_dirs = [ "$gen_dir/include" ] |
| } |
| |
| if (current_toolchain == default_toolchain) { |
| # TODO(mcgrathr): temporary until everything is renamed with . names |
| banjo_library_name = string_replace(banjo_target, "-", ".") |
| |
| banjo_path = string_replace(banjo_library_name, ".", "/") |
| banjo_name = string_replace(banjo_path, "ddk/protocol/", "") |
| |
| gen_files = [ |
| { |
| switch = "--ddk-header" |
| file = "$gen_dir/include/ddk/protocol/${banjo_name}.h" |
| }, |
| { |
| switch = "--ddktl-header" |
| file = "$gen_dir/include/ddktl/protocol/${banjo_name}.h" |
| }, |
| { |
| file = "$gen_dir/include/ddktl/protocol/${banjo_name}-internal.h" |
| }, |
| ] |
| gen_args = [ |
| "--name", |
| banjo_library_name, |
| ] |
| gen_outputs = [] |
| foreach(gen, gen_files) { |
| gen_outputs += [ gen.file ] |
| if (defined(gen.switch)) { |
| gen_args += [ |
| gen.switch, |
| rebase_path(gen.file, root_build_dir), |
| ] |
| } |
| } |
| |
| host_tool_action(banjo_target) { |
| forward_variables_from(invoker, |
| [ |
| "deps", |
| "public_deps", |
| "testonly", |
| "visibility", |
| ]) |
| if (!defined(public_deps)) { |
| public_deps = [] |
| } |
| public_deps += [ ":$config_target" ] |
| |
| tool = "$zx/system/host/banjo:banjoc" |
| outputs = gen_outputs |
| |
| # These inputs are needed both here and in every dependency |
| # banjo_library() action. |
| metadata = { |
| banjo_inputs = invoker.sources |
| banjo_input_args = |
| [ "--files" ] + rebase_path(invoker.sources, root_build_dir) |
| } |
| # Note the [ "" ] query includes this target itself, not just its |
| # dependencies. |
| # |
| # TODO(get_metadata): banjo needs this to collect metadata in |
| # topological order, but it's a pruned pre-order instead |
| sources = get_metadata([ "" ], [ "banjo_inputs" ], [], true) |
| args = gen_args + get_metadata([ "" ], [ "banjo_input_args" ]) |
| } |
| } else { |
| group(banjo_target) { |
| forward_variables_from(invoker, |
| [ |
| "testonly", |
| "visibility", |
| ]) |
| public_deps = [ |
| ":$banjo_target($default_toolchain)", |
| ] |
| not_needed(invoker, "*") |
| } |
| } |
| } |