xref: /aosp_15_r20/external/cronet/third_party/protobuf/BUILD.gn (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2014 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import("//build/config/linux/gtk/gtk.gni")
5import("//build/config/sanitizers/sanitizers.gni")
6
7# Includes default args like 'enable_js_protobuf'.
8import("proto_library.gni")
9import("proto_sources.gni")
10if (enable_js_protobuf) {
11  import("//third_party/closure_compiler/compile_js.gni")
12}
13
14config("protobuf_config") {
15  include_dirs = [ "src" ]
16  defines = [
17    "GOOGLE_PROTOBUF_NO_RTTI",
18    "GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER",
19    "GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE=0",
20  ]
21  if (!is_win) {
22    defines += [ "HAVE_PTHREAD" ]
23  }
24}
25
26config("protobuf_warnings") {
27  cflags = []
28  if (is_clang) {
29    # protobuf-3 contains a few functions that are unused.
30    cflags += [ "-Wno-unused-function" ]
31  }
32}
33
34config("protoc_warnings") {
35  cflags = []
36  if (is_clang) {
37    # Some generates contain a few fields that are not used.
38    cflags += [ "-Wno-unused-private-field" ]
39  }
40}
41
42if (is_component_build) {
43  config("protobuf_use_dlls") {
44    defines = [ "PROTOBUF_USE_DLLS" ]
45  }
46}
47
48# This config should be applied to targets using generated code from the proto
49# compiler. It sets up the include directories properly.
50config("using_proto") {
51  include_dirs = [
52    "src",
53    "$root_gen_dir/protoc_out",
54  ]
55}
56
57config("allow_deprecated_proto_fields") {
58  if (is_clang) {
59    cflags = [ "-DPROTOBUF_ALLOW_DEPRECATED=1" ]
60  }
61}
62
63protobuf_lite_cflags = []
64if (is_win) {
65  protobuf_lite_cflags = [
66    "/wd4018",  # signed/unsigned mismatch in comparison
67    "/wd4065",  # switch statement contains 'default' but no 'case' labels
68    "/wd4146",  # unary minus operator applied to unsigned type
69    "/wd4244",  # implicit conversion, possible loss of data
70    "/wd4267",  # size_t to int truncation
71    "/wd4291",  # no matching operator delete for a placement new.
72    "/wd4305",  # double to float truncation
73    "/wd4355",  # 'this' used in base member initializer list
74    "/wd4506",  # no definition for inline function (protobuf issue #240)
75    "/wd4715",  # not all control paths return a value (fixed in trunk)
76  ]
77}
78
79component("protobuf_lite") {
80  sources = protobuf_lite_sources + protobuf_headers
81
82  configs -= [ "//build/config/compiler:chromium_code" ]
83  configs += [
84    "//build/config/compiler:no_chromium_code",
85
86    # Must be after no_chromium_code for warning flags to be ordered
87    # correctly.
88    ":protobuf_warnings",
89  ]
90
91  # Build protobuf_lite with full optimizations so Clang can optimize the
92  # initializer out. See 0029-make-initializers-optimizable.patch.
93  if (!is_debug && is_android) {
94    configs -= [ "//build/config/compiler:default_optimization" ]
95    configs += [ "//build/config/compiler:optimize_max" ]
96  }
97
98  # Remove coverage and Sanitizers other than ASan for a performance boost when
99  # fuzzing. ASan can't be removed here because of a bug preventing unsanitized
100  # code from using libc++, which protobuf_full uses.
101  configs -= not_fuzzed_remove_nonasan_configs
102  configs += [ "//build/config/sanitizers:not_fuzzed" ]
103
104  if (is_win) {
105    configs -= [ "//build/config/win:lean_and_mean" ]
106  }
107
108  public_configs = [ ":protobuf_config" ]
109
110  if (is_android) {
111    libs = [ "log" ]  # Used by src/google/protobuf/stubs/common.cc
112  }
113
114  cflags = protobuf_lite_cflags
115
116  # Required for component builds. See http://crbug.com/172800.
117  if (is_component_build) {
118    public_configs += [ ":protobuf_use_dlls" ]
119    defines = [ "LIBPROTOBUF_EXPORTS" ]
120  }
121}
122
123# This is the full, heavy protobuf lib that's needed for c++ .protos that don't
124# specify the LITE_RUNTIME option. The protocol compiler itself (protoc) falls
125# into that category. Do not use in Chrome code.
126static_library("protobuf_full") {
127  if (defined(build_with_chromium) && build_with_chromium) {
128    # Prevent people from depending on this outside our file.
129    visibility = [
130      ":*",
131
132      # Used for testing protobuf generation.
133      "//base/test:proto_test_support",
134
135      # requires descriptors & reflection; testonly.
136      "//third_party/libprotobuf-mutator:*",
137
138      # Chromecast requires descriptors and reflection.
139      "//chromecast/*",
140
141      # libassistant requires descriptors and reflection for testing.
142      "//libassistant/*",
143
144      # Perfetto uses the full library for testing.
145      "//third_party/perfetto/gn:protobuf_full",
146
147      # Some tests inside ChromeOS need reflection to parse golden files.
148      # Not included in production code.
149      "//chrome/test:usage_time_limit_unittests",
150
151      # The protobuf-based SQLite and GPU fuzzers need protobuf_full and are not
152      # included in Chrome.
153      "//gpu:gl_lpm_fuzzer_proto",
154      "//gpu:gl_lpm_fuzzer_proto_gen",
155      "//gpu:gl_lpm_shader_to_string_unittest",
156      "//testing/libfuzzer/fuzzers:command_buffer_lpm_fuzzer_proto",
157      "//testing/libfuzzer/fuzzers:command_buffer_lpm_fuzzer_proto_gen",
158      "//third_party/sqlite:sqlite3_lpm_corpus_gen",
159
160      # The protobuf-based Mojo LPM fuzzer needs protobuf_full and is not included
161      # in Chrome.
162      "//mojo/public/tools/fuzzers:mojolpm",
163
164      # The root store tool is not part of Chrome itself, and needs to parse
165      # human-readable protobufs. Protobuf is stored in //net/cert however as
166      # browser needs to be able to parse serialized protobuf (which is exposed
167      # as a separate lite BUILD rule).
168      "//net/cert:root_store_proto_full",
169
170      # The spirv-fuzz fuzzer tool needs protobuf_full and is not included in
171      # Chrome.
172      "//third_party/vulkan-deps/spirv-tools/src:spirv-fuzz",
173      "//third_party/vulkan-deps/spirv-tools/src:spvtools_fuzz",
174      "//third_party/vulkan-deps/spirv-tools/src:spvtools_fuzz_proto",
175
176      # Some fuzzers for tint need protobuf_full and are not included in Chrome.
177      "//third_party/dawn/src/tint/*",
178
179      # Dawn LPM Fuzzers
180      "//third_party/dawn/src/dawn/fuzzers:dawn_lpm_proto",
181      "//third_party/dawn/src/dawn/fuzzers:dawn_lpm_proto_gen",
182
183      # The Cast Core gRPC generator tool.
184      "//third_party/cast_core/public/src/build/chromium:cast_core_grpc_generator",
185    ]
186  }
187
188  deps = [ "//third_party/zlib" ]
189
190  # In component build, protobuf_full can't depend on protobuf_lite because
191  # it uses non-PROTOBUF_EXPORT symbols; in non-component build, protobuf_full
192  # must have protobuf_lite as a dependency instead of building
193  # protobuf_lite_sources to avoid ODR violations in targets that link both.
194  # See crbug.com/1338164.
195  if (is_component_build) {
196    sources = protobuf_lite_sources + protobuf_sources + protobuf_headers
197  } else {
198    sources = protobuf_sources + protobuf_headers
199    deps += [ ":protobuf_lite" ]
200  }
201
202  if (is_android) {
203    libs = [ "log" ]  # Used by src/google/protobuf/stubs/common.cc
204  }
205
206  configs -= [ "//build/config/compiler:chromium_code" ]
207  configs += [
208    "//build/config/compiler:no_chromium_code",
209
210    # Must be after no_chromium_code for warning flags to be ordered
211    # correctly.
212    ":protobuf_warnings",
213  ]
214
215  # Remove coverage and Sanitizers other than ASan for a performance boost when
216  # fuzzing. ASan can't be removed here because of a bug preventing unsanitized
217  # code from using libc++, which protobuf_full uses.
218  configs -= not_fuzzed_remove_nonasan_configs
219  configs += [ "//build/config/sanitizers:not_fuzzed" ]
220
221  if (is_win) {
222    configs -= [ "//build/config/win:lean_and_mean" ]
223  }
224  public_configs = [ ":protobuf_config" ]
225
226  cflags = protobuf_lite_cflags
227
228  defines = [ "HAVE_ZLIB" ]
229}
230
231# Only compile the compiler for the host architecture.
232if (current_toolchain == host_toolchain) {
233  # protoc compiler is separated into protoc library and executable targets to
234  # support protoc plugins that need to link libprotoc, but not the main()
235  # itself. See src/google/protobuf/compiler/plugin.h
236  source_set("protoc_java") {
237    sources = protoc_java_sources + protoc_java_headers
238    configs -= [ "//build/config/compiler:chromium_code" ]
239    configs += [
240      "//build/config/compiler:no_chromium_code",
241
242      # Must be after no_chromium_code for warning flags to be ordered
243      # correctly.
244      ":protobuf_warnings",
245      ":protoc_warnings",
246    ]
247
248    public_configs = [ ":protobuf_config" ]
249
250    cflags = protobuf_lite_cflags
251
252    public_deps = [ ":protobuf_full" ]
253  }
254  source_set("protoc_python") {
255    sources = protoc_python_sources + protoc_python_headers
256    configs -= [ "//build/config/compiler:chromium_code" ]
257    configs += [
258      "//build/config/compiler:no_chromium_code",
259
260      # Must be after no_chromium_code for warning flags to be ordered
261      # correctly.
262      ":protobuf_warnings",
263      ":protoc_warnings",
264    ]
265
266    public_configs = [ ":protobuf_config" ]
267
268    cflags = protobuf_lite_cflags
269
270    public_deps = [ ":protobuf_full" ]
271  }
272  static_library("protoc_lib") {
273    sources = protoc_sources + protoc_headers
274
275    configs -= [ "//build/config/compiler:chromium_code" ]
276    configs += [
277      "//build/config/compiler:no_chromium_code",
278
279      # Must be after no_chromium_code for warning flags to be ordered
280      # correctly.
281      ":protobuf_warnings",
282      ":protoc_warnings",
283    ]
284    if (is_win) {
285      # This is defined internally, don't warn on duplicate.
286      configs -= [ "//build/config/win:lean_and_mean" ]
287    }
288
289    public_configs = [ ":protobuf_config" ]
290
291    cflags = protobuf_lite_cflags
292
293    public_deps = [ ":protobuf_full" ]
294    deps = [
295      ":protoc_java",
296      ":protoc_python",
297    ]
298  }
299
300  executable("protoc") {
301    sources = [ "src/google/protobuf/compiler/main.cc" ]
302
303    configs -= [ "//build/config/compiler:chromium_code" ]
304    configs += [ "//build/config/compiler:no_chromium_code" ]
305
306    cflags = protobuf_lite_cflags
307
308    deps = [
309      ":protoc_lib",
310
311      # Default manifest on Windows (a no-op elsewhere).
312      "//build/win:default_exe_manifest",
313    ]
314    if (enable_js_protobuf) {
315      deps += [ "//third_party/protobuf-javascript:protoc-gen-js" ]
316    }
317  }
318}
319
320google_python_dir = "$root_out_dir/pyproto/google"
321
322copy("copy_google_protobuf") {
323  # TODO(ncarter): protoc's python generator treats descriptor.proto
324  # specially, but only when the input path is exactly
325  # "google/protobuf/descriptor.proto".  I'm not sure how to execute a rule
326  # from a different directory.  For now, use a manually-generated copy of
327  # descriptor_pb2.py.
328  sources = pyproto_sources + [ "python/google/protobuf/descriptor_pb2.py" ]
329  outputs = [ "$google_python_dir/protobuf/{{source_file_part}}" ]
330}
331
332copy("copy_google_protobuf_internal") {
333  sources = pyproto_internal_sources
334  outputs = [ "$google_python_dir/protobuf/internal/{{source_file_part}}" ]
335}
336
337# Build time dependency for action rules.
338group("py_proto") {
339  public_deps = [
340    ":copy_google_protobuf",
341    ":copy_google_protobuf_internal",
342  ]
343}
344
345# Runtime dependency if the target needs the python scripts.
346group("py_proto_runtime") {
347  deps = [ ":py_proto" ]
348
349  # Targets that depend on this should depend on the copied data files.
350  data = get_target_outputs(":copy_google_protobuf")
351  data += get_target_outputs(":copy_google_protobuf_internal")
352}
353
354# JS protobuf library.
355if (enable_js_protobuf) {
356  js_library("js_proto") {
357    sources = [
358      "//third_party/google-closure-library/closure/goog/array/array.js",
359      "//third_party/google-closure-library/closure/goog/asserts/asserts.js",
360      "//third_party/google-closure-library/closure/goog/base.js",
361      "//third_party/google-closure-library/closure/goog/crypt/base64.js",
362      "//third_party/google-closure-library/closure/goog/crypt/crypt.js",
363      "//third_party/google-closure-library/closure/goog/debug/error.js",
364      "//third_party/google-closure-library/closure/goog/dom/asserts.js",
365      "//third_party/google-closure-library/closure/goog/dom/browserfeature.js",
366      "//third_party/google-closure-library/closure/goog/dom/dom.js",
367      "//third_party/google-closure-library/closure/goog/dom/htmlelement.js",
368      "//third_party/google-closure-library/closure/goog/dom/nodetype.js",
369      "//third_party/google-closure-library/closure/goog/dom/safe.js",
370      "//third_party/google-closure-library/closure/goog/dom/tagname.js",
371      "//third_party/google-closure-library/closure/goog/dom/tags.js",
372      "//third_party/google-closure-library/closure/goog/fs/blob.js",
373      "//third_party/google-closure-library/closure/goog/fs/url.js",
374      "//third_party/google-closure-library/closure/goog/functions/functions.js",
375      "//third_party/google-closure-library/closure/goog/goog.js",
376      "//third_party/google-closure-library/closure/goog/html/safehtml.js",
377      "//third_party/google-closure-library/closure/goog/html/safescript.js",
378      "//third_party/google-closure-library/closure/goog/html/safestyle.js",
379      "//third_party/google-closure-library/closure/goog/html/safestylesheet.js",
380      "//third_party/google-closure-library/closure/goog/html/safeurl.js",
381      "//third_party/google-closure-library/closure/goog/html/trustedresourceurl.js",
382      "//third_party/google-closure-library/closure/goog/html/trustedtypes.js",
383      "//third_party/google-closure-library/closure/goog/html/uncheckedconversions.js",
384      "//third_party/google-closure-library/closure/goog/i18n/bidi.js",
385      "//third_party/google-closure-library/closure/goog/labs/useragent/browser.js",
386      "//third_party/google-closure-library/closure/goog/labs/useragent/engine.js",
387      "//third_party/google-closure-library/closure/goog/labs/useragent/platform.js",
388      "//third_party/google-closure-library/closure/goog/labs/useragent/useragent.js",
389      "//third_party/google-closure-library/closure/goog/labs/useragent/util.js",
390      "//third_party/google-closure-library/closure/goog/math/coordinate.js",
391      "//third_party/google-closure-library/closure/goog/math/math.js",
392      "//third_party/google-closure-library/closure/goog/math/size.js",
393      "//third_party/google-closure-library/closure/goog/memoize/memoize.js",
394      "//third_party/google-closure-library/closure/goog/object/object.js",
395      "//third_party/google-closure-library/closure/goog/reflect/reflect.js",
396      "//third_party/google-closure-library/closure/goog/string/const.js",
397      "//third_party/google-closure-library/closure/goog/string/internal.js",
398      "//third_party/google-closure-library/closure/goog/string/string.js",
399      "//third_party/google-closure-library/closure/goog/string/typedstring.js",
400      "//third_party/google-closure-library/closure/goog/useragent/product.js",
401      "//third_party/google-closure-library/closure/goog/useragent/useragent.js",
402      "//third_party/protobuf-javascript/src/asserts.js",
403      "//third_party/protobuf-javascript/src/binary/arith.js",
404      "//third_party/protobuf-javascript/src/binary/constants.js",
405      "//third_party/protobuf-javascript/src/binary/decoder.js",
406      "//third_party/protobuf-javascript/src/binary/encoder.js",
407      "//third_party/protobuf-javascript/src/binary/reader.js",
408      "//third_party/protobuf-javascript/src/binary/utils.js",
409      "//third_party/protobuf-javascript/src/binary/writer.js",
410      "//third_party/protobuf-javascript/src/map.js",
411      "//third_party/protobuf-javascript/src/message.js",
412    ]
413  }
414}
415