Skip to content

Commit d714fbd

Browse files
rlalikStableCoder
authored andcommitted
Add hook to create ccov target each time that add_ex.. and add_lib... is called
1 parent 6291619 commit d714fbd

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This is a collection of quite useful scripts that expand the possibilities for b
1616
- [1b - Via target commands](#1b---via-target-commands)
1717
- [Example 2: Target instrumented, but with regex pattern of files to be excluded from report](#example-2-target-instrumented-but-with-regex-pattern-of-files-to-be-excluded-from-report)
1818
- [Example 3: Target added to the 'ccov' and 'ccov-all' targets](#example-3-target-added-to-the-ccov-and-ccov-all-targets)
19+
- [Example 4: Hook all targets](#example-4-hook-all-targets)
1920
- [AFL Fuzzing Instrumentation `afl-fuzzing.cmake`](#afl-fuzzing-instrumentation-afl-fuzzingcmake)
2021
- [Usage](#usage-2)
2122
- [Compiler Options `compiler-options.cmake`](#compiler-options-compiler-optionscmake)
@@ -176,6 +177,7 @@ To enable any code coverage instrumentation/targets, the single CMake option of
176177
From this point, there are two primary methods for adding instrumentation to targets:
177178
1. A blanket instrumentation by calling `add_code_coverage()`, where all targets in that directory and all subdirectories are automatically instrumented.
178179
2. Per-target instrumentation by calling `target_code_coverage(<TARGET_NAME>)`, where the target is given and thus only that target is instrumented. This applies to both libraries and executables.
180+
3. Automatically add coverage for each target with `-DCCOV_TARGETS_HOOK=On` and `-DCCOV_TARGETS_HOOK_ARGS=...` for default values, requires `add_code_coverage()` or similar.
179181

180182
To add coverage targets, such as calling `make ccov` to generate the actual coverage information for perusal or consumption, call `target_code_coverage(<TARGET_NAME>)` on an *executable* target.
181183

@@ -224,6 +226,20 @@ add_executable(theExe main.cpp non_covered.cpp)
224226
target_code_coverage(theExe AUTO ALL EXCLUDE non_covered.cpp test/*) # As an executable target, adds to the 'ccov' and ccov-all' targets, and the reports will exclude the non-covered.cpp file, and any files in a test/ folder.
225227
```
226228

229+
#### Example 4: Hook all targets
230+
```
231+
# this could be as well command line argument
232+
set(CCOV_TARGETS_HOOK ON) # enable 'add_executable' and 'add_library' hooks
233+
set(CCOV_TARGETS_HOOK_ARGS ALL AUTO) # set default arguments for coverage
234+
235+
add_code_coverage() # Adds instrumentation to all targets
236+
237+
add_library(theLib lib.cpp) # ccov-theLib target will be add
238+
239+
add_executable(theExe main.cpp) # ccov-theExe target will be add
240+
target_link_libraries(theExe PRIVATE theLib)
241+
```
242+
227243
## AFL Fuzzing Instrumentation [`afl-fuzzing.cmake`](afl-fuzzing.cmake)
228244

229245
> American fuzzy lop is a security-oriented fuzzer that employs a novel type of compile-time instrumentation and genetic algorithms to automatically discover clean, interesting test cases that trigger new internal states in the targeted binary. This substantially improves the functional coverage for the fuzzed code. The compact synthesized corpora produced by the tool are also useful for seeding other, more labor- or resource-intensive testing regimes down the road.

code-coverage.cmake

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,36 @@
7272
# add_executable(theExe main.cpp non_covered.cpp)
7373
# target_code_coverage(theExe AUTO ALL EXCLUDE non_covered.cpp test/*) # As an executable target, adds to the 'ccov' and ccov-all' targets, and the reports will exclude the non-covered.cpp file, and any files in a test/ folder.
7474
# ~~~
75+
#
76+
# Example 4: Hook all targets
77+
#
78+
# ~~~
79+
# set(CCOV_TARGETS_HOOK ON) # enable 'add_executable' and 'add_library' hooks
80+
# set(CCOV_TARGETS_HOOK_ARGS ALL AUTO) # set default arguments for coverage
81+
#
82+
# add_code_coverage() # Adds instrumentation to all targets
83+
#
84+
# add_library(theLib lib.cpp) # ccov-theLib target will be add
85+
#
86+
# add_executable(theExe main.cpp) # ccov-theExe target will be add
87+
# target_link_libraries(theExe PRIVATE theLib)
88+
# ~~~
7589

7690
# Options
7791
option(
7892
CODE_COVERAGE
7993
"Builds targets with code coverage instrumentation. (Requires GCC or Clang)"
8094
OFF)
8195

96+
option(
97+
CCOV_TARGETS_HOOK
98+
"Autocapture all new targets."
99+
OFF)
100+
101+
option(
102+
CCOV_TARGETS_HOOK_ARGS
103+
"Default arguments for all hooked targets.")
104+
82105
# Programs
83106
find_program(LLVM_COV_PATH llvm-cov)
84107
find_program(LLVM_PROFDATA_PATH llvm-profdata)
@@ -189,6 +212,25 @@ if(CODE_COVERAGE AND NOT CODE_COVERAGE_ADDED)
189212
else()
190213
message(FATAL_ERROR "Code coverage requires Clang or GCC. Aborting.")
191214
endif()
215+
216+
if (CCOV_TARGETS_HOOK)
217+
if (COMMAND _add_executable)
218+
message(FATAL_ERROR "add_executable was already redefined. Only one redefinitions is allowed.")
219+
endif()
220+
macro(add_executable)
221+
_add_executable(${ARGV})
222+
target_code_coverage(${ARGV0} ${CCOV_TARGETS_HOOK_ARGS})
223+
endmacro(add_executable)
224+
225+
if (COMMAND _add_library)
226+
message(FATAL_ERROR "add_library was already redefined. Only one redefinitions is allowed.")
227+
endif()
228+
macro(add_library)
229+
_add_library(${ARGV})
230+
target_code_coverage(${ARGV0} ${CCOV_TARGETS_HOOK_ARGS})
231+
endmacro(add_library)
232+
endif (CCOV_TARGETS_HOOK)
233+
192234
endif()
193235

194236
# Adds code coverage instrumentation to a library, or instrumentation/targets
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(code-coverage-hook C CXX)
3+
4+
# Set the searching location for cmake 'include' locations
5+
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../..;")
6+
# Include the code coverage module
7+
cmake_policy(SET CMP0077 NEW)
8+
9+
set(CCOV_TARGETS_HOOK ON)
10+
set(CCOV_TARGETS_HOOK_ARGS "ALL")
11+
12+
include(code-coverage)
13+
14+
# Require C++11
15+
include(c++-standards)
16+
cxx_11()
17+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
18+
19+
# This introduces the 'ccov-all' targets Also excludes the main file via a regex
20+
add_code_coverage_all_targets(EXCLUDE coverage.main.cpp)
21+
22+
# The library
23+
add_library(lib ../src/coverage.cpp)
24+
25+
# The executable
26+
add_executable(main ../src/coverage.main.cpp)
27+
target_link_libraries(main PUBLIC lib)
28+
29+
# The second executable
30+
add_executable(main2 ../src/coverage.main.cpp)
31+
target_link_libraries(main2 PUBLIC lib)

0 commit comments

Comments
 (0)