-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
252 lines (206 loc) · 7.96 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
# ####################################
# General project setup
# ####################################
cmake_minimum_required(VERSION 3.13)
project(somhunter)
set(BUILD_PLUGINS_STATIC ON
CACHE BOOL "prevent problems with dynamic builds")
set(IS_CI OFF
CACHE BOOL "If ON, no models ")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Specify build targets
set(SOMHUNTER_TARGET somhunter)
set(SOMHUNTER_TARGET_TESTS somhunter-tests)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
SET(WITH_CUDA 0 CACHE STRING "Set to 1 if you have CUDA installed.")
set(THIRD_PARTY_DIR "${CMAKE_HOME_DIRECTORY}/third-party")
# Output dirs
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} )
if (MSVC)
# Set warning level 0 to everything included with angle brackets
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /MP /experimental:external /external:anglebrackets /external:W0 /Oi" )
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR} )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR} )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR} )
endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
else (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fopenmp -D_GLIBCXX_USE_CXX11_ABI=1")
endif (MSVC)
# -march=native option
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
if(COMPILER_SUPPORTS_MARCH_NATIVE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
endif()
if (CMAKE_BUILD_TYPE)
else ()
message("Setting default CMAKE_BUILD_TYPE")
set(CMAKE_BUILD_TYPE Debug)
endif ()
if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Release" AND NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
set(CMAKE_BUILD_TYPE_RED "Release")
message("Setting reduced build type to: ${CMAKE_BUILD_TYPE_RED}")
else ()
set(CMAKE_BUILD_TYPE_RED ${CMAKE_BUILD_TYPE})
endif ()
if (WIN32)
set(PLATFORM "win")
else ()
set(PLATFORM "UNIX")
endif()
message("Platform is '${PLATFORM}'...")
if (WITH_CUDA)
set(CUDA_SCRIPT_OPT "--cuda=true")
else ()
endif ()
if (IS_CI)
set(IS_CI_OPT "--no-models=true")
else ()
endif ()
# ####################################
# Global preprocessor defines
# ####################################
add_compile_definitions("NOMINMAX")
# Disable "unsafe" warnings in the MSVC toolchain
add_compile_definitions("_CRT_SECURE_NO_WARNINGS")
# ####################################
# Preparation scripts
# ####################################
#
# Configure python script
#
message("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
message("Running 'config.py' script, this may take a while (it's downloading large files)...")
message("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
execute_process (
COMMAND python ${CMAKE_HOME_DIRECTORY}/scripts/configure.py
${PLATFORM} # What platform \in {win, UNIX}
${CMAKE_HOME_DIRECTORY}/config/config-core.json # Config file
${CMAKE_HOME_DIRECTORY}/config/config-install.json # Install config file
${CMAKE_HOME_DIRECTORY}/config/config-auth.json # Auth config
${CMAKE_BINARY_DIR} # Build dir
${THIRD_PARTY_DIR} # Third-party dir
"${CMAKE_BUILD_TYPE_RED}" # Build type \in { Release, Debug }
${CUDA_SCRIPT_OPT}
${IS_CI_OPT}
WORKING_DIRECTORY ${CMAKE_HOME_DIRECTORY}
RESULTS_VARIABLE conf_res
)
if (conf_res)
message(FATAL_ERROR "Configure script failed!")
else ()
message("Configure completed.")
endif ()
# ####################################
# Dependencies
# ####################################
find_package(cpprestsdk REQUIRED)
# libTorch
set(Torch_DIR "${THIRD_PARTY_DIR}/libtorch-${CMAKE_BUILD_TYPE_RED}/libtorch/share/cmake/Torch/")
message(${Torch_DIR})
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
if (WIN32)
# Don't know why!
set_target_properties(torch PROPERTIES
IMPORTED_IMPLIB "${THIRD_PARTY_DIR}/libtorch-${CMAKE_BUILD_TYPE_RED}/libtorch//lib/torch.lib"
IMPORTED_LOCATION "${THIRD_PARTY_DIR}/libtorch-${CMAKE_BUILD_TYPE_RED}/libtorch//lib/torch.dll"
)
set_target_properties(torch_cpu PROPERTIES
IMPORTED_IMPLIB "${THIRD_PARTY_DIR}/libtorch-${CMAKE_BUILD_TYPE_RED}/libtorch//lib/torch_cpu.lib"
IMPORTED_LOCATION "${THIRD_PARTY_DIR}/libtorch-${CMAKE_BUILD_TYPE_RED}/libtorch//lib/torch_cpu.dll"
)
set_target_properties(c10 PROPERTIES
IMPORTED_IMPLIB "${THIRD_PARTY_DIR}/libtorch-${CMAKE_BUILD_TYPE_RED}/libtorch//lib/c10.lib"
IMPORTED_LOCATION "${THIRD_PARTY_DIR}/libtorch-${CMAKE_BUILD_TYPE_RED}/libtorch//lib/c10.dll"
)
endif (WIN32)
# pthreads
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
# cURL
find_package(CURL REQUIRED)
# OpenCV - preparations
find_package(OpenCV REQUIRED)
find_package(OpenSSL REQUIRED)
#
# Add structure to the project (as the FS structure)
#
file(
GLOB_RECURSE SOMHUNTER_SOURCE_FILEPATHS
"*.h"
"*.hpp"
"*.cpp"
"*.cc"
)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOMHUNTER_SOURCE_FILEPATHS})
# ####################################
# Create the target
# ####################################
add_executable(${SOMHUNTER_TARGET})
target_compile_features(${SOMHUNTER_TARGET} PRIVATE cxx_std_17)
set_target_properties(${SOMHUNTER_TARGET} PROPERTIES CXX_STANDARD 17)
add_executable(${SOMHUNTER_TARGET_TESTS})
target_compile_features(${SOMHUNTER_TARGET_TESTS} PRIVATE cxx_std_17)
set_target_properties(${SOMHUNTER_TARGET_TESTS} PROPERTIES CXX_STANDARD 17)
# Add testing definition
target_compile_definitions(${SOMHUNTER_TARGET_TESTS} PRIVATE TESTING)
# ####################################
# Linking
# ####################################
# SET_TARGET_PROPERTIES(${SOMHUNTER_TARGET} PROPERTIES LINK_FLAGS "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\"")
# SET_TARGET_PROPERTIES(${SOMHUNTER_TARGET_TESTS} PROPERTIES LINK_FLAGS "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\"")
target_include_directories(${SOMHUNTER_TARGET} SYSTEM
PRIVATE
${CURL_INCLUDE_DIR}
${OpenCV_INCLUDE_DIRS}
${cpprestsdk_INCLUDE_DIRS}
)
target_include_directories(${SOMHUNTER_TARGET_TESTS} SYSTEM
PRIVATE
${CURL_INCLUDE_DIR}
${OpenCV_INCLUDE_DIRS}
${cpprestsdk_INCLUDE_DIRS}
)
if (MSVC)
set(ADDITIONAL OpenSSL::SSL OpenSSL::Crypto cpprestsdk::cpprest)
else ()
set(ADDITIONAL OpenSSL::SSL OpenSSL::Crypto cpprest tbb)
endif ()
target_link_libraries(${SOMHUNTER_TARGET}
PRIVATE
${OpenCV_LIBS}
${CURL_LIBRARIES}
${TORCH_LIBRARIES}
${cpprestsdk_LIBRARIES}
${ADDITIONAL}
)
target_link_libraries(${SOMHUNTER_TARGET_TESTS}
PRIVATE
${OpenCV_LIBS}
${CURL_LIBRARIES}
${TORCH_LIBRARIES}
${cpprestsdk_LIBRARIES}
${ADDITIONAL}
)
# ####################################
# Custom commands
# ####################################
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET somhunter
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:somhunter>)
endif (MSVC)
# ####################################
# Subdirectories
# ####################################
add_subdirectory(src)
add_subdirectory(third-party)
add_subdirectory(tests)