From 91b37d9a47ba51a7461b309ae1156ed9fd5d1867 Mon Sep 17 00:00:00 2001 From: Enrico Seiler Date: Mon, 11 Mar 2024 14:49:54 +0100 Subject: [PATCH] auto-add datasources --- .cmake-format.yaml | 8 ++++++++ CMakeLists.txt | 3 +-- cmake/app_datasources.cmake | 30 ++-------------------------- cmake/ccache.cmake | 39 +++++++++++++++++-------------------- test/CMakeLists.txt | 4 +--- test/data/datasources.cmake | 26 ++++++++++++++++++++----- 6 files changed, 51 insertions(+), 59 deletions(-) diff --git a/.cmake-format.yaml b/.cmake-format.yaml index 9024f71..61db2b9 100644 --- a/.cmake-format.yaml +++ b/.cmake-format.yaml @@ -29,6 +29,14 @@ parse: _help_additional_commands: - Specify structure for custom cmake functions additional_commands: + declare_datasource: + pargs: + nargs: '*' + flags: [] + kwargs: + FILE: 1 + URL: '*' + URL_HASH: 1 cpmgetpackage: pargs: 1 spelling: CPMGetPackage diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ea5069..72fa871 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,6 @@ list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") # Use ccache. include (ccache) -require_ccache () # Add packages. set (CPM_INDENT " CMake Package Manager CPM: ") @@ -40,9 +39,9 @@ target_link_libraries ("${PROJECT_NAME}_lib" PUBLIC "${PROJECT_NAME}_interface") add_executable ("${PROJECT_NAME}" src/main.cpp) target_link_libraries ("${PROJECT_NAME}" PRIVATE "${PROJECT_NAME}_lib") - ## TEST option (${PROJECT_NAME}_TEST "Enable testing for ${PROJECT_NAME}." ON) + if (${PROJECT_NAME}_TEST) list (APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure") # Must be before `enable_testing ()`. enable_testing () diff --git a/cmake/app_datasources.cmake b/cmake/app_datasources.cmake index e64a040..72ab4df 100644 --- a/cmake/app_datasources.cmake +++ b/cmake/app_datasources.cmake @@ -61,35 +61,9 @@ function (declare_datasource) TEST_COMMAND "" PREFIX "${CMAKE_CURRENT_BINARY_DIR}/_datasources" DOWNLOAD_NO_EXTRACT TRUE # don't extract archive files like .tar.gz. + EXCLUDE_FROM_ALL TRUE ${ARG_UNPARSED_ARGUMENTS} ) -endfunction () - -# Example call: -# -# ```cmake -# # my_app uses and needs the files RF00001.fa.gz, pdb100d.ent.gz, and GRCh38_latest_clinvar.vcf.gz. -# target_use_datasources (my_app FILES RF00001.fa.gz pdb100d.ent.gz GRCh38_latest_clinvar.vcf.gz) -# ``` -# -# Options: -# -# target_use_datasources ( FILES [...]) -# -# It declares that a uses and depends on the following files. -# -# This also sets the build requirement that the files must be downloaded before the will be build. -# -# The named must have been created by a command such as add_executable() or add_library(). -function (target_use_datasources target) - set (options "") - set (one_value_args) - set (multi_value_args "FILES") - - cmake_parse_arguments (ARG "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) - foreach (filename ${ARG_FILES}) - string (TOLOWER "datasource--${filename}" datasource_name) - add_dependencies ("${target}" "${datasource_name}") - endforeach () + add_dependencies (check "${datasource_name}") endfunction () diff --git a/cmake/ccache.cmake b/cmake/ccache.cmake index af2ed47..b6a11b9 100644 --- a/cmake/ccache.cmake +++ b/cmake/ccache.cmake @@ -11,25 +11,22 @@ include (FindPackageMessage) # See also # * https://ccache.dev/ # * https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_LAUNCHER.html -macro (require_ccache) - option (USE_CCACHE "Use ccache if available." ON) - set (FPROFILE_ABS_PATH "-fprofile-abs-path") - if (USE_CCACHE) - find_program (CCACHE_PROGRAM ccache) - - if (NOT CCACHE_PROGRAM) - find_package_message (CCACHE_PROGRAM " Ccache program: not available" "[${CCACHE_PROGRAM}]") - else () - find_package_message (CCACHE_PROGRAM " Ccache program: available" "[${CCACHE_PROGRAM}]") - set (FPROFILE_ABS_PATH "--ccache-skip -fprofile-abs-path") - - list (PREPEND CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}") - list (PREPEND CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}") - - list (PREPEND CMAKE_CXX_LINKER_LAUNCHER "${CCACHE_PROGRAM}") - list (PREPEND CMAKE_C_LINKER_LAUNCHER "${CCACHE_PROGRAM}") - endif () - - unset (CCACHE_PROGRAM) +option (USE_CCACHE "Use ccache if available." ON) + +if (USE_CCACHE) + find_program (CCACHE_PROGRAM ccache) + + if (NOT CCACHE_PROGRAM) + find_package_message (CCACHE_PROGRAM " Ccache program: not available" "[${CCACHE_PROGRAM}]") + else () + find_package_message (CCACHE_PROGRAM " Ccache program: available" "[${CCACHE_PROGRAM}]") + + list (PREPEND CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}") + list (PREPEND CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}") + + list (PREPEND CMAKE_CXX_LINKER_LAUNCHER "${CCACHE_PROGRAM}") + list (PREPEND CMAKE_C_LINKER_LAUNCHER "${CCACHE_PROGRAM}") endif () -endmacro () + + unset (CCACHE_PROGRAM) +endif () diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3650fe6..43c4f03 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,9 +7,7 @@ cmake_minimum_required (VERSION 3.16) include (test_config) include (data/datasources.cmake) -message (STATUS "You can run `make check` to build and run tests.") - add_app_test (api_fastq_coversion_test.cpp) add_app_test (cli_fastq_coversion_test.cpp) -target_use_datasources (check FILES in.fastq out.fasta) +message (STATUS "You can run `make check` to build and run tests.") diff --git a/test/data/datasources.cmake b/test/data/datasources.cmake index b6cf131..85dd86c 100644 --- a/test/data/datasources.cmake +++ b/test/data/datasources.cmake @@ -6,10 +6,26 @@ cmake_minimum_required (VERSION 3.16) include (app_datasources) -# copies file to /data/in.fastq -declare_datasource (FILE in.fastq URL ${CMAKE_SOURCE_DIR}/test/data/in.fastq URL_HASH - SHA256=6e30fc35f908a36fe0c68a7a35c47f51f9570da16622fb0c072a20e6a9ba5b3e +file (GLOB_RECURSE datasources + LIST_DIRECTORIES false + RELATIVE ${CMAKE_CURRENT_LIST_DIR} + CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/* ) -declare_datasource (FILE out.fasta URL ${CMAKE_SOURCE_DIR}/test/data/out.fasta URL_HASH - SHA256=2c1ccd1b391c45cbbe1b4448584106d2ad2dc996a1636dcfd67342b7f943116a +list (REMOVE_ITEM datasources datasources.cmake README.md) +list (FILTER datasources EXCLUDE REGEX "\.license") + +foreach (datasource IN LISTS datasources) + set (datasource_name "${datasource}") + string (REPLACE "/" "-" datasource_name "${datasource_name}") + file (SHA256 ${CMAKE_CURRENT_LIST_DIR}/${datasource} datasource_hash) + declare_datasource (FILE ${datasource_name} + URL ${CMAKE_CURRENT_LIST_DIR}/${datasource} + URL_HASH SHA256=${datasource_hash} + ) +endforeach () + +# copies file to /data/in.fastq +declare_datasource (FILE downloaded.fasta + URL https://ftp.seqan.de/app-template/downloaded.fasta + URL_HASH SHA256=c3cb990ca1a25c7e31be3c6c2d009238d9ac9a44b2b7c143753c1e2881699077 )