-
Notifications
You must be signed in to change notification settings - Fork 10
/
CMakeLists.txt
185 lines (158 loc) · 6.09 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
# Tutorial: https://cmake.org/cmake/help/latest/guide/tutorial/index.html
cmake_minimum_required(VERSION 3.14)
project(SPERR VERSION 0.8.1 LANGUAGES CXX DESCRIPTION "Lossy Scientific Compression with SPERR")
#
# specify the C++ standard
# CMake will try to add a -std=c++20 flag if the compiler supports, but if it doesn't,
# CMake will `decay` to a previous supported flag, e.g. -std=c++11.
#
set(CMAKE_CXX_STANDARD 20)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "RelWithDebInfo")
endif()
include(CMakeDependentOption)
#
# Test system endian. SPERR only works on little endian machines,
# which are pretty much all machines on the market in 2020.
#
include(TestBigEndian)
test_big_endian(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
message(FATAL_ERROR "CMake detects that the target machine is BIG ENDIAN,\
which SPERR cannot work on!")
else()
message(STATUS "Little endian machine, good!")
endif()
option( BUILD_SHARED_LIBS "Build shared SPERR library" ON )
option( BUILD_UNIT_TESTS "Build unit tests using GoogleTest" ON )
option( BUILD_CLI_UTILITIES "Build a set of command line utilities" ON )
option( USE_OMP "Use OpenMP parallelization on 3D volumes" OFF )
option( SPERR_PREFER_RPATH "Set RPATH; this can fight with package managers so turn off when building for them" ON )
mark_as_advanced(FORCE SPERR_PREFER_RPATH)
if(USE_OMP)
find_package(OpenMP REQUIRED)
if (OpenMP_CXX_FOUND)
message(STATUS "OpenMP found! (${OpenMP_CXX_LIB_NAMES}-${OpenMP_CXX_VERSION}: ${OpenMP_CXX_FLAGS})")
else()
message(FATAL_ERROR "OpenMP NOT found! Try turning it off to continue configuring SPERR!")
endif()
endif()
#
# Gather git commit SHA1
#
find_package(Git)
execute_process(COMMAND
${GIT_EXECUTABLE} describe --always --dirty
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_SHA1
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(COMMAND
${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
# Note: when git version 2.22 is more common, then we can use the following command
# to retrieve the current branch name.
# ${GIT_EXECUTABLE} branch --show-current
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS "Repository Git Branch: ${GIT_BRANCH}, SHA1: ${GIT_SHA1}")
#
# configure a header file to pass some of the CMake settings to the source code
#
configure_file("${CMAKE_SOURCE_DIR}/SperrConfig.h.in" SperrConfig.h @ONLY)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
if(SPERR_PREFER_RPATH)
#
# Basically always use full rpath when installing.
# These specifications need to be placed before defining any targets.
#
# Kitware documentation on CMake's handle or rpath:
# https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling
# and a good discussion on rpath topics:
# https://stackoverflow.com/questions/32469953/why-is-cmake-designed-so-that-it-removes-runtime-path-when-installing
#
set( CMAKE_SKIP_BUILD_RPATH FALSE )
set( CMAKE_BUILD_WITH_INSTALL_RPATH FALSE )
set( CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" )
set( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )
endif()
#
# Compile the SPERR library
#
add_subdirectory( src )
#
# Build command line utilities
#
if( BUILD_CLI_UTILITIES )
# Download and build CLI11
#
include(FetchContent)
# Control internal options of CLI11
#
set( CLI11_PRECOMPILED ON CACHE INTERNAL "Save precompiled CLI11")
set( CLI11_SINGLE_FILE OFF CACHE INTERNAL "Don't use single file CLI11")
FetchContent_Declare( cli11
GIT_REPOSITORY https://github.com/CLIUtils/CLI11
GIT_TAG 6c7b07a878ad834957b98d0f9ce1dbe0cb204fc9 # v2.4.2
)
FetchContent_MakeAvailable(cli11)
add_subdirectory( utilities ${CMAKE_BINARY_DIR}/bin )
endif()
#
# Fetch Google Test: https://google.github.io/googletest/quickstart-cmake.html
#
if( BUILD_UNIT_TESTS )
# Control internal options of GoogleTest
#
set( INSTALL_GTEST OFF CACHE INTERNAL "Not install GoogleTest")
set( BUILD_GMOCK ON CACHE INTERNAL "Build gmock")
# Let's use the new mechanism to incorporate GoogleTest
#
include(FetchContent)
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24")
message (STATUS "Fetching the latest GoogleTest Framework, CMake version >= 3.24")
FetchContent_Declare( googletest
URL https://github.com/google/googletest/archive/refs/heads/main.zip
DOWNLOAD_EXTRACT_TIMESTAMP NEW )
else()
message (STATUS "Fetching the latest GoogleTest Framework, CMake version < 3.24")
FetchContent_Declare( googletest
URL https://github.com/google/googletest/archive/refs/heads/main.zip )
endif()
# Prevent overriding the parent project's compiler/linker settings on Windows
#
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
# Copy test data sets to the build directory
#
file( COPY ${CMAKE_CURRENT_SOURCE_DIR}/test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )
enable_testing() # calling this function before adding subdirectory to enable
# invoking ctest from the top-level build directory.
add_subdirectory( test_scripts )
endif()
#
# Start installation using GNU installation rules
#
include( GNUInstallDirs )
# Install the SPERR library
#
if( BUILD_SHARED_LIBS )
install( TARGETS SPERR LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
else()
install( TARGETS SPERR ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
endif()
# Install utilities
#
if( BUILD_CLI_UTILITIES )
install( TARGETS show_version sperr3d sperr2d sperr3d_trunc
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
endif()
# Add a pkg-config file, also copy over SperrConfig.h
#
configure_file("${CMAKE_SOURCE_DIR}/SPERR.pc.in" SPERR.pc @ONLY)
install( FILES "${CMAKE_BINARY_DIR}/SPERR.pc" DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
install( FILES "${CMAKE_BINARY_DIR}/SperrConfig.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )