├── .appveyor.yml ├── .github └── workflows │ └── enforce-license-compliance.yml ├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── build.sh ├── img ├── Codecov.png └── TravisCI.png ├── src ├── CMakeLists.txt ├── complex.cpp └── complex.hpp └── tests ├── CMakeLists.txt └── complex_main.cpp /.appveyor.yml: -------------------------------------------------------------------------------- 1 | version: "{branch}-ci-{build}" 2 | image: Visual Studio 2017 3 | 4 | build: msvc 5 | platform: x86 6 | configuration: Debug 7 | 8 | install: 9 | - choco install opencppcoverage codecov 10 | - set PATH=C:\Program Files\OpenCppCoverage;%PATH% 11 | 12 | build_script: 13 | - mkdir build && cd build 14 | - cmake .. 15 | - MSBuild Example.sln /p:Configuration=%configuration% /p:Platform="Win32" /nologo /m /verbosity:minimal 16 | - OpenCppCoverage --export_type cobertura:coverage.xml --modules "*.exe" --cover_children -- ctest -C %configuration% --output-on-failure 17 | - codecov -f coverage.xml --root %APPVEYOR_BUILD_FOLDER% 18 | -------------------------------------------------------------------------------- /.github/workflows/enforce-license-compliance.yml: -------------------------------------------------------------------------------- 1 | name: Enforce License Compliance 2 | 3 | on: 4 | pull_request: 5 | branches: [main, master] 6 | 7 | jobs: 8 | enforce-license-compliance: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: 'Enforce License Compliance' 12 | uses: getsentry/action-enforce-license-compliance@57ba820387a1a9315a46115ee276b2968da51f3d # main 13 | with: 14 | fossa_api_key: ${{ secrets.FOSSA_API_KEY }} 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignored folders 2 | build/ 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | os: linux 3 | compiler: gcc 4 | 5 | cache: # see https://docs.travis-ci.com/user/caching/ 6 | - directories: 7 | - $HOME/.cache 8 | 9 | addons: 10 | apt: 11 | packages: lcov 12 | 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 18 | 19 | script: ./build.sh 20 | 21 | after_success: 22 | # Create lcov report 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) 29 | - lcov --list coverage.info 30 | # Uploading to CodeCov 31 | # '-f' specifies file(s) to use and disables manual coverage gathering and file search which has already been done above 32 | - bash <(curl -s https://codecov.io/bash) -f coverage.info || echo "Codecov did not collect coverage reports" 33 | 34 | notifications: 35 | email: 36 | - kosrok97@gmail.com 37 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.1) 2 | project(Example LANGUAGES CXX) 3 | 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) 10 | 11 | # Code Coverage Configuration 12 | add_library(coverage_config INTERFACE) 13 | 14 | option(CODE_COVERAGE "Enable coverage reporting" OFF) 15 | if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") 16 | # Add required flags (GCC & LLVM/Clang) 17 | target_compile_options(coverage_config INTERFACE 18 | -O0 # no optimization 19 | -g # generate debug info 20 | --coverage # sets all required flags 21 | ) 22 | if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13) 23 | target_link_options(coverage_config INTERFACE --coverage) 24 | else() 25 | target_link_libraries(coverage_config INTERFACE --coverage) 26 | endif() 27 | endif(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") 28 | 29 | 30 | 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 | 40 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at support@codecov.io. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Rok Kos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Codecov][1] CI CMake g++ cpp11 lcov Example 2 | [![Travis CI logo][travis-image]][travis-link] 3 | [![Codecov logo][codecov-image]][codecov-link] 4 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcodecov%2Fexample-cpp11-cmake.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-cpp11-cmake?ref=badge_shield) 5 | 6 | [![Build Status][travis-badge]][travis-link] 7 | [![codecov][codecov-badge]][codecov-link] 8 | [![MIT License][license-badge]](LICENSE.md) 9 | 10 | The goal of this project is to build project with following tools: 11 | * C++ version: `C++11` 12 | * Build system: [`CMake`](https://cmake.org/) 13 | * C++ compiler: `g++` or Visual Studio 14 | * Libraries: `STL` only 15 | * Code coverage report: [`lcov`](http://ltp.sourceforge.net/coverage/lcov.php) and [`OpenCppCoverage`](https://github.com/OpenCppCoverage/OpenCppCoverage)(note: it should show the code coverage is below 100%) 16 | * [`CodeCov`](https://codecov.io/) (code coverage is measured by CodeCov). 17 | * Source: multiple files 18 | 19 | ## Special Thanks 20 | Goes to [Richel Bilderbeek](https://github.com/richelbilderbeek) for inspiration and all work on [Travis CI tutorials](https://github.com/richelbilderbeek/travis_cpp_tutorial). 21 | Here is a [link](https://github.com/richelbilderbeek/travis_cmake_gcc_cpp11) to a project with the same structure (without `lcov`), 22 | and here is a [list](https://github.com/richelbilderbeek/travis_cpp_tutorial/blob/master/statuses.md) of all his Travis configuration examples. 23 | 24 | ## Prerequisites 25 | To build the project you need to install `CMake`. ([Install instructions](https://cmake.org/install/)) 26 | To display a code coverage report in the console, install `lcov`. ([`Download lcov`](http://ltp.sourceforge.net/coverage/lcov.php), [`Instructions`](http://ltp.sourceforge.net/coverage/lcov/readme.php)) 27 | 28 | ## Guide 29 | 1. Compile with code coverage instrumentation enabled [(GCC)](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html). 30 | 2. Execute the tests to generate the coverage data. 31 | 3. (Optionally) generate and customize reports with `lcov`. 32 | 4. Upload to CodeCov using the bash uploader. 33 | 34 | ### Travis Setup Using lcov 35 | Add to your `.travis.yml` file: 36 | ```yml 37 | addons: 38 | apt: 39 | packages: lcov 40 | 41 | after_success: 42 | # Create lcov report 43 | - lcov --capture --directory . --output-file coverage.info 44 | - lcov --remove coverage.info '/usr/*' --output-file coverage.info # filter system-files 45 | - lcov --list coverage.info # debug info 46 | # Uploading report to CodeCov 47 | - bash <(curl -s https://codecov.io/bash) -f coverage.info || echo "Codecov did not collect coverage reports" 48 | ``` 49 | 50 | ### Travis Setup without lcov 51 | By default the bash uploader processes the coverage data using gcov when no file is supplied. 52 | ```yml 53 | after_success: 54 | - bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports" 55 | ``` 56 | 57 | ## Caveats 58 | ### Private Repos 59 | Add to your `.travis.yml` file: 60 | ```yml 61 | after_success: 62 | - bash <(curl -s https://codecov.io/bash) -t uuid-repo-token 63 | ``` 64 | 65 | ## Example details 66 | This repo can serve as the starting point for a new project. The following is worth noticing: 67 | 1. Use of a build script instead of putting the commands into `.travis.yml` 68 | - Allows local testing 69 | - Allows usage of `set -e` to error out with meaningfull messages on any command failure 70 | 2. Separate testing source tree 71 | - Allows to easily enable/disable testing 72 | - Allows usage in parent projects (you don't want to build the tests if you are consumed) 73 | - You may want to exclude coverage of test files which is easier when they are in a separate folder. 74 | Remember to use **full paths** for patterns (like `'*/tests/*'`) 75 | 3. Use of travis cache to cache manually build 3rd-party dependencies (like boost) 76 | - Speeds up build 77 | - More can be added (e.g. `ccache`) 78 | - Those need to be excluded from coverage info too 79 | 80 | ## Authors 81 | * **RokKos** - [RokKos](https://github.com/RokKos) 82 | * **Rolf Eike Beer** - [DerDakon](https://github.com/DerDakon) 83 | * **Alexander Grund** - [Flamefire](https://github.com/Flamefire) 84 | 85 | ## License 86 | This project is licensed under the MIT License - see the [LICENSE](https://github.com/RokKos/classes-c-/blob/master/LICENSE) file for details. 87 | 88 | 1. More documentation at https://docs.codecov.io 89 | 2. Configure codecov through the `codecov.yml` https://docs.codecov.io/docs/codecov-yaml 90 | 91 | We are happy to help if you have any questions. Please email our Support at [support@codecov.io](mailto:support@codecov.io) 92 | 93 | [1]: https://codecov.io/ 94 | [travis-badge]: https://travis-ci.org/codecov/example-cpp11-cmake.svg?branch=master 95 | [travis-link]: https://travis-ci.org/codecov/example-cpp11-cmake 96 | [travis-image]: https://github.com/codecov/example-cpp1-cmake/blob/master/img/TravisCI.png 97 | [license-badge]: https://img.shields.io/badge/license-MIT-007EC7.svg 98 | [codecov-badge]: https://codecov.io/gh/codecov/example-cpp11-cmake/branch/master/graph/badge.svg 99 | [codecov-link]: https://codecov.io/gh/codecov/example-cpp11-cmake 100 | [codecov-image]: https://github.com/codecov/example-cpp1-cmake/blob/master/img/Codecov.png 101 | 102 | 103 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcodecov%2Fexample-cpp11-cmake.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-cpp11-cmake?ref=badge_large) -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 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 | 14 | -------------------------------------------------------------------------------- /img/Codecov.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codecov/example-cpp11-cmake/bcebf109a184591db417dbcda15f4df484236122/img/Codecov.png -------------------------------------------------------------------------------- /img/TravisCI.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codecov/example-cpp11-cmake/bcebf109a184591db417dbcda15f4df484236122/img/TravisCI.png -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(example complex.cpp) 2 | 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 | if(NOT MSVC) 5 | target_compile_options(example PUBLIC -Wall -pedantic) 6 | endif(NOT MSVC) 7 | # Include code-coverage settings: 8 | target_link_libraries(example PUBLIC coverage_config) 9 | 10 | -------------------------------------------------------------------------------- /src/complex.cpp: -------------------------------------------------------------------------------- 1 | #include "complex.hpp" 2 | 3 | Complex::Complex(double _real, double _imaginary) { 4 | this->real = _real; 5 | this->imaginary = _imaginary; 6 | } 7 | 8 | Complex::Complex(const Complex& rhs) { 9 | this->real = rhs.real; 10 | this->imaginary = rhs.imaginary; 11 | } 12 | 13 | Complex::~Complex() { 14 | 15 | } 16 | 17 | Complex& Complex::operator=(const Complex& rhs) { 18 | this->real = rhs.real; 19 | this->imaginary = rhs.imaginary; 20 | 21 | return *this; 22 | } 23 | 24 | double Complex::getReal() const { 25 | return this->real; 26 | } 27 | 28 | double Complex::getImag() const { 29 | return this->imaginary; 30 | } 31 | 32 | double Complex::abs() const { 33 | return sqrt(this->real * this->real + this->imaginary * this->imaginary); 34 | } 35 | -------------------------------------------------------------------------------- /src/complex.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _SRC_COMPLEX_HPP_ 2 | #define _SRC_COMPLEX_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * @file complex.hpp 9 | * @author Rok Kos 10 | * @brief Class that represent complex number. 11 | */ 12 | 13 | class Complex { 14 | private: 15 | double real; 16 | double imaginary; 17 | 18 | public: 19 | // Constructor 20 | Complex(double=0, double=0); 21 | // Copy constructor 22 | Complex(const Complex& rhs); 23 | // Destructor 24 | ~Complex(); 25 | // Assigment operator 26 | Complex& operator=(const Complex& rhs); 27 | // Methods 28 | double getReal() const; 29 | double getImag() const; 30 | double abs() const; 31 | }; 32 | 33 | #endif // _SRC_COMPLEX_HPP_ 34 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 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 | 7 | -------------------------------------------------------------------------------- /tests/complex_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "complex.hpp" 4 | 5 | int main() { 6 | Complex a(3,5); 7 | Complex b(3,0); 8 | Complex c(a); 9 | 10 | std::cout << a.getReal() << " " << a.getImag() << " " << a.abs() << std::endl; 11 | std::cout << b.getReal() << " " << b.getImag() << " " << b.abs() << std::endl; 12 | std::cout << c.getReal() << " " << c.getImag() << " " << c.abs() << std::endl; 13 | 14 | return 0; 15 | } 16 | --------------------------------------------------------------------------------