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)
77 py_out_dir = "$root_out_dir/pyproto/" + invoker.proto_out_dir
79 # Use the gen directory corresponding to the source file for C++ sources.
80 # This expansion will be done differently in the outputs and the args, so
81 # we don't need to worry about rebasing as above. Always put Python
82 # sources in "pyproto".
83 out_dir = "{{source_gen_dir}}"
84 rel_out_dir = "{{source_gen_dir}}"
85 py_out_dir = "$root_out_dir/pyproto/{{source_root_relative_dir}}"
87 rel_py_out_dir = rebase_path(py_out_dir, root_build_dir)
90 "$py_out_dir/{{source_name_part}}_pb2.py",
91 "$out_dir/{{source_name_part}}.pb.cc",
92 "$out_dir/{{source_name_part}}.pb.h",
96 if (defined(invoker.cc_include)) {
105 "$rel_out_dir/{{source_name_part}}.pb.h",
109 "{{source_file_part}}",
111 # TODO(brettw) support system protobuf compiler.
112 "--use-system-protobuf=0",
115 protoc_label = "//third_party/protobuf:protoc($host_toolchain)"
119 # Prepend with "./" so this will never pick up the system one (normally
120 # when not cross-compiling, protoc's output directory will be the same
121 # as the build dir, so the relative location will be empty).
123 rebase_path(get_label_info(protoc_label, "root_out_dir") + "/protoc",
127 # If passed cc_generator_options should end in a colon, which will separate
128 # it from the directory when we concatenate them. The proto compiler
129 # understands this syntax.
130 if (defined(invoker.cc_generator_options)) {
131 cc_generator_options = invoker.cc_generator_options
133 cc_generator_options = ""
136 # cc_generator_options is supposed to end in a colon if it's nonempty.
138 "$cc_generator_options$rel_out_dir",
146 if (defined(invoker.deps)) {
151 source_set(target_name) {
152 forward_variables_from(invoker,
158 sources = get_target_outputs(":$action_name")
160 if (defined(invoker.extra_configs)) {
161 configs += invoker.extra_configs
164 public_configs = [ "//third_party/protobuf:using_proto" ]
167 # The generated headers reference headers within protobuf_lite, so
168 # dependencies must be able to find those headers too.
169 "//third_party/protobuf:protobuf_lite",