CMakeLists.txt 13 KB

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