1
0
Fork 0
mirror of https://github.com/TheLartians/ModernCppStarter.git synced 2025-08-30 21:51:12 +02:00

Add CONFIGURE_DEPENDS flag to globs and cleanup (#5)

* join comments

* join target properties

* add empty lines

* update readme

* add CONFIGURE_DEPENDS to glob sources

* update comment about glob

* update comment about removing unused files

* update glob comment in the main CMakeLists
This commit is contained in:
Lars Melchior 2020-04-14 14:56:32 +02:00 committed by GitHub
parent 24aa8560db
commit 23abf01c55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 20 deletions

View file

@ -16,10 +16,10 @@ endif()
# ---- Add source files ---- # ---- Add source files ----
# Note: globbing sources is considered bad practice as CMake won't detect new files automatically. # Note: globbing sources is considered bad practice as CMake's generators may not detect new files automatically.
# Remember to always invoke cmake after changing files, or explicitly mention them here. # Keep that in mind when changing files, or explicitly mention them here.
FILE(GLOB_RECURSE headers "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h") FILE(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
FILE(GLOB_RECURSE sources "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp") FILE(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
# ---- Add dependencies via CPM ---- # ---- Add dependencies via CPM ----
# see https://github.com/TheLartians/CPM.cmake for more info # see https://github.com/TheLartians/CPM.cmake for more info
@ -35,12 +35,11 @@ CPMAddPackage(
# ---- Create library ---- # ---- Create library ----
# Note: # Note: for single header libraries create an interface target instead:
# for single header libraries use `add_library(Greeter INTERFACE)` instead # add_library(Greeter INTERFACE)
add_library(Greeter ${headers} ${sources})
# Note: for single header libraries use the following instead:
# set_target_properties(Greeter PROPERTIES INTERFACE_COMPILE_FEATURES cxx_std_17) # set_target_properties(Greeter PROPERTIES INTERFACE_COMPILE_FEATURES cxx_std_17)
add_library(Greeter ${headers} ${sources})
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17) set_target_properties(Greeter PROPERTIES CXX_STANDARD 17)
# Link dependencies (if required) # Link dependencies (if required)

View file

@ -32,7 +32,7 @@ This template is the result of learnings from many previous projects and should
- Add your project's codecov token to your project's github secrets under `CODECOV_TOKEN` - Add your project's codecov token to your project's github secrets under `CODECOV_TOKEN`
- Happy coding! - Happy coding!
Remember to eventually remove any unused files, such as the standalone directory or irrelevant tests for your project. Eventually, you can remove any unused files, such as the standalone directory or irrelevant github workflows for your project.
### Build and run the standalone target ### Build and run the standalone target
@ -81,18 +81,18 @@ See [Format.cmake](https://github.com/TheLartians/Format.cmake) for more options
Yes, however you will need to change the library type to an `INTERFACE` library as documented in the [CMakeLists.txt](CMakeLists.txt). Yes, however you will need to change the library type to an `INTERFACE` library as documented in the [CMakeLists.txt](CMakeLists.txt).
- You are using `GLOB` to add source files in CMakeLists.txt. Isn't that evil? - I see you are using `GLOB` to add source files in CMakeLists.txt. Isn't that evil?
Glob is considered bad because changes to source files won't be automatically caught by CMakes builders and you will need remember to invoke CMake on any changes. Glob is considered bad because any changes to the source file structure [might not be automatically caught](https://cmake.org/cmake/help/latest/command/file.html#filesystem) by CMake's builders and you will need to manually invoke CMake on changes.
I personally prefer the `GLOB` solution for its simplicity, but feel free to change it to explicitly listing sources. I personally prefer the `GLOB` solution for its simplicity, but feel free to change it to explicitly listing sources.
- I'm adding external dependencies to my project using CPM. Will this force users to use CPM as well? - I'm adding external dependencies to my project using CPM. Will this force users to use CPM as well?
CPM should be mostly invisible for your library users as it's self-contained and dependency free. CPM.cmake should be invisible for your library users as it's a self-contained CMake Script.
If problems do arise, they can always opt-out by defining `CPM_USE_LOCAL_PACKAGES`, which will override all calls to `CPMAddPackage` with `find_package`. If problems do arise, they can always opt-out by defining `CPM_USE_LOCAL_PACKAGES`, which will override all calls to `CPMAddPackage` with `find_package`.
If you are using `CPMFindPackage` instead of `CPMAddPackage`, CPM will always try to use `find_package` to add packages. If you concerned about this, you should prefer using `CPMFindPackage` instead of `CPMAddPackage`, as then CPM will try to use `find_package` to add packages whenever possible.
This approach should be compatible with any common C++ package manager without any user intervention, however at the cost of reproducible builds. `CPMFindPackage` approach should also be compatible with any common C++ package manager without modifications, however at the cost of reproducible builds.
For more info, see the [CPM.cmake documentation](https://github.com/TheLartians/CPM.cmake). For more information, see the [CPM.cmake documentation](https://github.com/TheLartians/CPM.cmake).
- Can I configure and build my project offline? - Can I configure and build my project offline?

View file

@ -24,8 +24,14 @@ CPMAddPackage(
# ---- Create standalone executable ---- # ---- Create standalone executable ----
file(GLOB sources ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp) file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
add_executable(GreeterStandalone ${sources}) add_executable(GreeterStandalone ${sources})
set_target_properties(GreeterStandalone PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra")
set_target_properties(GreeterStandalone PROPERTIES OUTPUT_NAME "Greeter") set_target_properties(GreeterStandalone PROPERTIES
CXX_STANDARD 17
COMPILE_FLAGS "-Wall -pedantic -Wextra"
OUTPUT_NAME "Greeter"
)
target_link_libraries(GreeterStandalone Greeter cxxopts) target_link_libraries(GreeterStandalone Greeter cxxopts)

View file

@ -37,7 +37,7 @@ CPMAddPackage(
# ---- Create binary ---- # ---- Create binary ----
file(GLOB sources ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp) file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
add_executable(GreeterTests ${sources}) add_executable(GreeterTests ${sources})
target_link_libraries(GreeterTests doctest Greeter) target_link_libraries(GreeterTests doctest Greeter)