CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
  2. include(ProcessorCount)
  3. PROJECT(AnKi)
  4. ################################################################################
  5. # Funcs #
  6. ################################################################################
  7. function(anki_install_executable EXE)
  8. if(NOT ANDROID)
  9. add_custom_command(TARGET ${EXE} POST_BUILD
  10. COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/Bin
  11. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${EXE}> ${CMAKE_BINARY_DIR}/Bin)
  12. endif()
  13. endfunction()
  14. macro(anki_add_source_files)
  15. foreach(f ${ARGV})
  16. set(AK_SOURCES "${AK_SOURCES} ${f}")
  17. endforeach()
  18. set(AK_SOURCES ${AK_SOURCES} PARENT_SCOPE)
  19. endmacro()
  20. macro(anki_new_executable)
  21. if(NOT ANDROID)
  22. add_executable(${ARGV})
  23. anki_install_executable(${ARGV0})
  24. else()
  25. set(_SKIP TRUE)
  26. foreach(ARG ${ARGV})
  27. if(_SKIP)
  28. set(_SKIP FALSE)
  29. else()
  30. list(APPEND _TMP_LIST ${ARG})
  31. endif()
  32. endforeach()
  33. add_library(${ARGV0} SHARED ${_TMP_LIST})
  34. endif()
  35. endmacro()
  36. ################################################################################
  37. # Determin the system to build for. Do that first #
  38. ################################################################################
  39. if(WIN32)
  40. if(NOT WINDOWS)
  41. set(WINDOWS TRUE)
  42. message("++ Building for windows")
  43. endif()
  44. elseif(UNIX AND NOT APPLE)
  45. if(CMAKE_SYSTEM_NAME MATCHES ".*Linux")
  46. set(LINUX TRUE)
  47. message("++ Building for Linux")
  48. elseif(ANDROID)
  49. message("++ Building for Android")
  50. else()
  51. message(FATAL_ERROR "Unknown unix")
  52. endif()
  53. elseif(APPLE)
  54. if(CMAKE_SYSTEM_NAME MATCHES ".*MacOS.*")
  55. set(MACOS TRUE)
  56. message("++ Building for MacOS")
  57. else()
  58. message(FATAL_ERROR "Unknown apple")
  59. endif()
  60. else()
  61. message(FATAL_ERROR "Unknown system")
  62. endif()
  63. if(${CMAKE_C_COMPILER_ID} MATCHES "GNU" OR ${CMAKE_C_COMPILER_ID} MATCHES "Clang")
  64. set(GCC TRUE)
  65. else()
  66. set(GCC FALSE)
  67. endif()
  68. if(${CMAKE_C_COMPILER_ID} MATCHES "Clang")
  69. set(CLANG TRUE)
  70. else()
  71. set(CLANG FALSE)
  72. endif()
  73. ################################################################################
  74. # Configuration #
  75. ################################################################################
  76. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  77. option(ANKI_EXTRA_CHECKS "Debugging checks (assertions)" OFF)
  78. option(ANKI_LTO "LTO compilation" OFF)
  79. option(ANKI_BUILD_TOOLS "Build tools" ON)
  80. option(ANKI_BUILD_TESTS "Build unit tests" OFF)
  81. option(ANKI_BUILD_SANDBOX "Build sandbox application" ON)
  82. option(ANKI_BUILD_SAMPLES "Build sample applications" ON)
  83. option(ANKI_STRIP "Strip the symbols from the executables" OFF)
  84. option(ANKI_TRACE "Enable performance tracing. Small overhead" OFF)
  85. if(ANKI_TRACE)
  86. set(_ANKI_ENABLE_TRACE 1)
  87. else()
  88. set(_ANKI_ENABLE_TRACE 0)
  89. endif()
  90. set(ANKI_CPU_ADDR_SPACE "0" CACHE STRING "The CPU architecture (0 or 32 or 64). Zero means native.")
  91. option(ANKI_SIMD "Enable SIMD optimizations" ON)
  92. option(ANKI_ADDRESS_SANITIZER "Enable address sanitizer (-fsanitize=address)" OFF)
  93. # Take a wild guess on the windowing system
  94. if(LINUX)
  95. set(_WIN_BACKEND "SDL")
  96. set(SDL TRUE)
  97. elseif(WINDOWS)
  98. set(_WIN_BACKEND "SDL")
  99. set(SDL TRUE)
  100. elseif(ANDROID)
  101. set(_WIN_BACKEND "ANDROID")
  102. elseif(MACOS)
  103. set(_WIN_BACKEND "SDL")
  104. set(SDL TRUE)
  105. else()
  106. message(FATAL_ERROR "Couldn't determine the window backend. You need to specify it manually.")
  107. endif()
  108. set(ANKI_GR_BACKEND "VULKAN" CACHE STRING "The graphics API to use (VULKAN or GL)")
  109. if(${ANKI_GR_BACKEND} STREQUAL "GL")
  110. set(GL TRUE)
  111. set(VULKAN FALSE)
  112. set(VIDEO_VULKAN TRUE) # Set for the SDL2 to pick up
  113. else()
  114. set(GL FALSE)
  115. set(VULKAN TRUE)
  116. endif()
  117. if(NOT DEFINED CMAKE_BUILD_TYPE)
  118. message(FATAL_ERROR "You need to define CMAKE_BUILD_TYPE")
  119. endif()
  120. if(${ANKI_CPU_ADDR_SPACE} STREQUAL "0" OR ${ANKI_CPU_ADDR_SPACE} STREQUAL "64")
  121. set(ARCH_64 TRUE)
  122. set(ARCH_32 FALSE)
  123. else()
  124. set(ARCH_64 FALSE)
  125. set(ARCH_32 TRUE)
  126. endif()
  127. ################################################################################
  128. # Compiler & linker flags #
  129. ################################################################################
  130. if(MINGW)
  131. set(COMPILER_FLAGS "${COMPILER_FLAGS} -mconsole ")
  132. endif()
  133. add_definitions(-DSPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS)
  134. add_definitions(-DANKI_BUILD)
  135. # ImGUI
  136. add_definitions(-DIMGUI_USER_CONFIG=<AnKi/Ui/ImGuiConfig.h>)
  137. if(NOT MSVC)
  138. # When building AnKi define this special flag
  139. add_definitions("-fPIC")
  140. add_definitions("-fno-exceptions")
  141. if(GCC AND NOT CLANG)
  142. add_definitions("-static-libstdc++")
  143. endif()
  144. if(LINUX OR MACOS OR WINDOWS)
  145. add_definitions("-msse4")
  146. endif()
  147. if(ANKI_LTO)
  148. add_definitions("-flto ")
  149. set(LINKER_FLAGS "${LINKER_FLAGS} -flto ")
  150. endif()
  151. if(ANKI_STRIP)
  152. set(LINKER_FLAGS "${LINKER_FLAGS} -s ")
  153. add_definitions("-s")
  154. endif()
  155. if(ANKI_ADDRESS_SANITIZER)
  156. add_definitions("-fsanitize=address ")
  157. endif()
  158. if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
  159. add_definitions("-O3 -DNDEBUG")
  160. elseif(${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
  161. add_definitions("-O3 -g3 -fno-omit-frame-pointer")
  162. set(LINKER_FLAGS "${LINKER_FLAGS} -rdynamic ") # For backtrace()
  163. elseif(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
  164. add_definitions("-O0 -g3")
  165. set(LINKER_FLAGS "${LINKER_FLAGS} -rdynamic ") # For backtrace()
  166. else()
  167. message(FATAL_ERROR "Wrong CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
  168. endif()
  169. # Set the flags to cmake now
  170. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
  171. else()
  172. #ProcessorCount(PC)
  173. #add_definitions("/MP${PC}")
  174. if(${CMAKE_BUILD_TYPE} STREQUAL "Release" OR ${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
  175. #add_definitions("/Ox")
  176. endif()
  177. # Full paths in compiler diagnostics else you can't click on visual studio have it open the file+line
  178. add_compile_options("/FC")
  179. endif()
  180. # Use LLD or gold linker
  181. if(UNIX AND NOT APPLE)
  182. execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=lld -Wl,--version ERROR_QUIET OUTPUT_VARIABLE ld_version)
  183. if("${ld_version}" MATCHES "compatible with GNU linkers" AND "${ld_version}" MATCHES "LLD")
  184. message("++ Will use LLD linker")
  185. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Wl,--disable-new-dtags")
  186. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Wl,--disable-new-dtags")
  187. else()
  188. execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE ld_version)
  189. if("${ld_version}" MATCHES "GNU gold")
  190. message("++ Will use gold linker")
  191. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
  192. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
  193. endif()
  194. endif()
  195. endif()
  196. ################################################################################
  197. # Thirdparty #
  198. ################################################################################
  199. set(ANKI_EXTERN_SUB_DIRS TinyXml2 Lua ZLib Bullet ImGui MeshOptimizer SprivCross)
  200. # Bullet config
  201. option(BUILD_BULLET2_DEMOS OFF)
  202. option(BUILD_BULLET3 OFF)
  203. option(BUILD_CPU_DEMOS OFF)
  204. option(BUILD_OPENGL3_DEMOS OFF)
  205. option(BUILD_EXTRAS OFF)
  206. if((LINUX OR MACOS OR WINDOWS) AND GL)
  207. set(ANKI_EXTERN_SUB_DIRS ${ANKI_EXTERN_SUB_DIRS} GLEW)
  208. endif()
  209. # SDL
  210. if(SDL)
  211. message("++ Configuring SDL2")
  212. add_subdirectory(ThirdParty/Sdl2)
  213. message("++ End configuring SDL2")
  214. # Include first the build directory.
  215. set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}/ThirdParty/Sdl2/include"
  216. "${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Sdl2/include")
  217. endif()
  218. # glslang
  219. message("++ Configuring glslang")
  220. add_subdirectory(ThirdParty/Glslang)
  221. message("++ End configuring glslang")
  222. foreach(TMP ${ANKI_EXTERN_SUB_DIRS})
  223. add_subdirectory(ThirdParty/${TMP})
  224. endforeach()
  225. if(ANDROID)
  226. add_library(AnKiAndroidNativeGlue ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
  227. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
  228. endif()
  229. ################################################################################
  230. # AnKi #
  231. ################################################################################
  232. # Revision
  233. find_package(Git)
  234. if(GIT_FOUND)
  235. execute_process(COMMAND
  236. "${GIT_EXECUTABLE}" log -1 --date=short --format=%h
  237. WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  238. OUTPUT_VARIABLE GIT_COMMIT
  239. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  240. set(ANKI_REVISION "\"${GIT_COMMIT}\"")
  241. else()
  242. set(ANKI_REVISION "\"unknown\"")
  243. endif()
  244. # Doxygen
  245. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Docs/Doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  246. find_package(Doxygen)
  247. if(DOXYGEN_FOUND)
  248. message("++ Doxygen found")
  249. add_custom_target(doc ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  250. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  251. COMMENT "Generating API documentation with Doxygen" VERBATIM)
  252. endif()
  253. # Config.h
  254. set(ANKI_VERSION_MAJOR 0)
  255. set(ANKI_VERSION_MINOR 1)
  256. message("++ AnKi version: ${ANKI_VERSION_MAJOR}.${ANKI_VERSION_MINOR}")
  257. if(ANKI_EXTRA_CHECKS)
  258. set(_ANKI_EXTRA_CHECKS 1)
  259. else()
  260. set(_ANKI_EXTRA_CHECKS 0)
  261. endif()
  262. if(ANKI_SIMD)
  263. set(_ANKI_ENABLE_SIMD 1)
  264. else()
  265. set(_ANKI_ENABLE_SIMD 0)
  266. endif()
  267. if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
  268. set(ANKI_DEBUG_SYMBOLS 1)
  269. set(ANKI_OPTIMIZE 0)
  270. elseif(${CMAKE_BUILD_TYPE} STREQUAL "Release")
  271. set(ANKI_DEBUG_SYMBOLS 0)
  272. set(ANKI_OPTIMIZE 1)
  273. elseif(${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
  274. set(ANKI_DEBUG_SYMBOLS 1)
  275. set(ANKI_OPTIMIZE 1)
  276. else()
  277. message(FATAL_ERROR "Wrong CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
  278. endif()
  279. if(ANKI_BUILD_TESTS)
  280. set(ANKI_TESTS 1)
  281. else()
  282. set(ANKI_TESTS 0)
  283. endif()
  284. configure_file("AnKi/Config.h.cmake" "${CMAKE_CURRENT_BINARY_DIR}/AnKi/Config.h")
  285. # Include & lib directories
  286. include_directories(
  287. "ThirdParty/Khronos"
  288. "${CMAKE_CURRENT_BINARY_DIR}"
  289. "${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Bullet/src"
  290. "ThirdParty"
  291. "ThirdParty/ZLib"
  292. ${CMAKE_CURRENT_SOURCE_DIR})
  293. if(SDL2_INCLUDE_DIRS)
  294. include_directories("${SDL2_INCLUDE_DIRS}")
  295. endif()
  296. if(LINUX OR MACOS OR WINDOWS)
  297. include_directories("ThirdParty/GLEW/include")
  298. else()
  299. #include_directories("ThirdParty/GLES3/include")
  300. endif()
  301. if(ANDROID)
  302. include_directories("${ANDROID_NDK}/sources/android/native_app_glue")
  303. endif()
  304. # AnKi compiler flags (Mainly warnings)
  305. if(NOT MSVC)
  306. add_definitions("-pedantic -Wno-unknown-warning-option -Wall -W -Wextra -Wstrict-aliasing -Wwrite-strings -Wunused "
  307. "-Wno-unused-parameter -Wundef -Wno-ignored-attributes -Wno-implicit-fallthrough -Wunused-result "
  308. "-Wconversion -Wno-sign-conversion -Wno-keyword-macro -Wno-string-conversion -Wno-class-memaccess "
  309. "-Wunused-variable")
  310. set(CMAKE_CXX_STANDARD 14)
  311. else()
  312. #add_definitions("/wd4996 /wd4244 /wd4262 /wd4267 /wd26495 /wd26439")
  313. add_compile_definitions("_CRT_SECURE_NO_WARNINGS=1") # Disable some string function warnings
  314. endif()
  315. # Set platform specific
  316. if(LINUX)
  317. if(GL)
  318. set(THIRD_PARTY_LIBS ${ANKI_GR_BACKEND} AnKiGlew)
  319. else()
  320. if(SDL)
  321. set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} X11-xcb)
  322. else()
  323. message(FATAL_ERROR "Unhandled case")
  324. endif()
  325. endif()
  326. set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} pthread dl)
  327. elseif(MACOS)
  328. find_package(OpenGL REQUIRED)
  329. set(THIRD_PARTY_LIBS ${OPENGL_LIBRARIES} AnKiGlew pthread)
  330. elseif(ANDROID)
  331. set(THIRD_PARTY_LIBS log android)
  332. #include_directories("${ANDROID_NDK}/sources/android/native_app_glue")
  333. #set(_SYS_SRC "${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c")
  334. set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} AnKiAndroidNativeGlue)
  335. elseif(WINDOWS)
  336. if(GL)
  337. set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} AnKiGlew opengl32)
  338. else()
  339. set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS})
  340. endif()
  341. set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} version Imm32 Winmm DbgHelp)
  342. else()
  343. message(FATAL_ERROR "Unhandled case")
  344. endif()
  345. if(SDL)
  346. set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} SDL2-static)
  347. endif()
  348. set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} BulletSoftBody BulletDynamics BulletCollision LinearMath
  349. AnKiSpirvCross AnKiTinyXml2 AnKiLua AnKiMeshOptimizer AnKiZLib glslang SPIRV OGLCompiler OSDependent AnKiImGui)
  350. # Add AnKi sub libraries
  351. add_subdirectory(AnKi)
  352. separate_arguments(AK_SOURCES)
  353. add_library(AnKi ${AK_SOURCES})
  354. target_compile_definitions(AnKi PRIVATE -DANKI_SOURCE_FILE)
  355. target_link_libraries(AnKi ${THIRD_PARTY_LIBS})
  356. ################################################################################
  357. # AnKi extra #
  358. ################################################################################
  359. if(ANKI_BUILD_TESTS)
  360. add_subdirectory(Tests)
  361. endif()
  362. if(ANKI_BUILD_TOOLS)
  363. add_subdirectory(Tools)
  364. endif()
  365. if(ANKI_BUILD_SANDBOX)
  366. add_subdirectory(Sandbox)
  367. endif()
  368. if(ANKI_BUILD_SAMPLES)
  369. add_subdirectory(Samples)
  370. endif()