mirror of
				https://github.com/TheLartians/ModernCppStarter.git
				synced 2025-10-31 02:01:33 +01:00 
			
		
		
		
	init
This commit is contained in:
		
						commit
						c94580f25c
					
				
					 15 changed files with 711 additions and 0 deletions
				
			
		
							
								
								
									
										16
									
								
								.clang-format
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								.clang-format
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,16 @@ | |||
| --- | ||||
|   BasedOnStyle: Google | ||||
|   AccessModifierOffset: '-2' | ||||
|   AlignTrailingComments: 'true' | ||||
|   AllowAllParametersOfDeclarationOnNextLine: 'false' | ||||
|   AlwaysBreakTemplateDeclarations: 'No' | ||||
|   BreakBeforeBraces: Attach | ||||
|   ColumnLimit: '100' | ||||
|   ConstructorInitializerAllOnOneLineOrOnePerLine: 'true' | ||||
|   IncludeBlocks: Regroup | ||||
|   IndentPPDirectives: AfterHash | ||||
|   IndentWidth: '2' | ||||
|   NamespaceIndentation: All | ||||
|   BreakBeforeBinaryOperators: All | ||||
|   BreakBeforeTernaryOperators: 'true' | ||||
| ... | ||||
							
								
								
									
										25
									
								
								.github/workflows/macos.yml
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								.github/workflows/macos.yml
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,25 @@ | |||
| name: Style | ||||
| 
 | ||||
| on: [push] | ||||
| 
 | ||||
| env: | ||||
|   CTEST_OUTPUT_ON_FAILURE: 1 | ||||
| 
 | ||||
| jobs: | ||||
|   build: | ||||
| 
 | ||||
|     runs-on: macos-latest | ||||
|      | ||||
|     steps: | ||||
|     - uses: actions/checkout@v1 | ||||
|      | ||||
|     - name: configure | ||||
|       run: cmake -Htest -Bbuild | ||||
| 
 | ||||
|     - name: build | ||||
|       run: cmake --build build --config Debug | ||||
| 
 | ||||
|     - name: test | ||||
|       run: | | ||||
|         cd build/debug | ||||
|         ctest --build-config Debug | ||||
							
								
								
									
										20
									
								
								.github/workflows/style.yml
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								.github/workflows/style.yml
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | |||
| name: MacOS | ||||
| 
 | ||||
| on: [push] | ||||
| 
 | ||||
| jobs: | ||||
|   build: | ||||
| 
 | ||||
|     runs-on: macos-latest | ||||
|      | ||||
|     steps: | ||||
|     - uses: actions/checkout@v1 | ||||
|      | ||||
|     - name: Install clang-format | ||||
|       run: brew install clang-format | ||||
| 
 | ||||
|     - name: configure | ||||
|       run: cmake -Htest -Bbuild | ||||
| 
 | ||||
|     - name: check style | ||||
|       run: cmake --build build --target check-format | ||||
							
								
								
									
										25
									
								
								.github/workflows/ubuntu.yml
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								.github/workflows/ubuntu.yml
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,25 @@ | |||
| name: Ubuntu | ||||
| 
 | ||||
| on: [push] | ||||
| 
 | ||||
| env: | ||||
|   CTEST_OUTPUT_ON_FAILURE: 1 | ||||
| 
 | ||||
| jobs: | ||||
|   build: | ||||
| 
 | ||||
|     runs-on: ubuntu-latest | ||||
|      | ||||
|     steps: | ||||
|     - uses: actions/checkout@v1 | ||||
|      | ||||
|     - name: configure | ||||
|       run: cmake -Htest -Bbuild | ||||
| 
 | ||||
|     - name: build | ||||
|       run: cmake --build build --config Debug | ||||
| 
 | ||||
|     - name: test | ||||
|       run: | | ||||
|         cd build/debug | ||||
|         ctest --build-config Debug | ||||
							
								
								
									
										25
									
								
								.github/workflows/windows.yml
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								.github/workflows/windows.yml
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,25 @@ | |||
| name: Windows | ||||
| 
 | ||||
| on: [push] | ||||
| 
 | ||||
| env: | ||||
|   CTEST_OUTPUT_ON_FAILURE: 1 | ||||
| 
 | ||||
| jobs: | ||||
|   build: | ||||
| 
 | ||||
|     runs-on: windows-latest | ||||
|      | ||||
|     steps: | ||||
|     - uses: actions/checkout@v1 | ||||
|      | ||||
|     - name: configure | ||||
|       run: cmake -Htest -Bbuild | ||||
| 
 | ||||
|     - name: build | ||||
|       run: cmake --build build --config Debug | ||||
| 
 | ||||
|     - name: test | ||||
|       run: | | ||||
|         cd build/debug | ||||
|         ctest --build-config Debug | ||||
							
								
								
									
										3
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | |||
| /build | ||||
| /.vscode | ||||
| .DS_Store | ||||
							
								
								
									
										103
									
								
								CMakeLists.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								CMakeLists.txt
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,103 @@ | |||
| cmake_minimum_required(VERSION 3.5 FATAL_ERROR) | ||||
| 
 | ||||
| # ---- Project ---- | ||||
| 
 | ||||
| project(Greeter  | ||||
|   VERSION 1.0 | ||||
|   LANGUAGES CXX | ||||
| ) | ||||
| 
 | ||||
| # ---- Include guards ---- | ||||
| 
 | ||||
| if(${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR}) | ||||
|     message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.") | ||||
| endif() | ||||
| 
 | ||||
| # ---- Add source files ---- | ||||
| 
 | ||||
| # Note: globbing sources is considered bad practice as CMake won't detect new files automatically. | ||||
| # Remember to always invoke cmake after changing files, or explicitly mention them here. | ||||
| FILE(GLOB_RECURSE headers "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h") | ||||
| FILE(GLOB_RECURSE sources "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp") | ||||
| 
 | ||||
| # ---- Add dependencies via CPM (if required) ---- | ||||
| 
 | ||||
| # Example: cxxopts | ||||
| # see https://github.com/TheLartians/CPM.cmake for more info | ||||
| # include(cmake/CPM.cmake) | ||||
| 
 | ||||
| # CPMAddPackage( | ||||
| #   NAME cxxopts | ||||
| #   GITHUB_REPOSITORY jarro2783/cxxopts | ||||
| #   VERSION 2.2.0 | ||||
| #   OPTIONS | ||||
| #     "CXXOPTS_BUILD_EXAMPLES Off" | ||||
| #     "CXXOPTS_BUILD_TESTS Off" | ||||
| # ) | ||||
| 
 | ||||
| # ---- Create library ---- | ||||
| 
 | ||||
| # Note: for single header libraries use the following 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 CXX_STANDARD 17) | ||||
| 
 | ||||
| # Link dependencies (if required) | ||||
| # target_link_libraries(Greeter cxxopts) | ||||
| 
 | ||||
| # Node: change PUBLIC to INTERFACE for single header libraries   | ||||
| target_include_directories(Greeter | ||||
|   PUBLIC  | ||||
|     $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> | ||||
|     $<INSTALL_INTERFACE:include> | ||||
| ) | ||||
| 
 | ||||
| # ---- Create an installable target ---- | ||||
| # this allows users install and find the library via `find_package(Greeter)`. | ||||
| 
 | ||||
| include(CMakePackageConfigHelpers) | ||||
| 
 | ||||
| write_basic_package_version_file( | ||||
|   "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" | ||||
|   VERSION ${PROJECT_VERSION} | ||||
|   COMPATIBILITY AnyNewerVersion | ||||
| ) | ||||
| 
 | ||||
| install( | ||||
|   TARGETS ${PROJECT_NAME} | ||||
|   EXPORT ${PROJECT_NAME}Targets | ||||
|   LIBRARY DESTINATION lib COMPONENT Runtime | ||||
|   ARCHIVE DESTINATION lib COMPONENT Development | ||||
|   RUNTIME DESTINATION bin COMPONENT Runtime | ||||
|   PUBLIC_HEADER DESTINATION include COMPONENT Development | ||||
|   BUNDLE DESTINATION bin COMPONENT Runtime | ||||
| ) | ||||
| 
 | ||||
| include(CMakePackageConfigHelpers) | ||||
| 
 | ||||
| configure_package_config_file( | ||||
|   "${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in" | ||||
|   "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" | ||||
|   INSTALL_DESTINATION lib/cmake/${PROJECT_NAME} | ||||
| ) | ||||
| 
 | ||||
| install( | ||||
|   EXPORT ${PROJECT_NAME}Targets  | ||||
|   DESTINATION lib/cmake/${PROJECT_NAME} | ||||
| ) | ||||
| 
 | ||||
| install( | ||||
|   FILES  | ||||
|     "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" | ||||
|     "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" | ||||
|   DESTINATION  | ||||
|     lib/cmake/${PROJECT_NAME} | ||||
| ) | ||||
| 
 | ||||
| install( | ||||
|   DIRECTORY ${PROJECT_SOURCE_DIR}/include/ | ||||
|   DESTINATION include | ||||
| ) | ||||
							
								
								
									
										54
									
								
								README.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								README.md
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,54 @@ | |||
| [](https://github.com/TheLartians/Greeter/actions) | ||||
| [](https://github.com/TheLartians/Greeter/actions) | ||||
| [](https://github.com/TheLartians/Greeter/actions) | ||||
| [](https://github.com/TheLartians/Greeter/actions) | ||||
| 
 | ||||
| # Greeter | ||||
| 
 | ||||
| A best-practice git template for modern C++ libraries and projects. | ||||
| 
 | ||||
| ## Features | ||||
| 
 | ||||
| - Modern CMakeLists.txt | ||||
| - Suited for single header libraries and larger projects | ||||
| - Creates a library that can be installed or included locally | ||||
| - Integrated test suite | ||||
| - Code formatting via clang-format via [Format.cmake](https://github.com/TheLartians/Format.cmake) | ||||
| - Continuous integration via GitHub Workflows | ||||
| - Reliable dependency management via [CPM.cmake](https://github.com/TheLartians/CPM.cmake) | ||||
| - Check compiler warnings | ||||
| 
 | ||||
| ## Roadmap | ||||
| 
 | ||||
| - Add code coverage checks | ||||
| - Add a script to automatically rename project / switch to single-header mode | ||||
|   | ||||
| ## Usage | ||||
| 
 | ||||
| ### Adjust the template to your needs | ||||
| 
 | ||||
| - Copy this repo and replace all occurrences of "Greeter" in the CMakeLists and Readme with project's name. | ||||
| - Note the comments in the CMakeLists for single-header libraries | ||||
| - Have fun! | ||||
| 
 | ||||
| ### Build and run test suite | ||||
| 
 | ||||
| ```bash | ||||
| cmake -Htest -Bbuild | ||||
| cmake --build build | ||||
| CTEST_OUTPUT_ON_FAILURE=1 cmake --build build --target test | ||||
| # or simply call the executable:  | ||||
| ./build/GreeterTests | ||||
| ``` | ||||
| 
 | ||||
| ### Run clang-format | ||||
| 
 | ||||
| ```bash | ||||
| cmake -Htest -Bbuild | ||||
| # view changes | ||||
| cmake --build build --target format | ||||
| # apply changes | ||||
| cmake --build build --target fix-format | ||||
| ``` | ||||
| 
 | ||||
| See [Format.cmake](https://github.com/TheLartians/Format.cmake) for more options. | ||||
							
								
								
									
										329
									
								
								cmake/CPM.cmake
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										329
									
								
								cmake/CPM.cmake
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,329 @@ | |||
| # TheLartians/CPM - A simple Git dependency manager | ||||
| # ================================================= | ||||
| # See https://github.com/TheLartians/CPM for usage and update instructions. | ||||
| # | ||||
| # MIT License | ||||
| # -----------  | ||||
| #[[ | ||||
|   Copyright (c) 2019 Lars Melchior | ||||
| 
 | ||||
|   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|   of this software and associated documentation files (the "Software"), to deal | ||||
|   in the Software without restriction, including without limitation the rights | ||||
|   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|   copies of the Software, and to permit persons to whom the Software is | ||||
|   furnished to do so, subject to the following conditions: | ||||
| 
 | ||||
|   The above copyright notice and this permission notice shall be included in all | ||||
|   copies or substantial portions of the Software. | ||||
| 
 | ||||
|   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
|   SOFTWARE. | ||||
| ]] | ||||
| 
 | ||||
| cmake_minimum_required(VERSION 3.14 FATAL_ERROR) | ||||
| 
 | ||||
| set(CURRENT_CPM_VERSION 0.17) | ||||
| 
 | ||||
| if(CPM_DIRECTORY) | ||||
|   if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR}) | ||||
|     if (${CPM_VERSION} VERSION_LESS ${CURRENT_CPM_VERSION}) | ||||
|       message(AUTHOR_WARNING "${CPM_INDENT} \ | ||||
| A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \ | ||||
| It is recommended to upgrade CPM to the most recent version. \ | ||||
| See https://github.com/TheLartians/CPM.cmake for more information." | ||||
|       ) | ||||
|     endif() | ||||
|     return() | ||||
|   endif() | ||||
| endif() | ||||
| 
 | ||||
| option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" $ENV{CPM_USE_LOCAL_PACKAGES}) | ||||
| option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" $ENV{CPM_LOCAL_PACKAGES_ONLY}) | ||||
| option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL}) | ||||
| 
 | ||||
| set(CPM_VERSION ${CURRENT_CPM_VERSION} CACHE INTERNAL "") | ||||
| set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "") | ||||
| set(CPM_PACKAGES "" CACHE INTERNAL "") | ||||
| set(CPM_DRY_RUN OFF CACHE INTERNAL "Don't download or configure dependencies (for testing)") | ||||
| 
 | ||||
| if(DEFINED ENV{CPM_SOURCE_CACHE}) | ||||
|   set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE}) | ||||
| else() | ||||
|   set(CPM_SOURCE_CACHE_DEFAULT OFF) | ||||
| endif() | ||||
| 
 | ||||
| set(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE_DEFAULT} CACHE PATH "Directory to downlaod CPM dependencies") | ||||
| 
 | ||||
| include(FetchContent) | ||||
| include(CMakeParseArguments) | ||||
| 
 | ||||
| # Initialize logging prefix | ||||
| if(NOT CPM_INDENT) | ||||
|   set(CPM_INDENT "CPM:") | ||||
| endif() | ||||
| 
 | ||||
| function(cpm_find_package NAME VERSION) | ||||
|   string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") | ||||
|   find_package(${NAME} ${VERSION} ${EXTRA_ARGS}) | ||||
|   if(${CPM_ARGS_NAME}_FOUND) | ||||
|     message(STATUS "${CPM_INDENT} using local package ${CPM_ARGS_NAME}@${${CPM_ARGS_NAME}_VERSION}") | ||||
|     CPMRegisterPackage(${CPM_ARGS_NAME} "${${CPM_ARGS_NAME}_VERSION}") | ||||
|     set(CPM_PACKAGE_FOUND YES PARENT_SCOPE) | ||||
|   else() | ||||
|     set(CPM_PACKAGE_FOUND NO PARENT_SCOPE) | ||||
|   endif() | ||||
| endfunction() | ||||
| 
 | ||||
| # Find a package locally or fallback to CPMAddPackage | ||||
| function(CPMFindPackage) | ||||
|   set(oneValueArgs | ||||
|     NAME | ||||
|     VERSION | ||||
|     FIND_PACKAGE_ARGUMENTS | ||||
|   ) | ||||
| 
 | ||||
|   cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN}) | ||||
|    | ||||
|   if (CPM_DOWNLOAD_ALL) | ||||
|     CPMAddPackage(${ARGN}) | ||||
|     cpm_export_variables() | ||||
|     return() | ||||
|   endif() | ||||
| 
 | ||||
|   cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) | ||||
| 
 | ||||
|   if(NOT CPM_PACKAGE_FOUND) | ||||
|     CPMAddPackage(${ARGN}) | ||||
|     cpm_export_variables() | ||||
|   endif() | ||||
| 
 | ||||
| endfunction() | ||||
| 
 | ||||
| # Download and add a package from source | ||||
| function(CPMAddPackage) | ||||
| 
 | ||||
|   set(oneValueArgs | ||||
|     NAME | ||||
|     VERSION | ||||
|     GIT_TAG | ||||
|     DOWNLOAD_ONLY | ||||
|     GITHUB_REPOSITORY | ||||
|     GITLAB_REPOSITORY | ||||
|     SOURCE_DIR | ||||
|     DOWNLOAD_COMMAND | ||||
|     FIND_PACKAGE_ARGUMENTS | ||||
|   ) | ||||
| 
 | ||||
|   set(multiValueArgs | ||||
|     OPTIONS | ||||
|   ) | ||||
| 
 | ||||
|   cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") | ||||
| 
 | ||||
|   if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY) | ||||
|     cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) | ||||
| 
 | ||||
|     if(CPM_PACKAGE_FOUND) | ||||
|       return() | ||||
|     endif() | ||||
| 
 | ||||
|     if(CPM_LOCAL_PACKAGES_ONLY)  | ||||
|       message(SEND_ERROR "CPM: ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})") | ||||
|     endif() | ||||
|   endif() | ||||
| 
 | ||||
|   if (NOT DEFINED CPM_ARGS_VERSION) | ||||
|     if (DEFINED CPM_ARGS_GIT_TAG)  | ||||
|       cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) | ||||
|     endif() | ||||
|     if (NOT DEFINED CPM_ARGS_VERSION)  | ||||
|       set(CPM_ARGS_VERSION 0) | ||||
|     endif() | ||||
|   endif() | ||||
| 
 | ||||
|   if (NOT DEFINED CPM_ARGS_GIT_TAG) | ||||
|     set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) | ||||
|   endif() | ||||
| 
 | ||||
|   list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) | ||||
| 
 | ||||
|   if(CPM_ARGS_DOWNLOAD_ONLY) | ||||
|     set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY}) | ||||
|   else() | ||||
|     set(DOWNLOAD_ONLY NO) | ||||
|   endif() | ||||
| 
 | ||||
|   if (CPM_ARGS_GITHUB_REPOSITORY) | ||||
|     list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git") | ||||
|   endif() | ||||
| 
 | ||||
|   if (CPM_ARGS_GITLAB_REPOSITORY) | ||||
|     list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") | ||||
|   endif() | ||||
| 
 | ||||
|   if (${CPM_ARGS_NAME} IN_LIST CPM_PACKAGES) | ||||
|     CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION) | ||||
|     if(${CPM_PACKAGE_VERSION} VERSION_LESS ${CPM_ARGS_VERSION}) | ||||
|       message(WARNING "${CPM_INDENT} requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION}).") | ||||
|     endif() | ||||
|     if (CPM_ARGS_OPTIONS) | ||||
|       foreach(OPTION ${CPM_ARGS_OPTIONS}) | ||||
|         cpm_parse_option(${OPTION}) | ||||
|         if(NOT "${${OPTION_KEY}}" STREQUAL ${OPTION_VALUE}) | ||||
|           message(WARNING "${CPM_INDENT} ignoring package option for ${CPM_ARGS_NAME}: ${OPTION_KEY} = ${OPTION_VALUE} (${${OPTION_KEY}})") | ||||
|         endif() | ||||
|       endforeach() | ||||
|     endif() | ||||
|     cpm_fetch_package(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY}) | ||||
|     cpm_get_fetch_properties(${CPM_ARGS_NAME}) | ||||
|     SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}") | ||||
|     SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}")   | ||||
|     SET(${CPM_ARGS_NAME}_ADDED NO) | ||||
|     cpm_export_variables() | ||||
|     return() | ||||
|   endif() | ||||
| 
 | ||||
|   CPMRegisterPackage(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION}) | ||||
| 
 | ||||
|   if (CPM_ARGS_OPTIONS) | ||||
|     foreach(OPTION ${CPM_ARGS_OPTIONS}) | ||||
|       cpm_parse_option(${OPTION}) | ||||
|       set(${OPTION_KEY} ${OPTION_VALUE} CACHE INTERNAL "") | ||||
|     endforeach() | ||||
|   endif() | ||||
| 
 | ||||
|   set(FETCH_CONTENT_DECLARE_EXTRA_OPTS "") | ||||
| 
 | ||||
|   if (DEFINED CPM_ARGS_GIT_TAG) | ||||
|     set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}") | ||||
|   else() | ||||
|     set(PACKAGE_INFO "${CPM_ARGS_VERSION}") | ||||
|   endif() | ||||
| 
 | ||||
|   if (DEFINED CPM_ARGS_DOWNLOAD_COMMAND) | ||||
|     set(FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) | ||||
|   elseif(DEFINED CPM_ARGS_SOURCE_DIR) | ||||
|     set(FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) | ||||
|   elseif (CPM_SOURCE_CACHE) | ||||
|     string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) | ||||
|     set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) | ||||
|     list(SORT origin_parameters) | ||||
|     string(SHA1 origin_hash "${origin_parameters}") | ||||
|     set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) | ||||
|     list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${download_directory}) | ||||
|     if (EXISTS ${download_directory}) | ||||
|       list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ":") | ||||
|       set(PACKAGE_INFO "${download_directory}") | ||||
|     else() | ||||
|       # remove timestamps so CMake will re-download the dependency | ||||
|       file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-subbuild) | ||||
|       set(PACKAGE_INFO "${PACKAGE_INFO} -> ${download_directory}") | ||||
|     endif() | ||||
|   endif() | ||||
| 
 | ||||
|   cpm_declare_fetch(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} ${PACKAGE_INFO} "${CPM_ARGS_UNPARSED_ARGUMENTS}" ${FETCH_CONTENT_DECLARE_EXTRA_OPTS}) | ||||
|   cpm_fetch_package(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY}) | ||||
|   cpm_get_fetch_properties(${CPM_ARGS_NAME}) | ||||
|   SET(${CPM_ARGS_NAME}_ADDED YES) | ||||
|   cpm_export_variables() | ||||
| endfunction() | ||||
| 
 | ||||
| # export variables available to the caller to the parent scope | ||||
| # expects ${CPM_ARGS_NAME} to be set | ||||
| macro(cpm_export_variables) | ||||
|   SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}" PARENT_SCOPE) | ||||
|   SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" PARENT_SCOPE) | ||||
|   SET(${CPM_ARGS_NAME}_ADDED "${${CPM_ARGS_NAME}_ADDED}" PARENT_SCOPE) | ||||
| endmacro() | ||||
| 
 | ||||
| # declares that a package has been added to CPM | ||||
| function(CPMRegisterPackage PACKAGE VERSION) | ||||
|   list(APPEND CPM_PACKAGES ${PACKAGE}) | ||||
|   set(CPM_PACKAGES ${CPM_PACKAGES} CACHE INTERNAL "") | ||||
|   set("CPM_PACKAGE_${PACKAGE}_VERSION" ${VERSION} CACHE INTERNAL "") | ||||
| endfunction() | ||||
| 
 | ||||
| # retrieve the current version of the package to ${OUTPUT} | ||||
| function(CPMGetPackageVersion PACKAGE OUTPUT) | ||||
|   set(${OUTPUT} "${CPM_PACKAGE_${PACKAGE}_VERSION}" PARENT_SCOPE) | ||||
| endfunction() | ||||
| 
 | ||||
| # declares a package in FetchContent_Declare  | ||||
| function (cpm_declare_fetch PACKAGE VERSION INFO) | ||||
|   message(STATUS "${CPM_INDENT} adding package ${PACKAGE}@${VERSION} (${INFO})") | ||||
| 
 | ||||
|   if (${CPM_DRY_RUN})  | ||||
|     message(STATUS "${CPM_INDENT} package not declared (dry run)") | ||||
|     return() | ||||
|   endif() | ||||
| 
 | ||||
|   FetchContent_Declare( | ||||
|     ${PACKAGE} | ||||
|     ${ARGN} | ||||
|   ) | ||||
| endfunction() | ||||
| 
 | ||||
| # returns properties for a package previously defined by cpm_declare_fetch | ||||
| function (cpm_get_fetch_properties PACKAGE) | ||||
|   if (${CPM_DRY_RUN})  | ||||
|     return() | ||||
|   endif() | ||||
|   FetchContent_GetProperties(${PACKAGE}) | ||||
|   string(TOLOWER ${PACKAGE} lpackage) | ||||
|   SET(${PACKAGE}_SOURCE_DIR "${${lpackage}_SOURCE_DIR}" PARENT_SCOPE) | ||||
|   SET(${PACKAGE}_BINARY_DIR "${${lpackage}_BINARY_DIR}" PARENT_SCOPE) | ||||
| endfunction() | ||||
| 
 | ||||
| # downloads a previously declared package via FetchContent | ||||
| function (cpm_fetch_package PACKAGE DOWNLOAD_ONLY)   | ||||
| 
 | ||||
|   if (${CPM_DRY_RUN})  | ||||
|     message(STATUS "${CPM_INDENT} package ${PACKAGE} not fetched (dry run)") | ||||
|     return() | ||||
|   endif() | ||||
| 
 | ||||
|   set(CPM_OLD_INDENT "${CPM_INDENT}") | ||||
|   set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") | ||||
|   if(${DOWNLOAD_ONLY}) | ||||
|     if(NOT "${PACKAGE}_POPULATED") | ||||
|       FetchContent_Populate(${PACKAGE}) | ||||
|     endif() | ||||
|   else() | ||||
|     FetchContent_MakeAvailable(${PACKAGE}) | ||||
|   endif() | ||||
|   set(CPM_INDENT "${CPM_OLD_INDENT}") | ||||
| endfunction() | ||||
| 
 | ||||
| # splits a package option | ||||
| function(cpm_parse_option OPTION) | ||||
|   string(REGEX MATCH "^[^ ]+" OPTION_KEY ${OPTION}) | ||||
|   string(LENGTH ${OPTION} OPTION_LENGTH) | ||||
|   string(LENGTH ${OPTION_KEY} OPTION_KEY_LENGTH) | ||||
|   if (OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH) | ||||
|     # no value for key provided, assume user wants to set option to "ON" | ||||
|     set(OPTION_VALUE "ON") | ||||
|   else() | ||||
|     math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1") | ||||
|     string(SUBSTRING ${OPTION} "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) | ||||
|   endif() | ||||
|   set(OPTION_KEY "${OPTION_KEY}" PARENT_SCOPE) | ||||
|   set(OPTION_VALUE "${OPTION_VALUE}" PARENT_SCOPE) | ||||
| endfunction() | ||||
| 
 | ||||
| # guesses the package version from a git tag | ||||
| function(cpm_get_version_from_git_tag GIT_TAG RESULT) | ||||
|   string(LENGTH ${GIT_TAG} length) | ||||
|   if (length EQUAL 40)  | ||||
|     # GIT_TAG is probably a git hash | ||||
|     SET(${RESULT} 0 PARENT_SCOPE) | ||||
|   else() | ||||
|     string(REGEX MATCH "v?([0123456789.]*).*" _ ${GIT_TAG}) | ||||
|     SET(${RESULT} ${CMAKE_MATCH_1} PARENT_SCOPE) | ||||
|   endif() | ||||
| endfunction() | ||||
							
								
								
									
										5
									
								
								cmake/Config.cmake.in
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								cmake/Config.cmake.in
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,5 @@ | |||
| @PACKAGE_INIT@ | ||||
| 
 | ||||
| include(CMakeFindDependencyMacro) | ||||
| include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") | ||||
| check_required_components("@PROJECT_NAME@") | ||||
							
								
								
									
										17
									
								
								include/greeter.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								include/greeter.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <string> | ||||
| 
 | ||||
| namespace greeter { | ||||
| 
 | ||||
|   enum class LanguageCode { EN, DE, ES, FR }; | ||||
| 
 | ||||
|   class Greeter { | ||||
|     std::string name; | ||||
| 
 | ||||
|   public: | ||||
|     Greeter(std::string name = "World"); | ||||
|     std::string greet(LanguageCode lang = LanguageCode::EN) const; | ||||
|   }; | ||||
| 
 | ||||
| }  // namespace greeter
 | ||||
							
								
								
									
										18
									
								
								source/greeter.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								source/greeter.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,18 @@ | |||
| #include <greeter.h> | ||||
| 
 | ||||
| using namespace greeter; | ||||
| 
 | ||||
| Greeter::Greeter(std::string _name) : name(_name) {} | ||||
| 
 | ||||
| std::string Greeter::greet(LanguageCode lang) const { | ||||
|   switch (lang) { | ||||
|     case LanguageCode::EN: | ||||
|       return "Hello, " + name + "!"; | ||||
|     case LanguageCode::DE: | ||||
|       return "Hallo " + name + "!"; | ||||
|     case LanguageCode::ES: | ||||
|       return "¡Hola " + name + "!"; | ||||
|     case LanguageCode::FR: | ||||
|       return "Bonjour " + name + "!"; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										55
									
								
								test/CMakeLists.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								test/CMakeLists.txt
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,55 @@ | |||
| cmake_minimum_required(VERSION 3.5 FATAL_ERROR) | ||||
| 
 | ||||
| project(GreeterTests | ||||
|   LANGUAGES CXX | ||||
| ) | ||||
| 
 | ||||
| # ---- Options ---- | ||||
| 
 | ||||
| option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF) | ||||
| 
 | ||||
| # ---- Dependencies ---- | ||||
| 
 | ||||
| include(../cmake/CPM.cmake) | ||||
| 
 | ||||
| CPMAddPackage( | ||||
|   NAME doctest | ||||
|   GITHUB_REPOSITORY onqtam/doctest | ||||
|   VERSION 2.3.2 | ||||
|   GIT_TAG 2.3.2 | ||||
| ) | ||||
| 
 | ||||
| CPMAddPackage( | ||||
|   NAME Greeter | ||||
|   SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/.. | ||||
| ) | ||||
| 
 | ||||
| CPMAddPackage( | ||||
|   NAME Format.cmake | ||||
|   GITHUB_REPOSITORY TheLartians/Format.cmake | ||||
|   VERSION 1.0 | ||||
| ) | ||||
| 
 | ||||
| # ---- Create binary ---- | ||||
| 
 | ||||
| file(GLOB GreeterTests_sources ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp) | ||||
| add_executable(GreeterTests ${GreeterTests_sources}) | ||||
| target_link_libraries(GreeterTests doctest Greeter) | ||||
| 
 | ||||
| if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") | ||||
|   set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror") | ||||
| else() | ||||
|   set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17) | ||||
| endif() | ||||
| 
 | ||||
| # ---- Add GreeterTests ---- | ||||
| 
 | ||||
| ENABLE_TESTING()  | ||||
| ADD_TEST(GreeterTests GreeterTests) | ||||
| 
 | ||||
| # ---- code coverage ---- | ||||
| 
 | ||||
| if (${ENABLE_TEST_COVERAGE}) | ||||
|   set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-O0 -g -fprofile-arcs -ftest-coverage --coverage") | ||||
|   target_link_options(GreeterTests PUBLIC "--coverage") | ||||
| endif() | ||||
							
								
								
									
										13
									
								
								test/source/greeter.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								test/source/greeter.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,13 @@ | |||
| #include <doctest/doctest.h> | ||||
| #include <greeter.h> | ||||
| 
 | ||||
| TEST_CASE("Greeter") { | ||||
|   using namespace greeter; | ||||
| 
 | ||||
|   Greeter greeter("World"); | ||||
| 
 | ||||
|   CHECK(greeter.greet(LanguageCode::EN) == "Hello, World!"); | ||||
|   CHECK(greeter.greet(LanguageCode::DE) == "Hello, World!"); | ||||
|   CHECK(greeter.greet(LanguageCode::ES) == "Hello, World!"); | ||||
|   CHECK(greeter.greet(LanguageCode::FR) == "Hello, World!"); | ||||
| } | ||||
							
								
								
									
										3
									
								
								test/source/main.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								test/source/main.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | |||
| #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN | ||||
| 
 | ||||
| #include <doctest/doctest.h> | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue