1
0
mirror of synced 2024-12-22 20:50:00 +01:00

Reordering and moving things around

This commit is contained in:
Henry Fredrick Schreiner 2018-03-29 19:30:00 +02:00
parent d77b62163a
commit 6aaaef8208
11 changed files with 230 additions and 101 deletions

View File

@ -14,10 +14,14 @@
* [C++11 and Beyond](chapters/cpp11.md) * [C++11 and Beyond](chapters/cpp11.md)
* [Adding Features](chapters/features.md) * [Adding Features](chapters/features.md)
* [How to Structure Your Project](chapters/structure.md) * [How to Structure Your Project](chapters/structure.md)
* [Including Small Projects](chapters/smallinc.md) * [Including Projects](chapters/projects.md)
* [Including Large Projects (X)](chapters/largeinc.md) * [Submodule](chapters/projects/submodule.md)
* [DownloadProject](chapters/projects/download.md)
* [Fetch (3.11)](chapters/projects/fetch.md)
* [Running Other Programs (X)](chapters/programs.md) * [Running Other Programs (X)](chapters/programs.md)
* [Testing (INCOMPLETE)](chapters/testing.md) * [Testing](chapters/testing.md)
* [GoogleTest](chapters/testing/googletest.md)
* [Catch](chapters/testing/catch.md)
* [Tidy and Format (X)](chapters/tidy.md) * [Tidy and Format (X)](chapters/tidy.md)
* [IDEs (X)](chapters/IDEs.md) * [IDEs (X)](chapters/IDEs.md)
* [Debugging (X)](chapters/debug.md) * [Debugging (X)](chapters/debug.md)

View File

@ -0,0 +1,34 @@
# GoogleTest: Download method
You can use the downloader in my [CMake helper repository][CLIUtils/cmake], using CMake's `include` command.
This is a downloader for [GoogleTest], based on the excellent [DownloadProject] tool. Downloading a copy for each project is the recommended way to use GoogleTest (so much so, in fact, that they have disabled the automatic CMake install target), so this respects that design decision. This method downloads the project at configure time, so that IDE's correctly find the libraries. Using it is simple:
```cmake
cmake_minimum_required(VERSION 3.4)
project(MyProject CXX)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
enable_testing() # Must be in main file
include(AddGoogleTest) # Could be in /tests/CMakeLists.txt
add_executable(SimpleTest SimpleTest.cu)
add_gtest(SimpleTest)
```
> Note: `add_gtest` is just a macro that adds `gtest`, `gmock`, and `gtest_main`, and then runs `add_test` to create a test with the same name:
> ```cmake
> target_link_libraries(SimpleTest gtest gmock gtest_main)
> add_test(SimpleTest SimpleTest)
> ```
## Catch
[^1]: Here I've assumed that you are working on a GitHub repository by using the relative path to googletest.
[CLIUtils/cmake]: https://github.com/CLIUtils/cmake
[GoogleTest]: https://github.com/google/googletest
[DownloadProject]: https://github.com/Crascit/DownloadProject

View File

@ -0,0 +1,15 @@
# Testing
## General Testing Information
## GoogleTest
## Fetch
[CLIUtils/cmake]: https://github.com/CLIUtils/cmake
[GoogleTest]: https://github.com/google/googletest
[DownloadProject]: https://github.com/Crascit/DownloadProject

View File

@ -0,0 +1,67 @@
# GoogleTest: Submodule method (preferred)
To use this method, just checkout GoogleTest as a submodule:[^1]
```cmake
git submodule add --branch=release-1.8.0 ../../google/googletest.git extern/googletest
```
Then, in your main `CMakeLists.txt`:
```cmake
option(PACKAGE_TESTS "Build the tests" ON)
if(PACKAGE_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
```
As mentioned before, you have to do the `enable_testing` in your main CMakeLists. Now, in your tests directory:
```cmake
add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest")
```
If you did this in your main CMakeLists, you could use a normal add_subdirectory; the extra path here is needed to correct the build path because we are calling it from a subdirectory.
The next line is optional, but keeps your `CACHE` cleaner:
```cmake
mark_as_advanced(
BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS
gmock_build_tests gtest_build_samples gtest_build_tests
gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols
)
```
If you are interested in keeping IDEs that support folders clean, I would also add these lines:
```cmake
set_target_properties(gtest PROPERTIES FOLDER extern)
set_target_properties(gtest_main PROPERTIES FOLDER extern)
set_target_properties(gmock PROPERTIES FOLDER extern)
set_target_properties(gmock_main PROPERTIES FOLDER extern)
```
Then, to add a test, I'd recommend the following macro:
```cmake
macro(package_add_test TESTNAME)
add_executable(${TESTNAME} ${ARGN})
target_link_libraries(${TESTNAME} gtest gmock gtest_main)
add_test(${TESTNAME} ${TESTNAME})
set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
endmacro()
package_add_test(test1 test1.cpp)
```
This will allow you to quickly and simply add tests. Feel free to adjust to suit your needs. If you haven't seen it before, `ARGN` is "every argument after the listed ones".
[^1]: Here I've assumed that you are working on a GitHub repository by using the relative path to googletest.
[CLIUtils/cmake]: https://github.com/CLIUtils/cmake
[GoogleTest]: https://github.com/google/googletest
[DownloadProject]: https://github.com/Crascit/DownloadProject

View File

@ -2,100 +2,11 @@
## General Testing Information ## General Testing Information
In your main CMakeLists.txt you need to add the following function call (not in a subfolder):
## GoogleTest
### Submodule method (preferred)
To use this method, just checkout GoogleTest as a submodule:[^1]
```cmake ```cmake
git submodule add --branch=release-1.8.0 ../../google/googletest.git extern/googletest enable_testing()
``` ```
Then, in your main `CMakeLists.txt`:
```cmake Look at the subchapters for recipes for popular frameworks.
option(PACKAGE_TESTS "Build the tests" ON)
if(PACKAGE_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
```
As mentioned before, you have to do the `enable_testing` in your main CMakeLists. Now, in your tests directory:
```cmake
add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest")
```
If you did this in your main CMakeLists, you could use a normal add_subdirectory; the extra path here is needed to correct the build path because we are calling it from a subdirectory.
The next line is optional, but keeps your `CACHE` cleaner:
```cmake
mark_as_advanced(
BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS
gmock_build_tests gtest_build_samples gtest_build_tests
gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols
)
```
If you are interested in keeping IDEs that support folders clean, I would also add these lines:
```cmake
set_target_properties(gtest PROPERTIES FOLDER extern)
set_target_properties(gtest_main PROPERTIES FOLDER extern)
set_target_properties(gmock PROPERTIES FOLDER extern)
set_target_properties(gmock_main PROPERTIES FOLDER extern)
```
Then, to add a test, I'd recommend the following macro:
```cmake
macro(package_add_test TESTNAME)
add_executable(${TESTNAME} ${ARGN})
target_link_libraries(${TESTNAME} gtest gmock gtest_main)
add_test(${TESTNAME} ${TESTNAME})
set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
endmacro()
package_add_test(test1 test1.cpp)
```
This will allow you to quickly and simply add tests. Feel free to adjust to suit your needs. If you haven't seen it before, `ARGN` is "every argument after the listed ones".
### Download method
You can use the downloader in my [CMake helper repository][CLIUtils/cmake], using CMake's `include` command.
This is a downloader for [GoogleTest], based on the excellent [DownloadProject] tool. Downloading a copy for each project is the recommended way to use GoogleTest (so much so, in fact, that they have disabled the automatic CMake install target), so this respects that design decision. This method downloads the project at configure time, so that IDE's correctly find the libraries. Using it is simple:
```cmake
cmake_minimum_required(VERSION 3.4)
project(MyProject CXX)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
enable_testing() # Must be in main file
include(AddGoogleTest) # Could be in /tests/CMakeLists.txt
add_executable(SimpleTest SimpleTest.cu)
add_gtest(SimpleTest)
```
> Note: `add_gtest` is just a macro that adds `gtest`, `gmock`, and `gtest_main`, and then runs `add_test` to create a test with the same name:
> ```cmake
> target_link_libraries(SimpleTest gtest gmock gtest_main)
> add_test(SimpleTest SimpleTest)
> ```
## Catch
[^1]: Here I've assumed that you are working on a GitHub repository by using the relative path to googletest.
[CLIUtils/cmake]: https://github.com/CLIUtils/cmake
[GoogleTest]: https://github.com/google/googletest
[DownloadProject]: https://github.com/Crascit/DownloadProject

View File

@ -0,0 +1,4 @@
# Catch

View File

@ -0,0 +1,94 @@
# GoogleTest
## Submodule method (preferred)
To use this method, just checkout GoogleTest as a submodule:[^1]
```cmake
git submodule add --branch=release-1.8.0 ../../google/googletest.git extern/googletest
```
Then, in your main `CMakeLists.txt`:
```cmake
option(PACKAGE_TESTS "Build the tests" ON)
if(PACKAGE_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
```
As mentioned before, you have to do the `enable_testing` in your main CMakeLists. Now, in your tests directory:
```cmake
add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest")
```
If you did this in your main CMakeLists, you could use a normal add_subdirectory; the extra path here is needed to correct the build path because we are calling it from a subdirectory.
The next line is optional, but keeps your `CACHE` cleaner:
```cmake
mark_as_advanced(
BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS
gmock_build_tests gtest_build_samples gtest_build_tests
gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols
)
```
If you are interested in keeping IDEs that support folders clean, I would also add these lines:
```cmake
set_target_properties(gtest PROPERTIES FOLDER extern)
set_target_properties(gtest_main PROPERTIES FOLDER extern)
set_target_properties(gmock PROPERTIES FOLDER extern)
set_target_properties(gmock_main PROPERTIES FOLDER extern)
```
Then, to add a test, I'd recommend the following macro:
```cmake
macro(package_add_test TESTNAME)
add_executable(${TESTNAME} ${ARGN})
target_link_libraries(${TESTNAME} gtest gmock gtest_main)
add_test(${TESTNAME} ${TESTNAME})
set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
endmacro()
package_add_test(test1 test1.cpp)
```
This will allow you to quickly and simply add tests. Feel free to adjust to suit your needs. If you haven't seen it before, `ARGN` is "every argument after the listed ones".
## Download method
You can use the downloader in my [CMake helper repository][CLIUtils/cmake], using CMake's `include` command.
This is a downloader for [GoogleTest], based on the excellent [DownloadProject] tool. Downloading a copy for each project is the recommended way to use GoogleTest (so much so, in fact, that they have disabled the automatic CMake install target), so this respects that design decision. This method downloads the project at configure time, so that IDE's correctly find the libraries. Using it is simple:
```cmake
cmake_minimum_required(VERSION 3.4)
project(MyProject CXX)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
enable_testing() # Must be in main file
include(AddGoogleTest) # Could be in /tests/CMakeLists.txt
add_executable(SimpleTest SimpleTest.cu)
add_gtest(SimpleTest)
```
> Note: `add_gtest` is just a macro that adds `gtest`, `gmock`, and `gtest_main`, and then runs `add_test` to create a test with the same name:
> ```cmake
> target_link_libraries(SimpleTest gtest gmock gtest_main)
> add_test(SimpleTest SimpleTest)
> ```
[^1]: Here I've assumed that you are working on a GitHub repository by using the relative path to googletest.
[CLIUtils/cmake]: https://github.com/CLIUtils/cmake
[GoogleTest]: https://github.com/google/googletest
[DownloadProject]: https://github.com/Crascit/DownloadProject

View File

@ -9,11 +9,11 @@ include("${ROOT_DIR}/modules/RootNewMacros.cmake")
message(STATUS "Found ROOT: ${ROOT_VERSION} at ${ROOT_DIR}") message(STATUS "Found ROOT: ${ROOT_VERSION} at ${ROOT_DIR}")
string(REPLACE " " ";" ROOT_CXX_FLAG_LIST "${ROOT_CXX_FLAGS}") separate_arguments(ROOT_CXX_FLAGS)
set_target_properties(ROOT::Core PROPERTIES set_target_properties(ROOT::Core PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${ROOT_INCLUDE_DIRS}" INTERFACE_INCLUDE_DIRECTORIES "${ROOT_INCLUDE_DIRS}"
INTERFACE_COMPILE_OPTIONS "${ROOT_CXX_FLAG_LIST}" INTERFACE_COMPILE_OPTIONS "${ROOT_CXX_FLAGS}"
) )
include_directories(ROOT_BUG) include_directories(ROOT_BUG)

View File

@ -7,11 +7,11 @@ project(RootSimpleExample311 LANGUAGES CXX)
find_package(ROOT CONFIG REQUIRED) find_package(ROOT CONFIG REQUIRED)
message(STATUS "Found ROOT: ${ROOT_VERSION} at ${ROOT_DIR}") message(STATUS "Found ROOT: ${ROOT_VERSION} at ${ROOT_DIR}")
string(REPLACE " " ";" ROOT_CXX_FLAG_LIST "${ROOT_CXX_FLAGS}") seperate_arguments(ROOT_CXX_FLAGS)
## [modern_fix] ## [modern_fix]
target_include_directories(ROOT::Core INTERFACE "${ROOT_INCLUDE_DIRS}") target_include_directories(ROOT::Core INTERFACE "${ROOT_INCLUDE_DIRS}")
target_compile_options(ROOT::Core INTERFACE "${ROOT_CXX_FLAG_LIST}") target_compile_options(ROOT::Core INTERFACE "${ROOT_CXX_FLAGS}")
## [modern_fix] ## [modern_fix]
add_executable(RootSimpleExample311 SimpleExample.cxx) add_executable(RootSimpleExample311 SimpleExample.cxx)

View File

@ -13,12 +13,12 @@ message(STATUS "Found ROOT: ${ROOT_VERSION} at ${ROOT_DIR}")
# ROOT targets are missing includes and flags # ROOT targets are missing includes and flags
## [setup_properties] ## [setup_properties]
# Fix for ROOT_CXX_FLAGS not actually being a CMake list # Fix for ROOT_CXX_FLAGS not actually being a CMake list
string(REPLACE " " ";" ROOT_CXX_FLAG_LIST "${ROOT_CXX_FLAGS}") separate_arguments(ROOT_CXX_FLAGS)
set_property(TARGET ROOT::Core PROPERTY set_property(TARGET ROOT::Core PROPERTY
INTERFACE_INCLUDE_DIRECTORIES "${ROOT_INCLUDE_DIRS}") INTERFACE_INCLUDE_DIRECTORIES "${ROOT_INCLUDE_DIRS}")
set_property(TARGET ROOT::Core APPEND PROPERTY set_property(TARGET ROOT::Core APPEND PROPERTY
INTERFACE_COMPILE_OPTIONS ${ROOT_CXX_FLAG_LIST}) INTERFACE_COMPILE_OPTIONS ${ROOT_CXX_FLAGS})
## [setup_properties] ## [setup_properties]
# Adding an exectuable program and linking to needed ROOT libraries # Adding an exectuable program and linking to needed ROOT libraries