blob: 9bcf50e0277a03960a4cb2d1f24cda2be1491856 [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
Henri Chataing85e787b2021-08-13 15:26:14 +000036 fuzzing_toolchains =
37 [ get_path_info("$dir_pigweed/targets/host:host_clang_fuzz", "abspath") ]
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070038
39 # This is how GN says 'elem in list':
40 can_fuzz = fuzzing_platforms + [ host_os ] - [ host_os ] != fuzzing_platforms
Keir Mierle6a106362021-01-14 16:27:44 -080041
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070042 can_fuzz = fuzzing_toolchains + [ current_toolchain ] -
43 [ current_toolchain ] != fuzzing_toolchains && can_fuzz
Keir Mierle6a106362021-01-14 16:27:44 -080044
Ali Zhang58765582021-01-29 12:04:06 -080045 if (can_fuzz && pw_toolchain_SANITIZERS != []) {
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070046 # Build the actual fuzzer using the fuzzing config.
47 pw_executable(target_name) {
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070048 forward_variables_from(invoker, "*", [ "visibility" ])
49 forward_variables_from(invoker, [ "visibility" ])
Wyatt Heplerb93749b2021-05-11 09:38:22 -070050
51 if (!defined(deps)) {
Wyatt Hepler8316f2c2021-05-11 18:20:32 -070052 deps = []
Wyatt Heplerb93749b2021-05-11 09:38:22 -070053 }
Wyatt Hepler8316f2c2021-05-11 18:20:32 -070054 deps += [ dir_pw_fuzzer ]
Wyatt Heplerb93749b2021-05-11 09:38:22 -070055
Alexei Frolov844ff0f2020-05-06 12:15:29 -070056 if (!defined(configs)) {
57 configs = []
58 }
Keir Mierle6a106362021-01-14 16:27:44 -080059 if (pw_toolchain_OSS_FUZZ_ENABLED) {
Aaron Greenb3ae1dc2020-04-13 11:37:05 -070060 configs += [ "$dir_pw_fuzzer:oss_fuzz" ]
61 } else {
62 configs += [ "$dir_pw_fuzzer:fuzzing" ]
63 }
Aaron Green58bba012020-04-06 15:49:04 -070064
Ali Zhang58765582021-01-29 12:04:06 -080065 _fuzzer_output_dir = "${target_out_dir}/bin"
66 if (defined(invoker.output_dir)) {
67 _fuzzer_output_dir = invoker.output_dir
68 }
69 output_dir = _fuzzer_output_dir
70
Aaron Green58bba012020-04-06 15:49:04 -070071 # Metadata for this fuzzer when used as part of a pw_test_group target.
72 metadata = {
73 tests = [
74 {
75 type = "fuzzer"
76 test_name = target_name
Ali Zhang58765582021-01-29 12:04:06 -080077 test_directory = rebase_path(output_dir, root_build_dir)
Aaron Green58bba012020-04-06 15:49:04 -070078 },
79 ]
80 }
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070081 }
82
Rob Mohr55bb0ad2021-05-22 10:59:52 -070083 # No-op target to satisfy `pw_test_group`. It is empty as we don't want to
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070084 # automatically run fuzzers.
Alexei Frolov69dccfd2020-11-13 12:10:24 -080085 group(target_name + ".run") {
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070086 }
Ali Zhang58765582021-01-29 12:04:06 -080087
Rob Mohr55bb0ad2021-05-22 10:59:52 -070088 # No-op target to satisfy `pw_test`. It is empty as we don't need a separate
Ali Zhang58765582021-01-29 12:04:06 -080089 # lib target.
90 group(target_name + ".lib") {
91 }
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070092 } else {
93 # Build a unit test that exercise the fuzz target function.
94 pw_test(target_name) {
Armando Montanez15b1daf2020-05-08 10:12:30 -070095 # TODO(pwbug/195): Re-enable when there's better configurability for
96 # on-device fuzz testing.
97 enable_if = false
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070098 sources = []
99 deps = []
100 forward_variables_from(invoker, "*", [ "visibility" ])
101 forward_variables_from(invoker, [ "visibility" ])
Aaron Green33c67822020-04-06 13:47:38 -0700102 sources += [ "$dir_pw_fuzzer/pw_fuzzer_disabled.cc" ]
Wyatt Heplerc5e511e2020-06-12 16:56:22 -0700103 deps += [ "$dir_pw_fuzzer:run_as_unit_test" ]
Aaron Greenf3c3d2b2020-04-02 23:12:31 -0700104 }
105 }
106}