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.
7 # The 'proto_in_dir' variable must be the relative path to the
8 # directory containing the .proto files. If left out, it defaults to '.'.
10 # The 'proto_out_dir' variable specifies the path suffix that output files are
11 # generated under. Targets that gyp-depend on my_proto_lib will be able to
12 # include the resulting proto headers with an include like:
13 # #include "dir/for/my_proto_lib/foo.pb.h"
14 # If undefined, this defaults to matching the input directory.
16 # If you need to add an EXPORT macro to a protobuf's C++ header, set the
17 # 'cc_generator_options' variable with the value: 'dllexport_decl=FOO_EXPORT:'
18 # e.g. 'dllexport_decl=BASE_EXPORT:'
20 # It is likely you also need to #include a file for the above EXPORT macro to
21 # work. You can do so with the 'cc_include' variable.
22 # e.g. 'base/base_export.h'
25 # proto_library("mylib") {
31 template("proto_library") {
32 assert(defined(invoker.sources), "Need sources for proto_library")
34 action_name = "${target_name}_gen"
35 source_set_name = target_name
36 action_foreach(action_name) {
37 visibility = ":$source_set_name"
39 script = "//tools/protoc_wrapper/protoc_wrapper.py"
41 sources = invoker.sources
43 # TODO(brettw) it would be better if this used the target gen dir.
44 if (defined(invoker.proto_out_dir)) {
45 proto_out_dir = invoker.proto_out_dir
47 # This computes the relative path inside the target_gen_dir that
48 # we'd put the files in, which maps to the current directory path.
49 # We'll insert "protoc_out" at the beginning for compatibility with GYP.
50 proto_out_dir = rebase_path(target_gen_dir, root_gen_dir)
52 cc_dir = "$root_gen_dir/protoc_out/$proto_out_dir"
53 py_dir = "$root_gen_dir/pyproto/$proto_out_dir"
56 "$py_dir/{{source_name_part}}_pb2.py",
57 "$cc_dir/{{source_name_part}}.pb.cc",
58 "$cc_dir/{{source_name_part}}.pb.h",
62 if (defined(invoker.cc_include)) {
63 args += [ "--include", invoker.cc_include ]
68 rebase_path("$cc_dir/{{source_name_part}}.pb.h", root_build_dir),
71 if (defined(invoker.proto_in_dir)) {
72 proto_in_dir = invoker.proto_in_dir
74 # Extract the current source dir.
75 proto_in_dir = get_label_info(":$target_name", "dir")
79 rebase_path(proto_in_dir, root_build_dir),
80 "--proto-in-file", "{{source_file_part}}",
81 # TODO(brettw) support system protobuf compiler.
82 "--use-system-protobuf=0",
85 protoc_label = "//third_party/protobuf:protoc($host_toolchain)"
88 # Prepend with "./" so this will never pick up the system one (normally
89 # when not cross-compiling, protoc's output directory will be the same
90 # as the build dir, so the relative location will be empty.
91 "./" + rebase_path(get_label_info(protoc_label, "root_out_dir") +
92 "/protoc", root_build_dir),
95 # If passed cc_generator_options should end in a colon, which will separate
96 # it from the directory when we concatenate them. The proto compiler
97 # understands this syntax.
98 if (defined(invoker.cc_generator_options)) {
99 cc_generator_options = invoker.cc_generator_options
101 cc_generator_options = ""
104 "--cpp_out", cc_generator_options + rebase_path(cc_dir, root_build_dir),
105 "--python_out", rebase_path(py_dir, root_build_dir),
108 deps = [ protoc_label ]
111 source_set(target_name) {
112 if (defined(invoker.visibility)) {
113 visibility = invoker.visibility
116 sources = get_target_outputs(":$action_name")
118 direct_dependent_configs = [ "//third_party/protobuf:using_proto" ]
122 "//third_party/protobuf:protobuf_lite",