forked from NIFTI-Imaging/nifti_clib
/
CMakeLists.txt
215 lines (190 loc) · 8.53 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
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)
set(NIFTI_MAX_VALIDATED_CMAKE_VERSION "3.13.1")
if("${CMAKE_VERSION}" VERSION_LESS_EQUAL "${NIFTI_MAX_VALIDATED_CMAKE_VERSION}")
# As of 2018-12-04 NIFTI has been validated to build with cmake version 3.13.1 new policies.
# Set and use the newest cmake policies that are validated to work
set(NIFTI_CMAKE_POLICY_VERSION "${CMAKE_VERSION}")
else()
set(NIFTI_CMAKE_POLICY_VERSION "${NIFTI_MAX_VALIDATED_CMAKE_VERSION}")
endif()
cmake_policy(VERSION ${NIFTI_CMAKE_POLICY_VERSION})
set(NIFTI_HOMEPAGE_URL "https://nifti-imaging.github.io")
execute_process(COMMAND git "describe" "--tags"
OUTPUT_VARIABLE GIT_REPO_VERSION_UNCLEANED
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
#Extract the GIT_REPO_VERSION as composed of non-negative integer components,
# i.e. <major>[.<minor>[.<patch>[.<tweak>]]]
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+((beta.*)?(alpha.*)?\\.[0-9]+(\\.g[0-9a-f]+)?)?"
GIT_REPO_VERSION "${GIT_REPO_VERSION_UNCLEANED}")
if( NOT GIT_REPO_VERSION )
message(NOTE "Invalid git tag does not match required regular expression, "
"can not extract version information from '${GIT_REPO_VERSION_UNCLEANED}'")
# NOTE: cmake -DGIT_REPO_VERSION:STRING=<major>[.<minor>[.<patch>[.<tweak>]]] can be used
# to set the repo string for non-git repos.
set(GIT_REPO_VERSION "0.0.0.0") #Manually set the version string for testing purposes
endif()
project(NIFTI
VERSION ${GIT_REPO_VERSION}
DESCRIPTION "Niftilib is a set of i/o libraries for reading and writing files in the nifti-1 data format. nifti-1 is a binary file format for storing medical image data, e.g. magnetic resonance image (MRI) and functional MRI (fMRI) brain images."
LANGUAGES C)
macro(set_if_not_defined var defaultvalue)
# Macro allowing to set a variable to its default value if not already defined.
# The default value is set with:
# (1) if set, the value environment variable <var>.
# (2) if set, the value of local variable variable <var>.
# (3) if none of the above, the value passed as a parameter.
# Setting the optional parameter 'OBFUSCATE' will display 'OBFUSCATED' instead of the real value.
set(_obfuscate FALSE)
foreach(arg ${ARGN})
if(arg STREQUAL "OBFUSCATE")
set(_obfuscate TRUE)
endif()
endforeach()
if(DEFINED ENV{${var}} AND NOT DEFINED ${var})
set(_value "$ENV{${var}}")
if(_obfuscate)
set(_value "OBFUSCATED")
endif()
message(STATUS "Setting '${var}' variable with environment variable value '${_value}'")
set(${var} $ENV{${var}})
endif()
if(NOT DEFINED ${var})
set(_value "${defaultvalue}")
if(_obfuscate)
set(_value "OBFUSCATED")
endif()
message(STATUS "Setting '${var}' variable with default value '${_value}'")
set(${var} "${defaultvalue}")
endif()
endmacro()
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
# add option to build shared libraries. The default is OFF to maintain the
# current build behavior
option( BUILD_SHARED_LIBS "Toggle building shared libraries." OFF)
#When including nifti as a subpackage, a prefix is often needed to avoid conflicts with sytem installed libraries.
set_if_not_defined(NIFTI_PACKAGE_PREFIX "")
# Set default shared library version
# This library version will be applied to all libraries in the package
# unless it is not explicitely for a certain lib.
set(NIFTI_SHAREDLIB_VERSION ${GIT_REPO_VERSION} )
if(BUILD_SHARED_LIBS AND NOT NIFTI_LIBRARY_PROPERTIES)
set(NIFTI_LIBRARY_PROPERTIES ${DEFAULT_SHARED_LIBS}
BUILD_SHARED_LIBS TRUE POSITION_INDEPENDENT_CODE TRUE VERSION ${GIT_REPO_VERSION} SOVERSION ${GIT_REPO_VERSION}
)
endif()
set_if_not_defined(NIFTI_INSTALL_RUNTIME_DIR "${CMAKE_INSTALL_PREFIX}/bin")
set_if_not_defined(NIFTI_INSTALL_LIBRARY_DIR "${CMAKE_INSTALL_PREFIX}/lib")
set_if_not_defined(NIFTI_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_PREFIX}/include/nifti")
set_if_not_defined(NIFTI_ZLIB_LIBRARIES "")
if(NOT NIFTI_ZLIB_LIBRARIES) # If using a custom zlib library, skip the find package
### USE AS STAND ALONE PACKAGE
find_package(ZLIB REQUIRED)
set(NIFTI_ZLIB_LIBRARIES ${ZLIB_LIBRARIES})
endif()
#message(STATUS "---------------------ZLIB -${NIFTI_ZLIB_LIBRARIES}--")
add_definitions(-DHAVE_ZLIB)
set_if_not_defined(NIFTI_INSTALL_NO_DEVELOPMENT 0)
set_if_not_defined(NIFTI_INSTALL_NO_LIBRARIES 0)
#######################################################################
enable_testing()
include(CTest)
#Needs an if clause and more work before testing can take place.
set_if_not_defined(NIFTI_BUILD_TESTING ${BUILD_TESTING})
if (NIFTI_BUILD_TESTING )
set_if_not_defined(NIFTI_SHELL_SCRIPT_TESTS "ON")
if ( CMAKE_VERSION VERSION_LESS 3.11.0 )
# CMAKE VERSION 3.11.0 needed for fetching data with cmake
message(FATAL_ERROR "ERROR: The testing framework for nifti_clib requires CMake version 3.11.0 or greater")
endif()
include(FetchContent) # fetch data a configure time to simplify tests
# If new or changed data is needed, add that data to the https://github.com/NIFTI-Imaging/nifti-test-data repo
# make a new release, and then update the URL and hash (shasum -a 256 <downloaded tarball>).
FetchContent_Declare( fetch_testing_data
URL https://github.com/NIFTI-Imaging/nifti-test-data/archive/v3.0.2.tar.gz
URL_HASH SHA256=5dafec078151018da7aaf3c941bd31f246f590bc34fa3fef29ce77a773db16a6
)
FetchContent_GetProperties(fetch_testing_data)
if(NOT fetch_testing_data)
set(FETCHCONTENT_QUIET OFF)
message(STATUS "Downloading testing data... please wait")
FetchContent_Populate( fetch_testing_data )
message(STATUS "download complete.")
endif()
endif()
#######################################################################
# Find unix math libraries
if(NOT WIN32)
find_library(NIFTI_SYSTEM_MATH_LIB m)
else()
set(NIFTI_SYSTEM_MATH_LIB "")
endif()
#######################################################################
add_subdirectory(znzlib)
add_subdirectory(niftilib)
option(USE_NIFTICDF_CODE "Build nifticdf library and tools" ON)
mark_as_advanced(USE_NIFTICDF_CODE)
if(USE_NIFTICDF_CODE)
add_subdirectory(nifticdf)
endif()
option(NIFTI_BUILD_APPLICATIONS "Build various utility tools" ON)
mark_as_advanced(NIFTI_BUILD_APPLICATIONS)
option(USE_NIFTI2_CODE "Build the nifti2 library and tools" ON)
mark_as_advanced(USE_NIFTI2_CODE)
include(CMakeDependentOption)
cmake_dependent_option(USE_CIFTI_CODE "Build the cifti library and tools" OFF "USE_NIFTI2_CODE" OFF)
mark_as_advanced(USE_CIFTI_CODE)
if( USE_NIFTI2_CODE )
add_subdirectory(nifti2)
if( USE_CIFTI_CODE )
add_subdirectory(cifti)
endif()
endif()
option(USE_FSL_CODE "If OFF, The copyright of this code is questionable for inclusion with nifti." OFF)
mark_as_advanced(USE_FSL_CODE)
# the order of add_subdirectory is important! fsliolob has to preceed examples
# as otherwise FSLIOLIB_SOURCE_DIR is undefined and hence the examples
# will fail to compile
if(USE_FSL_CODE)
add_subdirectory(fsliolib)
endif()
set_if_not_defined(NIFTI_INSTALL_EXPORT_NAME "NIFTITargets")
if(NIFTI_INSTALL_EXPORT_NAME EQUAL "NIFTITargets")
#######################################################################
# CMake itself and can use some CMake facilities for creating the package files.
# This allows for find_package(NIFTI 2.1.0 NO_MODULE) to work for pulling in
# NIFTI libraries into an external project
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/NIFTI/NIFTIConfigVersion.cmake"
VERSION ${GIT_REPO_VERSION}
COMPATIBILITY AnyNewerVersion
)
export(EXPORT NIFTITargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/NIFTI/NIFTITargets.cmake"
NAMESPACE ${NIFTI_PACKAGE_PREFIX}NIFTI::
)
configure_file(cmake/NIFTIConfig.cmake
"${CMAKE_CURRENT_BINARY_DIR}/NIFTI/NIFTIConfig.cmake"
COPYONLY
)
set(ConfigPackageLocation lib/cmake/NIFTI)
install(EXPORT NIFTITargets
FILE NIFTITargets.cmake
NAMESPACE ${NIFTI_PACKAGE_PREFIX}NIFTI::
DESTINATION ${ConfigPackageLocation}
)
install(FILES
cmake/NIFTIConfig.cmake
"${CMAKE_CURRENT_BINARY_DIR}/NIFTI/NIFTIConfigVersion.cmake"
DESTINATION ${ConfigPackageLocation}
COMPONENT Development
)
endif()
#######################################################################
## Add the cpack configuration settings last
option(NIFTI_USE_PACKAGING "Configure the packaging options for NIFTI" OFF)
mark_as_advanced(NIFTI_USE_PACKAGING)
if(NIFTI_USE_PACKAGING)
include(cmake/NIFTICPackConfig.cmake)
endif()