diff --git a/.gitignore b/.gitignore index 1ce0f6c6..060895d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,3 @@ -CMakeCache.txt -CMakeFiles -CMakeScripts -Testing -Makefile -cmake_install.cmake -install_manifest.txt -compile_commands.json -CTestTestfile.cmake -build -.vscode -.DS_Store \ No newline at end of file +/build* +/.vscode +*.DS_Store \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index d69cf031..9811ac31 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,3 @@ - language: cpp sudo: require dist: xenial @@ -17,7 +16,7 @@ matrix: addons: &gcc8 apt: sources: *all_sources - packages: + packages: - g++-8 env: - MATRIX_EVAL="export CC=gcc-8; export CXX=g++-8;" @@ -25,11 +24,11 @@ matrix: - os: linux compiler: clang addons: - apt: - sources: *all_sources - packages: - - g++-8 - - clang-6.0 + apt: + sources: *all_sources + packages: + - g++-8 + - clang-6.0 env: - MATRIX_EVAL="export CC=clang-6.0; export CXX=clang++-6.0;" @@ -38,7 +37,7 @@ before_install: - eval "${MATRIX_EVAL}" - echo "CC=$CC CXX=$CXX" # Install a supported cmake version (>= 3.14) - - wget -O cmake.sh https://cmake.org/files/v3.14/cmake-3.14.0-Linux-x86_64.sh + - wget -O cmake.sh https://cmake.org/files/v3.14/cmake-3.14.0-Linux-x86_64.sh - sudo sh cmake.sh --skip-license --exclude-subdir --prefix=/usr/local - export PATH=/usr/local/bin:$PATH - cmake --version @@ -48,5 +47,4 @@ script: - cmake -Htest -Bbuild/test - CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/test --target test # examples - - python3 examples/run_all.py - \ No newline at end of file + - python3 examples/build_all.py diff --git a/README.md b/README.md index d33f0ac2..dde666af 100644 --- a/README.md +++ b/README.md @@ -50,24 +50,22 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) project(MyProject) # add executable -add_executable(myProject myProject.cpp) -set_target_properties(myProject PROPERTIES CXX_STANDARD 17) +add_executable(tests tests.cpp) # add dependencies include(cmake/CPM.cmake) CPMAddPackage( - NAME LarsParser - VERSION 1.8 - GIT_REPOSITORY https://github.com/TheLartians/Parser.git - OPTIONS - "LARS_PARSER_BUILD_GLUE_EXTENSION ON" + NAME Catch2 + GITHUB_REPOSITORY catchorg/Catch2 + VERSION 2.5.0 ) -target_link_libraries(myProject LarsParser) +# link dependencies +target_link_libraries(tests Catch2) ``` -See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more examples with source code. +See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more many examples with source code or the [wiki](https://github.com/TheLartians/CPM/wiki/More-Snippets) for many example snippets. ## Adding CPM @@ -78,36 +76,38 @@ mkdir -p cmake wget -O cmake/CPM.cmake https://raw.githubusercontent.com/TheLartians/CPM/master/cmake/CPM.cmake ``` -You can also use CMake to download CPM for you. See the [wiki](https://github.com/TheLartians/CPM/wiki/Adding-CPM) for more details. +You can even use CMake to download CPM for you. See the [wiki](https://github.com/TheLartians/CPM/wiki/Adding-CPM) for more details. ## Updating CPM -To update CPM to the newest version, simply update the script in the project's cmake directory, for example by running the command above. Dependencies using CPM will automatically use the updated script of the outermost project. +To update CPM to the newest version, update the script in the project's root directory, for example by running the command above. Dependencies using CPM will automatically use the updated script of the outermost project. ## Advantages - **Small and reusable projects** CPM takes care of all project dependencies, allowing developers to focus on creating small, well-tested frameworks. -- **Cross-Platform** CPM adds projects via `add_subdirectory`, which is compatible with all cmake toolchains and generators. +- **Cross-Platform** CPM adds projects via `add_subdirectory`, which is compatible with all CMake toolchains and generators. - **Reproducable builds** By using versioning via git tags it is ensured that a project will always be in the same state everywhere. - **Recursive dependencies** Ensures that no dependency is added twice and is added in the minimum required version. - **Plug-and-play** No need to install anything. Just add the script to your project and you're good to go. - **No packaging required** There is a good chance your existing projects already work as CPM dependencies. -- **Simple source distribution** CPM makes including projects with source files and dependencies easy, reducing the need for monolithic header files. +- **Simple source distribution** CPM makes including projects with source files and dependencies easy, reducing the need for monolithic header files or git submodules. ## Limitations -- **No pre-built binaries** For every new project, all dependencies must be downloaded and built from scratch. A possible workaround is to use CPM to fetch a pre-built binary or to enable local packages (see [below](#local-packages)). +- **No pre-built binaries** For every new build directory, all dependencies are initially downloaded and built from scratch. To avoid extra downloads it is recommend to set the [`CPM_SOURCE_CACHE`](#CPM_SOURCE_CACHE) environmental variable. Using a caching compiler such as [sccahe](https://github.com/mozilla/sccache) can drastically reduce build time. - **Dependent on good CMakeLists** Many libraries do not have CMakeLists that work well for subprojects. Luckily this is slowly changing, however, until then, some manual configuration may be required (see the snippets [below](#snippets)). For best practices on preparing your projects for CPM, see the [wiki](https://github.com/TheLartians/CPM/wiki/Preparing-projects-for-CPM). - **First version used** In diamond-shaped dependency graphs (e.g. `A` depends on `C`@1.1 and `B`, which itself depends on `C`@1.2 the first added dependency will be used (in this case `C`@1.1). In this case, B requires a newer version of `C` than `A`, so CPM will emit an error. This can be resolved by updating the outermost dependency version. For projects with more complex needs and where an extra setup step doesn't matter, it is worth to check out fully featured C++ package managers such as [conan](https://conan.io), [vcpkg](https://github.com/microsoft/vcpkg) or [hunter](https://github.com/ruslo/hunter). -Support for package managers is also [planned](https://github.com/TheLartians/CPM/issues/51) for a future version of CPM. +Support for these package managers is also [planned](https://github.com/TheLartians/CPM/issues/51) for a future version of CPM. ## Options -### CPM_SOURCE_ROOT +### CPM_SOURCE_CACHE -To avoid re-downloading dependencies, configure the project with the cmake option `-DCPM_SOURCE_ROOT=`. +To avoid re-downloading dependencies, CPM has an option `CPM_SOURCE_CACHE` that can be passed to CMake as `-DCPM_SOURCE_CACHE=`. +It can also be defined system-wide as an environmental variable, by adding `export CPM_SOURCE_CACHE=$HOME/.cache/CPM` to your `.bashrc` or `.bash_profile`. +Note that passing the variable as a configure option to CMake will always override the value set by the environmental variable. ### CPM_USE_LOCAL_PACKAGES @@ -158,7 +158,7 @@ CPMAddPackage( CPMAddPackage( NAME yaml-cpp GITHUB_REPOSITORY jbeder/yaml-cpp - # 0.6.2 uses depricated CMake syntax + # 0.6.2 uses deprecated CMake syntax VERSION 0.6.3 # 0.6.3 is not released yet, so use a recent commit GIT_TAG 012269756149ae99745b6dafefd415843d7420bb @@ -247,4 +247,4 @@ For a full example on using CPM to download and configure lua with sol2 see [her ### Full Examples -See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for full examples with source code. +See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for full examples with source code and check out the [wiki](https://github.com/TheLartians/CPM/wiki/More-Snippets) for many more example snippets. diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 51cdbfae..b1f8be1f 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -28,7 +28,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) -set(CURRENT_CPM_VERSION 0.13) +set(CURRENT_CPM_VERSION 0.14) if(CPM_DIRECTORY) if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR}) @@ -46,7 +46,14 @@ set(CPM_DRY_RUN OFF CACHE INTERNAL "Don't download or configure dependencies (fo option(CPM_USE_LOCAL_PACKAGES "Use locally installed packages (find_package)" OFF) option(CPM_LOCAL_PACKAGES_ONLY "Use only locally installed packages" OFF) -set(CPM_SOURCE_ROOT OFF CACHE PATH "Directory to downlaod CPM dependencies") + +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) @@ -67,6 +74,7 @@ function(CPMAddPackage) GITHUB_REPOSITORY GITLAB_REPOSITORY SOURCE_DIR + DOWNLOAD_COMMAND ) set(multiValueArgs @@ -105,14 +113,6 @@ function(CPMAddPackage) set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) endif() - set(FETCH_CONTENT_DECLARE_EXTRA_OPTS "") - - if (CPM_SOURCE_ROOT AND NOT DEFINED CPM_ARGS_SOURCE_DIR) - string(TOLOWER ${CPM_ARGS_NAME} lname) - string(REPLACE "-" "_" source_path_name ${lname}) - list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${CPM_SOURCE_ROOT}/${source_path_name}) - endif() - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) if(CPM_ARGS_DOWNLOAD_ONLY) @@ -159,7 +159,36 @@ function(CPMAddPackage) endforeach() endif() - CPM_DECLARE_PACKAGE(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} ${CPM_ARGS_GIT_TAG} "${CPM_ARGS_UNPARSED_ARGUMENTS}" ${FETCH_CONTENT_DECLARE_EXTRA_OPTS}) + 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}) + else() + if (CPM_SOURCE_CACHE AND NOT DEFINED CPM_ARGS_SOURCE_DIR) + 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() + endif() + + CPM_DECLARE_PACKAGE(${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}) CPMGetProperties(${CPM_ARGS_NAME}) SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}" PARENT_SCOPE) @@ -167,8 +196,8 @@ function(CPMAddPackage) SET(${CPM_ARGS_NAME}_ADDED YES PARENT_SCOPE) endfunction() -function (CPM_DECLARE_PACKAGE PACKAGE VERSION GIT_TAG) - message(STATUS "${CPM_INDENT} adding package ${PACKAGE}@${VERSION} (${GIT_TAG})") +function (CPM_DECLARE_PACKAGE 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)") diff --git a/cmake/testing.cmake b/cmake/testing.cmake index 9dc614e6..5df6a71c 100644 --- a/cmake/testing.cmake +++ b/cmake/testing.cmake @@ -4,7 +4,19 @@ function(ASSERT_EQUAL) message(FATAL_ERROR "assertion failed: invalid argument count: ${ARGC}") endif() - if (NOT ${ARGV0} EQUAL ${ARGV1}) + if (NOT ${ARGV0} STREQUAL ${ARGV1}) message(FATAL_ERROR "assertion failed: '${ARGV0}' != '${ARGV1}'") + else() + message(STATUS "test passed: '${ARGV0}' == '${ARGV1}'") endif() endfunction() + +function(ASSERT_EMPTY) + if (NOT ARGC EQUAL 0) + message(FATAL_ERROR "assertion failed: input ${ARGC} not empty: '${ARGV}'") + endif() +endfunction() + +function(ASSERTION_FAILED) + message(FATAL_ERROR "assertion failed: ${ARGN}") +endfunction() diff --git a/examples/benchmark/CMakeLists.txt b/examples/benchmark/CMakeLists.txt index 2f5b9ee0..7f98b075 100644 --- a/examples/benchmark/CMakeLists.txt +++ b/examples/benchmark/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMExampleBenchmark) + # ---- Dependencies ---- include(../../cmake/CPM.cmake) diff --git a/examples/boost/CMakeLists.txt b/examples/boost/CMakeLists.txt index cfa59ee0..e4a26080 100644 --- a/examples/boost/CMakeLists.txt +++ b/examples/boost/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMExampleBoost) + # ---- Create binary ---- add_executable(CPMExampleBoost main.cpp) diff --git a/examples/run_all.py b/examples/build_all.py similarity index 90% rename from examples/run_all.py rename to examples/build_all.py index 03471a70..eb5f18c4 100644 --- a/examples/run_all.py +++ b/examples/build_all.py @@ -22,7 +22,7 @@ def runCommand(command): print("running example %s" % example.name) print("================" + ('=' * len(example.name))) project = Path(".") / 'build' / example.name - configure = runCommand('cmake -H%s -B%s -DCMAKE_BUILD_TYPE=RelWithDebInfo' % (example, project)) + configure = runCommand('cmake -H%s -B%s' % (example, project)) print(' ' + '\n '.join([line for line in configure.split('\n') if 'CPM:' in line])) build = runCommand('cmake --build %s -j4' % (project)) print(' ' + '\n '.join([line for line in build.split('\n') if 'Built target' in line])) diff --git a/examples/catch2/CMakeLists.txt b/examples/catch2/CMakeLists.txt index 89932cab..cdde8323 100644 --- a/examples/catch2/CMakeLists.txt +++ b/examples/catch2/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMExampleCatch2) + # ---- Options ---- option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF) diff --git a/examples/cxxopts/CMakeLists.txt b/examples/cxxopts/CMakeLists.txt index 14c4617f..b6aee340 100644 --- a/examples/cxxopts/CMakeLists.txt +++ b/examples/cxxopts/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMExampleCXXOpts) + # ---- Options ---- option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF) diff --git a/examples/doctest/CMakeLists.txt b/examples/doctest/CMakeLists.txt index c0c6a343..d2512bd3 100644 --- a/examples/doctest/CMakeLists.txt +++ b/examples/doctest/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMExampleDoctest) + # ---- Options ---- option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF) diff --git a/examples/entt/CMakeLists.txt b/examples/entt/CMakeLists.txt index 5a8f7191..c7206433 100644 --- a/examples/entt/CMakeLists.txt +++ b/examples/entt/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMEnTTExample) + # ---- Dependencies ---- include(../../cmake/CPM.cmake) diff --git a/examples/json/CMakeLists.txt b/examples/json/CMakeLists.txt index b91b9309..93a0759c 100644 --- a/examples/json/CMakeLists.txt +++ b/examples/json/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMJSONExample) + # ---- Dependencies ---- include(../../cmake/CPM.cmake) diff --git a/examples/linenoise/CMakeLists.txt b/examples/linenoise/CMakeLists.txt index 2d529175..d763f7f1 100644 --- a/examples/linenoise/CMakeLists.txt +++ b/examples/linenoise/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMlinenoiseExample) + # ---- Dependencies ---- include(../../cmake/CPM.cmake) diff --git a/examples/parser-lua/CMakeLists.txt b/examples/parser-lua/CMakeLists.txt index 06d8f3a6..2152df35 100644 --- a/examples/parser-lua/CMakeLists.txt +++ b/examples/parser-lua/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR) +project(CPMParserLuaExample) + include(../../cmake/CPM.cmake) # ---- Dependencies ---- diff --git a/examples/parser/CMakeLists.txt b/examples/parser/CMakeLists.txt index 2fb49006..10c0a144 100644 --- a/examples/parser/CMakeLists.txt +++ b/examples/parser/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR) +project(CPMParserExample) + # add dependencies include(../../cmake/CPM.cmake) diff --git a/examples/range-v3/CMakeLists.txt b/examples/range-v3/CMakeLists.txt index e96b3144..ed9a3a16 100644 --- a/examples/range-v3/CMakeLists.txt +++ b/examples/range-v3/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMRangev3Example) + # ---- Dependencies ---- include(../../cmake/CPM.cmake) diff --git a/examples/simple_match/CMakeLists.txt b/examples/simple_match/CMakeLists.txt index e68f7858..952418dd 100644 --- a/examples/simple_match/CMakeLists.txt +++ b/examples/simple_match/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMSimpleMatchExample) + # ---- Dependencies ---- include(../../cmake/CPM.cmake) diff --git a/examples/sol2/CMakeLists.txt b/examples/sol2/CMakeLists.txt index e8d33fbf..07f8d63b 100644 --- a/examples/sol2/CMakeLists.txt +++ b/examples/sol2/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMSol2Example) + # ---- Dependencies ---- include(../../cmake/CPM.cmake) diff --git a/examples/yaml/CMakeLists.txt b/examples/yaml/CMakeLists.txt index 1237bb18..6e271d0a 100644 --- a/examples/yaml/CMakeLists.txt +++ b/examples/yaml/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMYamlExample) + # ---- Dependencies ---- include(../../cmake/CPM.cmake) @@ -7,7 +9,7 @@ include(../../cmake/CPM.cmake) CPMAddPackage( NAME yaml-cpp GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git - # 0.6.2 uses depricated CMake syntax + # 0.6.2 uses deprecated CMake syntax VERSION 0.6.3 # 0.6.3 is not released yet, so use the most recent commit GIT_TAG 012269756149ae99745b6dafefd415843d7420bb diff --git a/logo/CPM.afdesign b/logo/CPM.afdesign index 1bd2ad40..f977b780 100644 Binary files a/logo/CPM.afdesign and b/logo/CPM.afdesign differ diff --git a/logo/CPM.png b/logo/CPM.png index 9a576177..0c48acc6 100644 Binary files a/logo/CPM.png and b/logo/CPM.png differ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a1290d40..de83b39e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(CPMTests) + file(GLOB tests "${CMAKE_CURRENT_SOURCE_DIR}/unit/*.cmake") enable_testing() @@ -11,3 +13,5 @@ foreach(test ${tests}) COMMAND ${CMAKE_COMMAND} -DCPM_PATH=${CMAKE_CURRENT_SOURCE_DIR}/../cmake -P "${test}" ) endforeach() + +add_custom_target(test-verbose COMMAND ${CMAKE_CTEST_COMMAND} --verbose) diff --git a/test/unit/cache.cmake b/test/unit/cache.cmake new file mode 100644 index 00000000..d3c13787 --- /dev/null +++ b/test/unit/cache.cmake @@ -0,0 +1,125 @@ +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +include(${CPM_PATH}/testing.cmake) +include(CMakePackageConfigHelpers) + + +set(CPM_SOURCE_CACHE_DIR "${CMAKE_CURRENT_BINARY_DIR}/CPM") +set(TEST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/cache) + +function(clear_cache) + FILE(REMOVE_RECURSE ${CPM_SOURCE_CACHE_DIR}) + + if (EXISTS "${CPM_SOURCE_CACHE_DIR}") + ASSERTION_FAILED("cache not cleared") + endif() +endfunction() + +function(update_cmake_lists) + configure_package_config_file( + "${CMAKE_CURRENT_LIST_DIR}/cache/CMakeLists.txt.in" + "${CMAKE_CURRENT_LIST_DIR}/cache/CMakeLists.txt" + INSTALL_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/junk + ) +endfunction() + +function(reset_test) + clear_cache() + FILE(REMOVE_RECURSE ${TEST_BUILD_DIR}) + update_cmake_lists() +endfunction() + +set(CATCH2_VERSION 2.8.0) + +## Read CPM_SOURCE_CACHE from arguments + +reset_test() + +execute_process( + COMMAND + ${CMAKE_COMMAND} "-H${CMAKE_CURRENT_LIST_DIR}/cache" "-B${TEST_BUILD_DIR}" "-DCPM_SOURCE_CACHE=${CPM_SOURCE_CACHE_DIR}" + RESULT_VARIABLE ret +) + +ASSERT_EQUAL(${ret} "0") + +if (NOT EXISTS "${CPM_SOURCE_CACHE_DIR}/catch2") + ASSERTION_FAILED("catch2 not in cache") +endif() + +FILE(GLOB catch2_versions "${CPM_SOURCE_CACHE_DIR}/catch2/*") +list(LENGTH catch2_versions catch2_version_count) +ASSERT_EQUAL(${catch2_version_count} "1") + +FILE(GLOB fibonacci_versions "${CPM_SOURCE_CACHE_DIR}/fibonacci/*") +list(LENGTH fibonacci_versions fibonacci_version_count) +ASSERT_EQUAL(${fibonacci_version_count} "1") + +## Update dependency and keep CPM_SOURCE_CACHE + +set(CATCH2_VERSION 2.9.0) +update_cmake_lists() + +execute_process( + COMMAND + ${CMAKE_COMMAND} ${TEST_BUILD_DIR} + RESULT_VARIABLE ret +) + +ASSERT_EQUAL(${ret} "0") + +FILE(GLOB catch2_versions "${CPM_SOURCE_CACHE_DIR}/catch2/*") +list(LENGTH catch2_versions catch2_version_count) +ASSERT_EQUAL(${catch2_version_count} "2") + +FILE(GLOB fibonacci_versions "${CPM_SOURCE_CACHE_DIR}/fibonacci/*") +list(LENGTH fibonacci_versions fibonacci_version_count) +ASSERT_EQUAL(${fibonacci_version_count} "1") + +## Clear cache and update + +clear_cache() + +execute_process( + COMMAND + ${CMAKE_COMMAND} ${TEST_BUILD_DIR} + RESULT_VARIABLE ret +) + +ASSERT_EQUAL(${ret} "0") + +if (NOT EXISTS "${CPM_SOURCE_CACHE_DIR}/catch2") + ASSERTION_FAILED("catch2 not in cache") +endif() + +## Read CPM_SOURCE_CACHE from environment + +reset_test() + +execute_process( + COMMAND + ${CMAKE_COMMAND} -E env "CPM_SOURCE_CACHE=${CPM_SOURCE_CACHE_DIR}" ${CMAKE_COMMAND} "-H${CMAKE_CURRENT_LIST_DIR}/cache" "-B${TEST_BUILD_DIR}" + RESULT_VARIABLE ret +) + +ASSERT_EQUAL(${ret} "0") + +if (NOT EXISTS "${CPM_SOURCE_CACHE_DIR}/catch2") + ASSERTION_FAILED("catch2 not in cache") +endif() + +## Overwrite CPM_SOURCE_CACHE with argument + +reset_test() + +execute_process( + COMMAND + ${CMAKE_COMMAND} -E env "CPM_SOURCE_CACHE=${CMAKE_CURRENT_BINARY_DIR}/junk" ${CMAKE_COMMAND} "-H${CMAKE_CURRENT_LIST_DIR}/cache" "-B${TEST_BUILD_DIR}" "-DCPM_SOURCE_CACHE=${CPM_SOURCE_CACHE_DIR}" + RESULT_VARIABLE ret +) + +ASSERT_EQUAL(${ret} "0") + +if (NOT EXISTS "${CPM_SOURCE_CACHE_DIR}/catch2") + ASSERTION_FAILED("catch2 not in cache") +endif() diff --git a/test/unit/cache/.gitignore b/test/unit/cache/.gitignore new file mode 100644 index 00000000..96730bdd --- /dev/null +++ b/test/unit/cache/.gitignore @@ -0,0 +1 @@ +/CMakeLists.txt \ No newline at end of file diff --git a/test/unit/cache/CMakeLists.txt.in b/test/unit/cache/CMakeLists.txt.in new file mode 100644 index 00000000..a7ea371b --- /dev/null +++ b/test/unit/cache/CMakeLists.txt.in @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +project(CPMExampleCatch2) + +# ---- Options ---- + +option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF) + +# ---- Dependencies ---- + +include(@CPM_PATH@/CPM.cmake) + +CPMAddPackage( + NAME fibonacci + GIT_REPOSITORY https://github.com/TheLartians/Fibonacci.git + VERSION 1.0 +) + +CPMAddPackage( + NAME Catch2 + GITHUB_REPOSITORY catchorg/Catch2 + VERSION @CATCH2_VERSION@ + GIT_SHALLOW YES +) + +# ---- Create binary ---- + +add_executable(CPMExampleCatch2 main.cpp) +target_link_libraries(CPMExampleCatch2 fibonacci Catch2) +set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror") diff --git a/test/unit/cache/main.cpp b/test/unit/cache/main.cpp new file mode 100644 index 00000000..0fdd3f91 --- /dev/null +++ b/test/unit/cache/main.cpp @@ -0,0 +1,13 @@ +#define CATCH_CONFIG_MAIN + +#include +#include + +TEST_CASE("fibonnacci"){ + REQUIRE(fibonnacci(0) == 0); + REQUIRE(fibonnacci(1) == 1); + REQUIRE(fibonnacci(2) == 1); + REQUIRE(fibonnacci(3) == 2); + REQUIRE(fibonnacci(4) == 3); + REQUIRE(fibonnacci(5) == 5); +} diff --git a/test/unit/dependency_properties.cmake b/test/unit/dependency_properties.cmake index c1094edc..784eb351 100644 --- a/test/unit/dependency_properties.cmake +++ b/test/unit/dependency_properties.cmake @@ -10,20 +10,31 @@ CPMAddPackage( GIT_TAG 1.2.3 ) +CPMAddPackage( + NAME A + VERSION 1.2.3 +) + CPM_GET_PACKAGE_VERSION(A VERSION) ASSERT_EQUAL(${VERSION} "1.2.3") +CPMAddPackage( + NAME B + VERSION 2.4.1 +) + CPMAddPackage( NAME B GIT_TAG v2.3.1 ) CPM_GET_PACKAGE_VERSION(B VERSION) -ASSERT_EQUAL(${VERSION} "2.3.1") +ASSERT_EQUAL(${VERSION} "2.4.1") CPMAddPackage( NAME C GIT_TAG v3.1.2-a + VERSION 3.1.2 ) CPM_GET_PACKAGE_VERSION(C VERSION)