Skip to content

Commit

Permalink
Merge 3f929a8 into 8cd0e53
Browse files Browse the repository at this point in the history
  • Loading branch information
izvolov committed Aug 10, 2019
2 parents 8cd0e53 + 3f929a8 commit 1c577d5
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 43 deletions.
24 changes: 15 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ language: cpp
os: linux
dist: bionic
install:
- wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | sudo apt-key add -
- sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main'

- sudo apt update
- sudo apt install --yes git cmake $COMPILER_PACKAGE

- git clone https://github.com/onqtam/doctest.git third_party/doctest
- git -C third_party/doctest checkout 2.3.3
- cmake -BBuild/third_party/doctest -Hthird_party/doctest -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=$COMPILER_NAME
- sudo cmake --build Build/third_party/doctest --target install/fast
- /usr/bin/cmake -BBuild/third_party/doctest -Hthird_party/doctest -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=$COMPILER_NAME
- sudo /usr/bin/cmake --build Build/third_party/doctest --target install/fast
script:
- cmake -BBuild/mylib -H. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_COMPILER=$COMPILER_NAME
- cmake --build Build/mylib --target check
- /usr/bin/cmake -BBuild/mylib -H. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_COMPILER=$COMPILER_NAME
- /usr/bin/cmake --build Build/mylib --target check

.macos-job-template: &macos-job
os: osx
Expand All @@ -35,20 +38,23 @@ language: cpp
os: linux
dist: bionic
install:
- wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | sudo apt-key add -
- sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main'

- sudo apt update
- sudo apt install --yes git cmake gcovr python-pip

- pip install --user cpp-coveralls

- git clone https://github.com/onqtam/doctest.git third_party/doctest
- git -C third_party/doctest checkout 2.3.3
- cmake -BBuild/third_party/doctest -Hthird_party/doctest -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=$COMPILER_NAME
- sudo cmake --build Build/third_party/doctest --target install/fast
- /usr/bin/cmake -BBuild/third_party/doctest -Hthird_party/doctest -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=$COMPILER_NAME
- sudo /usr/bin/cmake --build Build/third_party/doctest --target install/fast
script:
- cmake -BBuild/mylib -H. -DCMAKE_BUILD_TYPE=Debug -DMYLIB_COVERAGE=ON
- cmake --build Build/mylib --target coverage
- /usr/bin/cmake -BBuild/mylib -H. -DCMAKE_BUILD_TYPE=Debug -DMYLIB_COVERAGE=ON
- /usr/bin/cmake --build Build/mylib --target coverage
after_success:
- coveralls --root . --build-root Build/mylib --include include
- coveralls --root . --build-root Build/mylib --include include --include src

matrix:
include:
Expand Down
17 changes: 14 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
##
###################################################################################################

cmake_minimum_required(VERSION 3.12)
cmake_minimum_required(VERSION 3.14)
project(Mylib VERSION 1.0 LANGUAGES CXX)

get_directory_property(IS_SUBPROJECT PARENT_DIRECTORY)
Expand Down Expand Up @@ -51,7 +51,7 @@ endif()

###################################################################################################
##
## Основная цель
## Заголовочная библиотека
##
###################################################################################################

Expand All @@ -64,6 +64,17 @@ target_compile_features(mylib INTERFACE cxx_std_17)

add_library(Mylib::mylib ALIAS mylib)

###################################################################################################
##
## Компилируемая библиотека
##
###################################################################################################

add_library(myfeature src/mylib/myfeature.cpp)
target_link_libraries(myfeature PUBLIC mylib)

add_library(Mylib::myfeature ALIAS myfeature)

###################################################################################################
##
## Установка
Expand All @@ -72,7 +83,7 @@ add_library(Mylib::mylib ALIAS mylib)

install(DIRECTORY include/mylib DESTINATION include)

install(TARGETS mylib EXPORT MylibConfig)
install(TARGETS mylib myfeature EXPORT MylibConfig)
install(EXPORT MylibConfig NAMESPACE Mylib:: DESTINATION share/Mylib/cmake)

include(CMakePackageConfigHelpers)
Expand Down
66 changes: 53 additions & 13 deletions README.en.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[:ru: Оригинал](README.md)

CMake-based project template for header-only C++ library
========================================================
CMake-based template for C++ project
====================================

Fork, replace "Mylib" with the desired name, and it's done.

Expand All @@ -17,14 +17,18 @@ Contents
3. [MYLIB_DOXYGEN_LANGUAGE](#MYLIB_DOXYGEN_LANGUAGE)
3. [Targets](#targets)
1. [Default](#default)
2. [mylib-unit-tests](#mylib-unit-tests)
3. [check](#check)
4. [coverage](#coverage)
5. [doc](#doc)
6. [wandbox](#wandbox)
2. [myfeature](#myfeature)
3. [mylib-unit-tests](#mylib-unit-tests)
4. [check](#check)
5. [coverage](#coverage)
6. [doc](#doc)
7. [wandbox](#wandbox)
4. [Examples](#examples)
5. [Tools](#tools)
6. [Bonus](#bonus)
5. [Usage](#usage)
1. [Through the installation](#through-the-installation)
2. [As a submodule](#as-a-submodule)
6. [Tools](#tools)
7. [Bonus](#bonus)

Build
-----
Expand Down Expand Up @@ -66,7 +70,7 @@ cmake -S ... -B ... -DMYLIB_TESTING=OFF [other options ...]

Provides the ability to turn off unit testing and hence the [`check`](#check) target. As a result, the code coverage measurement is also turned off (see [Code coverage](#MYLIB_COVERAGE)).

Also, testing is automatically disabled if the project is included to another project as a subproject using the [`add_subdirectory`](https://cmake.org/cmake/help/v3.8/command/add_subdirectory.html) command.
Also, testing is automatically disabled if the project is included to another project as a subproject using the [`add_subdirectory`](https://cmake.org/cmake/help/v3.14/command/add_subdirectory.html) command.

### MYLIB_DOXYGEN_LANGUAGE

Expand All @@ -90,6 +94,14 @@ cmake --build path/to/build/directory --target all

If a target is not specified (which is equivalent to the `all` target), it builds everything possible including unit tests and also calls the [`check`](#check) target.

### myfeature

```shell
cmake --build path/to/build/directory --target myfeature
```

Compiles the `myfeature` library. Enabled by default.

### mylib-unit-tests

```shell
Expand Down Expand Up @@ -167,12 +179,40 @@ cmake -S path/to/sources -B path/to/build/directory -DCMAKE_BUILD_TYPE=Release -
cmake --build path/to/build/directory --target doc
```

Usage
-----

### Through the installation

One of the ways to use the module is to install it into the system.

```shell
cmake --build path/to/build/directory --target install
```

After that, all the libraries from the `Mylib::` namespace can be used from any other project using the [`find_package`](https://cmake.org/cmake/help/v3.14/command/find_package.html) command:

```cmake
find_package(Mylib 1.0 REQUIRED)
add_executable(some_executable some.cpp sources.cpp)
target_link_libraries(some_executable PRIVATE Mylib::myfeature)
```

`Mylib::mylib` library is used for the headers only, and `Mylib::myfeature` library is used when it is also needed to link with the `libmyfeature` library.

### As a submodule

The project can also be used by another project as a submodule using the [`add_subdirectory`](https://cmake.org/cmake/help/v3.14/command/add_subdirectory.html) command:

In this case, libraries `Mylib::myfeature` and `Mylib::mylib` will be available in the same manner.

Tools
-----

1. [CMake](https://cmake.org) 3.13
1. [CMake](https://cmake.org) 3.14

In fact, version 3.13 is only required to run some of the console commands described in this help. The syntax of all the CMake scripts in this project conforms to version 3.8.
CMake 3.14 is required because of incorrect work of the command `install(TARGETS ... EXPORT ...)`: is does not set default install paths properly.

2. [doctest](https://github.com/onqtam/doctest) testing framework

Expand All @@ -195,7 +235,7 @@ With CMake and a couple of good tools, you can get a static analysis with minima

CMake has build-in support for [Cppcheck](http://cppcheck.sourceforge.net).

It is provided by the [`CMAKE_CXX_CPPCHECK`](https://cmake.org/cmake/help/v3.10/variable/CMAKE_LANG_CPPCHECK.html#variable:CMAKE_<LANG>_CPPCHECK) option:
It is provided by the [`CMAKE_CXX_CPPCHECK`](https://cmake.org/cmake/help/v3.14/variable/CMAKE_LANG_CPPCHECK.html#variable:CMAKE_<LANG>_CPPCHECK) option:

```shell
cmake -S path/to/sources -B path/to/build/directory -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_CPPCHECK="cppcheck;--enable=all;-Ipath/to/sources/include"
Expand Down
62 changes: 51 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
3. [MYLIB_DOXYGEN_LANGUAGE](#MYLIB_DOXYGEN_LANGUAGE)
3. [Сборочные цели](#сборочные-цели)
1. [По умолчанию](#по-умолчанию)
2. [mylib-unit-tests](#mylib-unit-tests)
3. [check](#check)
4. [coverage](#coverage)
5. [doc](#doc)
6. [wandbox](#wandbox)
2. [myfeature](#myfeature)
3. [mylib-unit-tests](#mylib-unit-tests)
4. [check](#check)
5. [coverage](#coverage)
6. [doc](#doc)
7. [wandbox](#wandbox)
4. [Примеры](#примеры)
5. [Инструменты](#инструменты)
6. [Бонус](#бонус)
5. [Использование](#использование)
1. [Через установку](#через-установку)
2. [В качестве подмодуля](#в-качестве-подмодуля)
6. [Инструменты](#инструменты)
7. [Бонус](#бонус)

Сборка
------
Expand Down Expand Up @@ -69,7 +73,7 @@ cmake -S ... -B ... -DMYLIB_TESTING=OFF [прочие опции ...]

Предоставляет возможность выключить сборку модульных тестов и цель [`check`](#check). Как следствие, выключается замер покрытия кода тестами (см. [Покрытие](#MYLIB_COVERAGE)).

Также тестирование автоматически отключается в случае, если проект подключается в другой проект качестве подпроекта с помощью команды [`add_subdirectory`](https://cmake.org/cmake/help/v3.8/command/add_subdirectory.html).
Также тестирование автоматически отключается в случае, если проект подключается в другой проект качестве подпроекта с помощью команды [`add_subdirectory`](https://cmake.org/cmake/help/v3.14/command/add_subdirectory.html).

### MYLIB_DOXYGEN_LANGUAGE

Expand All @@ -93,6 +97,14 @@ cmake --build path/to/build/directory --target all

Если цель не указана (что эквивалентно цели `all`), собирает всё, что можно, а также вызывает цель [`check`](#check).

### myfeature

```shell
cmake --build путь/к/сборочной/директории --target myfeature
```

Компилирует библиотеку `myfeature`. Включено по умолчанию.

### mylib-unit-tests

```shell
Expand Down Expand Up @@ -170,12 +182,40 @@ cmake -S путь/к/исходникам -B путь/к/сборочной/ди
cmake --build путь/к/сборочной/директории --target doc
```

Использование
-------------

### Через установку

Один из вариантов использования модуля — установить его в систему.

```shell
cmake --build путь/к/сборочной/директории --target install
```

После этого любой другой проект, вызвав команду [`find_package`](https://cmake.org/cmake/help/v3.14/command/find_package.html), получает возможность использовать все библиотеки из пространства имён `Mylib::`:

```cmake
find_package(Mylib 1.0 REQUIRED)
add_executable(some_executable some.cpp sources.cpp)
target_link_libraries(some_executable PRIVATE Mylib::myfeature)
```

Библиотеку `Mylib::myfeature` нужно подключать тогда, когда необходимо слинковаться с библиотекой `libmyfeature`. Если достаточно заголовков, то тогда стоит использовать библиотеку `Mylib::mylib`.

### В качестве подмодуля

Также проект может быть подключён к другому проекту в качестве подмодуля с помощью команды [`add_subdirectory`](https://cmake.org/cmake/help/v3.14/command/add_subdirectory.html):

В этом случае аналогичным образом будут доступны библиотеки `Mylib::myfeature` и `Mylib::mylib`.

Инструменты
-----------

1. [CMake](https://cmake.org) 3.13
1. [CMake](https://cmake.org) 3.14

На самом деле версия CMake 3.13 требуется только для запуска некоторых консольных команд, описанных в данной справке. С точки зрения синтаксиса CMake-скриптов достаточно версии 3.8, если генерацию вызывать другими способами.
CMake 3.14 требуется потому, что в предыдущих версиях некорректно работает команда `install(TARGETS ... EXPORT ...)`, а именно — не прописываются пути по умолчанию.

2. Библиотека тестирования [doctest](https://github.com/onqtam/doctest)

Expand All @@ -198,7 +238,7 @@ cmake --build путь/к/сборочной/директории --target doc

В CMake встроена поддержка инструмента для статического анализа [Cppcheck](http://cppcheck.sourceforge.net).

Для этого нужно воспользоваться опцией [`CMAKE_CXX_CPPCHECK`](https://cmake.org/cmake/help/v3.10/variable/CMAKE_LANG_CPPCHECK.html#variable:CMAKE_<LANG>_CPPCHECK):
Для этого нужно воспользоваться опцией [`CMAKE_CXX_CPPCHECK`](https://cmake.org/cmake/help/v3.14/variable/CMAKE_LANG_CPPCHECK.html#variable:CMAKE_<LANG>_CPPCHECK):

```shell
cmake -S путь/к/исходникам -B путь/к/сборочной/директории -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_CPPCHECK="cppcheck;--enable=all;-Iпуть/к/исходникам/include"
Expand Down
5 changes: 1 addition & 4 deletions include/mylib/myfeature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,5 @@ namespace mylib
\~ \see mystruct
*/
inline bool myfunc (mystruct)
{
return true;
}
bool myfunc (mystruct);
}
9 changes: 9 additions & 0 deletions src/mylib/myfeature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <mylib/myfeature.hpp>

namespace mylib
{
bool myfunc (mystruct)
{
return true;
}
}
11 changes: 8 additions & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ add_executable(mylib-unit-tests test_main.cpp)
target_sources(mylib-unit-tests PRIVATE mylib/myfeature.cpp)
target_link_libraries(mylib-unit-tests
PRIVATE
Mylib::mylib
Mylib::myfeature
doctest::doctest
)

Expand All @@ -15,11 +15,16 @@ if(MYLIB_COVERAGE AND GCOVR_EXECUTABLE)
target_compile_options(mylib-unit-tests PRIVATE --coverage)
target_link_libraries(mylib-unit-tests PRIVATE gcov)

target_compile_options(myfeature PRIVATE --coverage)
target_link_libraries(myfeature PRIVATE gcov)

add_custom_target(coverage
COMMAND
${GCOVR_EXECUTABLE}
--root=${PROJECT_SOURCE_DIR}/include/
--object-directory=${CMAKE_CURRENT_BINARY_DIR}
--root=${PROJECT_SOURCE_DIR}/
--filter=${PROJECT_SOURCE_DIR}/include
--filter=${PROJECT_SOURCE_DIR}/src
--object-directory=${PROJECT_BINARY_DIR}
DEPENDS
check
)
Expand Down

0 comments on commit 1c577d5

Please sign in to comment.