forked from WL-Amigo/waifu2x-converter-cpp
-
Notifications
You must be signed in to change notification settings - Fork 37
/
CMakeLists.txt
260 lines (213 loc) · 8.67 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
cmake_minimum_required(VERSION 2.8)
add_executable(conv conv.c)
find_package(PkgConfig)
include(CheckIncludeFileCXX)
pkg_check_modules(OPENCV_PKG opencv)
string(TIMESTAMP TS)
add_definitions(-DBUILD_TS="${TS}")
if (${OPENCV_PKG_FOUND})
set(CMAKE_REQUIRED_INCLUDES ${OPENCV_PKG_INCLUDE_DIRS})
include_directories(${OPENCV_PKG_INCLUDE_DIRS})
link_directories(${OPENCV_PKG_LIBRARY_DIRS})
else()
set(OPENCV_PREFIX "/usr"
CACHE FILEPATH "OpenCV 2.x path")
set(CMAKE_REQUIRED_INCLUDES ${OPENCV_PREFIX}/include)
include_directories(${OPENCV_PREFIX}/include)
link_directories(${OPENCV_PREFIX}/lib)
endif()
check_include_file_cxx("opencv2/opencv.hpp" HAVE_OPENCV)
find_library(IMGCODECS_LIBRARY NAMES opencv_imgcodecs opencv_highgui
HINTS ${OPENCV_PKG_INCLUDE_DIRS} ${OPENCV_PREFIX}/lib)
if(HAVE_OPENCV)
add_definitions(-DHAVE_OPENCV)
set(OPENCV_LIBRARIES opencv_core opencv_imgproc ${IMGCODECS_LIBRARY} opencv_features2d)
endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
# include_directories("/usr")
else()
find_package(OpenCL REQUIRED)
include_directories(${OpenCL_INCLUDE_DIRS})
endif()
find_package(CUDA)
if(CUDA_FOUND)
add_definitions(-DHAVE_CUDA)
else()
message(WARNING "cuda not found. disabled.")
endif()
include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/src)
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
if(CMAKE_COMPILER_IS_GNUCXX)
set(OCV_COMPILER_DIRNAME "mingw")
elseif(MSVC11)
set(OCV_COMPILER_DIRNAME "vc11")
else()
set(OCV_COMPILER_DIRNAME "vc12")
endif()
if(MSVC)
set(CMAKE_CXX_FLAGS_RELEASE "/O2 /MT")
set(CMAKE_CXX_FLAGS_DEBUG "/Zi /MT")
endif()
if(CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static")
endif()
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(OCV_SYSTEM_DIRNAME "x64")
else()
set(OCV_SYSTEM_DIRNAME "x86")
endif()
link_directories(${OPENCV_PREFIX}/${OCV_SYSTEM_DIRNAME}/${OCV_COMPILER_DIRNAME}/staticlib)
endif()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64" OR
CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64" OR
CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR
CMAKE_SYSTEM_PROCESSOR STREQUAL "x86" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
set(X86_TRUE true)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch|^arm")
set(ARM_TRUE true)
else()
set(X86_TRUE false)
endif()
option(X86OPT "Enable X86 SIMD optimizations" ${X86_TRUE})
if(X86OPT)
add_definitions(-DX86OPT)
set(OPT_SOURCES src/modelHandler_avx.cpp src/modelHandler_fma.cpp src/modelHandler_sse.cpp)
endif()
option(ARMOPT "Enable ARM SIMD optimizations" ${ARM_TRUE})
if(ARMOPT)
add_definitions(-DARMOPT)
set(OPT_SOURCES src/modelHandler_neon.cpp)
include(CheckCXXCompilerFlag)
# -mfloat-abi=hard and -mfloat-abi=softfp cannot be mixed
# -mfpu=neon and -march=armv7-a are not supported on armv8
foreach(flag -mfloat-abi=hard -mfloat-abi=softfp -mfpu=neon -march=armv7-a)
string(REGEX REPLACE "[-=]" "_" flagp ${flag})
check_cxx_compiler_flag(${flag} ${flagp})
if(${flagp} AND NOT ${flag} MATCHES "-march")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
endif()
endforeach()
endif()
add_library(w2xc SHARED
src/modelHandler.cpp ${OPT_SOURCES}
src/modelHandler_OpenCL.cpp src/convertRoutine.cpp src/threadPool.cpp
src/modelHandler_CUDA.cpp src/w2xconv.cpp src/common.cpp
src/cvwrap.cpp
src/Env.cpp src/Buffer.cpp
)
add_dependencies(w2xc gensrcs)
if(CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
set(REMOVE_SYMBOL "")
else()
set(REMOVE_SYMBOL "-s")
endif()
set(CMAKE_CXX_FLAGS_RELEASE "-O2 ${REMOVE_SYMBOL}")
set_source_files_properties(src/modelHandler_avx.cpp PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -O2 -mavx ${REMOVE_SYMBOL}")
set_source_files_properties(src/modelHandler_fma.cpp PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -O2 -mfma ${REMOVE_SYMBOL}")
set_source_files_properties(src/modelHandler_sse.cpp PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -O2 -save-temps -msse3 ${REMOVE_SYMBOL}")
if(_march_armv7_a)
set_source_files_properties(src/modelHandler_neon.cpp PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -O2 -march=armv7-a ${REMOVE_SYMBOL}")
endif()
elseif(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd 4819 /wd 4996 /wd 4800")
set_source_files_properties(src/modelHandler_avx.cpp PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} /O2 /arch:AVX")
set_source_files_properties(src/modelHandler_fma.cpp PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} /O2 /arch:AVX2")
endif()
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
list(APPEND CUDA_NVCC_FLAGS "-m32")
endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler" "/wd 4819")
set_target_properties(w2xc PROPERTIES PREFIX "")
set_target_properties(w2xc PROPERTIES IMPORT_PREFIX "")
set_target_properties(w2xc PROPERTIES IMPORT_SUFFIX ".lib")
else()
# Lower GCC version to avoid #error in host_config.h
find_program(GCC NAMES g++-4.7 g++47 g++-4.8 g++48)
if(GCC)
list(APPEND CUDA_NVCC_FLAGS "-ccbin" "${GCC}")
endif()
endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
if(CMAKE_COMPILER_IS_GNUCXX)
target_link_libraries(w2xc
opencv_core300 opencv_imgproc300 opencv_imgcodecs300 opencv_features2d300 libjpeg libpng libtiff libjasper opencv_hal300
IlmImf zlib libwebp user32 opencv_core300 opencv_hal300) # xx 300
else()
target_link_libraries(w2xc opencv_core300 opencv_imgproc300 opencv_imgcodecs300
opencv_features2d300 ippicvmt libjpeg libpng libtiff libjasper
opencv_hal300 IlmImf zlib libwebp user32 opencv_core300 opencv_imgproc300)
endif()
add_executable(w2xcr WIN32 w32-apps/w2xcr.c w32-apps/w2xcr.rc)
target_link_libraries(w2xcr w2xc user32 shlwapi gdi32)
else()
target_link_libraries(w2xc ${OPENCV_LIBRARIES} ${CMAKE_DL_LIBS} pthread)
endif()
set(CONV_EXE "$<TARGET_FILE_DIR:conv>/conv")
add_custom_command(
OUTPUT modelHandler_OpenCL.cl.h
COMMAND ${CONV_EXE} ${CMAKE_CURRENT_SOURCE_DIR}/src/modelHandler_OpenCL.cl ${CMAKE_CURRENT_BINARY_DIR}/modelHandler_OpenCL.cl.h str
DEPENDS src/modelHandler_OpenCL.cl conv
)
set(GPU_CODE modelHandler_OpenCL.cl.h)
if(CUDA_FOUND)
add_custom_command(
OUTPUT modelHandler_CUDA.ptx20.h
COMMAND ${CONV_EXE} modelHandler_CUDA.ptx20 modelHandler_CUDA.ptx20.h str
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/modelHandler_CUDA.ptx20 conv
)
add_custom_command(
OUTPUT modelHandler_CUDA.ptx30.h
COMMAND ${CONV_EXE} modelHandler_CUDA.ptx30 modelHandler_CUDA.ptx30.h str
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/modelHandler_CUDA.ptx30 conv
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/modelHandler_CUDA.ptx20
COMMAND ${CUDA_NVCC_EXECUTABLE} ${CUDA_NVCC_FLAGS} -arch=sm_20 -ptx -o ${CMAKE_CURRENT_BINARY_DIR}/modelHandler_CUDA.ptx20 ${CMAKE_CURRENT_SOURCE_DIR}/src/modelHandler_CUDA.cu
DEPENDS src/modelHandler_CUDA.cu
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/modelHandler_CUDA.ptx30
COMMAND ${CUDA_NVCC_EXECUTABLE} ${CUDA_NVCC_FLAGS} -arch=sm_30 -ptx -o ${CMAKE_CURRENT_BINARY_DIR}/modelHandler_CUDA.ptx30 ${CMAKE_CURRENT_SOURCE_DIR}/src/modelHandler_CUDA.cu
DEPENDS src/modelHandler_CUDA.cu
)
set(GPU_CODE ${GPU_CODE} modelHandler_CUDA.ptx30.h modelHandler_CUDA.ptx20.h)
endif()
add_custom_target(gensrcs ALL
DEPENDS ${GPU_CODE})
enable_testing()
if (${HAVE_OPENCV})
add_executable(waifu2x-converter-cpp
src/main.cpp)
target_link_libraries(waifu2x-converter-cpp LINK_PUBLIC w2xc)
add_executable(runtest w32-apps/runtest.c)
target_link_libraries(runtest w2xc)
add_test(runtest runtest)
install(TARGETS w2xc waifu2x-converter-cpp
RUNTIME DESTINATION bin
LIBRARY DESTINATION "lib${LIB_SUFFIX}"
ARCHIVE DESTINATION "lib${LIB_SUFFIX}"
)
endif()
add_executable(runbench w32-apps/runbench.c)
target_link_libraries(runbench LINK_PUBLIC w2xc)
install(FILES src/w2xconv.h DESTINATION include)
# Better add support for multiple directories where to find models
# with paths separated by : (a la PATH) then install unconditionally
option(INSTALL_MODELS "Install waifu2x RGB models" false)
if(INSTALL_MODELS)
add_definitions(-DDEFAULT_MODELS_DIRECTORY="${CMAKE_INSTALL_PREFIX}/share/waifu2x-converter-cpp")
install(DIRECTORY models_rgb/ DESTINATION share/waifu2x-converter-cpp)
endif()
option(INSTALL_DOCS "Install developer documentation" false)
if(INSTALL_DOCS)
install(DIRECTORY appendix docs/internals
DESTINATION share/doc/waifu2x-converter-cpp)
endif()