CMakeLists.txt 12 KB

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