chromium/tools/generate_stubs/rules.gni

# Create a source_set with generated stubs for a POSIX shared library.
#
# Based on C-style signatures, it will generate a source and a header file
# and expose it as a source_set.
#
# See //tools/generate_stubs/generate_stubs.py for more info.
#
# Variables
#   sigs: list of files with C-style signatures (*.sig)
#   output_name: name of the generated files: $output_name.h and $output_name.cc
#   extra_header: prepend the contents of this file to the generated .cc file
#   logging_function: override the used logging function (default: VLOG(1))
#   logging_include: override the additional include (default: base/logging.h)
#   path_from_source: relative path of generated files from the project root
#   macro_include: overrides the include for DISABLE_CFI_ICALL
#                  (default: base/compiler_specific.h)
#   macro_deps: override the dependency for macro_include (default: //base)
#
# Example
#   generate_stubs("libfoo_stubs") {
#     sigs = [ "foo/foo.sigs" ]
#     extra_header = "foo/foo_stub_header.fragment"
#     output_name = "foo/foo_stubs"
#     deps = [
#       "//base",
#     ]
#   }
#
# Targets that depend on this target can `#include "path/to/foo/foo_stubs.h"`
template("generate_stubs") {
  forward_variables_from(invoker, [ "testonly" ])

  _gen_dir = get_path_info(invoker.output_name, "gen_dir")

  action("${target_name}__stubs_gen") {
    script = "//tools/generate_stubs/generate_stubs.py"
    sources = invoker.sigs
    inputs = [ invoker.extra_header ]
    outputs = [
      "${target_gen_dir}/${invoker.output_name}.cc",
      "${target_gen_dir}/${invoker.output_name}.h",
    ]
    args = [
      "--intermediate_dir",
      rebase_path(_gen_dir, root_build_dir),
      "--output",
      rebase_path(_gen_dir, root_build_dir),
      "--type",
      "posix_stubs",
      "--extra_stub_header",
      rebase_path(invoker.extra_header, root_build_dir),
      "--stubfile_name",
      get_path_info(invoker.output_name, "name"),
      "--path_from_source",
      rebase_path(_gen_dir, root_gen_dir),
    ]
    if (defined(invoker.path_from_source)) {
      args += [
        "--path_from_source",
        invoker.path_from_source,
      ]
    } else {
      args += [
        "--path_from_source",
        rebase_path(_gen_dir, root_gen_dir),
      ]
    }
    if (defined(invoker.logging_function)) {
      args += [
        "--logging-function",
        invoker.logging_function,
      ]
    }
    if (defined(invoker.logging_include)) {
      args += [
        "--logging-include",
        invoker.logging_include,
      ]
    }
    if (defined(invoker.macro_include)) {
      args += [
        "--macro-include",
        invoker.macro_include,
      ]
    }
    args += rebase_path(invoker.sigs, root_build_dir)
  }

  source_set(target_name) {
    forward_variables_from(invoker,
                           [
                             "deps",
                             "public_deps",
                             "macro_deps",
                             "visibility",
                           ])
    if (!defined(deps)) {
      deps = []
    }
    if (!defined(macro_deps)) {
      deps += [ "//base" ]
    } else {
      deps += macro_deps
    }
    if (!defined(public_deps)) {
      public_deps = []
    }
    public_deps += [ ":${target_name}__stubs_gen" ]
    sources = [
      "${target_gen_dir}/${invoker.output_name}.cc",
      "${target_gen_dir}/${invoker.output_name}.h",
    ]
    libs = [ "dl" ]
    include_dirs = [ target_gen_dir ]
    if (defined(invoker.configs)) {
      configs += invoker.configs
    }
  }
}