• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/error.gni")
18import("$dir_pw_toolchain/host_clang/toolchains.gni")
19import("$dir_pw_unit_test/test.gni")
20
21# Creates a libFuzzer-based fuzzer executable target and unit test
22#
23# This will link `sources` and `deps` with the libFuzzer compiler runtime. The
24# `sources` and `deps` should include a definition of the standard LLVM fuzz
25# target function, `LLVMFuzzerTestOneInput`. For more details, see:
26#   //pw_fuzzer/docs.rst
27#   https://llvm.org/docs/LibFuzzer.html
28#
29# Additionally, this creates a unit test that does not generate fuzzer inputs
30# and simply executes the fuzz target function with fixed inputs. This is useful
31# for verifying the fuzz target function compiles, links, and runs even when not
32# using a fuzzing-capable host or toolchain.
33#
34# Args:
35#   - enable_test_if: (optional) Passed as `enable_if` to the unit test.
36#   Remaining arguments are the same as `pw_executable`.
37#
38template("pw_fuzzer") {
39  if (!pw_toolchain_FUZZING_ENABLED) {
40    pw_error(target_name) {
41      message_lines = [ "Toolchain does not enable fuzzing." ]
42    }
43    not_needed(invoker, "*")
44  } else if (pw_toolchain_SANITIZERS == []) {
45    pw_error(target_name) {
46      message_lines = [ "No sanitizer runtime set." ]
47    }
48    not_needed(invoker, "*")
49  } else {
50    pw_executable(target_name) {
51      configs = []
52      deps = []
53      forward_variables_from(invoker,
54                             "*",
55                             [
56                               "enable_test_if",
57                               "visibility",
58                             ])
59      forward_variables_from(invoker, [ "visibility" ])
60      configs += [ "$dir_pw_fuzzer:engine" ]
61      deps += [ dir_pw_fuzzer ]
62    }
63  }
64
65  pw_test("${target_name}_test") {
66    deps = []
67    forward_variables_from(invoker, "*", [ "visibility" ])
68    forward_variables_from(invoker, [ "visibility" ])
69    deps += [ "$dir_pw_fuzzer:run_as_unit_test" ]
70    enable_if = !defined(enable_test_if) || enable_test_if
71  }
72}
73