-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCMakeLists.txt
347 lines (291 loc) · 12.9 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Copyright (c) 2023, Romain Bailly
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
cmake_minimum_required(VERSION 3.8...3.31)
if (PROJECT_NAME)
set(RMGR_SSIM_IS_TOP_LEVEL 0)
else()
set(RMGR_SSIM_IS_TOP_LEVEL 1)
endif()
if (RMGR_SSIM_IS_TOP_LEVEL)
# Add a build config named RelWithAsserts
if (RMGR_SSIM_BUILD_TESTS)
get_property(multiConfig GLOBAL PROPERTY "GENERATOR_IS_MULTI_CONFIG")
if (multiConfig)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo;RelWithAsserts")
endif()
endif()
if (CMAKE_GENERATOR_TOOLSET MATCHES "_clang_c2$")
set(CMAKE_CXX_FLAGS_DEBUG "/Z7")
#add_compile_options("$<$<CONFIG:Debug>:/Z7")
endif()
endif()
project(rmgr-ssim
VERSION 2.1.0
LANGUAGES CXX
)
include(GNUInstallDirs)
###################################################################################################
# Options
option(RMGR_SSIM_USE_DOUBLE "Whether to use double instead of float for increased precision (but slower)" OFF)
option(RMGR_SSIM_NO_OPENMP "Don't build rmgr-ssim-openmp" OFF)
option(RMGR_SSIM_BUILD_CLI "Whether to build the command line interface" OFF)
option(RMGR_SSIM_BUILD_SAMPLE "Whether to build the sample program " OFF)
option(RMGR_SSIM_BUILD_TESTS "Whether to build rmgr::ssim's unit tests" OFF)
set(RMGR_SSIM_COMPILE_OPTIONS)
set(RMGR_SSIM_DEFINITIONS)
if (RMGR_SSIM_USE_DOUBLE)
list(APPEND RMGR_SSIM_DEFINITIONS "RMGR_SSIM_USE_DOUBLE=1")
else()
list(APPEND RMGR_SSIM_DEFINITIONS "RMGR_SSIM_USE_DOUBLE=0")
endif()
# Set flags for the RelWithAsserts config
if (RMGR_SSIM_IS_TOP_LEVEL AND CMAKE_CONFIGURATION_TYPES MATCHES "RelWithAsserts")
# Use flags from RelWithDebInfo as base
foreach (tool C CXX EXE_LINKER MODULE_LINKER SHARED_LINKER STATIC_LINKER)
set(CMAKE_${tool}_FLAGS_RELWITHASSERTS "${CMAKE_${tool}_FLAGS_RELWITHDEBINFO}")
endforeach()
# Remove the NDEBUG macro
foreach (lang C CXX)
string(REGEX REPLACE "[/-]DNDEBUG" "" CMAKE_${lang}_FLAGS_RELWITHASSERTS "${CMAKE_${lang}_FLAGS_RELWITHASSERTS}")
endforeach()
# MSVC-specific nightmare
if (MSVC)
# Debug info format
set(cmp0141Status "")
if (POLICY CMP0091)
cmake_policy(GET CMP0141 cmp0141Status)
endif()
if (cmp0141Status STREQUAL "NEW")
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<CONFIG:Debug,RelWithDebInfo,RelWithAsserts>,ProgramDatabase,>")
endif()
# Use the debug version of MSVCRT (so as to enable the _ASSERTE macro)
set(cmp0091Status "")
if (POLICY CMP0091)
cmake_policy(GET CMP0091 cmp0091Status)
endif()
if (cmp0091Status STREQUAL "NEW")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<IF:$<CONFIG:Debug,RelWithAsserts>,Debug,>DLL")
else()
foreach (lang C CXX)
if (CMAKE_${lang}_FLAGS_RELWITHASSERTS MATCHES "\\/MT")
string(REGEX REPLACE "\\/MT[ \\t]" "/MTd " CMAKE_${lang}_FLAGS_RELWITHASSERTS "${CMAKE_${lang}_FLAGS_RELWITHASSERTS}")
elseif (CMAKE_${lang}_FLAGS_RELWITHASSERTS MATCHES "\\/MD")
string(REGEX REPLACE "\\/MD[ \\t]" "/MDd " CMAKE_${lang}_FLAGS_RELWITHASSERTS "${CMAKE_${lang}_FLAGS_RELWITHASSERTS}")
else()
set(CMAKE_${lang}_FLAGS_RELWITHASSERTS "${CMAKE_${lang}_FLAGS_RELWITHASSERTS} /MDd")
endif()
endforeach()
endif()
endif()
endif()
###################################################################################################
# Compiler-specific flags
macro(rmgr_check_predef_macros)
include(CheckCXXSymbolExists)
foreach (macro ${ARGN})
check_cxx_symbol_exists("${macro}" "" ${macro})
if (${macro})
break()
endif()
endforeach()
endmacro()
if (MSVC OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
list(APPEND RMGR_SSIM_COMPILE_OPTIONS "/W4")
list(APPEND RMGR_SSIM_COMPILE_OPTIONS "/MP") # Multi-processor build
rmgr_check_predef_macros(_M_AMD64 _M_ARM64 _M_IX86 _M_ARM) # In order of decreasing likeliness
if (_M_IX86 OR _M_AMD64)
if (_M_IX86)
set(RMGR_SSIM_SSE_FLAGS "/arch:SSE2")
endif()
set(RMGR_SSIM_AVX_FLAGS "/arch:AVX")
set(RMGR_SSIM_FMA_FLAGS "/arch:AVX2")
set(RMGR_SSIM_AVX512_FLAGS "/arch:AVX512")
elseif (_M_ARM OR _M_ARM64)
set(RMGR_SSIM_NEON_FLAGS "")
endif()
elseif (CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang"))
list(APPEND RMGR_SSIM_COMPILE_OPTIONS "-Wall")
rmgr_check_predef_macros(__amd64__ __aarch64__ __i386__ __arm__) # In order of decreasing likeliness
if (__i386__ OR __amd64__)
set(RMGR_SSIM_SSE_FLAGS "-msse2")
set(RMGR_SSIM_AVX_FLAGS "-mavx")
set(RMGR_SSIM_FMA_FLAGS "-mfma")
set(RMGR_SSIM_AVX512_FLAGS "-mavx512f")
if (CMAKE_COMPILER_IS_GNUCXX AND (WIN32 OR CYGWIN))
list(APPEND RMGR_SSIM_AVX512_FLAGS "-fno-exceptions" "-fno-asynchronous-unwind-tables") # Fixes a build error in AVX-512 code
endif()
elseif (__aarch64__)
set(RMGR_SSIM_NEON_FLAGS "")
elseif (__arm__)
set(RMGR_SSIM_NEON_FLAGS "-mfpu=neon")
check_cxx_symbol_exists("__ARM_PCS_VFP" "" __ARM_PCS_VFP)
if (__ARM_PCS_VFP)
list(APPEND RMGR_SSIM_NEON_FLAGS "-mfloat-abi=hard")
else()
list(APPEND RMGR_SSIM_NEON_FLAGS "-mfloat-abi=softfp")
endif()
endif()
endif()
###################################################################################################
# Source files
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/ssim-version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/rmgr/ssim-version.h")
set(RMGR_SSIM_FILES
"include/rmgr/ssim.h"
"src/ssim.cpp"
"src/ssim_internal.h"
"src/ssim_sse.cpp"
"src/ssim_avx.cpp"
"src/ssim_avx512.cpp"
"src/ssim_fma.cpp"
"src/ssim_neon.cpp"
"${CMAKE_CURRENT_BINARY_DIR}/rmgr/ssim-version.h"
)
# Apply SIMD compile flags to source files
include(CheckCXXCompilerFlag)
foreach (is sse avx fma neon avx512)
string(TOUPPER "${is}" ISU)
if (RMGR_SSIM_${ISU}_FLAGS)
check_cxx_compiler_flag("${RMGR_SSIM_${ISU}_FLAGS}" RMGR_SSIM_${ISU}_FLAGS_SUPPORTED)
if (RMGR_SSIM_${ISU}_FLAGS_SUPPORTED)
set_source_files_properties("src/ssim_${is}.cpp" PROPERTIES COMPILE_OPTIONS "${RMGR_SSIM_${ISU}_FLAGS}")
endif()
endif()
endforeach()
source_group("Source Files" FILES ${RMGR_SSIM_FILES})
###################################################################################################
# Libraries
add_library(rmgr-ssim STATIC ${RMGR_SSIM_FILES})
target_include_directories(rmgr-ssim
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
INTERFACE
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_FULL_INCLUDE_DIR}>"
)
target_compile_options(rmgr-ssim PRIVATE ${RMGR_SSIM_COMPILE_OPTIONS})
target_compile_definitions(rmgr-ssim PRIVATE ${RMGR_SSIM_DEFINITIONS} _USE_MATH_DEFINES)
if (NOT RMGR_SSIM_NO_OPENMP)
enable_language(C)
find_package(OpenMP COMPONENTS C)
endif()
if ((NOT RMGR_SSIM_NO_OPENMP) AND OpenMP_C_FOUND)
set(RMGR_SSIM_OPENMP_FILES
"include/rmgr/ssim-openmp.h"
"src/ssim-openmp.c"
)
source_group("Source Files" FILES ${RMGR_SSIM_OPENMP_FILES})
add_library(rmgr-ssim-openmp ${RMGR_SSIM_OPENMP_FILES})
target_compile_options(rmgr-ssim-openmp PRIVATE ${OpenMP_C_FLAGS})
target_link_libraries(rmgr-ssim-openmp
PUBLIC rmgr-ssim
PRIVATE OpenMP::OpenMP_C
)
endif()
###############################################################################
# stb_image
if (RMGR_SSIM_BUILD_CLI OR RMGR_SSIM_BUILD_TESTS OR RMGR_SSIM_BUILD_SAMPLE)
set(STB_HASH "5c205738c191bcb0abc65c4febfa9bd25ff35234")
set(STBI_URL "https://raw.githubusercontent.com/nothings/stb/${STB_HASH}/stb_image.h")
set(STBIW_URL "https://raw.githubusercontent.com/nothings/stb/${STB_HASH}/stb_image_write.h")
set(STBI_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/stb_image.h")
set(STBIW_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/stb_image_write.h")
foreach (prefix STBI STBIW)
if (NOT EXISTS "${${prefix}_PATH}")
file(DOWNLOAD "${${prefix}_URL}" "${${prefix}_PATH}" STATUS ${prefix}_STATUS)
list(GET ${prefix}_STATUS 0 ${prefix}_STATUS_VALUE)
if (NOT ${prefix}_STATUS_VALUE EQUAL 0)
list(GET ${prefix}_STATUS 1 ${prefix}_STATUS_MSG)
message(FATAL_ERROR "Failed to download ${${prefix}_URL}: ${${prefix}_STATUS_MSG}")
if (EXISTS "${${prefix}_PATH}")
file(REMOVE "${${prefix}_PATH}")
endif()
endif()
endif()
endforeach()
endif()
###################################################################################################
# Command line interface
if (RMGR_SSIM_BUILD_CLI)
add_executable(rmgr-ssim-cli "src/ssim-cli.cpp")
if (TARGET rmgr-ssim-openmp)
target_link_libraries(rmgr-ssim-cli PRIVATE rmgr-ssim-openmp)
target_compile_definitions(rmgr-ssim-cli PRIVATE "RMGR_SSIM_USE_OPENMP=1")
else()
target_link_libraries(rmgr-ssim-cli PRIVATE rmgr-ssim)
endif()
target_compile_options(rmgr-ssim-cli PRIVATE ${RMGR_SSIM_COMPILE_OPTIONS})
target_compile_definitions(rmgr-ssim-cli PRIVATE ${RMGR_SSIM_DEFINITIONS})
set_target_properties(rmgr-ssim-cli PROPERTIES OUTPUT_NAME "rmgr-ssim")
if (WIN32)
target_compile_definitions(rmgr-ssim-cli PRIVATE "_UNICODE" "_CRT_SECURE_NO_WARNINGS")
endif()
endif()
###################################################################################################
# Sample
if (RMGR_SSIM_BUILD_SAMPLE)
add_executable(rmgr-ssim-sample "sample/rmgr-ssim-sample.cpp")
if (TARGET rmgr-ssim-openmp)
target_link_libraries(rmgr-ssim-sample PRIVATE rmgr-ssim-openmp)
target_compile_definitions(rmgr-ssim-sample PRIVATE "RMGR_SSIM_USE_OPENMP=1")
else()
target_link_libraries(rmgr-ssim-sample PRIVATE rmgr-ssim)
endif()
target_compile_options(rmgr-ssim-sample PRIVATE ${RMGR_SSIM_COMPILE_OPTIONS})
target_include_directories(rmgr-ssim-sample PRIVATE "src")
if (WIN32)
target_compile_definitions(rmgr-ssim-sample PRIVATE "_CRT_SECURE_NO_WARNINGS")
endif()
endif()
###################################################################################################
# C++ version
set(RMGR_SSIM_CXX_VERSION) # Only meant for testing, leave empty
if (RMGR_SSIM_CXX_VERSION)
foreach (target rmgr-ssim rmgr-ssim-cli rmgr-ssim-sample)
if (TARGET ${target})
set_target_properties(${target} PROPERTIES
CXX_STANDARD ${RMGR_SSIM_CXX_VERSION}
CXX_STANDARD_REQUIRED TRUE
)
endif()
endforeach()
endif()
###################################################################################################
# Install rules
install(TARGETS rmgr-ssim ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")
install(
FILES
"include/rmgr/ssim.h"
"${CMAKE_CURRENT_BINARY_DIR}/rmgr/ssim-version.h"
DESTINATION
"${CMAKE_INSTALL_INCLUDEDIR}/rmgr"
)
if (TARGET rmgr-ssim-openmp)
install(FILES "include/rmgr/ssim-openmp.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/rmgr")
endif()
if (WIN32)
set(CPACK_GENERATOR "ZIP")
else()
set(CPACK_GENERATOR "TGZ")
endif()
include(CPack)
###################################################################################################
# Sub-projects
if (RMGR_SSIM_BUILD_TESTS)
add_subdirectory(tests)
set_directory_properties(PROPERTIES VS_STARTUP_PROJECT rmgr-ssim-tests)
endif()