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  # Only make the proto library fuzzable if we are doing a build that we can
20  # use LPM on (i.e. libFuzzer not on Chrome OS).
21  if (use_libfuzzer && current_toolchain != "//build/toolchain/cros:target") {
22    proto_library("proto_library_" + target_name) {
23      forward_variables_from(invoker, "*")
24      assert(current_toolchain == host_toolchain)
25      if (!defined(proto_deps)) {
26        proto_deps = []
27      }
28      proto_deps +=
29          [ "//third_party/libprotobuf-mutator:override_lite_runtime_plugin" ]
30
31      extra_configs = [ "//third_party/protobuf:protobuf_config" ]
32    }
33
34    # Inspired by proto_library.gni's handling of
35    # component_build_force_source_set.
36    if (defined(component_build_force_source_set) &&
37        component_build_force_source_set && is_component_build) {
38      link_target_type = "source_set"
39    } else {
40      link_target_type = "static_library"
41    }
42
43    # By making target a static_library or source_set, we can add protobuf_full
44    # to public_deps.
45    target(link_target_type, target_name) {
46      if (defined(invoker.testonly)) {
47        testonly = invoker.testonly
48      }
49      sources = [ "//third_party/libprotobuf-mutator/dummy.cc" ]
50      public_deps = [
51        ":proto_library_" + target_name,
52        "//third_party/libprotobuf-mutator:protobuf_full",
53      ]
54    }
55  } else {
56    # fuzzable_proto_library should behave like a proto_library when
57    # !use_libfuzzer.
58    proto_library(target_name) {
59      forward_variables_from(invoker, "*")
60    }
61  }
62}
63