00-Common.cmake 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. # -*- cmake -*-
  2. if (COMMON_CMAKE_INCLUDED)
  3. return()
  4. endif (COMMON_CMAKE_INCLUDED)
  5. set (COMMON_CMAKE_INCLUDED TRUE)
  6. # Include compilation options
  7. include(00-BuildOptions)
  8. # Early download of utilities used for building the viewer under Windows and
  9. # macOS: this is currently for zstd[.exe] only, which is used to unpack LL's
  10. # newest *.tar.zst pre-built library packages, without needing to install the
  11. # pyzst Python module under Windows or macOS, since Python cannot natively
  12. # deal with them. Linux builds currently do not make use of *.zst packages,
  13. # and should they do at some point in the future, zstd would likely already
  14. # be installed on the build system already. HB
  15. include(BuildUtils)
  16. # This is for cmake v3.11.1 and newer:
  17. set(OpenGL_GL_PREFERENCE "LEGACY")
  18. # Platform-specific compilation flags.
  19. if (DARWIN)
  20. # Note: we adopt just one build type corresponding to the requested type,
  21. # so that this type gets auto-selected in Xcode's schemes. HB
  22. set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}" CACHE STRING
  23. "Supported build types." FORCE)
  24. # Specify the C and C++ standard in use for clang
  25. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c17")
  26. # Use C++17 for possible better optimizations, even though our viewer code
  27. # itself does not make use of C++17 features just yet (but, e.g. phmap.h
  28. # does use some when available). Also, libc++ must always be used instead
  29. # of libstdc++ on Macs.
  30. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -stdlib=libc++")
  31. set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
  32. # OpenMP support is missing from Apple's llvm/clang...
  33. #if (OPENMP)
  34. # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
  35. #endif (OPENMP)
  36. # Never add thread-locking code to local static variables (we never use
  37. # non-thread_local static local variables in thread-sensitive code):
  38. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-threadsafe-statics")
  39. # Compiler-version-specific warnings disabling:
  40. set(IGNORED_C_WARNINGS "-Wno-implicit-int-conversion -Wno-shorten-64-to-32 -Wno-unknown-warning-option")
  41. set(IGNORED_CXX_WARNINGS "-Wno-implicit-int-conversion -Wno-shorten-64-to-32 -Wno-unknown-warning-option")
  42. # Vectorized maths selection
  43. if (ARCH STREQUAL "arm64")
  44. # We need a conversion header to get the SSE2 code translated into its
  45. # NEON counterpart (thanks to sse2neon.h).
  46. add_definitions(-DSSE2NEON=1)
  47. # If not already manualy specified, add the necessary -march tunes for
  48. # sse2neon.h to take full effect. See the "Usage" section at:
  49. # https://github.com/DLTcollab/sse2neon/blob/master/README.md
  50. if (NOT "${CMAKE_C_FLAGS}" MATCHES ".*-march.*")
  51. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+fp+simd")
  52. endif ()
  53. if (NOT "${CMAKE_CXX_FLAGS}" MATCHES ".*-march=.*")
  54. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+fp+simd")
  55. endif ()
  56. else (ARCH STREQUAL "arm64")
  57. # We only support 64 bits builds for x86 targets
  58. set(ARCH_FLAG "-m64")
  59. # SSE2 is always enabled by default for 64 bits targets
  60. set(COMPILER_MATHS "-mfpmath=sse")
  61. if (USEAVX2)
  62. set(COMPILER_MATHS "-mavx2 -mfma -mfpmath=sse")
  63. elseif (USEAVX)
  64. set(COMPILER_MATHS "-mavx -mfpmath=sse")
  65. endif ()
  66. endif (ARCH STREQUAL "arm64")
  67. # Compilation flags common to all build types and compilers
  68. # NOTE: do not use -ffast-math: it makes us loose the Moon (!) and probably
  69. # causes other issues as well...
  70. # Please, note that -fno-strict-aliasing is *required*, because the code is
  71. # not strict aliasing safe and omitting this option would cause warnings and
  72. # bogus optimizations by gcc.
  73. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pipe -g -fexceptions -fno-strict-aliasing -fvisibility=hidden -fsigned-char ${ARCH_FLAG} ${COMPILER_MATHS} -fno-math-errno -fno-trapping-math -pthread")
  74. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -g -fexceptions -fno-strict-aliasing -fvisibility=hidden -fsigned-char ${ARCH_FLAG} ${COMPILER_MATHS} -fno-math-errno -fno-trapping-math -pthread")
  75. if (PROTECTSTACK)
  76. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
  77. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
  78. else (PROTECTSTACK)
  79. # We do not care for protecting the stack with canaries. This option
  80. # provides some (small) speed and caches usage gains.
  81. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-stack-protector")
  82. endif (PROTECTSTACK)
  83. # XCode 9 clang-llvm got a totally broken optimizer at -O3, resulting in
  84. # viewer crashes. So let's use -O2.
  85. set(OPT_FLAGS "-O2 -fno-delete-null-pointer-checks -mno-retpoline -mno-retpoline-external-thunk")
  86. add_definitions(-DLL_DARWIN=1)
  87. set(CMAKE_CXX_LINK_FLAGS "-Wl,-headerpad_max_install_names,-search_paths_first")
  88. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
  89. # NOTE: it is critical to have both CXX_FLAGS and C_FLAGS covered.
  90. set(CMAKE_C_FLAGS_DEBUG "-fno-inline -D_DEBUG -DLL_DEBUG=1")
  91. set(CMAKE_CXX_FLAGS_DEBUG "-fno-inline -D_DEBUG -DLL_DEBUG=1")
  92. set(CMAKE_C_FLAGS_RELWITHDEBINFO "-fno-inline -DLL_NO_FORCE_INLINE=1 -DNDEBUG")
  93. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-fno-inline -DLL_NO_FORCE_INLINE=1 -DNDEBUG")
  94. set(CMAKE_C_FLAGS_RELEASE "${OPT_FLAGS} -DNDEBUG")
  95. set(CMAKE_CXX_FLAGS_RELEASE "${OPT_FLAGS} -DNDEBUG")
  96. endif (DARWIN)
  97. if (LINUX)
  98. # Supported build types:
  99. set(CMAKE_CONFIGURATION_TYPES "Release;RelWithDebInfo;Debug" CACHE STRING
  100. "Supported build types." FORCE)
  101. set(CMAKE_SKIP_RPATH TRUE)
  102. if (${CLANG_VERSION} LESS 340 AND ${GCC_VERSION} LESS 600)
  103. message(FATAL_ERROR "Cannot compile any more with gcc below version 6.0 or clang below version 3.4, sorry...")
  104. endif ()
  105. # Compiler-version-specific warnings disabling:
  106. set(IGNORED_C_WARNINGS "")
  107. set(IGNORED_CXX_WARNINGS "")
  108. # LTO got largely broken with gcc 12... It now spews totally bogus warnings
  109. # at link time !
  110. if (USELTO AND NOT ${GCC_VERSION} LESS 1200)
  111. set(IGNORED_CXX_WARNINGS "${IGNORED_CXX_WARNINGS} -Wno-stringop-overflow -Wno-alloc-size-larger-than")
  112. endif ()
  113. if (USE_TRACY AND ${GCC_VERSION} GREATER 0)
  114. # Avoid warnings seen in Tracy headers with gcc...
  115. set(IGNORED_CXX_WARNINGS "${IGNORED_CXX_WARNINGS} -Wno-maybe-uninitialized")
  116. endif ()
  117. # Debugging information generation options
  118. if (${CLANG_VERSION} GREATER 0)
  119. set(DEBUG_OPTIONS "-g")
  120. else ()
  121. set(DEBUG_OPTIONS "-g -gdwarf-4 -fno-var-tracking-assignments")
  122. endif ()
  123. # Specify the C standard in use.
  124. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c17")
  125. # Specify the C++ standard in use; C++17 is fully supported in gcc 7+ and
  126. # clang 5+, so let's use it for possible better optimizations, even though
  127. # our viewer code does not make use of C++17 features just yet (but, e.g.
  128. # phmap.h does use some when available). HB
  129. if (${GCC_VERSION} GREATER 699 OR ${CLANG_VERSION} GREATER 499)
  130. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
  131. else ()
  132. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
  133. endif ()
  134. if (PROTECTSTACK)
  135. # Also enable "safe" glibc functions variants usage (_FORTIFY_SOURCE)
  136. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector -D_FORTIFY_SOURCE=2")
  137. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector -D_FORTIFY_SOURCE=2")
  138. else (PROTECTSTACK)
  139. # We do not care for protecting the stack with canaries. This option
  140. # provides some (small) speed and caches usage gains. Also forbid the
  141. # usage of slower but "safe" glibc functions variants.
  142. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-stack-protector -U_FORTIFY_SOURCE")
  143. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-stack-protector -U_FORTIFY_SOURCE")
  144. endif (PROTECTSTACK)
  145. if (GPROF)
  146. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
  147. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
  148. endif (GPROF)
  149. if (ASAN)
  150. if (${CLANG_VERSION} GREATER 0)
  151. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address --param asan-stack=0 --param asan-globals=0")
  152. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address --param asan-stack=0 --param asan-globals=0")
  153. else (${CLANG_VERSION} GREATER 0)
  154. message(WARNING "GGC's ASAN would not work properly, and would immeditaley stop the viewer on CEF memory leaks... ASAN *not* compiled in. Please use llvm/clang instead for ASAN builds.")
  155. endif (${CLANG_VERSION} GREATER 0)
  156. endif (ASAN)
  157. # Prevent executable stack (used by gcc's implementation of lambdas) when
  158. # possible.
  159. if (NOEXECSTACK AND ${GCC_VERSION} GREATER 0)
  160. if (${GCC_VERSION} LESS 700)
  161. # With gcc 6 this merely results in removing the executable stack
  162. # flag from the final binary without preventing the actual use of
  163. # it, which poses a question: what happens if the OS is configured
  164. # to totally forbid executable stack for that program and the
  165. # latter still tries and uses it ?... A crash, probably. This would
  166. # need serious tests, under SELinux for example...
  167. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,--noexecstack -Wl,-z,noexecstack")
  168. message(STATUS "Marking stack as non-executable despite the use of GNU lambdas: this is UNTESTED.")
  169. else (${GCC_VERSION} LESS 700)
  170. # With gcc 7+ we may use this new option, but then the generated
  171. # code (which uses function descriptors for lambdas) is slightly
  172. # slower since "most calls through function pointers then need to
  173. # test a bit in the pointer to identify descriptors".
  174. # See the implementor's note:
  175. # http://gcc.1065356.n8.nabble.com/RFC-PATCH-C-ADA-use-function-descriptors-instead-of-trampolines-in-C-td1503095.html#a1546131
  176. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-trampolines")
  177. endif (${GCC_VERSION} LESS 700)
  178. endif (NOEXECSTACK AND ${GCC_VERSION} GREATER 0)
  179. # Never add thread-locking code to local static variables (we never use
  180. # non-thread_local static local variables in thread-sensitive code):
  181. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-threadsafe-statics")
  182. # Use -fPIC under Linux for 64 bits (especially for our libraries),
  183. # else linking errors may happen.
  184. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
  185. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
  186. # Remember whether the user asked for native optimization option or not.
  187. set(TUNED_BUILD OFF)
  188. if ("${CMAKE_CXX_FLAGS}" MATCHES ".*-march=native.*")
  189. set(TUNED_BUILD ON)
  190. endif ()
  191. # Vectorized maths selection
  192. if (ARCH STREQUAL "arm64")
  193. # We need a conversion header to get the SSE2 code translated into its
  194. # NEON counterpart (thanks to sse2neon.h).
  195. add_definitions(-DSSE2NEON=1)
  196. # If not already manualy specified, add the necessary -march tunes for
  197. # sse2neon.h to take full effect. See the "Usage" section at:
  198. # https://github.com/DLTcollab/sse2neon/blob/master/README.md
  199. if (NOT "${CMAKE_C_FLAGS}" MATCHES ".*-march.*")
  200. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+fp+simd")
  201. endif ()
  202. if (NOT "${CMAKE_CXX_FLAGS}" MATCHES ".*-march=.*")
  203. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+fp+simd")
  204. endif ()
  205. else (ARCH STREQUAL "arm64")
  206. # We only support 64 bits builds for x86 targets
  207. set(ARCH_FLAG "-m64")
  208. # SSE2 is always enabled by default for 64 bits targets
  209. set(COMPILER_MATHS "-mfpmath=sse")
  210. if (USEAVX2)
  211. set(COMPILER_MATHS "-mavx2 -mfma -mfpmath=sse")
  212. elseif (USEAVX)
  213. set(COMPILER_MATHS "-mavx -mfpmath=sse")
  214. endif ()
  215. endif (ARCH STREQUAL "arm64")
  216. # Compilation flags common to all build types and compilers.
  217. # NOTE: do not use -ffast-math: it makes us loose the Moon (!) and probably
  218. # causes other issues as well...
  219. # Please, note that -fno-strict-aliasing is *required*, because the code is
  220. # not strict aliasing safe and omitting this option would cause warnings and
  221. # possibly bogus optimizations by gcc.
  222. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pipe ${DEBUG_OPTIONS} -fexceptions -fno-strict-aliasing -fvisibility=hidden -fsigned-char ${ARCH_FLAG} ${COMPILER_MATHS} -fno-math-errno -fno-trapping-math -pthread")
  223. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe ${DEBUG_OPTIONS} -fexceptions -fno-strict-aliasing -fvisibility=hidden -fsigned-char ${ARCH_FLAG} ${COMPILER_MATHS} -fno-math-errno -fno-trapping-math -pthread")
  224. # OpenMP support, if requested
  225. if (OPENMP)
  226. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
  227. endif (OPENMP)
  228. # OpenMP support, if requested
  229. if (OPENMP)
  230. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
  231. endif (OPENMP)
  232. # Use OPT_FLAGS to set optimizations (used for the release builds) that would
  233. # break debugging
  234. if (${CLANG_VERSION} GREATER 0)
  235. set(OPT_FLAGS "-O3")
  236. if (${CLANG_VERSION} GREATER 799)
  237. set(OPT_FLAGS "${OPT_FLAGS} -fno-delete-null-pointer-checks -mno-retpoline -mno-retpoline-external-thunk")
  238. endif ()
  239. if (${CLANG_VERSION} GREATER 1099)
  240. set(OPT_FLAGS "${OPT_FLAGS} -mno-lvi-cfi -mno-lvi-hardening")
  241. endif ()
  242. else ()
  243. # Do not use our aggressive registers tuning options if the user
  244. # asked for -march=native optimization (i.e. let gcc set the adequate
  245. # optimizations in that case).
  246. if (TUNED_BUILD)
  247. set(OPT_FLAGS "-O3 -fno-delete-null-pointer-checks -fno-ipa-cp-clone -fno-align-labels -fno-align-loops")
  248. else (TUNED_BUILD)
  249. set(OPT_FLAGS "-O3 -fno-delete-null-pointer-checks -fno-ipa-cp-clone -fno-align-labels -fno-align-loops -fsched-pressure -frename-registers -fweb -fira-hoist-pressure")
  250. endif (TUNED_BUILD)
  251. endif ()
  252. # Using ld.bfd with clang when LTO is enabled now (i.e. with llgltf added)
  253. # triggers the following assert in the gold plugin of llvm/clang (seen at
  254. # least with llvm/clang v18.1 and ld.bfd v2.40):
  255. # ld: llvm-18.1.6/llvm/tools/gold/gold-plugin.cpp:1048:
  256. # std::vector<std::pair<llvm::SmallString<128>, bool> > runLTO():
  257. # Assertion `ObjFilename.second' failed.
  258. # This fault does not happen with gcc/LTO and ld.bfd, neither with
  259. # clang/LTO and ld.lld, ld.gold or ld.mold... So try and use one of the
  260. # latter linkers when using clang/LTO, and warn if none of them is present
  261. # on the build system.
  262. if (USELTO AND ${CLANG_VERSION} GREATER 0)
  263. # Check to see what linker type is in use...
  264. execute_process(
  265. COMMAND sh -c "ld --version"
  266. OUTPUT_VARIABLE LD_VER
  267. )
  268. # If the system default linker is indeed ld.bfd, then try and change
  269. # for another, clang-LTO-compatible linker...
  270. if (LD_VER MATCHES "^GNU ld.*")
  271. # Give the priority to lld, since it should provide full clang
  272. # compatibility...
  273. find_program(LLD ld.lld)
  274. if (LLD)
  275. set(SELECTED_LINKER "lld")
  276. else (LLD)
  277. # Then try ld.gold, which is most often present on Linux
  278. # systems and a well known, LTO-capable linker.
  279. find_program(GOLD ld.gold)
  280. if (GOLD)
  281. set(SELECTED_LINKER "gold")
  282. else (GOLD)
  283. # Finally, try ld.mold, which is much younger, but should
  284. # work just fine too (tested successfully with mold 2.32).
  285. find_program(MOLD ld.mold)
  286. if (MOLD)
  287. set(SELECTED_LINKER "mold")
  288. else (MOLD)
  289. message(WARNING "Could not find either of ld.lld, ld.gold or ld.mold linkers: linking may fail (ld.bfd or clang bug) when using ld.bfd together with clang and LTO enabled...")
  290. endif (MOLD)
  291. endif (GOLD)
  292. endif (LLD)
  293. endif ()
  294. if (SELECTED_LINKER)
  295. # clang is *stupidly* warning about "unused command line argument"
  296. # when not in the linking phase and the "-fuse-ld=" option was
  297. # passed... So we need to add -Wno-unused-command-line-argument. HB
  298. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-command-line-argument -fuse-ld=${SELECTED_LINKER}")
  299. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-command-line-argument -fuse-ld=${SELECTED_LINKER}")
  300. endif (SELECTED_LINKER)
  301. endif (USELTO AND ${CLANG_VERSION} GREATER 0)
  302. add_definitions(-DLL_LINUX=1)
  303. set(CMAKE_C_FLAGS_DEBUG "-fno-inline -D_DEBUG -DLL_DEBUG=1")
  304. set(CMAKE_CXX_FLAGS_DEBUG "-fno-inline -D_DEBUG -DLL_DEBUG=1")
  305. set(CMAKE_C_FLAGS_RELWITHDEBINFO "-fno-inline -DLL_NO_FORCE_INLINE=1 -DNDEBUG")
  306. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-fno-inline -DLL_NO_FORCE_INLINE=1 -DNDEBUG")
  307. set(CMAKE_C_FLAGS_RELEASE "${OPT_FLAGS} -DNDEBUG")
  308. set(CMAKE_CXX_FLAGS_RELEASE "${OPT_FLAGS} -DNDEBUG")
  309. endif (LINUX)
  310. if (LINUX OR DARWIN)
  311. set(IGNORED_C_WARNINGS "-Wall ${IGNORED_C_WARNINGS}")
  312. set(IGNORED_CXX_WARNINGS "-Wall -Wno-reorder ${IGNORED_CXX_WARNINGS}")
  313. if (NOT NO_FATAL_WARNINGS)
  314. set(IGNORED_C_WARNINGS "${IGNORED_C_WARNINGS} -Werror")
  315. set(IGNORED_CXX_WARNINGS "${IGNORED_CXX_WARNINGS} -Werror")
  316. endif (NOT NO_FATAL_WARNINGS)
  317. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${IGNORED_C_WARNINGS}")
  318. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${IGNORED_CXX_WARNINGS}")
  319. endif (LINUX OR DARWIN)
  320. if (WINDOWS)
  321. set(CMAKE_CONFIGURATION_TYPES "Release" CACHE STRING
  322. "Supported build types." FORCE)
  323. # Remove default /Zm1000 flag that cmake inserts
  324. string (REPLACE "/Zm1000" " " CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  325. # Do not build DLLs.
  326. set(BUILD_SHARED_LIBS OFF)
  327. # SSE2 is always enabled by default for 64 bits targets
  328. set(COMPILER_MATHS "/fp:fast")
  329. if (USEAVX2)
  330. set(COMPILER_MATHS "/arch:AVX2 /fp:fast")
  331. elseif (USEAVX)
  332. set(COMPILER_MATHS "/arch:AVX /fp:fast")
  333. endif ()
  334. # Compilation flags common to all build types
  335. if (USING_CLANG)
  336. set(IGNORED_WARNINGS "-Wno-deprecated-declarations -Wno-reorder-ctor -Wno-inconsistent-dllimport -Wno-dll-attribute-on-redeclaration -Wno-ignored-pragma-optimize -Wno-writable-strings -Wno-unused-function -Wno-unused-local-typedef -Wno-unknown-warning-option")
  337. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /EHs ${COMPILER_MATHS} -m64 /MP /W2 /c")
  338. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17 /EHs ${COMPILER_MATHS} -m64 -fno-strict-aliasing -fno-delete-null-pointer-checks /MP /TP /W2 /c ${IGNORED_WARNINGS}")
  339. else (USING_CLANG)
  340. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /EHs ${COMPILER_MATHS} /MP /W2 /c /nologo")
  341. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17 /EHs ${COMPILER_MATHS} /MP /TP /W2 /c /nologo")
  342. endif (USING_CLANG)
  343. # Not strictly stack protection, but buffer overrun detection/protection
  344. if (PROTECTSTACK)
  345. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GS")
  346. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /GS")
  347. else (PROTECTSTACK)
  348. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GS-")
  349. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /GS-")
  350. endif (PROTECTSTACK)
  351. # Never add thread-locking code to local static variables (we never use
  352. # non-thread_local static local variables in thread-sensitive code):
  353. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:threadSafeInit-")
  354. # OpenMP support, if requested
  355. if (OPENMP)
  356. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /openmp")
  357. endif (OPENMP)
  358. # Build type and compiler dependent compilation flags
  359. if (USING_CLANG)
  360. set(CMAKE_CXX_FLAGS_RELEASE "/clang:-O3 /DNDEBUG /D_SECURE_SCL=0 /D_HAS_ITERATOR_DEBUGGING=0"
  361. CACHE STRING "C++ compiler release options" FORCE)
  362. else (USING_CLANG)
  363. set(CMAKE_CXX_FLAGS_RELEASE "/O2 /Oi /DNDEBUG /D_SECURE_SCL=0 /D_HAS_ITERATOR_DEBUGGING=0"
  364. CACHE STRING "C++ compiler release options" FORCE)
  365. endif (USING_CLANG)
  366. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
  367. # zlib has assembly-language object files incompatible with SAFESEH
  368. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
  369. set(CMAKE_CXX_STANDARD_LIBRARIES "")
  370. set(CMAKE_C_STANDARD_LIBRARIES "")
  371. add_definitions(/DLL_WINDOWS=1 /DUNICODE /D_UNICODE)
  372. # See: http://msdn.microsoft.com/en-us/library/aa383745%28v=VS.85%29.aspx
  373. # Configure the win32 API for Windows 7 compatibility
  374. set(WINVER "0x0601" CACHE STRING "Win32 API target version")
  375. add_definitions("/DWINVER=${WINVER}" "/D_WIN32_WINNT=${WINVER}")
  376. if (NOT NO_FATAL_WARNINGS)
  377. add_definitions(/WX)
  378. endif ()
  379. endif (WINDOWS)
  380. # Add link-time optimization if requested, enabling it via cmake (which knows
  381. # what compilers support it and what option(s) to use). Note that only cmake
  382. # v3.9 and newer can deal with LTO for gcc and clang, and cmake v3.13 or newer
  383. # for MSVC.
  384. # From my experiments, LTO does not make the resulting binary any faster with
  385. # gcc (and actually slower with gcc versions below 10)... I suspect that gcc's
  386. # LTO causes some normally (and flagged to be) inlined functions to get wrongly
  387. # factorized into normal functions... Clang 12 and newer, however, does take
  388. # benefit from LTO to produce faster viewer binaries.
  389. if (USELTO AND ${CMAKE_VERSION} VERSION_GREATER 3.8)
  390. if (NOT WINDOWS OR ${CMAKE_VERSION} VERSION_GREATER 3.12)
  391. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
  392. endif ()
  393. endif ()
  394. if (NOT USESYSTEMLIBS)
  395. if (LINUX)
  396. set(${ARCH}_linux_INCLUDES
  397. glib-2.0
  398. freetype
  399. )
  400. endif (LINUX)
  401. endif (NOT USESYSTEMLIBS)
  402. # Add defines corresponding to compilation options
  403. if (OPENMP)
  404. add_definitions(-DLL_OPENMP=1)
  405. endif (OPENMP)
  406. if (USE_JEMALLOC)
  407. add_definitions(-DLL_JEMALLOC=1)
  408. elseif (USE_MIMALLOC)
  409. add_definitions(-DLL_MIMALLOC=1)
  410. endif ()
  411. if (NO_PHMAP)
  412. add_definitions(-DLL_NO_PHMAP=1)
  413. endif (NO_PHMAP)
  414. if (ENABLE_PUPPETRY)
  415. add_definitions(-DLL_PUPPETRY=1)
  416. endif (ENABLE_PUPPETRY)
  417. if (USE_TRACY)
  418. if (TRACY_WITH_FAST_TIMERS)
  419. if (TRACY_MEMORY)
  420. add_definitions(-DTRACY_ENABLE=4)
  421. else (TRACY_MEMORY)
  422. add_definitions(-DTRACY_ENABLE=3)
  423. endif (TRACY_MEMORY)
  424. else (TRACY_WITH_FAST_TIMERS)
  425. if (TRACY_MEMORY)
  426. add_definitions(-DTRACY_ENABLE=2)
  427. else (TRACY_MEMORY)
  428. add_definitions(-DTRACY_ENABLE=1)
  429. endif (TRACY_MEMORY)
  430. endif (TRACY_WITH_FAST_TIMERS)
  431. endif (USE_TRACY)
  432. if (WINDOWS AND USE_NETBIOS)
  433. add_definitions(-DLL_NETBIOS=1)
  434. endif (WINDOWS AND USE_NETBIOS)