blob: 776fa5d5c068f856d61798a6d2c3ca65fca49009 [file] [log] [blame]
Aaron Greenf3c3d2b2020-04-02 23:12:31 -07001# 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
Armando Montanezfb3d3fb2020-06-09 18:12:12 -070015import("//build_overrides/pigweed.gni")
16
Alexei Frolov844ff0f2020-05-06 12:15:29 -070017import("$dir_pw_toolchain/host_clang/toolchains.gni")
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070018import("$dir_pw_unit_test/test.gni")
19
20# Creates a libFuzzer-based fuzzer executable target.
21#
22# This will link `sources` and `deps` with the libFuzzer compiler runtime. The
23# `sources` and `deps` should include a definition of the standard LLVM fuzz
24# target function, `LLVMFuzzerTestOneInput`. For more details, see:
25# //pw_fuzzer/docs.rst
26# https://llvm.org/docs/LibFuzzer.html
27#
28template("pw_fuzzer") {
Keir Mierle6a106362021-01-14 16:27:44 -080029 # This currently is ONLY supported on Linux and Mac using clang (debug).
30 # TODO(pwbug/179): Add Windows here after testing.
31 fuzzing_platforms = [
32 "linux",
33 "mac",
34 ]
Ali Zhang58765582021-01-29 12:04:06 -080035
36 fuzzing_toolchains = [ "//targets/host:host_clang_fuzz" ]
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070037
38 # This is how GN says 'elem in list':
39 can_fuzz = fuzzing_platforms + [ host_os ] - [ host_os ] != fuzzing_platforms
Keir Mierle6a106362021-01-14 16:27:44 -080040
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070041 can_fuzz = fuzzing_toolchains + [ current_toolchain ] -
42 [ current_toolchain ] != fuzzing_toolchains && can_fuzz
Keir Mierle6a106362021-01-14 16:27:44 -080043
Ali Zhang58765582021-01-29 12:04:06 -080044 if (can_fuzz && pw_toolchain_SANITIZERS != []) {
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070045 # Build the actual fuzzer using the fuzzing config.
46 pw_executable(target_name) {
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070047 forward_variables_from(invoker, "*", [ "visibility" ])
48 forward_variables_from(invoker, [ "visibility" ])
Alexei Frolov844ff0f2020-05-06 12:15:29 -070049 if (!defined(configs)) {
50 configs = []
51 }
Ali Zhangdc4510a2021-01-26 15:01:20 -080052 configs += [ "$dir_pw_fuzzer:default_config" ]
Keir Mierle6a106362021-01-14 16:27:44 -080053 if (pw_toolchain_OSS_FUZZ_ENABLED) {
Aaron Greenb3ae1dc2020-04-13 11:37:05 -070054 configs += [ "$dir_pw_fuzzer:oss_fuzz" ]
55 } else {
56 configs += [ "$dir_pw_fuzzer:fuzzing" ]
57 }
Aaron Green58bba012020-04-06 15:49:04 -070058
Ali Zhang58765582021-01-29 12:04:06 -080059 _fuzzer_output_dir = "${target_out_dir}/bin"
60 if (defined(invoker.output_dir)) {
61 _fuzzer_output_dir = invoker.output_dir
62 }
63 output_dir = _fuzzer_output_dir
64
Aaron Green58bba012020-04-06 15:49:04 -070065 # Metadata for this fuzzer when used as part of a pw_test_group target.
66 metadata = {
67 tests = [
68 {
69 type = "fuzzer"
70 test_name = target_name
Ali Zhang58765582021-01-29 12:04:06 -080071 test_directory = rebase_path(output_dir, root_build_dir)
Aaron Green58bba012020-04-06 15:49:04 -070072 },
73 ]
74 }
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070075 }
76
77 # Dummy target to satisfy `pw_test_group`. It is empty as we don't want to
78 # automatically run fuzzers.
Alexei Frolov69dccfd2020-11-13 12:10:24 -080079 group(target_name + ".run") {
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070080 }
Ali Zhang58765582021-01-29 12:04:06 -080081
82 # Dummy target to satisfy `pw_test`. It is empty as we don't need a separate
83 # lib target.
84 group(target_name + ".lib") {
85 }
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070086 } else {
87 # Build a unit test that exercise the fuzz target function.
88 pw_test(target_name) {
Armando Montanez15b1daf2020-05-08 10:12:30 -070089 # TODO(pwbug/195): Re-enable when there's better configurability for
90 # on-device fuzz testing.
91 enable_if = false
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070092 sources = []
93 deps = []
94 forward_variables_from(invoker, "*", [ "visibility" ])
95 forward_variables_from(invoker, [ "visibility" ])
Aaron Green33c67822020-04-06 13:47:38 -070096 sources += [ "$dir_pw_fuzzer/pw_fuzzer_disabled.cc" ]
Wyatt Heplerc5e511e2020-06-12 16:56:22 -070097 deps += [ "$dir_pw_fuzzer:run_as_unit_test" ]
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070098 }
99 }
100}