Skip to content

Commit 36835df

Browse files
committed
Add hook to create ccov target each time that add_ex.. and add_lib... is called
1 parent 8034d91 commit 36835df

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
@@ -15,6 +15,7 @@ This is a collection of quite useful scripts that expand the possibilities for b
1515
- [1b - Via target commands](#1b---via-target-commands)
1616
- [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)
1717
- [Example 3: Target added to the 'ccov' and 'ccov-all' targets](#example-3-target-added-to-the-ccov-and-ccov-all-targets)
18+
- [Example 4: Hook all targets](#example-4-hook-all-targets)
1819
- [AFL Fuzzing Instrumentation `afl-fuzzing.cmake`](#afl-fuzzing-instrumentation-afl-fuzzingcmake)
1920
- [Usage](#usage-1)
2021
- [Compiler Options `compiler-options.cmake`](#compiler-options-compiler-optionscmake)
@@ -136,6 +137,7 @@ To enable any code coverage instrumentation/targets, the single CMake option of
136137
From this point, there are two primary methods for adding instrumentation to targets:
137138
1. A blanket instrumentation by calling `add_code_coverage()`, where all targets in that directory and all subdirectories are automatically instrumented.
138139
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.
140+
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.
139141

140142
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.
141143

@@ -184,6 +186,20 @@ add_executable(theExe main.cpp non_covered.cpp)
184186
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.
185187
```
186188

189+
#### Example 4: Hook all targets
190+
```
191+
# this could be as well command line argument
192+
set(CCOV_TARGETS_HOOK ON) # enable 'add_executable' and 'add_library' hooks
193+
set(CCOV_TARGETS_HOOK_ARGS ALL AUTO) # set default arguments for coverage
194+
195+
add_code_coverage() # Adds instrumentation to all targets
196+
197+
add_library(theLib lib.cpp) # ccov-theLib target will be add
198+
199+
add_executable(theExe main.cpp) # ccov-theExe target will be add
200+
target_link_libraries(theExe PRIVATE theLib)
201+
```
202+
187203
## AFL Fuzzing Instrumentation [`afl-fuzzing.cmake`](afl-fuzzing.cmake)
188204

189205
> 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)
@@ -181,6 +204,25 @@ if(CODE_COVERAGE AND NOT CODE_COVERAGE_ADDED)
181204
else()
182205
message(FATAL_ERROR "Code coverage requires Clang or GCC. Aborting.")
183206
endif()
207+
208+
if (CCOV_TARGETS_HOOK)
209+
if (COMMAND _add_executable)
210+
message(FATAL_ERROR "add_executable was already redefined. Only one redefinitions is allowed.")
211+
endif()
212+
macro(add_executable)
213+
_add_executable(${ARGV})
214+
target_code_coverage(${ARGV0} ${CCOV_TARGETS_HOOK_ARGS})
215+
endmacro(add_executable)
216+
217+
if (COMMAND _add_library)
218+
message(FATAL_ERROR "add_library was already redefined. Only one redefinitions is allowed.")
219+
endif()
220+
macro(add_library)
221+
_add_library(${ARGV})
222+
target_code_coverage(${ARGV0} ${CCOV_TARGETS_HOOK_ARGS})
223+
endmacro(add_library)
224+
endif (CCOV_TARGETS_HOOK)
225+
184226
endif()
185227

186228
# 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)