-
Notifications
You must be signed in to change notification settings - Fork 6
/
CMakeLists.txt
330 lines (248 loc) · 11.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
cmake_minimum_required(VERSION 3.16)
project(ASSET VERSION 0.5.1 LANGUAGES CXX)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set( CMAKE_MESSAGE_LOG_LEVEL "STATUS" )
################################################################################
################## Includes ####################################################
################################################################################
include(cmake/git-submodule-init.cmake)
include(cmake/git-submodule-update.cmake)
################################################################################
############### Warnings and Errors ############################################
################################################################################
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt.")
endif()
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
message(FATAL_ERROR "You cannot build ASSET with the Microsoft Compiler. Please use Clang or GCC and try again.")
endif()
if(NOT WIN32)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
set(CMAKE_EXE_LINKER_FLAGS "-fuse-ld=lld")
endif()
endif()
################################################################################
############### Build Settings #################################################
################################################################################
set(LINK_TIME_OPT FALSE) ### DO LTO - Recommended for full release
set(CLANG_MAX_INLINE_DEPTH 225)
#set(BUILD_ASSET_WHEEL TRUE) ### CI Build for generic wheels
#set(BUILD_SPHINX_DOCS TRUE)
################################################################################
########################## Set Compiler Flags ################################
################################################################################
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Release Flags
list(APPEND RELEASE_FLAGS "-O2")
## Generic Binary Flags
if(BUILD_ASSET_WHEEL)
#list(APPEND RELEASE_FLAGS "-mcx16;-mpopcnt;-msse3;-msse4.1;-msse4.2;-mssse3;-mavx;-mavx2;-mbmi;-mbmi2;-mf16c;-mfma;-mlzcnt;-mmovbe;-mxsave")
# x86-64-v3 flags written out for older compilers
list(APPEND RELEASE_FLAGS "-mcx16")
list(APPEND RELEASE_FLAGS "-mpopcnt")
list(APPEND RELEASE_FLAGS "-msse3")
list(APPEND RELEASE_FLAGS "-msse4.1")
list(APPEND RELEASE_FLAGS "-msse4.2")
list(APPEND RELEASE_FLAGS "-mssse3")
list(APPEND RELEASE_FLAGS "-mavx")
list(APPEND RELEASE_FLAGS "-mavx2")
list(APPEND RELEASE_FLAGS "-mbmi")
list(APPEND RELEASE_FLAGS "-mbmi2")
list(APPEND RELEASE_FLAGS "-mf16c")
list(APPEND RELEASE_FLAGS "-mfma")
list(APPEND RELEASE_FLAGS "-mlzcnt")
list(APPEND RELEASE_FLAGS "-mmovbe")
list(APPEND RELEASE_FLAGS "-mxsave")
# Windows runs out of ram with LINK_TIME_OPT on gh-actions
if(NOT WIN32)
list(APPEND RELEASE_FLAGS "-mtune=skylake")
endif()
set(LINK_TIME_OPT TRUE) ### DO LTO - Recommended for full release
set(CLANG_MAX_INLINE_DEPTH 400)
else()
list(APPEND RELEASE_FLAGS "-march=native")
if(NOT WIN32)
list(APPEND RELEASE_FLAGS "-mtune=native")
endif()
endif()
if(WIN32)
list(APPEND RELEASE_FLAGS "/MD")
list(APPEND RELEASE_FLAGS "/DNDEBUG")
list(APPEND RELEASE_FLAGS "/O2")
list(APPEND RELEASE_FLAGS "/Ob2")
list(APPEND RELEASE_FLAGS "/arch:AVX2")
list(APPEND RELEASE_FLAGS "/fp:fast")
list(APPEND RELEASE_FLAGS "/GS-")
list(APPEND RELEASE_FLAGS "/Gw")
endif()
if(NOT WIN32)
# Debugging Flags
set(DEBUG_FLAGS "-g" "-ggdb3")
# Common Flags
list(APPEND COMMON_FLAGS "-pthread")
endif()
if(LINK_TIME_OPT)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
if(WIN32)
## clang-cl flag
if(BUILD_ASSET_WHEEL)
## Do full LTO for CI builds since it is single threaded anyway
list(APPEND RELEASE_FLAGS "-flto=full")
list(APPEND RELEASE_FLAGS "/opt:lldltojobs=1")
else()
list(APPEND RELEASE_FLAGS "-flto=thin")
list(APPEND RELEASE_FLAGS "/opt:lldltojobs=8")
endif()
else()
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
endif()
elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
set(CMAKE_CXX_COMPILE_OPTIONS_IPO ${CMAKE_CXX_COMPILE_OPTIONS_IPO} -flto=1)
endif()
else()
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)
endif()
list(APPEND COMMON_FLAGS "$<$<STREQUAL:${CMAKE_CXX_COMPILER_ID},Clang>:-mllvm>")
list(APPEND COMMON_FLAGS "$<$<STREQUAL:${CMAKE_CXX_COMPILER_ID},Clang>:-inline-threshold=${CLANG_MAX_INLINE_DEPTH}>")
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" AND FALSE)
list(APPEND RELEASE_FLAGS "-finline-limit=1000")
list(APPEND RELEASE_FLAGS "--param=inline-unit-growth=500") #40,40
list(APPEND RELEASE_FLAGS "--param=early-inlining-insns=50") #14,14
list(APPEND RELEASE_FLAGS "--param=large-function-insns=3500") #2700,2700
list(APPEND RELEASE_FLAGS "--param=large-function-growth=120") #100,100
list(APPEND RELEASE_FLAGS "--param=inline-min-speedup=10") #30,15
endif()
if(NOT WIN32)
list(APPEND RELEASE_FLAGS "-fomit-frame-pointer")
list(APPEND RELEASE_FLAGS "-fno-stack-protector")
#list(APPEND RELEASE_FLAGS "-fno-stack-clash-protection")
#list(APPEND RELEASE_FLAGS "-fcf-protection=none")
list(APPEND RELEASE_FLAGS "-fno-asynchronous-unwind-tables")
list(APPEND RELEASE_FLAGS "-ffast-math")
endif()
if(APPLE)
list(APPEND COMMON_FLAGS "-Xlinker -undefined")
list(APPEND COMMON_FLAGS "-Xlinker dynamic_lookup")
endif()
# Combine Flags
set(COMPILE_FLAGS PUBLIC ${COMMON_FLAGS})
list(APPEND COMPILE_FLAGS "$<$<OR:$<CONFIG:RELEASE>,$<CONFIG:RELWITHDEBINFO>>:${RELEASE_FLAGS}>")
list(APPEND COMPILE_FLAGS "$<$<OR:$<CONFIG:DEBUG>,$<CONFIG:RELWITHDEBINFO>>:${DEBUG_FLAGS}>")
################################################################################
################## ASSET PREPROCCESSOR MACROS #################################
################################################################################
add_definitions(-DEIGEN_INITIALIZE_MATRICES_BY_ZERO) ##NEVER EVER GET RID OF THIS EVER
add_definitions(-DEIGEN_DONT_PARALLELIZE) ##NEVER EVER GET RID OF THIS EVER
add_definitions(-DASSET_DEFAULT_QP_THREADS=8) ## no spaces between = !!!! otherwise it is set to one
add_definitions(-DASSET_DEFAULT_FUNC_THREADS=16) ## no spaces between = !!!! otherwise it is set to one
### initial size of asset arena memory allocator in number of doubles/superscalars
add_definitions(-DASSET_DEFAULT_ARENA_SIZE=256) ## no spaces between = !!!! otherwise it is set to one
### Comment out to roll back to old memory allocator. We will get rid of this once confident in arena
add_definitions(-DASSET_MEMORYMAN)
add_definitions(-DASSET_COMPILERVERSION="${CMAKE_CXX_COMPILER_VERSION}")
add_definitions(-DASSET_VERSIONSTRING="${CMAKE_PROJECT_VERSION}")
add_definitions(-DASSET_COMPILERSTRING="${CMAKE_CXX_COMPILER_ID}")
add_definitions(-DASSET_COMPILERVERSION="${CMAKE_CXX_COMPILER_VERSION}")
add_definitions(-DASSET_OS="${CMAKE_SYSTEM_NAME}")
add_definitions(-DASSET_OSVERSION="${CMAKE_SYSTEM_VERSION}")
add_definitions(-DFMT_HEADER_ONLY)
if(WIN32)
## See : https://developercommunity.visualstudio.com/t/Access-violation-in-_Thrd_yield-after-up/10664660#T-N10669129-N10678728
add_definitions(-D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)
endif()
################################################################################
###################### Find and set our dependencies ###########################
################################################################################
if(APPLE)
include(FindPythonEnv)
endif()
# Set up submodule dependencies
include_directories(dep/eigen)
include_directories(dep/fmt/include)
add_subdirectory(dep)
set(PYBIND11_CPP_STANDARD -std=c++17)
# Set up external dependencies
find_package(Threads REQUIRED)
if(UNIX AND NOT APPLE)
# find_package(OpenMP REQUIRED)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
list(APPEND COMPILE_FLAGS "-fopenmp=libiomp5")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GCC")
list(APPEND COMPILE_FLAGS "-fopenmp")
endif()
elseif(APPLE)
if(FALSE)
include_directories("/usr/local/opt/llvm/include")
link_directories("/usr/local/opt/llvm/lib")
set(CMAKE_LIBRARY_PATH /usr/local/opt/llvm/lib ${CMAKE_LIBRARY_PATH})
set(CMAKE_LIBRARY_PATH $ENV{MKLROOT}/lib ${CMAKE_LIBRARY_PATH})
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(OpenMP_C "${CMAKE_C_COMPILER}")
set(OpenMP_C_FLAGS "-fopenmp=libomp")
set(OpenMP_C_LIB_NAMES "libomp")
set(OpenMP_libomp_LIBRARY "omp")
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-fopenmp=libomp")
set(OpenMP_CXX_LIB_NAMES "libomp")
set(OpenMP_libomp_LIBRARY "omp")
find_package(OpenMP REQUIRED)
list(APPEND COMPILE_FLAGS ${OpenMP_CXX_FLAGS})
endif()
endif()
endif()
find_package(MKL REQUIRED)
find_package(Python ${PYVERSION_EXACT} REQUIRED COMPONENTS Interpreter Development)
# Set dependency variables
set(INCLUDE_DIRS ${PYBIND11_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS} ${MKL_INCLUDE_DIRS})
set(LINK_LIBS ${PYTHON_LIBRARIES} ${MKL_LIBRARIES} Threads::Threads ${CMAKE_DL_LIBS})
if(UNIX)
list(APPEND LINK_LIBS m)
endif()
################################################################################
################ Linker Flags ##################################################
if(BUILD_SHARED_LIBS AND NOT APPLE AND NOT WIN32) # Windows and Linux Dynamic Linking
list(APPEND LINKER_FLAGS "${OpenMP_CXX_FLAGS} -Wl,--no-undefined -Wl,--start-group ${MKL_LIBRARIES_LIST} -Wl, --end-group")
elseif(UNIX AND NOT APPLE) # Linux Static Linking
list(APPEND LINKER_FLAGS "${OpenMP_CXX_FLAGS} -Wl,--no-undefined -Wl,--start-group ${MKL_LIBRARIES_LIST} -Wl,--end-group")
elseif(APPLE) # Apple Dynamic and Static Linking
list(APPEND LINKER_FLAGS "${OpenMP_CXX_FLAGS} -Xlinker -undefined -Xlinker dynamic_lookup")
else() # Windows Static Linking
list(APPEND LINKER_FLAGS "${OpenMP_CXX_FLAGS}")
endif()
###########################################################
######## Find Correct Python Install Location ############
if(NOT PIP_INSTALL)
if(WIN32)
set(PYTHON_LOCAL_INSTALL_DIR ${Python_SITEARCH})
else()
execute_process ( COMMAND ${PYTHON_EXECUTABLE} -m site --user-site
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
set(PYTHON_LOCAL_INSTALL_DIR ${PYTHON_SITE_PACKAGES})
endif()
if(PYTHON_LOCAL_INSTALL_DIR)
message(STATUS "ASSET will be installed to " ${PYTHON_LOCAL_INSTALL_DIR})
else()
message(STATUS "Could not find appropriate install directory")
endif()
endif()
message(STATUS "CMAKE DIR LOC" ${CMAKE_CURRENT_SOURCE_DIR})
message(STATUS ${OpenMP_CXX_FLAGS})
message(STATUS ${OpenMP_CXX_LIBRARIES})
message(STATUS ${PYTHON_EXECUTABLE})
################################################################################
# Code Directories
add_subdirectory(src)
add_subdirectory(extensions)
# Documentation
# READ: pip install sphinx,breathe,furo before trying to build docs
if(BUILD_SPHINX_DOCS)
add_subdirectory(doc)
endif()
add_subdirectory(pypiwheel)
# Formatting
include(cmake/clang-format.cmake)