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

style: update style

This commit is contained in:
Henry Schreiner 2020-08-03 18:16:52 -04:00
parent 6c75155baf
commit 9aa936555e
39 changed files with 95 additions and 120 deletions

View File

@ -34,4 +34,3 @@ pages:
paths: paths:
- public - public
expire_in: 1 week expire_in: 1 week

View File

@ -94,4 +94,3 @@ There are some other places to find good information on the web. Here are some o
Modern CMake was originally written by [Henry Schreiner](https://iscinumpy.gitlab.io). Other contributors can be found [listed on GitLab](https://gitlab.com/CLIUtils/modern-cmake/-/network/master). Modern CMake was originally written by [Henry Schreiner](https://iscinumpy.gitlab.io). Other contributors can be found [listed on GitLab](https://gitlab.com/CLIUtils/modern-cmake/-/network/master).
[^1]: CMake 3.0 also removed several long deprecated features from very old versions of CMake and make one very tiny backwards incompatible change to syntax related to square brackets, so this is not entirely fair; there might be some very, very old CMake files that would stop working with 3. I've never seen one, though. [^1]: CMake 3.0 also removed several long deprecated features from very old versions of CMake and make one very tiny backwards incompatible change to syntax related to square brackets, so this is not entirely fair; there might be some very, very old CMake files that would stop working with 3. I've never seen one, though.

View File

@ -173,4 +173,3 @@ target_link_libraries(calc PUBLIC calclib)
[^2]: You will sometimes see `FATAL_ERROR` here, that was needed to support nice failures when running this in CMake <2.6, which should not be a problem anymore. [^2]: You will sometimes see `FATAL_ERROR` here, that was needed to support nice failures when running this in CMake <2.6, which should not be a problem anymore.
[^3]: The `::` syntax was originally intended for `INTERFACE IMPORTED` libraries, which were explicitly supposed to be libraries defined outside the current project. But, because of this, most of the `target_*` commands don't work on `IMPORTED` libraries, making them hard to set up yourself. So don't use the `IMPORTED` keyword for now, and use an `ALIAS` target instead; it will be fine until you start exporting targets. This limitation was fixed in CMake 3.11. [^3]: The `::` syntax was originally intended for `INTERFACE IMPORTED` libraries, which were explicitly supposed to be libraries defined outside the current project. But, because of this, most of the `target_*` commands don't work on `IMPORTED` libraries, making them hard to set up yourself. So don't use the `IMPORTED` keyword for now, and use an `ALIAS` target instead; it will be fine until you start exporting targets. This limitation was fixed in CMake 3.11.

View File

@ -51,4 +51,3 @@ project(My LANGUAGES CXX VERSION ${VERSION_STRING})
``` ```
Above, `file(STRINGS file_name variable_name REGEX regex)` picks lines that match a regex; and the same regex is used to then pick out the parentheses capture group with the version part. Replace is used with back substitution to output only that one group. Above, `file(STRINGS file_name variable_name REGEX regex)` picks lines that match a regex; and the same regex is used to then pick out the parentheses capture group with the version part. Replace is used with back substitution to output only that one group.

View File

@ -42,4 +42,3 @@ Here, the generation happens after `some_target` is complete, and happens when y
A useful tool in writing CMake builds that work cross-platform is `cmake -E <mode>` (seen in CMake files as `${CMAKE_COMMAND} -E`). This mode allows CMake to do a variety of things without calling system tools explicitly, like `copy`, `make_directory`, and `remove`. It is mostly used for the build time commands. Note that the very useful `create_symlink` mode used to be Unix only, but was added for Windows in CMake 3.13. [See the docs](https://cmake.org/cmake/help/latest/manual/cmake.1.html#command-line-tool-mode). A useful tool in writing CMake builds that work cross-platform is `cmake -E <mode>` (seen in CMake files as `${CMAKE_COMMAND} -E`). This mode allows CMake to do a variety of things without calling system tools explicitly, like `copy`, `make_directory`, and `remove`. It is mostly used for the build time commands. Note that the very useful `create_symlink` mode used to be Unix only, but was added for Windows in CMake 3.13. [See the docs](https://cmake.org/cmake/help/latest/manual/cmake.1.html#command-line-tool-mode).
[execute_process]: https://cmake.org/cmake/help/latest/command/execute_process.html [execute_process]: https://cmake.org/cmake/help/latest/command/execute_process.html

View File

@ -62,4 +62,3 @@ endif()
``` ```
See the [extended code example here](https://gitlab.com/CLIUtils/modern-cmake/tree/master/examples/extended-project). See the [extended code example here](https://gitlab.com/CLIUtils/modern-cmake/tree/master/examples/extended-project).

View File

@ -49,4 +49,3 @@ If you add `--trace-expand`, the variables will be expanded into their values.
For single-configuration generators, you can build your code with `-DCMAKE_BUILD_TYPE=Debug` to get debugging flags. In multi-configuration generators, like many IDEs, you can pick the configuration in the IDE. There are distinct flags for this mode (variables ending in `_DEBUG` as opposed to `_RELEASE`), as well as a generator expression value `CONFIG:Debug` or `CONFIG:Release`. For single-configuration generators, you can build your code with `-DCMAKE_BUILD_TYPE=Debug` to get debugging flags. In multi-configuration generators, like many IDEs, you can pick the configuration in the IDE. There are distinct flags for this mode (variables ending in `_DEBUG` as opposed to `_RELEASE`), as well as a generator expression value `CONFIG:Debug` or `CONFIG:Release`.
Once you make a debug build, you can run a debugger, such as gdb or lldb on it. Once you make a debug build, you can run a debugger, such as gdb or lldb on it.

View File

@ -22,4 +22,3 @@ export(PACKAGE MyLib)
Now, if you `find_package(MyLib)`, CMake can find the build folder. Look at the generated `MyLibTargets.cmake` file to help you understand exactly what is created; it's just a normal CMake file, with the exported targets. Now, if you `find_package(MyLib)`, CMake can find the build folder. Look at the generated `MyLibTargets.cmake` file to help you understand exactly what is created; it's just a normal CMake file, with the exported targets.
Note that there's a downside: if you have imported dependencies, they will need to be imported before you `find_package`. That will be fixed in the next method. Note that there's a downside: if you have imported dependencies, they will need to be imported before you `find_package`. That will be fixed in the next method.

View File

@ -34,4 +34,3 @@ Finally, you need to include the CPack module:
```cmake ```cmake
include(CPack) include(CPack)
``` ```

View File

@ -94,4 +94,3 @@ These are common CMake options to most packages:
## Debugging your CMake files ## Debugging your CMake files
We've already mentioned verbose output for the build, but you can also see verbose CMake configure output too. The `--trace` option will print every line of CMake that is run. Since this is very verbose, CMake 3.7 added `--trace-source="filename"`, which will print out every executed line of just the file you are interested in when it runs. If you select the name of the file you are interested in debugging (usually by selecting the parent directory when debugging a CMakeLists.txt, since all of those have the same name), you can just see the lines that run in that file. Very useful! We've already mentioned verbose output for the build, but you can also see verbose CMake configure output too. The `--trace` option will print every line of CMake that is run. Since this is very verbose, CMake 3.7 added `--trace-source="filename"`, which will print out every executed line of just the file you are interested in when it runs. If you select the name of the file you are interested in debugging (usually by selecting the parent directory when debugging a CMakeLists.txt, since all of those have the same name), you can just see the lines that run in that file. Very useful!

View File

@ -133,5 +133,3 @@ You'll also might want to allow a user to check for the arch flags of their curr
```cmake ```cmake
cuda_select_nvcc_arch_flags(ARCH_FLAGS) # optional argument for arch to add cuda_select_nvcc_arch_flags(ARCH_FLAGS) # optional argument for arch to add
``` ```

View File

@ -132,4 +132,3 @@ set_property(TARGET ROOT::Flags_CXX APPEND PROPERTY
# Make sure you link with ROOT::Flags_CXX too! # Make sure you link with ROOT::Flags_CXX too!
``` ```

View File

@ -4,4 +4,3 @@ This is where a good Git system plus CMake shines. You might not be able to solv
this is pretty close for C++! this is pretty close for C++!
There are several methods listed in the chapters in this section. There are several methods listed in the chapters in this section.

View File

@ -49,4 +49,3 @@ endif()
Now you have the CMake 3.14+ syntax in CMake 3.11+. Now you have the CMake 3.14+ syntax in CMake 3.11+.
[FetchContent]: https://cmake.org/cmake/help/latest/module/FetchContent.html [FetchContent]: https://cmake.org/cmake/help/latest/module/FetchContent.html

View File

@ -2,46 +2,45 @@
cmake_minimum_required(VERSION 3.11...3.16) cmake_minimum_required(VERSION 3.11...3.16)
# Project name and a few useful settings. Other commands can pick up the results # Project name and a few useful settings. Other commands can pick up the results
project(ModernCMakeExample project(
VERSION 0.1 ModernCMakeExample
DESCRIPTION "An example project with CMake" VERSION 0.1
LANGUAGES CXX) DESCRIPTION "An example project with CMake"
LANGUAGES CXX)
# Only do these if this is the main project, and not if it is included through add_subdirectory # Only do these if this is the main project, and not if it is included through add_subdirectory
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# Optionally set things like CMAKE_CXX_STANDARD, CMAKE_POSITION_INDEPENDENT_CODE here # Optionally set things like CMAKE_CXX_STANDARD, CMAKE_POSITION_INDEPENDENT_CODE here
# Let's ensure -std=c++xx instead of -std=g++xx # Let's ensure -std=c++xx instead of -std=g++xx
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
# Let's nicely support folders in IDEs # Let's nicely support folders in IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# Testing only available if this is the main app # Testing only available if this is the main app
# Note this needs to be done in the main CMakeLists # Note this needs to be done in the main CMakeLists
# since it calls enable_testing, which must be in the # since it calls enable_testing, which must be in the
# main CMakeLists. # main CMakeLists.
include(CTest) include(CTest)
# Docs only available if this is the main app # Docs only available if this is the main app
find_package(Doxygen) find_package(Doxygen)
if(Doxygen_FOUND) if(Doxygen_FOUND)
add_subdirectory(docs) add_subdirectory(docs)
else() else()
message(STATUS "Doxygen not found, not building docs") message(STATUS "Doxygen not found, not building docs")
endif() endif()
endif() endif()
# FetchContent added in CMake 3.11, downloads during the configure step # FetchContent added in CMake 3.11, downloads during the configure step
include(FetchContent) include(FetchContent)
# FetchContent_MakeAvailable was not added until CMake 3.14; use our shim # FetchContent_MakeAvailable was not added until CMake 3.14; use our shim
if(${CMAKE_VERSION} VERSION_LESS 3.14) if(${CMAKE_VERSION} VERSION_LESS 3.14)
include(cmake/add_FetchContent_MakeAvailable.cmake) include(cmake/add_FetchContent_MakeAvailable.cmake)
endif() endif()
# Accumulator library # Accumulator library
# This is header only, so could be replaced with git submodules or FetchContent # This is header only, so could be replaced with git submodules or FetchContent
find_package(Boost REQUIRED) find_package(Boost REQUIRED)
@ -51,8 +50,7 @@ find_package(Boost REQUIRED)
FetchContent_Declare( FetchContent_Declare(
fmtlib fmtlib
GIT_REPOSITORY https://github.com/fmtlib/fmt.git GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 5.3.0 GIT_TAG 5.3.0)
)
FetchContent_MakeAvailable(fmtlib) FetchContent_MakeAvailable(fmtlib)
# Adds fmt::fmt # Adds fmt::fmt
@ -64,7 +62,7 @@ add_subdirectory(apps)
# Testing only available if this is the main app # Testing only available if this is the main app
# Emergency override MODERN_CMAKE_BUILD_TESTING provided as well # Emergency override MODERN_CMAKE_BUILD_TESTING provided as well
if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING) AND BUILD_TESTING) if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING)
add_subdirectory(tests) AND BUILD_TESTING)
add_subdirectory(tests)
endif() endif()

View File

@ -1,8 +1,7 @@
macro(FetchContent_MakeAvailable NAME) macro(FetchContent_MakeAvailable NAME)
FetchContent_GetProperties(${NAME}) FetchContent_GetProperties(${NAME})
if(NOT ${NAME}_POPULATED) if(NOT ${NAME}_POPULATED)
FetchContent_Populate(${NAME}) FetchContent_Populate(${NAME})
add_subdirectory(${${NAME}_SOURCE_DIR} ${${NAME}_BINARY_DIR}) add_subdirectory(${${NAME}_SOURCE_DIR} ${${NAME}_BINARY_DIR})
endif() endif()
endmacro() endmacro()

View File

@ -1,9 +1,5 @@
set(DOXYGEN_EXTRACT_ALL YES) set(DOXYGEN_EXTRACT_ALL YES)
set(DOXYGEN_BUILTIN_STL_SUPPORT YES) set(DOXYGEN_BUILTIN_STL_SUPPORT YES)
doxygen_add_docs(docs doxygen_add_docs(docs modern/lib.hpp "${CMAKE_CURRENT_SOURCE_DIR}/mainpage.md"
modern/lib.hpp WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/include")
"${CMAKE_CURRENT_SOURCE_DIR}/mainpage.md"
WORKING_DIRECTORY
"${PROJECT_SOURCE_DIR}/include"
)

View File

@ -10,5 +10,3 @@
std::tuple<double, double> accumulate_vector( std::tuple<double, double> accumulate_vector(
const std::vector<double>& values ///< The vector of values const std::vector<double>& values ///< The vector of values
); );

View File

@ -1,4 +1,3 @@
# Note that headers are optional, and do not affect add_library, but they will not # Note that headers are optional, and do not affect add_library, but they will not
# show up in IDEs unless they are listed in add_library. # show up in IDEs unless they are listed in add_library.
@ -19,4 +18,7 @@ target_link_libraries(modern_library PRIVATE Boost::boost)
target_compile_features(modern_library PUBLIC cxx_std_11) target_compile_features(modern_library PUBLIC cxx_std_11)
# IDEs should put the headers in a nice place # IDEs should put the headers in a nice place
source_group(TREE "${PROJECT_SOURCE_DIR}/include" PREFIX "Header Files" FILES ${HEADER_LIST}) source_group(
TREE "${PROJECT_SOURCE_DIR}/include"
PREFIX "Header Files"
FILES ${HEADER_LIST})

View File

@ -1,10 +1,8 @@
# Testing library # Testing library
FetchContent_Declare( FetchContent_Declare(
catch2 catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.9.1 GIT_TAG v2.9.1)
)
FetchContent_MakeAvailable(catch2) FetchContent_MakeAvailable(catch2)
# Adds Catch2::Catch2 # Adds Catch2::Catch2

View File

@ -9,4 +9,3 @@ TEST_CASE( "Quick check", "[main]" ) {
REQUIRE( mean == 2.0 ); REQUIRE( mean == 2.0 );
REQUIRE( moment == Approx(4.666666) ); REQUIRE( moment == Approx(4.666666) );
} }

View File

@ -1,4 +1,3 @@
## [main] ## [main]
cmake_minimum_required(VERSION 3.4...3.16) cmake_minimum_required(VERSION 3.4...3.16)
@ -28,6 +27,7 @@ message(STATUS "Original definitions: ${ROOT_DEFINITIONS}")
message(STATUS "Original exe flags: ${ROOT_EXE_LINKER_FLAGS}") message(STATUS "Original exe flags: ${ROOT_EXE_LINKER_FLAGS}")
enable_testing() enable_testing()
add_test(NAME RootDictExample add_test(
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" NAME RootDictExample
COMMAND "${ROOT_root_CMD}" -b -l -q "${CMAKE_CURRENT_SOURCE_DIR}/CheckLoad.C") WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
COMMAND "${ROOT_root_CMD}" -b -l -q "${CMAKE_CURRENT_SOURCE_DIR}/CheckLoad.C")

View File

@ -1,4 +1,3 @@
## [main] ## [main]
cmake_minimum_required(VERSION 3.1...3.16) cmake_minimum_required(VERSION 3.1...3.16)
@ -9,7 +8,6 @@ project(RootSimpleExample LANGUAGES CXX)
find_package(ROOT 6.16 CONFIG REQUIRED) find_package(ROOT 6.16 CONFIG REQUIRED)
## [find_package] ## [find_package]
# Adding an executable program and linking to needed ROOT libraries # Adding an executable program and linking to needed ROOT libraries
## [add_and_link] ## [add_and_link]
add_executable(RootSimpleExample SimpleExample.cxx) add_executable(RootSimpleExample SimpleExample.cxx)

View File

@ -1,4 +1,3 @@
## [main] ## [main]
cmake_minimum_required(VERSION 3.1...3.16) cmake_minimum_required(VERSION 3.1...3.16)
@ -17,7 +16,8 @@ include("${ROOT_USE_FILE}")
separate_arguments(ROOT_EXE_LINKER_FLAGS) separate_arguments(ROOT_EXE_LINKER_FLAGS)
add_executable(RootUseFileExample SimpleExample.cxx) add_executable(RootUseFileExample SimpleExample.cxx)
target_link_libraries(RootUseFileExample PUBLIC ${ROOT_LIBRARIES} ${ROOT_EXE_LINKER_FLAGS}) target_link_libraries(RootUseFileExample PUBLIC ${ROOT_LIBRARIES}
${ROOT_EXE_LINKER_FLAGS})
## [core] ## [core]
## [main] ## [main]

View File

@ -1,4 +1,3 @@
## [main] ## [main]
# Almost all CMake files should start with this # Almost all CMake files should start with this
@ -9,7 +8,10 @@ cmake_minimum_required(VERSION 3.1...3.16)
# This is your project statement. You should always list languages; # This is your project statement. You should always list languages;
# Listing the version is nice here since it sets lots of useful variables # Listing the version is nice here since it sets lots of useful variables
project(ModernCMakeExample VERSION 1.0 LANGUAGES CXX) project(
ModernCMakeExample
VERSION 1.0
LANGUAGES CXX)
# If you set any CMAKE_ variables, that can go here. # If you set any CMAKE_ variables, that can go here.
# (But usually don't do this, except maybe for C++ standard) # (But usually don't do this, except maybe for C++ standard)
@ -39,4 +41,3 @@ target_link_libraries(MyExample PRIVATE MyLibExample)
# you'll probably want tests too # you'll probably want tests too
enable_testing() enable_testing()
add_test(NAME MyExample COMMAND MyExample) add_test(NAME MyExample COMMAND MyExample)