-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
247 lines (214 loc) · 8.32 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
cmake_minimum_required(VERSION 3.1)
project(ann)
# Set build type to RELEASE by default:
if (NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type provided, default to RELEASE.")
set(CMAKE_BUILD_TYPE "RELEASE")
endif ()
# for other libraries
set(ANN_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE STRING "ANN INCLUDE DIRS")
# for unit tests using google test
option(BuildUnitTest "BuildUnitTest" ON)
# for benchmarking using google bench
option(BuildBenchmarks "BuildBenchmarks" OFF)
# for cuda when this is off will use stl threads and lapack instead of cuda and cublas
option(BuildCuda "BuildCuda" ON)
# library type
option(BuildShared "BuildShared" OFF)
# nesting protection
option(AnnUseCodeCoverage "AnnUseCodeCoverage" ON)
# sanitizer support
option(Sanitize "Sanitize" OFF)
#threading options
option(PTHREADS "PTHREADS" ON)
option(WIN32_THREADS "WIN32_THREADS" OFF)
# set lib type
if(BuildShared)
set(LIB_TYPE SHARED)
else()
set(LIB_TYPE STATIC)
endif()
# conditionally add multi_core and bmp directories
message(status ${ZINHART_INCLUDE_DIR})
if("${ZINHART_INCLUDE_DIR}" MATCHES "^$")
add_subdirectory(multi_core)
add_subdirectory(bmp)
add_subdirectory(examples) # to do make this a build option
endif()
#set module path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
# set lib type
if(PTHREADS)
set(THREADS_PREFER_PTHREAD_FLAG ON)
elseif(WIN32_THREADS)
set(THREADS_PREFER_WIN32_FLAG ON)
elseif(PTHREADS AND WIN32_THREADS)
message(FATAL_ERROR "Choose 1 threading library")
endif()
# threading library
find_package(Threads REQUIRED)
# mkl blas
set(MKL_USE_SINGLE_DYNAMIC_LIBRARY OFF CACHE BOOL "Use single dynamic library interface")
set(MKL_MULTI_THREADED OFF CACHE BOOL "Use multi-threading(intel tbb, omp etc)")
find_package(MKL REQUIRED)
# find cuda package and set compute achitecture
if(BuildCuda)
find_package(CUDA REQUIRED)
# check to see if CUDA_ENABLED was defined by another library using this one
string(REGEX MATCH "CUDA_ENABLED" result "${COMPILE_DEFINITIONS}" )
if(${result} MATCHES "CUDA_ENABLED")
# do nothing
else()
# set macro to conditionaly include headers define functions etc w.r.t gpu code
add_definitions(-DCUDA_ENABLED=true)
endif()
# set nvcc flags for debugging
if("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
# check if nvcc flags have been set already
if(CUDA_NVCC_FLAGS STREQUAL "")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -arch=sm_61 -G -Wno-deprecated-gpu-targets;")
endif()
# otherwise set release flags
else()
if(CUDA_NVCC_FLAGS STREQUAL "")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -arch=sm_61 -Xptxas -O3 -lineinfo")
endif()
endif()
# set include paths
include_directories(. include ${MKL_INCLUDE_DIR} ${CUDA_INCLUDE_DIRS} multi_core/include)
else()
string(REGEX MATCH "CUDA_ENABLED" result "${COMPILE_DEFINITIONS}")
if(${result} MATCHES "CUDA_ENABLED")
else()
add_definitions(-DCUDA_ENABLED=false)
endif()
endif()
# set cxx standard
set(CMAKE_CXX_STANDARD 11)
# force cxx standard
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# set cxx flags
set(CMAKE_CXX_FLAGS "-Wall -std=c++11 ${CMAKE_CXX_FLAGS}")
# set cxx release flags
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -fPIC")
# set cxx debug flags
if(AnnUseCodeCoverage)
set(CMAKE_CXX_FLAGS_DEBUG "-g -pedantic -fno-omit-frame-pointer -fno-inline-functions -fPIC --coverage")
else()
set(CMAKE_CXX_FLAGS_DEBUG "-g -pedantic -fno-omit-frame-pointer -fno-inline-functions -fPIC")
endif()
# sanitize flags
if (Sanitize)
set(CMAKE_CXX_FLAGS "-fsantize=address -fsanitize=undefined ${CMAKE_CXX_FLAGS}")
endif()
# add src directory
add_subdirectory(src)
#enable testing and add test directory
if(BuildUnitTest)
enable_testing()
find_package(GTest)
if(GTEST_FOUND)
# add include directories
include_directories(${GTEST_INCLUDE_DIRS})
else()
# Download and unpack googletest at configure time
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/ExternalProjects/CMakeLists.txt.in.gtest googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Conditionally Add googletest directly to our build, if this is not a sub project. This defines
# the gtest and gtest_main targets.
string(REGEX MATCH "${CMAKE_CURRENT_SOURCE_DIR}" result "${CMAKE_SOURCE_DIR}")
if(${result} MATCHES "${CMAKE_CURRENT_SOURCE_DIR}")
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL
)
endif()
endif()
add_subdirectory(unit_tests)
endif()
if(BuildBenchmarks)
# set(CMAKE_ARGS "${CMAKE_ARGS} -DBENCHMARK_ENABLE_TESTING${STRING}=OFF") would like to config this with cmake args
# set(CMAKE_CACHE_ARGS "${CMAKE_CACHE_ARGS} -DBENCHMARK_ENABLE_TESTING${STRING}=OFF")
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Suppressing benchmark's tests" FORCE)
if(BuildUnitTest)
set(GTEST_ROOT_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/googletest-src/googletest" CACHE PATH "Path to externally added gtest")
#$ list(APPEND CMAKE_ARGS "-D${GTEST_ROOT}:${string}=\"${GTEST_ROOT_LOCATION}\"")
endif()
# Download and unpack googlebench at configure time
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/ExternalProjects/CMakeLists.txt.in.gbench googlebench-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googlebench-download )
if(result)
message(FATAL_ERROR "CMake step for googlebench failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googlebench-download )
if(result)
message(FATAL_ERROR "Build step for googlebench failed: ${result}")
endif()
# Add googlebench directly to our build. This defines
# the gtest and gtest_main targets.
string(REGEX MATCH "${CMAKE_CURRENT_SOURCE_DIR}" result "${CMAKE_SOURCE_DIR}")
if(${result} MATCHES "${CMAKE_CURRENT_SOURCE_DIR}")
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googlebench-src
${CMAKE_CURRENT_BINARY_DIR}/googlebench-build
EXCLUDE_FROM_ALL
)
endif()
add_subdirectory(benchmarks)
endif()
# code coverage
if(AnnUseCodeCoverage)
if(CMAKE_COMPILER_IS_GNUCXX)
include(CodeCoverage)
# ignore extraneous files
set(COVERAGE_LCOV_EXCLUDES '/usr/include/*' '${PROJECT_BINARY_DIR}/googlebench-src/*' '${PROJECT_BINARY_DIR}/googletest-src/*' '${PROJECT_BINARY_DIR}/multi_core/*')
SETUP_TARGET_FOR_COVERAGE_LCOV(NAME ${PROJECT_NAME}_coverage
EXECUTABLE ./unit_tests/ann_unit_tests
DEPENDENCIES ${PROJECT_NAME})
endif()
endif()
message(STATUS "")
message(STATUS "BUILD SUMMARY")
message(STATUS " CMAKE_GENERATOR : ${CMAKE_GENERATOR}")
message(STATUS " Compiler ID : ${CMAKE_CXX_COMPILER_ID}")
message(STATUS " Build type : ${CMAKE_BUILD_TYPE}")
message(STATUS " Library type : ${LIB_TYPE}")
if(BuildUnitTest)
message(STATUS " Build Tests : ${BuildUnitTest}")
endif()
if(BuildBenchmarks)
message(STATUS " Build Benchmarks : ${BuildBenchmarks}")
endif()
message(STATUS " Sanitize flags : ${Sanitize}")
if(BuildCuda)
message(STATUS " Found CUDA : ${CUDA_FOUND}")
message(STATUS " CUDA_NVCC_FLAGS : ${CUDA_NVCC_FLAGS}")
else()
message(STATUS " FOUND THREADS : ${THREADS_FOUND}")
message(STATUS " Found MKL include : ${MKL_INCLUDE_DIR}")
message(STATUS " Found MKL libraries : ${MKL_LIBRARIES}")
endif()
message(STATUS " CXX_FLAGS : ${CMAKE_CXX_FLAGS}")
if ("${CMAKE_BUILD_TYPE}" STREQUAL "RELEASE")
message(STATUS " CXX_FLAGS_RELEASE : ${CMAKE_CXX_FLAGS_RELEASE}")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
message(STATUS " CXX_FLAGS_DEBUG : ${CMAKE_CXX_FLAGS_DEBUG}")
endif ()
message(STATUS "")