-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
369 lines (293 loc) · 12.3 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
cmake_minimum_required(VERSION 3.28.0 FATAL_ERROR)
project(libpgo LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CUDA_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
OPTION(PGO_USE_MKL "Use MKL" OFF)
option(PGO_ENABLE_PYTHON "Build python module" OFF)
OPTION(PGO_ENABLE_FULL "Enable all functionalities" OFF)
option(PGO_BUILD_SUBPROJECTS "Include subprojects" OFF)
option(PGO_UI "Build pgo ui" OFF)
option(PGO_RENDER_USE_ASSIMP "Use Assimp" OFF)
OPTION(PGO_RELEASE_MODE_DEBUG "Enable debug in release mode" OFF)
OPTION(PGO_IGNORE_DEBUG_FLAG "Ignore debug flag" OFF)
OPTION(PGO_CREATE_MISSING_FILE "Create file when it is missing" OFF)
OPTION(PGO_OPT_USE_IPOPT "Use IPOPT" OFF)
OPTION(PGO_OPT_USE_KNITRO "Use KNITRO" OFF)
option(PGO_MKL_LINK_DYNAMIC "MKL Link" ON)
set(PGO_CMAKE_MODULES ${CMAKE_SOURCE_DIR}/CMakeModules)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PGO_CMAKE_MODULES})
message(STATUS "CMake module path: ${CMAKE_MODULE_PATH}")
include(CompilationUtils)
find_package(OpenMP)
if(OPENMP_FOUND)
set(OPENMP_FLAG "${OpenMP_CXX_FLAGS}")
add_compile_definitions(USE_OPENMP)
endif()
# openmp may cause mkl pardiso error
include(Find_AVX)
CHECK_FOR_AVX()
# basic debug flags
add_library(compilation_flag_for_debug INTERFACE)
add_flag(compilation_flag_for_debug MSVC Release INTERFACE /ZI)
add_flag(compilation_flag_for_debug MSVC Release INTERFACE /Oi-)
add_flag(compilation_flag_for_debug MSVC Release INTERFACE /Ob0)
add_def(compilation_flag_for_debug MSVC Debug INTERFACE _DEBUG)
add_def(compilation_flag_for_debug MSVC Release INTERFACE NDEBUG)
add_flag_poxis(compilation_flag_for_debug INTERFACE -ggdb3)
add_flag_poxis(compilation_flag_for_debug INTERFACE -O0)
add_flag_poxis(compilation_flag_for_debug INTERFACE -fsanitize=address)
add_flag_poxis(compilation_flag_for_debug INTERFACE -fsanitize=undefined)
add_flag(compilation_flag_for_debug GNU All INTERFACE -fsanitize=leak)
add_flag(compilation_flag_for_debug Clang All INTERFACE -fsanitize=leak)
add_library(compilation_flag INTERFACE)
add_library(cuda_compilation_flag INTERFACE)
message(STATUS "PGO compiler: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "OS: ${CMAKE_SYSTEM_NAME}")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
if(EXISTS "/usr/bin/g++")
message(STATUS "Set CUDA host compiler: /usr/bin/g++")
set(CMAKE_CUDA_HOST_COMPILER /usr/bin/g++)
else()
endif()
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
message(STATUS "CMake build type: ${CMAKE_BUILD_TYPE}")
if(CMAKE_BUILD_TYPE STREQUAL "")
message(STATUS "CMake build type is empty and will be set to Release")
set(CMAKE_BUILD_TYPE Release)
endif()
if(CMAKE_GENERATOR STREQUAL "Ninja")
add_flag_poxis(compilation_flag INTERFACE -fdiagnostics-color=always)
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
message(STATUS "Windows platform...")
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
if(CMAKE_GENERATOR STREQUAL Ninja)
message(STATUS "use ninja")
else()
set(CMAKE_CONFIGURATION_TYPES Debug Release)
endif()
else()
message(FATAL_ERROR "Unsupported compiler")
endif()
# basic compilation flags
add_flag_poxis(compilation_flag INTERFACE -Wall)
add_flag_poxis(compilation_flag INTERFACE -Wextra)
add_flag_poxis(compilation_flag INTERFACE -frounding-math)
add_flag_poxis(compilation_flag INTERFACE -fvisibility=hidden)
add_flag_poxis(compilation_flag INTERFACE -march=native)
add_flag_poxis(compilation_flag INTERFACE -mtune=native)
add_link_flag_poxis(compilation_flag -march=native)
add_link_flag_poxis(compilation_flag -mtune=native)
# static gcc
add_flag(compilation_flag GNU All INTERFACE -static-libstdc++)
add_flag(compilation_flag GNU All INTERFACE -static-libgcc)
add_link_flag(compilation_flag GNU All -static-libstdc++)
add_link_flag(compilation_flag GNU All -static-libgcc)
# if has openmp flags
if(OPENMP_FOUND)
add_flag(compilation_flag GNU All INTERFACE ${OPENMP_FLAG})
add_flag(compilation_flag Clang All INTERFACE ${OPENMP_FLAG})
add_link_flag(compilation_flag GNU All ${OPENMP_FLAG})
add_link_flag(compilation_flag Clang All ${OPENMP_FLAG})
add_flag(compilation_flag MSVC All INTERFACE ${OPENMP_FLAG})
endif()
# msvc flags
add_def(compilation_flag MSVC All INTERFACE __BASE_FILE__=__FILE__)
add_flag(compilation_flag MSVC All INTERFACE /bigobj)
add_flag(compilation_flag MSVC All INTERFACE /Zc:__cplusplus)
add_flag_cuda(cuda_compilation_flag MSVC All INTERFACE "-Xcompiler=/bigobj")
if(HAVE_AVX512_EXTENSIONS)
add_flag(compilation_flag MSVC All INTERFACE /arch:AVX512)
add_flag_cuda(cuda_compilation_flag MSVC All INTERFACE "-Xcompiler=/arch:AVX512")
elseif(HAVE_AVX2_EXTENSIONS)
add_flag(compilation_flag MSVC All INTERFACE /arch:AVX2)
add_flag_cuda(cuda_compilation_flag MSVC All INTERFACE "-Xcompiler=/arch:AVX2")
elseif(HAVE_AVX_EXTENSIONS)
add_flag(compilation_flag MSVC All INTERFACE /arch:AVX)
add_flag_cuda(cuda_compilation_flag MSVC All INTERFACE "-Xcompiler=/arch:AVX")
endif()
add_flag(compilation_flag MSVC All INTERFACE /MP)
add_flag_cuda(cuda_compilation_flag MSVC All INTERFACE "-Xcompiler=/MP")
# set(cuda_compilation_flag "${cuda_compilation_flag};/MP")
add_def(compilation_flag MSVC All INTERFACE NOMINMAX)
add_def(compilation_flag MSVC All INTERFACE _USE_MATH_DEFINES)
add_def(compilation_flag MSVC All INTERFACE _CRT_SECURE_NO_WARNINGS)
add_def(compilation_flag MSVC All INTERFACE HAVE_STRUCT_TIMESPEC)
add_def(compilation_flag MSVC All INTERFACE GLOG_NO_ABBREVIATED_SEVERITIES)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "Debug Mode..")
add_flag_poxis(compilation_flag INTERFACE -O0)
add_flag_poxis(compilation_flag INTERFACE -ggdb3)
add_link_flag_poxis(compilation_flag -ggdb3)
else()
message(STATUS "Release Mode..")
add_flag_poxis(compilation_flag INTERFACE -O3)
endif()
# ================== third party ==========================
find_package(Threads REQUIRED)
if(PGO_UI)
find_package(Vulkan REQUIRED)
if(PGO_RENDER_USE_ASSIMP)
find_package(assimp REQUIRED)
endif()
include(${PROJECT_SOURCE_DIR}/CMakeModules/third-party/glfw.cmake)
include(${PROJECT_SOURCE_DIR}/CMakeModules/third-party/glm.cmake)
endif()
add_subdirectory(third-party)
include(CMakeModules/third-party/tbb.cmake)
include(CMakeModules/third-party/mkl.cmake)
if(PGO_ENABLE_PYTHON OR PGO_ENABLE_FULL)
include(CMakeModules/third-party/suitesparse.cmake)
endif()
include(CMakeModules/third-party/eigen.cmake)
include(CMakeModules/third-party/fmt.cmake)
include(CMakeModules/third-party/nlohmann_json.cmake)
include(CMakeModules/third-party/spdlog.cmake)
include(CMakeModules/third-party/autodiff.cmake)
include(CMakeModules/third-party/argparse.cmake)
if(PGO_OPT_USE_KNITRO)
include(CMakeModules/third-party/knitro.cmake)
if(TARGET Knitro::Knitro)
add_compile_definitions(PGO_HAS_KNITRO)
endif()
endif()
if(PGO_ENABLE_PYTHON OR PGO_ENABLE_FULL)
include(CMakeModules/third-party/ceres.cmake)
include(CMakeModules/third-party/boost.cmake)
include(CMakeModules/third-party/cgal.cmake)
endif()
if(PGO_ENABLE_FULL)
include(CMakeModules/third-party/geogram.cmake)
include(CMakeModules/third-party/libigl.cmake)
igl_include(copyleft core)
igl_include(copyleft cgal)
# include(CMakeModules/third-party/arpackng.cmake)
endif()
if(PGO_ENABLE_PYTHON)
include(CMakeModules/third-party/pybind11.cmake)
endif()
if(TARGET MKL::MKL)
add_compile_definitions(PGO_HAS_MKL)
endif()
if(TARGET CGAL::CGAL)
add_compile_definitions(PGO_HAS_CGAL)
endif()
if(TARGET Ceres::ceres)
add_compile_definitions(PGO_HAS_CERES)
else()
endif()
# =================== external ====================
if(MSVC)
find_program(POWERSHELL_PATH powershell REQUIRED)
message(STATUS "Powershell: ${POWERSHELL_PATH}")
get_filename_component(VCPKG_TOOLCHAIN_FILE_DIR "${CMAKE_TOOLCHAIN_FILE}" DIRECTORY)
set(APPLOCAL_FILE_PATH "${VCPKG_TOOLCHAIN_FILE_DIR}/msbuild/applocal.ps1")
message(STATUS "applocal.ps1: ${APPLOCAL_FILE_PATH}")
if(EXISTS ${APPLOCAL_FILE_PATH})
set(PGO_HAS_APPLOCAL ON)
message(STATUS "Found.")
else()
set(PGO_HAS_APPLOCAL OFF)
message(STATUS "Not using vcpkg.")
endif()
endif()
set(PGO_BINARY_ROOT ${CMAKE_CURRENT_BINARY_DIR})
function(add_libpgo_lib tgt sources headers)
if(PGO_CREATE_MISSING_FILE)
foreach(src ${sources})
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${src})
message(STATUS "Create file ${CMAKE_CURRENT_SOURCE_DIR}/${src}")
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/${src} "")
endif()
endforeach()
foreach(h ${headers})
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${h})
message(STATUS "Create file ${CMAKE_CURRENT_SOURCE_DIR}/${h}")
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/${h} "")
endif()
endforeach()
endif()
add_library(${tgt} STATIC ${sources} ${headers})
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set_target_properties(${tgt} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PGO_BINARY_ROOT}/lib)
set_target_properties(${tgt} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PGO_BINARY_ROOT}/lib)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set_target_properties(${tgt} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PGO_BINARY_ROOT}/lib/Debug)
set_target_properties(${tgt} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PGO_BINARY_ROOT}/lib/Release)
else()
message(FATAL_ERROR "Unsupported compiler")
endif()
target_include_directories(${tgt} PUBLIC ./)
target_link_libraries(${tgt} PRIVATE compilation_flag)
if(PGO_RELEASE_MODE_DEBUG)
target_link_libraries(${tgt} PRIVATE compilation_flag_for_debug)
endif()
set_property(TARGET ${tgt} PROPERTY FOLDER libraries)
# set(PGO_GLOBAL_LIBRARY_TARGETS ${PGO_GLOBAL_LIBRARY_TARGETS} ${tgt} CACHE INTERNAL "global library targets")
endfunction()
function(add_libpgo_tools tgt sources deps)
if(PGO_CREATE_MISSING_FILE)
foreach(src ${sources})
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${src})
message(STATUS "Create file ${CMAKE_CURRENT_SOURCE_DIR}/${src}")
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/${src} "")
endif()
endforeach()
endif()
add_executable(${tgt} ${sources})
foreach(d ${deps})
target_link_libraries(${tgt} PRIVATE ${d})
endforeach()
fix_mkl_tbb_debug(${tgt})
fix_glut32(${tgt})
target_link_libraries(${tgt} PRIVATE compilation_flag)
if(PGO_RELEASE_MODE_DEBUG)
target_link_libraries(${tgt} PRIVATE compilation_flag_for_debug)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set_target_properties(${tgt} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PGO_BINARY_ROOT}/bin)
set_target_properties(${tgt} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PGO_BINARY_ROOT}/bin)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set_target_properties(${tgt} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PGO_BINARY_ROOT}/bin/Debug)
set_target_properties(${tgt} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PGO_BINARY_ROOT}/bin/Release)
endif()
set_property(TARGET ${tgt} PROPERTY FOLDER "tools")
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND PGO_HAS_APPLOCAL)
add_custom_command(TARGET ${tgt} POST_BUILD
COMMAND ${POWERSHELL_PATH} -noprofile -executionpolicy Bypass -file ${APPLOCAL_FILE_PATH}
-targetBinary $<TARGET_FILE:${tgt}>
-installedDir "${EXT_LIB_ROOT}/bin"
-OutVariable out
)
add_custom_command(TARGET ${tgt} POST_BUILD
COMMAND ${POWERSHELL_PATH} -noprofile -executionpolicy Bypass -file ${APPLOCAL_FILE_PATH}
-targetBinary $<TARGET_FILE:${tgt}>
-installedDir "${EXT_LIB_ROOT}/bin/$<CONFIG>"
-OutVariable out
)
endif()
# if(PGO_COMPILER STREQUAL "GNU")
# install(TARGETS ${tgt} RUNTIME DESTINATION bin)
# elseif(PGO_COMPILER STREQUAL "MSVC")
# install(TARGETS ${tgt} RUNTIME DESTINATION bin/$<CONFIG>)
# endif()
endfunction()
add_subdirectory(src)
if(PGO_BUILD_SUBPROJECTS)
add_subdirectory(projects/MSD)
endif()
# generate config file to be used in common build tree
configure_package_config_file(
CMakeModules/pgoConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/pgoConfig.cmake
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/pgo/"
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/pgoConfig.cmake
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/pgo/"
)