Skip to content

Commit 2036eab

Browse files
authored
Merge pull request #13 from Flamefire/improve_example
Improve example
2 parents 46a1cd0 + 6377609 commit 2036eab

File tree

7 files changed

+73
-30
lines changed

7 files changed

+73
-30
lines changed

.travis.yml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,33 @@ language: cpp
22
os: linux
33
compiler: gcc
44

5+
cache: # see https://docs.travis-ci.com/user/caching/
6+
- directories:
7+
- $HOME/.cache
8+
59
addons:
610
apt:
711
packages: lcov
812

9-
before_script:
10-
- mkdir -p build && cd build
13+
install:
14+
# (fake) install dependencies (usually involves wget, configure, make, ...)
15+
# install into cache folder (build binaries+headers only, no sources and do NOT build there)
16+
- mkdir -p $HOME/.cache
17+
- touch $HOME/.cache/mydependency.so
1118

12-
script:
13-
- cmake -DCODE_COVERAGE=ON ..
14-
- cmake --build . --config Debug -- -j $(nproc)
15-
- ctest -j $(nproc) --output-on-failure
19+
script: ./build.sh
1620

1721
after_success:
1822
# Create lcov report
19-
- lcov --capture --directory . --output-file coverage.info
20-
- lcov --remove coverage.info '/usr/*' --output-file coverage.info # filter system-files
23+
# capture coverage info
24+
- lcov --directory . --capture --output-file coverage.info
25+
# filter out system and extra files.
26+
# To also not include test code in coverage add them with full path to the patterns: '*/tests/*'
27+
- lcov --remove coverage.info '/usr/*' "${HOME}"'/.cache/*' --output-file coverage.info
28+
# output coverage data for debugging (optional)
2129
- lcov --list coverage.info
2230
# Uploading to CodeCov
31+
# '-f' specifies file(s) to use and disables manual coverage gathering and file search which has already been done above
2332
- bash <(curl -s https://codecov.io/bash) -f coverage.info || echo "Codecov did not collect coverage reports"
2433

2534
notifications:

CMakeLists.txt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
cmake_minimum_required (VERSION 3.0)
1+
cmake_minimum_required (VERSION 3.1)
22
project(Example LANGUAGES CXX)
33

4-
option(CODE_COVERAGE "Enable coverage reporting" OFF)
5-
6-
# Common configuration and force C++ standard
7-
add_compile_options(-Wall -pedantic)
8-
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.1)
9-
set(CMAKE_CXX_STANDARD 11)
10-
else()
11-
add_compile_options(-std=c++11)
12-
endif()
13-
4+
# Use C++11
5+
set(CMAKE_CXX_STANDARD 11)
6+
# Require (at least) it
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
# Don't use e.g. GNU extension (like -std=gnu++11) for portability
9+
set(CMAKE_CXX_EXTENSIONS OFF)
1410

1511
# Code Coverage Configuration
1612
add_library(coverage_config INTERFACE)
1713

18-
if(CODE_COVERAGE)
14+
option(CODE_COVERAGE "Enable coverage reporting" OFF)
15+
if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
1916
# Add required flags (GCC & LLVM/Clang)
2017
target_compile_options(coverage_config INTERFACE
2118
-O0 # no optimization
@@ -30,5 +27,13 @@ if(CODE_COVERAGE)
3027
endif(CODE_COVERAGE)
3128

3229

33-
enable_testing() # CTest
3430
add_subdirectory(src)
31+
32+
option (BUILD_TESTING "Build the testing tree." ON)
33+
# Only build tests if we are the top-level project
34+
# Allows this to be used by super projects with `add_subdirectory`
35+
if (BUILD_TESTING AND (PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR))
36+
enable_testing()
37+
add_subdirectory(tests)
38+
endif()
39+

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The goal of this project is to build project with following tools:
1616
* Source: multiple files
1717

1818
## Special Thanks
19-
Goes to [Richel Bilderbeek](https://github.com/richelbilderbeek) for inspiration and all work on [Travis CI tutorials](https://github.com/richelbilderbeek/travis_cpp_tutorial).
19+
Goes to [Richel Bilderbeek](https://github.com/richelbilderbeek) for inspiration and all work on [Travis CI tutorials](https://github.com/richelbilderbeek/travis_cpp_tutorial).
2020
Here is a [link](https://github.com/richelbilderbeek/travis_cmake_gcc_cpp11) to a project with the same structure (without `lcov`),
2121
and here is a [list](https://github.com/richelbilderbeek/travis_cpp_tutorial/blob/master/statuses.md) of all his Travis configuration examples.
2222

@@ -61,9 +61,25 @@ after_success:
6161
- bash <(curl -s https://codecov.io/bash) -t uuid-repo-token
6262
```
6363

64+
## Example details
65+
This repo can serve as the starting point for a new project. The following is worth noticing:
66+
1. Use of a build script instead of putting the commands into `.travis.yml`
67+
- Allows local testing
68+
- Allows usage of `set -e` to error out with meaningfull messages on any command failure
69+
2. Separate testing source tree
70+
- Allows to easily enable/disable testing
71+
- Allows usage in parent projects (you don't want to build the tests if you are consumed)
72+
- You may want to exclude coverage of test files which is easier when they are in a separate folder.
73+
Remember to use **full paths** for patterns (like `'*/tests/*'`)
74+
3. Use of travis cache to cache manually build 3rd-party dependencies (like boost)
75+
- Speeds up build
76+
- More can be added (e.g. `ccache`)
77+
- Those need to be excluded from coverage info too
78+
6479
## Authors
6580
* **RokKos** - [RokKos](https://github.com/RokKos)
6681
* **Rolf Eike Beer** - [DerDakon](https://github.com/DerDakon)
82+
* **Alexander Grund** - [Flamefire](https://github.com/Flamefire)
6783

6884
## License
6985
This project is licensed under the MIT License - see the [LICENSE](https://github.com/RokKos/classes-c-/blob/master/LICENSE) file for details.

build.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
mkdir -p build && cd build
6+
7+
# Configure
8+
cmake -DCODE_COVERAGE=ON -DCMAKE_BUILD_TYPE=Debug ..
9+
# Build (for Make on Unix equivalent to `make -j $(nproc)`)
10+
cmake --build . --config Debug -- -j $(nproc)
11+
# Test
12+
ctest -j $(nproc) --output-on-failure
13+

src/CMakeLists.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
cmake_minimum_required(VERSION 3.0)
2-
3-
# The library
41
add_library(example complex.cpp)
52
target_include_directories(example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
3+
# Setting this to the library only avoids errors in 3rd party dependencies which are compile with e.g. -Werror
4+
target_compile_options(example PUBLIC -Wall -pedantic)
65
# Include code-coverage settings:
76
target_link_libraries(example PUBLIC coverage_config)
87

9-
10-
# Tests for the library
11-
add_executable(tests complex_main.cpp)
12-
target_link_libraries(tests PRIVATE example) # inherits coverage_config
13-
add_test(NAME example_test COMMAND tests)

tests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_executable(tests complex_main.cpp)
2+
# Linking up all libraries (includes the coverage settings set by 'example' library)
3+
target_link_libraries(tests PRIVATE example)
4+
5+
add_test(NAME example_test COMMAND tests)
6+
File renamed without changes.

0 commit comments

Comments
 (0)