xref: /aosp_15_r20/external/openscreen/third_party/libprotobuf-mutator/fuzzable_proto_library.gni (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1# Copyright 2020 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.
4
5# A fuzzable_proto_library is a proto_library that is the same as any other in
6# non-fuzzer builds (ie: use_libfuzzer=false). However, in fuzzer builds, the
7# proto_library is built with the full protobuf runtime and any "optimize_for =
8# LITE_RUNTIME" options are ignored. This is done because libprotobuf-mutator
9# needs the full protobuf runtime, but proto_libraries shipped in Chrome must
10# use the optimize for LITE_RUNTIME option which is incompatible with the full
11# protobuf runtime. tl;dr: A fuzzable_proto_library is a proto_library that can
12# be fuzzed with libprotobuf-mutator and shipped in Chrome.
13
14import("//build_overrides/build.gni")
15import("//testing/libfuzzer/fuzzer_test.gni")
16import("//third_party/protobuf/proto_library.gni")
17
18template("fuzzable_proto_library") {
19  if (use_libfuzzer) {
20    proto_library("proto_library_" + target_name) {
21      forward_variables_from(invoker, "*")
22      assert(current_toolchain == host_toolchain)
23
24      cc_generator_options = "speed"
25      extra_configs = [ "//third_party/protobuf:protobuf_config" ]
26    }
27
28    # Inspired by proto_library.gni's handling of
29    # component_build_force_source_set.
30    if (defined(component_build_force_source_set) &&
31        component_build_force_source_set && is_component_build) {
32      link_target_type = "source_set"
33    } else {
34      link_target_type = "static_library"
35    }
36
37    # By making target a static_library or source_set, we can add protobuf_full
38    # to public_deps.
39    target(link_target_type, target_name) {
40      if (defined(invoker.testonly)) {
41        testonly = invoker.testonly
42      }
43      sources = [ "//third_party/libprotobuf-mutator/dummy.cc" ]
44      public_deps = [
45        ":proto_library_" + target_name,
46        "//third_party/libprotobuf-mutator:protobuf_full",
47      ]
48    }
49  } else {
50    # fuzzable_proto_library should behave like a proto_library when
51    # !use_libfuzzer.
52    proto_library(target_name) {
53      forward_variables_from(invoker, "*")
54    }
55  }
56}
57