chromium/third_party/wayland/wayland_protocol.gni

# Copyright 2018 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Template to generate wayland protocol code with wayland-scanner.
#
# Generates C source and header files from XML manifest files that declare
# Wayland protocols.
#
# The invoker MUST define `sources` which is the list of XML files.
#
# The invoker MAY define `generator_type` which is one of: protocol-marshalling,
# protocol-server, protocol-client, all (default assumed if the parameter is not
# specified).
#
# Example:
#  wayland_protocol("foo") {
#    sources = [ "foo.xml", "bar.xml" ]
#    generator_type = "protocol-client"  # optional, "all" is the default
#  }

template("wayland_protocol") {
  assert(defined(invoker.sources), "Need sources for wayland protocol")

  if (defined(invoker.generator_type)) {
    assert(invoker.generator_type == "protocol-marshalling" ||
           invoker.generator_type == "protocol-client" ||
           invoker.generator_type == "protocol-server")
    generator_type = invoker.generator_type
  } else {
    generator_type = "all"
  }

  # Calculate the output paths.
  protocol_outputs = []
  output_dirs = []
  foreach(protocol, invoker.sources) {
    dir = "$root_gen_dir/" + rebase_path(get_path_info(protocol, "dir"), "//")
    name = get_path_info(protocol, "name")

    if (generator_type == "protocol-marshalling") {
      # If it's a protocol marshalling code, only private marshalling code will
      # be generated.
      protocol_outputs += [ "${dir}/${name}-protocol.c" ]
    } else if (generator_type == "protocol-server") {
      # In case of server protocol generation, only server headers are generated.
      protocol_outputs += [
        "${dir}/${name}-server-protocol-core.h",
        "${dir}/${name}-server-protocol.h",
      ]
    } else if (generator_type == "protocol-client") {
      # In case of server protocol generation, only client headers are generated.
      protocol_outputs += [
        "${dir}/${name}-client-protocol-core.h",
        "${dir}/${name}-client-protocol.h",
      ]
    } else {
      assert(generator_type == "all")

      # In all the other cases(generation of protocols from
      # //third_party/wayland-protocols), private source with server and client
      # headers are generated.
      protocol_outputs += [
        "${dir}/${name}-protocol.c",
        "${dir}/${name}-server-protocol.h",
        "${dir}/${name}-client-protocol.h",
      ]
    }
    output_dirs += [ dir ]
  }

  action_name = "${target_name}_gen"
  config_name = "${target_name}_config"
  source_set_name = "${target_name}"

  # Action which runs wayland-scanner to generate the code.
  action(action_name) {
    visibility = [ ":$source_set_name" ]
    script = "//third_party/wayland/wayland_scanner_wrapper.py"
    sources = invoker.sources
    outputs = protocol_outputs

    # Paths in invoker.sources are relative to the invoker.
    # Make it relative to the src root.
    args = rebase_path(invoker.sources, "//")

    args += [
      "--src-root",
      rebase_path("//", root_build_dir),
    ]
    args += [
      "--root-gen-dir",
      rebase_path(root_gen_dir, root_build_dir),
    ]

    args += [
      "--generator-type",
      generator_type,
    ]

    wayland_scanner_label =
        "//third_party/wayland:wayland_scanner($host_toolchain)"
    deps = [ wayland_scanner_label ]
    wayland_scanner_path = get_label_info(wayland_scanner_label,
                                          "root_out_dir") + "/wayland_scanner"
    args += [
      "--cmd",
      "./" + rebase_path(wayland_scanner_path, root_build_dir),
    ]
    sources += [ wayland_scanner_path ]
  }

  # Config to include the generated headers only with the file names.
  # e.g. #include <foo-client-protocol.h>
  config(config_name) {
    include_dirs = output_dirs
  }

  # Source set which consists of the generated code.
  source_set(source_set_name) {
    sources = get_target_outputs(":$action_name")

    deps = [
      ":$action_name",
      "//third_party/wayland:wayland_util",
    ]

    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [ "//build/config/compiler:no_chromium_code" ]

    public_configs = [ ":$config_name" ]
  }
}