-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
286 lines (234 loc) · 10.8 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
cmake_minimum_required (VERSION 3.8)
project (popvcf LANGUAGES C CXX)
include(ExternalProject)
# Build popvcf in release by default
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RELEASE" CACHE STRING "Choose the type of build." FORCE)
endif()
# Build paw (dependency) in release by default
if(NOT PAW_CMAKE_BUILD_TYPE)
set(PAW_CMAKE_BUILD_TYPE "RELEASE" CACHE STRING "Choose the type of build for paw." FORCE)
endif()
message (STATUS "Build type: ${CMAKE_BUILD_TYPE}")
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
set (STATIC_DIR "" CACHE STRING "Build in 'static' mode and include libraries in this directory.")
############
## popvcf ##
############
set (popvcf_VERSION_MAJOR 1)
set (popvcf_VERSION_MINOR 1)
set (popvcf_VERSION_PATCH 1)
set (popvcf_VERSION ${popvcf_VERSION_MAJOR}.${popvcf_VERSION_MINOR}.${popvcf_VERSION_PATCH})
add_subdirectory(src) # Exposes "popvcf_sources", which contains all source files of popvcf
add_library(popvcf_objects OBJECT ${popvcf_sources})
target_compile_features(popvcf_objects PUBLIC cxx_std_17)
target_compile_options(popvcf_objects PUBLIC -Wall -Wextra -Wfatal-errors -pedantic -Wno-variadic-macros -march=x86-64 -mtune=generic)
add_dependencies(popvcf_objects project_paw) # popvcf depends on building paw
target_include_directories(popvcf_objects PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_BINARY_DIR}/include/popvcf)
# Add popvcf executable
add_executable(popvcf src/main.cpp $<TARGET_OBJECTS:popvcf_objects>)
# Inherit stuff from popvcf_objects
target_include_directories(popvcf PRIVATE $<TARGET_PROPERTY:popvcf_objects,INTERFACE_INCLUDE_DIRECTORIES>)
target_compile_features(popvcf PRIVATE $<TARGET_PROPERTY:popvcf_objects,COMPILE_FEATURES>)
target_compile_options(popvcf PRIVATE $<TARGET_PROPERTY:popvcf_objects,COMPILE_OPTIONS>)
# configure a header file to pass some of the CMake settings to the source code
configure_file (
${PROJECT_SOURCE_DIR}/src/in.constants.hpp
${PROJECT_BINARY_DIR}/include/popvcf/constants.hpp)
target_include_directories(popvcf_objects PUBLIC ${PROJECT_BINARY_DIR}/include)
########################
## Build dependencies ##
########################
### paw ###
if (CMAKE_BUILD_TYPE STREQUAL "DEBUG")
message (STATUS "paw in debug mode")
ExternalProject_Add(
project_paw
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/submodules/paw
BUILD_IN_SOURCE 0
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/paw
CONFIGURE_COMMAND ${CMAKE_COMMAND} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -H${CMAKE_CURRENT_SOURCE_DIR}/submodules/paw -B${CMAKE_CURRENT_BINARY_DIR}/paw -DCMAKE_BUILD_TYPE=${PAW_CMAKE_BUILD_TYPE} -DFORCE_AVX_FAST=1
BUILD_COMMAND $(MAKE) -C ${CMAKE_CURRENT_BINARY_DIR}/paw static
INSTALL_COMMAND "")
else()
message (STATUS "paw in non-debug mode")
ExternalProject_Add(
project_paw
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/submodules/paw
BUILD_IN_SOURCE 0
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/paw
CONFIGURE_COMMAND ${CMAKE_COMMAND} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -H${CMAKE_CURRENT_SOURCE_DIR}/submodules/paw -B${CMAKE_CURRENT_BINARY_DIR}/paw -DCMAKE_BUILD_TYPE=${PAW_CMAKE_BUILD_TYPE} -DFORCE_AVX_FAST=1
BUILD_COMMAND $(MAKE) -C ${CMAKE_CURRENT_BINARY_DIR}/paw static
INSTALL_COMMAND "")
endif()
add_library(paw STATIC IMPORTED)
target_include_directories(popvcf_objects SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/submodules/paw/include)
target_include_directories(popvcf_objects SYSTEM PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/paw/include)
set(paw_location ${CMAKE_CURRENT_BINARY_DIR}/paw/lib/libpaw.a)
message(STATUS "paw target location is ${paw_location}")
set_property(TARGET paw PROPERTY IMPORTED_LOCATION ${paw_location})
add_dependencies(paw project_paw)
add_dependencies(popvcf_objects paw)
target_link_libraries(popvcf PUBLIC paw)
### htslib
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/submodules/htslib/Makefile)
message(FATAL_ERROR "htslib not found. Run 'git submodule update --init' to fetch all submodules.")
endif()
ExternalProject_Add(
project_htslib
BUILD_IN_SOURCE 1
SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/htslib
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/htslib
CONFIGURE_COMMAND cp -a ${CMAKE_CURRENT_SOURCE_DIR}/submodules/htslib ${CMAKE_CURRENT_BINARY_DIR}/ COMMAND autoheader COMMAND autoconf COMMAND ${CMAKE_CURRENT_BINARY_DIR}/htslib/configure --disable-libcurl --disable-gcs --disable-lzma --disable-bz2 --with-libdeflate
"CFLAGS=${MYCFLAGS} -g -Wall -O3 ${CMAKE_C_FLAGS} -I${CMAKE_CURRENT_BINARY_DIR}/libdeflate"
"LDFLAGS=${MYLDFLAGS} -L${CMAKE_CURRENT_BINARY_DIR}/libdeflate"
"CC=${CMAKE_C_COMPILER}"
BUILD_COMMAND $(MAKE) -C ${CMAKE_CURRENT_BINARY_DIR}/htslib libhts.a
INSTALL_COMMAND ""
)
add_library(htslib STATIC IMPORTED)
target_include_directories(popvcf_objects SYSTEM PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/htslib)
set(htslib_location ${CMAKE_CURRENT_BINARY_DIR}/htslib/libhts.a)
message(STATUS "htslib target location is ${htslib_location}")
set_property(TARGET htslib PROPERTY IMPORTED_LOCATION ${htslib_location})
add_dependencies(htslib project_htslib)
add_dependencies(project_htslib libdeflate)
add_dependencies(popvcf_objects htslib)
target_link_libraries(popvcf PUBLIC ${htslib_location})
### libdeflate
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/submodules/libdeflate/Makefile)
message(FATAL_ERROR "libdeflate not found. Run 'git submodule update --init' to fetch all submodules.")
endif ()
ExternalProject_Add(
project_libdeflate
BUILD_IN_SOURCE 1
SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/libdeflate
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libdeflate
CONFIGURE_COMMAND cp -a ${CMAKE_CURRENT_SOURCE_DIR}/submodules/libdeflate ${CMAKE_CURRENT_BINARY_DIR}/
BUILD_COMMAND $(MAKE) -C ${CMAKE_CURRENT_BINARY_DIR}/libdeflate
PREFIX="${CMAKE_CURRENT_BINARY_DIR}/libdeflate"
"CC=${CMAKE_C_COMPILER}"
"CFLAGS=-fPIC -O3" libdeflate.a
INSTALL_COMMAND ""
)
add_library(libdeflate STATIC IMPORTED)
set(libdeflate_location ${CMAKE_CURRENT_BINARY_DIR}/libdeflate/libdeflate.a)
message(STATUS "libdeflate target location is ${libdeflate_location}")
set_property(TARGET libdeflate PROPERTY IMPORTED_LOCATION ${libdeflate_location})
add_dependencies(libdeflate project_libdeflate)
target_link_libraries(popvcf PUBLIC libdeflate)
### parallel_hashmap ###
target_include_directories(popvcf_objects SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/submodules/parallel-hashmap)
### threads ###
if (STATIC_DIR STREQUAL "")
find_package(Threads)
target_link_libraries(popvcf PUBLIC ${CMAKE_THREAD_LIBS_INIT})
else()
target_link_libraries(popvcf PUBLIC "${STATIC_DIR}/libpthread.a")
endif()
### rt and filesystem ###
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
message(STATUS "Using GCC")
if (STATIC_DIR STREQUAL "")
target_link_libraries(popvcf PUBLIC "rt")
# target_link_libraries(popvcf PUBLIC "stdc++fs")
else()
target_link_libraries(popvcf PUBLIC "${STATIC_DIR}/librt.a")
endif()
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
message(STATUS "Using Clang")
else()
message(WARNING "Unsupported compiler")
endif ()
### zlib ###
message (STATUS "Checking for zlib")
find_package(ZLIB REQUIRED)
target_include_directories(popvcf_objects SYSTEM PUBLIC ${ZLIB_INCLUDE_DIRS})
if (STATIC_DIR STREQUAL "")
target_link_libraries(popvcf PUBLIC ${ZLIB_LIBRARIES})
else()
target_link_libraries(popvcf PUBLIC "${STATIC_DIR}/libz.a")
endif()
### GCC ###
# LOCAL binaries have static GCC, PREBUILT are all static
if (STATIC_DIR STREQUAL "")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
endif ()
################
# clang-format #
################
# Add clang-format check target
find_program(CLANG_FORMAT "clang-format-12")
if (CLANG_FORMAT STREQUAL "" OR CLANG_FORMAT STREQUAL "CLANG_FORMAT-NOTFOUND")
find_program(CLANG_FORMAT "clang-format")
endif()
if (NOT CLANG_FORMAT STREQUAL "" AND NOT CLANG_FORMAT STREQUAL "CLANG_FORMAT-NOTFOUND")
add_custom_target (check_format "find" "${CMAKE_CURRENT_SOURCE_DIR}/src" "-name" "\"*.hpp\"" "-o" "-name" "\"*.cpp\"" "-exec" "${CLANG_FORMAT}" "--style=file" "--dry-run" "--Werror" "{}" "+" COMMENT "Checking all files format with clang-format.")
endif()
#################
# Documentation #
#################
## Adds a target to generate API documentation with Doxygen
find_package(Doxygen)
option(BUILD_DOCUMENTATION "Create and install the HTML based API documentation (requires Doxygen)" ${DOXYGEN_FOUND})
if(BUILD_DOCUMENTATION)
if(NOT DOXYGEN_FOUND)
message(FATAL_ERROR "Doxygen is needed to build the documentation.")
endif()
set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in)
set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
configure_file(${doxyfile_in} ${doxyfile} @ONLY)
add_custom_target(doc
COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM)
#install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html DESTINATION share/doc)
endif()
###########
# ARCHIVE #
###########
add_custom_target(archive
COMMAND sh -c "bash .git-archive-all.sh --format tar.gz --prefix popvcf-v${popvcf_VERSION}/ ${CMAKE_CURRENT_BINARY_DIR}/popvcf-v${popvcf_VERSION}.tar.gz"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Generating an archive which includes submodules."
VERBATIM)
###########
# Testing #
###########
enable_testing(true)
add_test(NAME test_popvcf COMMAND sh -c "set -e; sh ${CMAKE_CURRENT_SOURCE_DIR}/test/create_test_data.sh > test.vcf ; ${CMAKE_CURRENT_BINARY_DIR}/popvcf encode test.vcf -Oz > test.popvcf.gz ; ${CMAKE_CURRENT_BINARY_DIR}/popvcf decode test.popvcf.gz > test.new.vcf ; diff test.vcf test.new.vcf")
# TODO add tabix to workflow or make popvcf build the index
# tabix -p vcf test.popvcf.gz ; ${CMAKE_CURRENT_BINARY_DIR}/popvcf decode test.popvcf.gz --region=chr2:10000-10200 | grep -v ^# | wc -l | grep -q -w -F 2
set_tests_properties(test_popvcf PROPERTIES DEPENDS popvcf)
###########
## Other ##
###########
# Get the current working git branch
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE)
# Get the latest abbreviated SHA commit hash of the working branch
execute_process(
COMMAND git log -1 --format=%h
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_SHORT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE)
# Get the latest SHA commit hash of the working branch
execute_process(
COMMAND git rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_LONG_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE)
# Get the latest SHA commit hash of the working branch
execute_process(
COMMAND git diff COMMAND wc -l
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_NUM_DIRTY_LINES
OUTPUT_STRIP_TRAILING_WHITESPACE)