1 # Copyright 2014 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.
5 # Compile a protocol buffer.
9 # proto_out_dir (optional)
10 # Specifies the path suffix that output files are generated under. This
11 # path will be appended to the root_gen_dir.
13 # Targets that depend on the proto target will be able to include the
14 # resulting proto headers with an include like:
15 # #include "dir/for/my_proto_lib/foo.pb.h"
16 # If undefined, this defaults to matching the input directory for each
17 # .proto file (you should almost always use the default mode).
19 # cc_generator_options (optional)
20 # List of extra flags passed to the protocol compiler. If you need to
21 # add an EXPORT macro to a protobuf's C++ header, set the
22 # 'cc_generator_options' variable with the value:
23 # 'dllexport_decl=FOO_EXPORT:' (note trailing colon).
25 # It is likely you also need to #include a file for the above EXPORT
26 # macro to work. See cc_include.
28 # cc_include (optional)
29 # String listing an extra include that should be passed.
30 # Example: cc_include = "foo/bar.h"
33 # Additional dependencies.
35 # Parameters for compiling the generated code:
38 # Defines to supply to the source set that compiles the generated source
41 # extra_configs (optional)
42 # A list of config labels that will be appended to the configs applying
46 # proto_library("mylib") {
52 template("proto_library") {
53 assert(defined(invoker.sources), "Need sources for proto_library")
55 # Don't apply OS-specific sources filtering to the assignments later on.
56 # Platform files should have gotten filtered out in the sources assignment
57 # when this template was invoked. If they weren't, it was on purpose and
58 # this template shouldn't re-apply the filter.
59 set_sources_assignment_filter([])
61 action_name = "${target_name}_gen"
62 source_set_name = target_name
63 action_foreach(action_name) {
64 visibility = [ ":$source_set_name" ]
66 script = "//tools/protoc_wrapper/protoc_wrapper.py"
68 sources = invoker.sources
70 # Compute the output directory, both relative to the source root (for
71 # declaring "outputs") and relative to the build dir (for passing to the
73 if (defined(invoker.proto_out_dir)) {
74 # Put the results in the specified dir in the gen tree.
75 out_dir = "$root_gen_dir/" + invoker.proto_out_dir
76 rel_out_dir = rebase_path(out_dir, root_build_dir)
78 # Use the gen directory corresponding to the source file. This expansion
79 # will be done differently in the outputs and the args, so we don't need
80 # to worry about rebasing as above.
81 out_dir = "{{source_gen_dir}}"
82 rel_out_dir = "{{source_gen_dir}}"
86 "$out_dir/{{source_name_part}}_pb2.py",
87 "$out_dir/{{source_name_part}}.pb.cc",
88 "$out_dir/{{source_name_part}}.pb.h",
92 if (defined(invoker.cc_include)) {
101 "$rel_out_dir/{{source_name_part}}.pb.h",
105 "{{source_file_part}}",
107 # TODO(brettw) support system protobuf compiler.
108 "--use-system-protobuf=0",
111 protoc_label = "//third_party/protobuf:protoc($host_toolchain)"
115 # Prepend with "./" so this will never pick up the system one (normally
116 # when not cross-compiling, protoc's output directory will be the same
117 # as the build dir, so the relative location will be empty).
119 rebase_path(get_label_info(protoc_label, "root_out_dir") + "/protoc",
123 # If passed cc_generator_options should end in a colon, which will separate
124 # it from the directory when we concatenate them. The proto compiler
125 # understands this syntax.
126 if (defined(invoker.cc_generator_options)) {
127 cc_generator_options = invoker.cc_generator_options
129 cc_generator_options = ""
132 # cc_generator_options is supposed to end in a colon if it's nonempty.
134 "$cc_generator_options$rel_out_dir",
143 if (defined(invoker.deps)) {
148 source_set(target_name) {
149 if (defined(invoker.visibility)) {
150 visibility = invoker.visibility
153 sources = get_target_outputs(":$action_name")
155 if (defined(invoker.defines)) {
156 defines = invoker.defines
158 if (defined(invoker.extra_configs)) {
159 configs += invoker.extra_configs
162 public_configs = [ "//third_party/protobuf:using_proto" ]
165 # The generated headers reference headers within protobuf_lite, so
166 # dependencies must be able to find those headers too.
167 "//third_party/protobuf:protobuf_lite",