Skip to content

Commit

Permalink
auto-add datasources
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Mar 11, 2024
1 parent 2464270 commit 91b37d9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 59 deletions.
8 changes: 8 additions & 0 deletions .cmake-format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")
Expand Down Expand Up @@ -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 ()
Expand Down
30 changes: 2 additions & 28 deletions cmake/app_datasources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<target> FILES <file1> [<file2>...])
#
# It declares that a <target> uses and depends on the following files.
#
# This also sets the build requirement that the files must be downloaded before the <target> will be build.
#
# The named <target> 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 ()
39 changes: 18 additions & 21 deletions cmake/ccache.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
4 changes: 1 addition & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
26 changes: 21 additions & 5 deletions test/data/datasources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@ cmake_minimum_required (VERSION 3.16)

include (app_datasources)

# copies file to <build>/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 <build>/data/in.fastq
declare_datasource (FILE downloaded.fasta
URL https://ftp.seqan.de/app-template/downloaded.fasta
URL_HASH SHA256=c3cb990ca1a25c7e31be3c6c2d009238d9ac9a44b2b7c143753c1e2881699077
)

0 comments on commit 91b37d9

Please sign in to comment.