xref: /aosp_15_r20/external/openscreen/third_party/protobuf/proto_library.gni (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
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    if (defined(invoker.cc_generator_options)) {
62      args += [
63        "--cc-options",
64        invoker.cc_generator_options,
65      ]
66    }
67    inputs = [ protoc_path ]
68    deps = [ protoc_label ]
69  }
70
71  config_name = "${target_name}_config"
72  config(config_name) {
73    include_dirs = []
74  }
75
76  source_set(source_set_name) {
77    forward_variables_from(invoker,
78                           [
79                             "defines",
80                             "testonly",
81                             "visibility",
82                           ])
83
84    sources = get_path_info(protogens_cc, "abspath")
85    public_configs = [
86      "//third_party/protobuf:using_proto",
87      ":$config_name",
88    ]
89    public_deps = [ ":$action_name" ]
90    if (defined(invoker.use_protobuf_full) &&
91        invoker.use_protobuf_full == true) {
92      public_deps += [ "//third_party/protobuf:protobuf_full" ]
93    } else {
94      public_deps += [ "//third_party/protobuf:protobuf_lite" ]
95    }
96
97    deps = []
98    if (defined(invoker.deps)) {
99      deps += invoker.deps
100    }
101
102    if (defined(invoker.removed_configs)) {
103      configs -= invoker.removed_configs
104    }
105    if (defined(invoker.extra_configs)) {
106      configs += invoker.extra_configs
107    }
108  }
109}
110