Skip to content

Commit 8f5a832

Browse files
committed
Doc: Add sources for integrated instrumentation examples
1 parent 6eda572 commit 8f5a832

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

doc/gnatcov/integrated_instr.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ generated compiler wrapper instead of the original compiler.
6262
A simple Makefile example
6363
=========================
6464

65+
The sources for the following example can be found under the
66+
``share/examples/gnatcoverage/doc/integrated`` directory of the GNATDAS
67+
distribution.
68+
6569
The following considers that the instrumentation runtime was previously
6670
installed with ``gnatcov setup``, and that the ``GPR_PROJECT_PATH`` variable
6771
contains its installed location. See :ref:`instr-rts`.
@@ -111,6 +115,12 @@ when run, that can be analyzed with ``gnatcov coverage``.
111115
A simple CMake example
112116
======================
113117

118+
The sources for the following example can be found under the
119+
``share/examples/gnatcoverage/doc/integrated`` directory of the GNATDAS
120+
distribution. To ensure that the Makefile provided with the example sources
121+
uses CMake as a build system, specify ``BUILD_SYSTEM=CMake`` on the `make`
122+
invocation.
123+
114124
The following considers that the instrumentation runtime was installed through
115125
the use of ``gnatcov setup``.
116126

@@ -129,8 +139,8 @@ The CMakeLists.txt file to be used to compile the main.cpp file is :
129139

130140
.. code-block:: cmake
131141
142+
cmake_minimum_required(VERSION 3.5)
132143
project(HelloWorld)
133-
cmake_minimum_required(VERSION 3.0)
134144
135145
add_executable(hello_world main.cpp)
136146
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This showcases how to use the integrated instrumentation mode with a Makefile
2+
# or a CMake build system. For more details see subsections "A simple Makefile
3+
# example" and "A simple CMake example" of section "Producing source traces
4+
# with integrated instrumentation".
5+
6+
RTS_DIR = rts_install
7+
ARTIFACTS_DIR = gnatcov_artifacts
8+
9+
.PHONY: setup-instr build run coverage clean
10+
11+
# Set this variable to either Makefile or CMake to select the desired build system
12+
BUILD_SYSTEM ?= Makefile
13+
14+
default: coverage
15+
16+
# Only needs to be called once, can be re-used across multiple GNATcov workflows
17+
$(RTS_DIR)/share/gpr/gnatcov_rts.gpr:
18+
gnatcov setup --prefix=$(RTS_DIR)
19+
20+
setup-instr: export GPR_PROJECT_PATH := $(shell pwd)/$(RTS_DIR)/share/gpr:$(GPR_PROJECT_PATH)
21+
setup-instr: $(RTS_DIR)/share/gpr/gnatcov_rts.gpr
22+
gnatcov setup-integration --files=hello_world/main.cpp --compilers=g++ \
23+
--level=stmt --output-dir=$(ARTIFACTS_DIR)
24+
25+
build-Makefile: export PATH:=$(shell pwd)/$(ARTIFACTS_DIR):$(PATH)
26+
build-Makefile: setup-instr
27+
make -C hello_world test
28+
cp hello_world/test .
29+
30+
build-CMake: setup-instr
31+
cmake hello_world -B cmake_build -DCMAKE_CXX_COMPILER=$(shell pwd)/$(ARTIFACTS_DIR)/g++
32+
cmake --build cmake_build
33+
cp cmake_build/test .
34+
35+
run: build-$(BUILD_SYSTEM)
36+
./test
37+
38+
coverage: run
39+
gnatcov coverage --sid $(ARTIFACTS_DIR)/main.cpp.sid --level=stmt \
40+
--annotate=xcov *.srctrace
41+
cat main.cpp.xcov
42+
43+
clean:
44+
rm -rf $(ARTIFACTS_DIR) $(RTS_DIR) main*.srctrace hello_world/main.o \
45+
hello_world/test stats *.xcov CMakeFiles CMakeCache.txt cmake_* test
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(HelloWorld)
3+
4+
add_executable(test main.cpp)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CC=g++
2+
OBJ = main.o
3+
4+
%.o: %.c
5+
$(CC) -c -o $@ $<
6+
7+
test: $(OBJ)
8+
$(CC) -o $@ $^
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/******************************************************
2+
* GNATcoverage *
3+
* Copyright (C) 2012-2025, AdaCore *
4+
******************************************************/
5+
6+
#include <iostream>
7+
8+
int
9+
main (int argc, char **argv)
10+
{
11+
std::cout << "Hello World" << std::endl;
12+
return 0;
13+
}

0 commit comments

Comments
 (0)