blob: c328ef471965227bae7bbedd3f4214aa0aba2a81 [file] [log] [blame]
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import("//build_overrides/pigweed.gni")
import("$dir_pw_build/error.gni")
import("$dir_pw_third_party/abseil-cpp/abseil-cpp.gni")
import("$dir_pw_third_party/fuzztest/fuzztest.gni")
import("$dir_pw_third_party/re2/re2.gni")
import("$dir_pw_toolchain/host_clang/toolchains.gni")
import("$dir_pw_unit_test/test.gni")
# Creates a libFuzzer-based fuzzer executable target and unit test
#
# This will link `sources` and `deps` with the libFuzzer compiler runtime. The
# `sources` and `deps` should include a definition of the standard LLVM fuzz
# target function, `LLVMFuzzerTestOneInput`. For more details, see:
# //pw_fuzzer/docs.rst
# https://llvm.org/docs/LibFuzzer.html
#
# Additionally, this creates a unit test that does not generate fuzzer inputs
# and simply executes the fuzz target function with fixed inputs. This is useful
# for verifying the fuzz target function compiles, links, and runs even when not
# using a fuzzing-capable host or toolchain.
#
# Args:
# - enable_test_if: (optional) Passed as `enable_if` to the unit test.
# Remaining arguments are the same as `pw_executable`.
#
template("pw_fuzzer") {
if (!pw_toolchain_FUZZING_ENABLED) {
pw_error(target_name) {
message_lines = [ "Toolchain does not enable fuzzing." ]
}
not_needed(invoker, "*")
} else if (pw_toolchain_SANITIZERS == []) {
pw_error(target_name) {
message_lines = [ "No sanitizer runtime set." ]
}
not_needed(invoker, "*")
} else {
pw_executable(target_name) {
configs = []
deps = []
forward_variables_from(invoker,
"*",
[
"enable_test_if",
"visibility",
])
forward_variables_from(invoker, [ "visibility" ])
configs += [ "$dir_pw_fuzzer:libfuzzer_config" ]
deps += [ "$dir_pw_fuzzer:libfuzzer" ]
}
}
pw_test("${target_name}_test") {
deps = []
forward_variables_from(invoker, "*", [ "visibility" ])
forward_variables_from(invoker, [ "visibility" ])
deps += [ "$dir_pw_fuzzer:libfuzzer_test" ]
enable_if = !defined(enable_test_if) || enable_test_if
}
}