-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
220 lines (184 loc) · 6.76 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
cmake_minimum_required(VERSION 2.8)
# PROJECT(fibergen CXX)
# Set verbose output while testing CMake
SET(CMAKE_VERBOSE_MAKEFILE 1)
# Use C++ 11
SET(CMAKE_CXX_STANDARD 11)
# Note for the use of -ffast-math:
# http://stackoverflow.com/questions/22931147/stdisinf-does-not-work-with-ffast-math-how-to-check-for-infinity
SET(PYTHON_VERSION_MAJOR 3)
SET(PYTHON_VERSION_MINOR 6)
OPTION(RELEASE_BUILD "Relase build" ON)
OPTION(DEBUG_INFO_BUILD "Enable debug info on release build" OFF)
OPTION(USE_OPENMP "Use OpenMP" ON)
IF(APPLE)
SET(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
ENDIF()
# Compiler check
IF(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
SET(GNU_COMPILER ON)
ELSE()
SET(GNU_COMPILER OFF)
ENDIF()
IF(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
SET(CLANG_COMPILER ON)
ELSE()
SET(CLANG_COMPILER OFF)
ENDIF()
IF(RELEASE_BUILD)
IF(DEBUG_INFO_BUILD)
SET(CMAKE_BUILD_TYPE ReleaseWithDebInfo)
IF(GNU_COMPILER)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g")
ENDIF()
ELSE()
SET(CMAKE_BUILD_TYPE Release)
IF(GNU_COMPILER)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
ENDIF()
ENDIF()
IF(GNU_COMPILER)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -DNDBUG -Wall -Wno-unknown-pragmas -Wno-unused-variable -Wno-unused-local-typedefs -Wno-long-long")
ENDIF()
ELSE()
SET(CMAKE_BUILD_TYPE Debug)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNAN_FILL")
SET(Boost_USE_DEBUG_PYTHON ON)
SET(Boost_USE_DEBUG_RUNTIME ON)
SET(Boost_USE_RELEASE_LIBS OFF)
SET(Boost_USE_DEBUG_LIBS ON)
ENDIF()
# Add cmake dir to search path
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
# Find Python library and executable
FIND_PACKAGE(PythonInterp ${PYTHON_VERSION_MAJOR} REQUIRED)
FIND_PACKAGE(PythonLibs ${PYTHON_VERSION_MAJOR} REQUIRED)
MESSAGE("Python executable: " ${PYTHON_EXECUTABLE})
MESSAGE("Python include dirs: " ${PYTHON_INCLUDE_DIRS})
MESSAGE("Python libraries: " ${PYTHON_LIBRARIES})
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${PYTHON_INCLUDE_DIRS})
# Find NumPy library
SET(NUMPY_FIND_REQUIRED ON)
FIND_PACKAGE(NumPy REQUIRED)
MESSAGE("NumPy include dir: " ${NUMPY_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${NUMPY_INCLUDE_DIR})
# Find PNG library
FIND_PACKAGE(PNG REQUIRED)
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${PNG_INCLUDE_DIR})
# Find ZLIB library
FIND_PACKAGE(ZLIB REQUIRED)
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIR})
# Find OpenMP
IF(USE_OPENMP)
SET(OpenMP_CXX_FLAGS "-fopenmp")
IF(APPLE)
IF(CLANG_COMPILER)
SET(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
SET(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
SET(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
SET(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
SET(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
SET(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES})
ENDIF()
ENDIF()
FIND_PACKAGE(OpenMP QUIET)
IF(OpenMP_FOUND)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ADD_DEFINITIONS(-DOPENMP_ENABLED)
ENDIF()
ENDIF()
# Find ITK library
FIND_PACKAGE(ITK QUIET)
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${ITK_INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${ITK_LIBRARY_DIRS})
IF(ITK_FOUND)
ADD_DEFINITIONS(-DITK_ENABLED)
ENDIF()
#MESSAGE("ITK include dirs: " ${ITK_INCLUDE_DIRS})
#MESSAGE("ITK libraries: " ${ITK_LIBRARY_DIRS})
# Find threads library https://cmake.org/cmake/help/v3.2/module/FindThreads.html
FIND_PACKAGE(Threads REQUIRED)
# Find Boost library
SET(Boost_USE_MULTITHREADED ${USE_OPENMP})
SET(Boost_USE_STATIC_RUNTIME OFF)
# Try to find boost python
SET(Boost_Python python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR})
FIND_PACKAGE(Boost QUIET OPTIONAL_COMPONENTS ${Boost_Python})
STRING(TOUPPER "${Boost_Python}" BPY)
IF(NOT Boost_FOUND OR NOT ${Boost_${BPY}_FOUND})
SET(Boost_Python python-py${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR})
SET(Boost_Python python)
ENDIF()
# TODO: Boost >= 1.68: also add gil component?
FIND_PACKAGE(Boost REQUIRED COMPONENTS system program_options thread ${Boost_Python} iostreams filesystem)
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
# Find Boost Numeric Bindings library
FIND_PACKAGE(BoostNumericBindings REQUIRED)
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${BoostNumericBindings_INCLUDES})
# Find LAPACK library
SET(LAPACK_FIND_REQUIRED ON)
FIND_PACKAGE(LAPACK REQUIRED)
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${LAPACK_INCLUDE_DIR})
# Find FFTW library
SET(USE_FFTWF ON)
SET(USE_FFTWD ON)
SET(USE_FFTWL OFF)
SET(FFTWF_FIND_REQUIRED OFF)
SET(FFTWD_FIND_REQUIRED ON)
SET(FFTWL_FIND_REQUIRED OFF)
FIND_PACKAGE(FFTW REQUIRED)
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${FFTW_INCLUDES})
IF(FFTWF_FOUND)
ADD_DEFINITIONS(-DFFTWF_ENABLED)
ENDIF()
SET(SOURCES src/fibergen.cpp)
SET(LIBS ${Boost_LIBRARIES} ${PNG_LIBRARY} ${ZLIB_LIBRARY} ${FFTWF_LIB} ${FFTWD_LIB} ${FFTWL_LIB} ${LAPACK_LIBRARIES} ${PYTHON_LIBRARIES} ${ITK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
SET(PROJECT_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
SITE_NAME(HOST_NAME)
STRING(REPLACE "-" "_" HOST_NAME ${HOST_NAME})
STRING(REPLACE " " "_" HOST_NAME ${HOST_NAME})
STRING(REPLACE "." "_" HOST_NAME ${HOST_NAME})
ADD_DEFINITIONS(-DHOST_NAME=${HOST_NAME})
SET(EXE_NAME fibergen_${HOST_NAME})
SET(LIB_NAME pyfibergen_${HOST_NAME})
# Generate Python module header
#MESSAGE(${CMAKE_BINARY_DIR})
SET(FG_PYTHON_HEADER_NAME "${CMAKE_BINARY_DIR}/PythonModuleHeader_${HOST_NAME}.h")
ADD_DEFINITIONS(-DFG_PYTHON_HEADER_NAME="${FG_PYTHON_HEADER_NAME}")
CONFIGURE_FILE("cmake/PythonModuleHeader.h" "${FG_PYTHON_HEADER_NAME}")
# Add the executable
ADD_EXECUTABLE(${EXE_NAME} ${SOURCES})
TARGET_LINK_LIBRARIES(${EXE_NAME} ${LIBS})
# Copy the executable to bin folder
ADD_CUSTOM_COMMAND(TARGET ${EXE_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${EXE_NAME} ${PROJECT_BINARY_DIR}/.
)
IF(FALSE)
SET(IACA_EXE_NAME fibergen_iaca_${HOST_NAME})
# Add IACA enabled executable
ADD_EXECUTABLE(${IACA_EXE_NAME} ${SOURCES})
TARGET_LINK_LIBRARIES(${IACA_EXE_NAME} ${LIBS})
SET_TARGET_PROPERTIES(${IACA_EXE_NAME} PROPERTIES COMPILE_FLAGS "-DIACA_ENABLED")
# Copy the executable to bin folder
ADD_CUSTOM_COMMAND(TARGET ${IACA_EXE_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${IACA_EXE_NAME} ${PROJECT_BINARY_DIR}/.
)
ENDIF()
IF(TRUE)
ADD_DEFINITIONS(-DLIB_BUILD)
# Add the Python library
ADD_LIBRARY(${LIB_NAME} SHARED ${SOURCES})
TARGET_LINK_LIBRARIES(${LIB_NAME} ${LIBS})
# Copy the library to lib folder
SET(PROJECT_LIB_DIR ${CMAKE_SOURCE_DIR}/lib)
ADD_CUSTOM_COMMAND(TARGET ${LIB_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_LIB_DIR}
COMMAND ${CMAKE_COMMAND} -E copy lib${LIB_NAME}.so ${PROJECT_LIB_DIR}/${EXE_NAME}.so
)
ENDIF()