proto_library.gni 6.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright 2019 the V8 project 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("//build_overrides/build.gni")

# This file should not be pulled in chromium builds.
assert(!build_with_chromium)

template("proto_library") {
  assert(defined(invoker.sources))
  proto_sources = invoker.sources

  set_sources_assignment_filter([])

16 17 18 19 20 21
  if (host_os == "win") {
    host_executable_suffix = ".exe"
  } else {
    host_executable_suffix = ""
  }

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  # All the proto imports should be relative to the project root.
  proto_in_dir = "//"
  if (defined(invoker.proto_in_dir)) {
    proto_in_dir = invoker.proto_in_dir
  }
  assert(defined(invoker.proto_out_dir),
         "proto_out_dir must be explicitly defined")
  proto_out_dir = invoker.proto_out_dir

  # We don't support generate_python in the standalone build, but still must
  # check that the caller sets this to false. This is because when building in
  # the chromium tree, chromium's proto_library.gni in chrome (!= this) defaults
  # generate_python = true.
  assert(defined(invoker.generate_python) && !invoker.generate_python)

  # If false will not generate the default .pb.{cc,h} files. Used for custom
  # codegen plugins.
  generate_cc = true
  if (defined(invoker.generate_cc)) {
    generate_cc = invoker.generate_cc
  }

  generate_descriptor = ""
  if (defined(invoker.generate_descriptor)) {
    generate_descriptor = invoker.generate_descriptor
  }

  if (defined(invoker.generator_plugin_label)) {
    plugin_host_label = invoker.generator_plugin_label + "($host_toolchain)"
51 52 53
    plugin_path =
        get_label_info(plugin_host_label, "root_out_dir") + "/" +
        get_label_info(plugin_host_label, "name") + host_executable_suffix
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    generate_with_plugin = true
  } else if (defined(invoker.generator_plugin_script)) {
    plugin_path = invoker.generator_plugin_script
    generate_with_plugin = true
  } else {
    generate_with_plugin = false
  }

  if (generate_with_plugin) {
    if (defined(invoker.generator_plugin_suffix)) {
      generator_plugin_suffixes = [
        "${invoker.generator_plugin_suffix}.h",
        "${invoker.generator_plugin_suffix}.cc",
      ]
    } else {
      generator_plugin_suffixes = invoker.generator_plugin_suffixes
    }
  }

  cc_out_dir = "$root_gen_dir/" + proto_out_dir
  rel_cc_out_dir = rebase_path(cc_out_dir, root_build_dir)

  protos = rebase_path(proto_sources, proto_in_dir)
  protogens = []

  if (generate_descriptor != "") {
    protogens += [ "$root_gen_dir/" + generate_descriptor ]
  }

  foreach(proto, protos) {
    proto_dir = get_path_info(proto, "dir")
    proto_name = get_path_info(proto, "name")
    proto_path = proto_dir + "/" + proto_name

    if (generate_cc) {
      protogens += [
        "$cc_out_dir/$proto_path.pb.h",
        "$cc_out_dir/$proto_path.pb.cc",
      ]
    }
    if (generate_with_plugin) {
      foreach(suffix, generator_plugin_suffixes) {
        protogens += [ "$cc_out_dir/${proto_path}${suffix}" ]
      }
    }
  }

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

  config(config_name) {
    include_dirs = [ cc_out_dir ]
  }

  # The XXX_gen action that generates the .pb.{cc,h} files.
  action(action_name) {
    visibility = [ ":$source_set_name" ]
    script = "//build/gn_run_binary.py"
    sources = proto_sources
    outputs = get_path_info(protogens, "abspath")

    protoc_label = "//:protoc($host_toolchain)"
117 118
    protoc_path = get_label_info(protoc_label, "root_out_dir") + "/protoc" +
                  host_executable_suffix
119 120 121 122 123 124 125 126
    args = [
      # Path should be rebased because |root_build_dir| for current toolchain
      # may be different from |root_out_dir| of protoc built on host toolchain.
      "./" + rebase_path(protoc_path, root_build_dir),
      "--proto_path",
      rebase_path(proto_in_dir, root_build_dir),
    ]
    if (generate_cc) {
127 128 129 130
      cc_generator_options_ = ""
      if (defined(invoker.cc_generator_options)) {
        cc_generator_options_ = invoker.cc_generator_options
      }
131 132
      args += [
        "--cpp_out",
133
        cc_generator_options_ + rel_cc_out_dir,
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      ]
    }
    if (generate_descriptor != "") {
      args += [
        "--include_imports",
        "--descriptor_set_out",
        rebase_path("$root_gen_dir/" + generate_descriptor, root_build_dir),
      ]
    }

    if (generate_with_plugin) {
      plugin_path_rebased = rebase_path(plugin_path, root_build_dir)
      plugin_out_args = ""
      if (defined(invoker.generator_plugin_options)) {
        plugin_out_args += invoker.generator_plugin_options
      }
      plugin_out_args += ":$rel_cc_out_dir"

      args += [
        "--plugin=protoc-gen-plugin=$plugin_path_rebased",
        "--plugin_out=$plugin_out_args",
      ]
    }

    args += rebase_path(proto_sources, root_build_dir)

160
    inputs = [ protoc_path ]
161

162
    deps = [ protoc_label ]
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    if (generate_with_plugin) {
      inputs += [ plugin_path ]
      if (defined(plugin_host_label)) {
        # Action depends on native generator plugin but for host toolchain only.
        deps += [ plugin_host_label ]
      }
    }

    if (defined(invoker.proto_deps)) {
      deps += invoker.proto_deps
    }
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
  }  # action "${target_name}_gen"

  # The source_set that builds the generated .pb.cc files.
  source_set(target_name) {
    forward_variables_from(invoker,
                           [
                             "defines",
                             "include_dirs",
                             "public_configs",
                             "testonly",
                             "visibility",
                           ])

190 191
    # Exclude the config.descriptor file which is an output for some reason.
    set_sources_assignment_filter([ "*.descriptor" ])
192
    sources = get_target_outputs(":$action_name")
193
    set_sources_assignment_filter(sources_assignment_filter)
194 195 196 197 198 199 200 201 202 203

    # configs -= [ "//gn/standalone:extra_warnings" ]
    if (defined(invoker.extra_configs)) {
      configs += invoker.extra_configs
    }

    if (!defined(invoker.public_configs)) {
      public_configs = []
    }

204 205 206 207 208 209 210 211 212 213
    public_configs += [ "//:protobuf_gen_config" ]

    propagate_imports_configs = !defined(invoker.propagate_imports_configs) ||
                                invoker.propagate_imports_configs
    if (propagate_imports_configs) {
      public_configs += [ ":$config_name" ]
    } else {
      # Embedder handles include directory propagation to dependents.
      configs += [ ":$config_name" ]
    }
214 215 216 217

    # Use protobuf_full only for tests.
    if (defined(invoker.use_protobuf_full) &&
        invoker.use_protobuf_full == true) {
218
      deps = [ "//:protobuf_full" ]
219
    } else {
220
      deps = [ "//:protobuf_lite" ]
221 222 223 224 225 226 227 228 229 230 231
    }

    deps += [ ":$action_name" ]
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    if (defined(invoker.link_deps)) {
      deps += invoker.link_deps
    }
  }  # source_set(target_name)
}  # template