1# Copyright 2019 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5template("proto_library") {
6  assert(defined(invoker.sources), "Need sources for proto_library")
7  proto_sources = invoker.sources
8
9  proto_in_dir = rebase_path(get_path_info(proto_sources[0], "dir"), ".")
10
11  if (defined(invoker.proto_out_dir)) {
12    proto_out_dir = rebase_path(invoker.proto_out_dir, "//")
13  } else {
14    # Absolute path to the directory of current BUILD.gn file excluding "//".
15    proto_out_dir = rebase_path(".", "//")
16    if (proto_in_dir != ".") {
17      proto_out_dir += "/$proto_in_dir"
18    }
19  }
20  cc_out_dir = "$root_gen_dir/" + proto_out_dir
21  rel_cc_out_dir = rebase_path(cc_out_dir, root_build_dir)
22
23  protos = rebase_path(invoker.sources, proto_in_dir)
24  protogens_cc = []
25
26  # List output files.
27  foreach(proto, protos) {
28    proto_dir = get_path_info(proto, "dir")
29    proto_name = get_path_info(proto, "name")
30    proto_path = proto_dir + "/" + proto_name
31
32    protogens_cc += [
33      "$cc_out_dir/$proto_path.pb.h",
34      "$cc_out_dir/$proto_path.pb.cc",
35    ]
36  }
37
38  action_name = "${target_name}_gen"
39  source_set_name = "$target_name"
40  action(action_name) {
41    visibility = [ ":$source_set_name" ]
42    script = "//third_party/protobuf/protoc_wrapper.py"
43    sources = proto_sources
44    outputs = get_path_info(protogens_cc, "abspath")
45    args = protos
46
47    protoc_label = "//third_party/protobuf:protoc($host_toolchain)"
48    protoc_path = get_label_info(protoc_label, "root_out_dir") + "/protoc"
49    args += [
50      # Wrapper should never pick a system protoc.
51      # Path should be rebased because |root_build_dir| for current toolchain
52      # may be different from |root_out_dir| of protoc built on host toolchain.
53      "--protoc",
54      "./" + rebase_path(protoc_path, root_build_dir),
55      "--proto-in-dir",
56      rebase_path(proto_in_dir, root_build_dir),
57      "--cc-out-dir",
58      rel_cc_out_dir,
59    ]
60
61    inputs = [ protoc_path ]
62    deps = [ protoc_label ]
63  }
64
65  config_name = "${target_name}_config"
66  config(config_name) {
67    include_dirs = []
68  }
69
70  source_set(source_set_name) {
71    forward_variables_from(invoker,
72                           [
73                             "defines",
74                             "testonly",
75                             "visibility",
76                           ])
77
78    sources = get_path_info(protogens_cc, "abspath")
79    public_configs = [
80      "//third_party/protobuf:using_proto",
81      ":$config_name",
82    ]
83    public_deps = [ ":$action_name" ]
84    if (defined(invoker.use_protobuf_full) &&
85        invoker.use_protobuf_full == true) {
86      public_deps += [ "//third_party/protobuf:protobuf_full" ]
87    } else {
88      public_deps += [ "//third_party/protobuf:protobuf_lite" ]
89    }
90
91    deps = []
92    if (defined(invoker.deps)) {
93      deps += invoker.deps
94    }
95
96    if (defined(invoker.removed_configs)) {
97      configs -= invoker.removed_configs
98    }
99    if (defined(invoker.extra_configs)) {
100      configs += invoker.extra_configs
101    }
102  }
103}
104