CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
  2. PROJECT(anki)
  3. ################################################################################
  4. # Determin the system to build for. Do that first #
  5. ################################################################################
  6. if(WIN32)
  7. if(NOT WINDOWS)
  8. set(WINDOWS TRUE)
  9. message("++ Building for windows")
  10. endif()
  11. elseif(UNIX AND NOT APPLE)
  12. if(CMAKE_SYSTEM_NAME MATCHES ".*Linux")
  13. if(NOT ANDROID)
  14. set(LINUX TRUE)
  15. message("++ Building for Linux")
  16. else()
  17. message("++ Building for Android")
  18. endif()
  19. else()
  20. message(FATAL_ERROR "Unknown unix")
  21. endif()
  22. elseif(APPLE)
  23. if(CMAKE_SYSTEM_NAME MATCHES ".*MacOS.*")
  24. set(MACOS TRUE)
  25. message("++ Building for MacOS")
  26. else()
  27. message(FATAL_ERROR "Unknown apple")
  28. endif()
  29. else()
  30. message(FATAL_ERROR "Unknown system")
  31. endif()
  32. if(${CMAKE_C_COMPILER_ID} MATCHES "GNU" OR ${CMAKE_C_COMPILER_ID} MATCHES "Clang")
  33. set(GCC TRUE)
  34. else()
  35. set(GCC FALSE)
  36. endif()
  37. if(${CMAKE_C_COMPILER_ID} MATCHES "Clang")
  38. set(CLANG TRUE)
  39. else()
  40. set(CLANG FALSE)
  41. endif()
  42. ################################################################################
  43. # Configuration #
  44. ################################################################################
  45. option(ANKI_EXTRA_CHECKS "Debugging checks (assertions)" OFF)
  46. option(ANKI_LTO "LTO compilation" OFF)
  47. option(ANKI_BUILD_TOOLS "Build tools" OFF)
  48. option(ANKI_BUILD_TESTS "Build unit tests" OFF)
  49. option(ANKI_BUILD_SANDBOX "Build sandbox application" ON)
  50. option(ANKI_BUILD_BENCH "Build benchmark application" OFF)
  51. option(ANKI_BUILD_SAMPLES "Build sample applications" ON)
  52. option(ANKI_STRIP "Srip the symbols from the executables" OFF)
  53. option(ANKI_ENABLE_TRACE "Enable performance tracing. Small overhead" OFF)
  54. if(ANKI_ENABLE_TRACE)
  55. set(_ANKI_ENABLE_TRACE 1)
  56. else()
  57. set(_ANKI_ENABLE_TRACE 0)
  58. endif()
  59. set(ANKI_CPU_ADDR_SPACE "0" CACHE STRING "The CPU architecture (0 or 32 or 64). If zero go native")
  60. option(ANKI_ENABLE_SIMD "Enable or not SIMD optimizations" ON)
  61. option(ANKI_ADDRESS_SANITIZER "Enable address sanitizer (-fsanitize=address)" OFF)
  62. # Take a wild guess on the windowing system
  63. if(LINUX)
  64. set(_WIN_BACKEND "SDL")
  65. set(SDL TRUE)
  66. elseif(WINDOWS)
  67. set(_WIN_BACKEND "SDL")
  68. set(SDL TRUE)
  69. elseif(ANDROID)
  70. set(_WIN_BACKEND "ANDROID")
  71. elseif(MACOS)
  72. set(_WIN_BACKEND "SDL")
  73. set(SDL TRUE)
  74. else()
  75. message(FATAL_ERROR "Couldn't determine the window backend. You need to specify it manually")
  76. endif()
  77. set(ANKI_GR_BACKEND "GL" CACHE STRING "The graphics API (GL or VULKAN)")
  78. if(${ANKI_GR_BACKEND} STREQUAL "GL")
  79. set(GL TRUE)
  80. set(VULKAN FALSE)
  81. else()
  82. set(GL FALSE)
  83. set(VULKAN TRUE)
  84. endif()
  85. if(NOT DEFINED CMAKE_BUILD_TYPE)
  86. message(FATAL_ERROR "You need to define CMAKE_BUILD_TYPE")
  87. endif()
  88. if(${ANKI_CPU_ADDR_SPACE} STREQUAL "0" OR ${ANKI_CPU_ADDR_SPACE} STREQUAL "64")
  89. set(ARCH_64 TRUE)
  90. set(ARCH_32 FALSE)
  91. else()
  92. set(ARCH_64 FALSE)
  93. set(ARCH_32 TRUE)
  94. endif()
  95. ################################################################################
  96. # Compiler & linker flags #
  97. ################################################################################
  98. set(CXX_FLAGS "")
  99. set(COMPILER_FLAGS "")
  100. set(LINKER_FLAGS "")
  101. add_definitions(-D_NEWTON_STATIC_LIB -D_CUSTOM_JOINTS_STATIC_LIB)
  102. if(ARCH_64)
  103. add_definitions(-D_POSIX_VER_64)
  104. else()
  105. add_definitions(-D_POSIX_VER_32)
  106. endif()
  107. # Newton wants that
  108. if(MINGW AND ARCH_64)
  109. add_definitions(-D_MINGW_64_VER)
  110. elseif(MINGW AND ARCH_32)
  111. add_definitions(-D_MINGW_32_VER)
  112. endif()
  113. add_definitions(-DGLEW_NO_GLU)
  114. add_definitions(-DSPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS)
  115. add_definitions(-DANKI_BUILD)
  116. # When building AnKi define this special flag
  117. set(COMPILER_FLAGS "${COMPILER_FLAGS} -fPIC ")
  118. if(NOT ANKI_CPU_ADDR_SPACE STREQUAL "0")
  119. set(LINKER_FLAGS "${LINKER_FLAGS} -m${ANKI_CPU_ADDR_SPACE} ")
  120. set(COMPILER_FLAGS "${COMPILER_FLAGS} -m${ANKI_CPU_ADDR_SPACE} ")
  121. endif()
  122. set(COMPILER_FLAGS "${COMPILER_FLAGS} -fno-exceptions ")
  123. if(GCC AND NOT CLANG)
  124. set(CXX_FLAGS "${CXX_FLAGS} -static-libstdc++ ")
  125. endif()
  126. if(ANKI_ENABLE_SIMD)
  127. if(LINUX OR MACOS OR WINDOWS)
  128. set(COMPILER_FLAGS "${COMPILER_FLAGS} -msse4 ")
  129. else()
  130. set(COMPILER_FLAGS "${COMPILER_FLAGS} -mfpu=neon ")
  131. endif()
  132. endif()
  133. if(ANKI_LTO)
  134. set(COMPILER_FLAGS "${COMPILER_FLAGS} -flto ")
  135. set(LINKER_FLAGS "${LINKER_FLAGS} -flto ")
  136. endif()
  137. if(ANKI_STRIP)
  138. set(LINKER_FLAGS "${LINKER_FLAGS} -s ")
  139. set(COMPILER_FLAGS "${COMPILER_FLAGS} -s ")
  140. endif()
  141. if(ANKI_ADDRESS_SANITIZER)
  142. set(COMPILER_FLAGS "${COMPILER_FLAGS} -fsanitize=address ")
  143. endif()
  144. if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
  145. set(COMPILER_FLAGS "${COMPILER_FLAGS} -O3 -DNDEBUG ")
  146. set(CMAKE_CXX_FLAGS_RELEASE "")
  147. set(CMAKE_C_FLAGS_RELEASE "")
  148. elseif(${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
  149. set(COMPILER_FLAGS "${COMPILER_FLAGS} -O3 -g3 ")
  150. set(CMAKE_CXX_FLAGS_RELWITHDBGINFO "")
  151. set(CMAKE_C_FLAGS_RELWITHDBGINFO "")
  152. elseif(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
  153. set(COMPILER_FLAGS "${COMPILER_FLAGS} -O0 -g3 ")
  154. set(CMAKE_CXX_FLAGS_DEBUG "")
  155. set(CMAKE_C_FLAGS_DEBUG "")
  156. else()
  157. message(FATAL_ERROR "Wrong CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
  158. endif()
  159. # Set the flags to cmake now
  160. set(CMAKE_CXX_FLAGS "${CXX_FLAGS} ${COMPILER_FLAGS}")
  161. set(CMAKE_C_FLAGS "${COMPILER_FLAGS}")
  162. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
  163. ################################################################################
  164. # Install #
  165. ################################################################################
  166. set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/src/" CACHE PATH "The subdirectory to the header prefix")
  167. set(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Library install path")
  168. message("++ Include install dir: ${INCLUDE_INSTALL_DIR}")
  169. message("++ Lib install dir: ${LIB_INSTALL_DIR}")
  170. ################################################################################
  171. # Thirdparty #
  172. ################################################################################
  173. set(ANKI_EXTERN_SUB_DIRS tinyxml2 lua z newton)
  174. if((LINUX OR MACOS OR WINDOWS) AND GL)
  175. set(ANKI_EXTERN_SUB_DIRS ${ANKI_EXTERN_SUB_DIRS} GLEW)
  176. endif()
  177. if(VULKAN)
  178. set(ANKI_EXTERN_SUB_DIRS ${ANKI_EXTERN_SUB_DIRS} SPIRV-Cross)
  179. endif()
  180. if(ANKI_BUILD_TOOLS)
  181. set(ANKI_EXTERN_SUB_DIRS ${ANKI_EXTERN_SUB_DIRS} assimp)
  182. endif()
  183. # SDL
  184. if(SDL)
  185. message("++ Configuring SDL2")
  186. add_subdirectory(thirdparty/SDL2)
  187. message("++ End configuring SDL2")
  188. # Include first the build directory.
  189. set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}/thirdparty/SDL2/include"
  190. "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/SDL2/include")
  191. else()
  192. set(SDL2_INCLUDE_DIRS "")
  193. endif()
  194. # freetype
  195. message("++ Configuring freetype")
  196. add_subdirectory(thirdparty/freetype)
  197. message("++ End configuring freetype")
  198. # glslang
  199. if(VULKAN)
  200. message("++ Configuring glslang")
  201. add_subdirectory(thirdparty/glslang)
  202. message("++ End configuring glslang")
  203. endif()
  204. foreach(TMP ${ANKI_EXTERN_SUB_DIRS})
  205. add_subdirectory(thirdparty/${TMP})
  206. endforeach()
  207. ################################################################################
  208. # AnKi #
  209. ################################################################################
  210. # Revision
  211. find_package(Git)
  212. if(GIT_FOUND)
  213. execute_process(COMMAND
  214. "${GIT_EXECUTABLE}" log -1 --date=short --format=%h
  215. WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  216. OUTPUT_VARIABLE GIT_COMMIT
  217. ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  218. set(ANKI_REVISION "\"${GIT_COMMIT}\"")
  219. else()
  220. set(ANKI_REVISION "\"unknown\"")
  221. endif()
  222. # Doxygen
  223. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docs/doxyfile ${CMAKE_CURRENT_BINARY_DIR}/doxyfile @ONLY)
  224. find_package(Doxygen)
  225. if(DOXYGEN_FOUND)
  226. message("++ Doxygen found")
  227. add_custom_target(doc ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/doxyfile
  228. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  229. COMMENT "Generating API documentation with Doxygen" VERBATIM)
  230. endif()
  231. # Config.h
  232. set(ANKI_VERSION_MAJOR 0)
  233. set(ANKI_VERSION_MINOR 1)
  234. message("++ AnKi version: ${ANKI_VERSION_MAJOR}.${ANKI_VERSION_MINOR}")
  235. if(ANKI_EXTRA_CHECKS)
  236. set(_ANKI_EXTRA_CHECKS 1)
  237. else()
  238. set(_ANKI_EXTRA_CHECKS 0)
  239. endif()
  240. if(ANKI_ENABLE_SIMD)
  241. set(_ANKI_ENABLE_SIMD 1)
  242. else()
  243. set(_ANKI_ENABLE_SIMD 0)
  244. endif()
  245. if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
  246. set(ANKI_DEBUG_SYMBOLS 1)
  247. set(ANKI_OPTIMIZE 0)
  248. elseif(${CMAKE_BUILD_TYPE} STREQUAL "Release")
  249. set(ANKI_DEBUG_SYMBOLS 0)
  250. set(ANKI_OPTIMIZE 1)
  251. elseif(${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
  252. set(ANKI_DEBUG_SYMBOLS 1)
  253. set(ANKI_OPTIMIZE 1)
  254. else()
  255. message(FATAL_ERROR "Wrong CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
  256. endif()
  257. configure_file("src/anki/Config.h.cmake" "${CMAKE_CURRENT_BINARY_DIR}/anki/Config.h")
  258. install(FILES "${CMAKE_CURRENT_BINARY_DIR}/anki/Config.h" DESTINATION "${INCLUDE_INSTALL_DIR}/anki")
  259. # Include & lib directories
  260. include_directories("src"
  261. "thirdparty/tinyxml2/include"
  262. "thirdparty/lua"
  263. "thirdparty/z"
  264. "${SDL2_INCLUDE_DIRS}"
  265. "thirdparty/freetype/include"
  266. "${CMAKE_CURRENT_BINARY_DIR}/thirdparty/freetype/include/freetype2"
  267. "thirdparty/newton/coreLibrary_300/source/newton"
  268. "thirdparty/newton/packages/dCustomJoints"
  269. "thirdparty/newton/packages/dContainers"
  270. "thirdparty/newton/packages/dMath"
  271. "thirdparty/newton/packages/thirdParty/timeTracker/"
  272. "thirdparty/khronos"
  273. "${CMAKE_CURRENT_BINARY_DIR}"
  274. "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/glslang"
  275. "thirdparty")
  276. if(LINUX OR MACOS OR WINDOWS)
  277. include_directories("thirdparty/GLEW/include")
  278. else()
  279. #include_directories("thirdparty/GLES3/include")
  280. endif()
  281. if(ANDROID)
  282. include_directories("${ANDROID_NDK}/sources/android/native_app_glue")
  283. endif()
  284. # AnKi compiler flags (Mainly warnings)
  285. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -W -Wextra -Wstrict-aliasing -Wwrite-strings -Wunused -Wunused-variable -Wno-unused-parameter -Wundef -Wno-ignored-attributes -Wunused-result -std=c++14")
  286. # Set platform specific
  287. if(LINUX)
  288. if(GL)
  289. set(_SYS ${ANKI_GR_BACKEND} ankiglew)
  290. else()
  291. set(_SYS vulkan)
  292. if(SDL)
  293. set(_SYS ${_SYS} X11-xcb)
  294. else()
  295. message(FATAL_ERROR "Unhandled case")
  296. endif()
  297. endif()
  298. set(_SYS ${_SYS} pthread dl)
  299. elseif(MACOS)
  300. find_package(OpenGL REQUIRED)
  301. set(_SYS ${OPENGL_LIBRARIES} ankiglew pthread)
  302. elseif(ANDROID)
  303. set(_SYS GLESv3 EGL log android)
  304. include_directories("${ANDROID_NDK}/sources/android/native_app_glue")
  305. set(_SYS_SRC "${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c")
  306. elseif(WINDOWS)
  307. if(GL)
  308. set(_SYS ankiglew opengl32)
  309. else()
  310. if(NOT DEFINED ENV{VULKAN_SDK})
  311. message(FATAL_ERROR "You need to have VULKAN SDK installed and the VULKAN_SDK env variable set")
  312. endif()
  313. link_directories($ENV{VULKAN_SDK}/Bin)
  314. set(_SYS vulkan-1)
  315. endif()
  316. set(_SYS ${_SYS} version Imm32 Winmm DbgHelp)
  317. else()
  318. message(FATAL_ERROR "Unhandled case")
  319. endif()
  320. link_directories(${FREETYPE_INSTALL_DIR}/lib)
  321. # Add anki sub libraries
  322. set(ANKI_SUB_DIRS core script renderer scene ui event input physics resource misc gr collision math util)
  323. set(ANKI_LIBS "")
  324. foreach(TMP ${ANKI_SUB_DIRS})
  325. add_subdirectory(src/anki/${TMP})
  326. set(ANKI_LIBS ${ANKI_LIBS} anki${TMP})
  327. endforeach()
  328. add_library(anki src/anki/Dummy.cpp "${_SYS_SRC}")
  329. target_link_libraries(anki ${ANKI_LIBS} ankitinyxml2 ankilua ankiz ${_SYS})
  330. ################################################################################
  331. # AnKi extra #
  332. ################################################################################
  333. if(ANKI_BUILD_TESTS)
  334. add_subdirectory(tests)
  335. endif()
  336. if(ANKI_BUILD_TOOLS)
  337. add_subdirectory(tools)
  338. endif()
  339. if(ANKI_BUILD_SANDBOX)
  340. add_subdirectory(sandbox)
  341. endif()
  342. if(ANKI_BUILD_SAMPLES)
  343. add_subdirectory(samples)
  344. endif()
  345. if(ANKI_BUILD_BENCH)
  346. add_subdirectory(bench)
  347. endif()