aboutsummaryrefslogtreecommitdiff
path: root/third_party/protobuf/proto_library.gni
blob: 9c5504575e7aee0c8bd066b85cfc7239c25b316f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
51
52
53
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
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

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

  proto_in_dir = rebase_path(get_path_info(proto_sources[0], "dir"), ".")

  if (defined(invoker.proto_out_dir)) {
    proto_out_dir = rebase_path(invoker.proto_out_dir, "//")
  } else {
    # Absolute path to the directory of current BUILD.gn file excluding "//".
    proto_out_dir = rebase_path(".", "//")
    if (proto_in_dir != ".") {
      proto_out_dir += "/$proto_in_dir"
    }
  }
  cc_out_dir = "$root_gen_dir/" + proto_out_dir
  rel_cc_out_dir = rebase_path(cc_out_dir, root_build_dir)

  protos = rebase_path(invoker.sources, proto_in_dir)
  protogens_cc = []

  # List output files.
  foreach(proto, protos) {
    proto_dir = get_path_info(proto, "dir")
    proto_name = get_path_info(proto, "name")
    proto_path = proto_dir + "/" + proto_name

    protogens_cc += [
      "$cc_out_dir/$proto_path.pb.h",
      "$cc_out_dir/$proto_path.pb.cc",
    ]
  }

  action_name = "${target_name}_gen"
  source_set_name = "$target_name"
  action(action_name) {
    visibility = [ ":$source_set_name" ]
    script = "//third_party/protobuf/protoc_wrapper.py"
    sources = proto_sources
    outputs = get_path_info(protogens_cc, "abspath")
    args = protos

    protoc_label = "//third_party/protobuf:protoc($host_toolchain)"
    protoc_path = get_label_info(protoc_label, "root_out_dir") + "/protoc"
    args += [
      # Wrapper should never pick a system protoc.
      # Path should be rebased because |root_build_dir| for current toolchain
      # may be different from |root_out_dir| of protoc built on host toolchain.
      "--protoc",
      "./" + rebase_path(protoc_path, root_build_dir),
      "--proto-in-dir",
      rebase_path(proto_in_dir, root_build_dir),
      "--cc-out-dir",
      rel_cc_out_dir,
    ]

    if (defined(invoker.cc_generator_options)) {
      args += [
        "--cc-options",
        invoker.cc_generator_options,
      ]
    }
    inputs = [ protoc_path ]
    deps = [ protoc_label ]
  }

  config_name = "${target_name}_config"
  config(config_name) {
    include_dirs = []
  }

  source_set(source_set_name) {
    forward_variables_from(invoker,
                           [
                             "defines",
                             "testonly",
                             "visibility",
                           ])

    sources = get_path_info(protogens_cc, "abspath")
    public_configs = [
      "//third_party/protobuf:using_proto",
      ":$config_name",
    ]
    public_deps = [ ":$action_name" ]
    if (defined(invoker.use_protobuf_full) &&
        invoker.use_protobuf_full == true) {
      public_deps += [ "//third_party/protobuf:protobuf_full" ]
    } else {
      public_deps += [ "//third_party/protobuf:protobuf_lite" ]
    }

    deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    if (defined(invoker.removed_configs)) {
      configs -= invoker.removed_configs
    }
    if (defined(invoker.extra_configs)) {
      configs += invoker.extra_configs
    }
  }
}