| # 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("config/config.gni") |
| |
| # Define a Fuchsia FIDL library component() target. |
| # |
| # $namespace.$library_name must match the the library name specified in the FIDL |
| # files. |
| # |
| # Parameters |
| # |
| # sources |
| # Required: List of .fidl files. |
| # |
| # library_name |
| # Optional: Name of the library. target_name is used if name |
| # is not specified explicitly. |
| # |
| # namespace |
| # Optional: Namespace for the library. |
| # |
| # deps |
| # Optional: List of other fidl_library() targets that this |
| # FIDL library depends on. |
| # |
| template("fidl_library") { |
| forward_variables_from(invoker, [ "namespace" ]) |
| |
| _library_basename = target_name |
| if (defined(invoker.library_name)) { |
| _library_basename = invoker.library_name |
| } |
| |
| if (defined(namespace)) { |
| _library_name = "${namespace}.${_library_basename}" |
| _namespace_path = string_replace(namespace, ".", "/") |
| _library_path = "${_namespace_path}/${_library_basename}" |
| } else { |
| _library_name = _library_basename |
| _library_path = string_replace(_library_basename, ".", "/") |
| } |
| |
| _response_file = "$target_gen_dir/$target_name.rsp" |
| _json_representation = "$target_gen_dir/${_library_name}.fidl.json" |
| _output_gen_dir = "$target_gen_dir/fidl" |
| _output_base = "$_output_gen_dir/${_library_path}/cpp/fidl" |
| _tables_file = "$_output_gen_dir/${_library_name}.fidl-tables.c" |
| |
| action("${target_name}_response_file") { |
| script = "${fuchsia_sdk}/build/gen_fidl_response_file.py" |
| |
| forward_variables_from(invoker, |
| [ |
| "deps", |
| "public_deps", |
| "sources", |
| "testonly", |
| ]) |
| |
| _libraries_file = "$target_gen_dir/${invoker.target_name}.fidl_libraries" |
| |
| outputs = [ |
| _response_file, |
| _libraries_file, |
| ] |
| |
| args = [ |
| "--out-response-file", |
| rebase_path(_response_file, root_build_dir), |
| "--out-libraries", |
| rebase_path(_libraries_file, root_build_dir), |
| "--tables", |
| rebase_path(_tables_file, root_build_dir), |
| "--json", |
| rebase_path(_json_representation, root_build_dir), |
| "--name", |
| _library_name, |
| "--sources", |
| ] + rebase_path(sources, root_build_dir) |
| |
| if (defined(invoker.deps) || defined(invoker.public_deps)) { |
| merged_deps = [] |
| |
| if (defined(invoker.deps)) { |
| merged_deps += invoker.deps |
| } |
| |
| if (defined(invoker.public_deps)) { |
| merged_deps += invoker.public_deps |
| } |
| |
| dep_libraries = [] |
| deps = [] |
| |
| foreach(dep, merged_deps) { |
| gen_dir = get_label_info(dep, "target_gen_dir") |
| dep_toolchain = get_label_info(dep, "toolchain") |
| name = get_label_info(dep, "name") |
| dep_libraries += [ "$gen_dir/$name.fidl_libraries" ] |
| dep_dir = |
| get_label_info(get_label_info(dep, "label_no_toolchain"), "dir") |
| deps += [ "${dep_dir}:${name}_response_file($dep_toolchain)" ] |
| } |
| |
| inputs = dep_libraries |
| |
| args += [ "--dep-libraries" ] + rebase_path(dep_libraries, root_build_dir) |
| } |
| } |
| |
| action("${target_name}_compile") { |
| forward_variables_from(invoker, [ "testonly" ]) |
| |
| visibility = [ ":*" ] |
| |
| deps = [ ":${invoker.target_name}_response_file" ] |
| |
| script = "${fuchsia_sdk}/build/gn_run_binary.py" |
| |
| inputs = [ |
| # Depend on the SDK hash, to ensure rebuild if the SDK tools change. |
| fuchsia_sdk_manifest_file, |
| _response_file, |
| ] |
| |
| outputs = [ |
| _json_representation, |
| _tables_file, |
| ] |
| |
| rebased_response_file = rebase_path(_response_file, root_build_dir) |
| args = [ |
| rebase_path("${fuchsia_sdk}/tools/fidlc", root_build_dir), |
| "@$rebased_response_file", |
| ] |
| } |
| |
| action("${target_name}_cpp_gen") { |
| visibility = [ ":${invoker.target_name}" ] |
| forward_variables_from(invoker, [ "testonly" ]) |
| |
| deps = [ ":${invoker.target_name}_compile" ] |
| |
| inputs = [ |
| # Depend on the SDK hash, to ensure rebuild if the SDK tools change. |
| fuchsia_sdk_manifest_file, |
| _json_representation, |
| ] |
| |
| outputs = [ |
| "${_output_base}.h", |
| "${_output_base}.cc", |
| ] |
| |
| script = "${fuchsia_sdk}/build/gn_run_binary.py" |
| args = [ |
| rebase_path("${fuchsia_sdk}/tools/fidlgen", root_build_dir), |
| "-generators", |
| "cpp", |
| "-json", |
| rebase_path(_json_representation, root_build_dir), |
| "-include-base", |
| rebase_path(_output_gen_dir, root_build_dir), |
| "-output-base", |
| rebase_path("${_output_base}", root_build_dir), |
| ] |
| } |
| |
| config("${target_name}_config") { |
| visibility = [ ":${invoker.target_name}" ] |
| include_dirs = [ _output_gen_dir ] |
| } |
| |
| source_set(target_name) { |
| forward_variables_from(invoker, |
| [ |
| "deps", |
| "public_deps", |
| "testonly", |
| "visibility", |
| ]) |
| |
| sources = [ |
| "${_output_base}.cc", |
| "${_output_base}.h", |
| _tables_file, |
| ] |
| |
| # Metadata to allow us to query all FIDL IR files. |
| metadata = { |
| fidl_json = [ rebase_path(_json_representation, root_build_dir) ] |
| } |
| |
| if (!defined(deps)) { |
| deps = [] |
| } |
| deps += [ ":${invoker.target_name}_compile" ] |
| deps += [ ":${invoker.target_name}_cpp_gen" ] |
| |
| if (!defined(public_deps)) { |
| public_deps = [] |
| } |
| public_deps += [ "${fuchsia_sdk}/pkg/fidl" ] |
| public_deps += [ "${fuchsia_sdk}/pkg/fidl_cpp" ] |
| |
| public_configs = [ ":${invoker.target_name}_config" ] |
| } |
| } |