💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
12 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. cmake_minimum_required(VERSION 3.7)
  2. project(antkeeper
  3. VERSION "0.0.0"
  4. )
  5. option(STANDALONE "Distribute in a standalone archive." OFF)
  6. set(LANGUAGE "en-us" CACHE STRING "")
  7. # Determine target build platform according to binary dir
  8. get_filename_component(PLATFORM ${PROJECT_BINARY_DIR} NAME CACHE)
  9. # Check for architecture mismatches
  10. if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
  11. if (${PLATFORM} STREQUAL "win64" OR ${PLATFORM} STREQUAL "linux64")
  12. message(FATAL_ERROR "Compiler architecture is 32-bit but target architecture is 64-bit.")
  13. endif()
  14. else()
  15. if (${PLATFORM} STREQUAL "win32" OR ${PLATFORM} STREQUAL "linux32")
  16. message(FATAL_ERROR "Compiler architecture is 64-bit but target architecture is 32-bit.")
  17. endif()
  18. endif()
  19. # Setup configuration strings
  20. set(ANTKEEPER_VERSION ${PROJECT_VERSION})
  21. set(ANTKEEPER_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
  22. set(ANTKEEPER_VERSION_MINOR ${PROJECT_VERSION_MINOR})
  23. set(ANTKEEPER_VERSION_PATCH ${PROJECT_VERSION_PATCH})
  24. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  25. set(ANTKEEPER_BUILD_TYPE debug)
  26. set(ANTKEEPER_DEBUG ON)
  27. else()
  28. set(ANTKEEPER_BUILD_TYPE release)
  29. set(ANTKEEPER_DEBUG OFF)
  30. endif()
  31. # Setup build type paths
  32. set(BUILD_DIR ${PROJECT_SOURCE_DIR}/bin)
  33. set(BUILD_DEBUG_DIR ${BUILD_DIR}/debug)
  34. set(BUILD_RELEASE_DIR ${BUILD_DIR}/release)
  35. # Set package name
  36. set(PACKAGE_NAME ${PROJECT_NAME}-${PROJECT_VERSION}-${PLATFORM})
  37. set(PACKAGE_BUILD_NAME ${PACKAGE_NAME}-${ANTKEEPER_BUILD_TYPE})
  38. # Set package directory
  39. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  40. set(PLATFORM_PACKAGE_DIR ${BUILD_DEBUG_DIR}/${PACKAGE_NAME})
  41. else()
  42. set(PLATFORM_PACKAGE_DIR ${BUILD_RELEASE_DIR}/${PACKAGE_NAME})
  43. endif()
  44. # Set C++ compiler flags for debug and release build types
  45. if(MSVC)
  46. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W2")
  47. elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
  48. # Update if necessary
  49. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  50. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -g")
  51. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -s -DNDEBUG")
  52. endif()
  53. # Set C and C++ compiler flags for the target architecture
  54. if(${PLATFORM} STREQUAL "win32" OR ${PLATFORM} STREQUAL "linux32")
  55. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
  56. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
  57. elseif(${PLATFORM} STREQUAL "win64" OR ${PLATFORM} STREQUAL "linux64")
  58. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
  59. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
  60. elseif(${PLATFORM} STREQUAL "osx" AND CMAKE_OSX_ARCHITECTURES STREQUAL "")
  61. # 32-bit
  62. #set(CMAKE_OSX_ARCHITECTURES "i386" CACHE STRING "" FORCE)
  63. # 64-bit
  64. #set(CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "" FORCE)
  65. # 96-bit universal
  66. set(CMAKE_OSX_ARCHITECTURES "x86_64;i386" CACHE STRING "" FORCE)
  67. endif()
  68. # Disable .manifest generation
  69. if(${PLATFORM} STREQUAL "win32" OR ${PLATFORM} STREQUAL "win64")
  70. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO /OPT:REF /OPT:ICF")
  71. endif()
  72. # Set C++ version
  73. set(CMAKE_CXX_STANDARD 11)
  74. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  75. set(CMAKE_CXX_EXTENSIONS OFF)
  76. # Include ExternalProject_Add command
  77. include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
  78. # Configure, build and install emergent
  79. set(EMERGENT_BUILD_DIR ${PROJECT_BINARY_DIR}/deps/build/emergent)
  80. set(EMERGENT_INSTALL_DIR ${PROJECT_BINARY_DIR}/deps/install/emergent)
  81. set(EMERGENT_INCLUDE_DIR ${EMERGENT_INSTALL_DIR}/include)
  82. set(EMERGENT_GL3W_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/lib/emergent/lib/gl3w/include)
  83. set(EMERGENT_GLM_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/lib/emergent/lib/glm)
  84. set(EMERGENT_STB_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/lib/emergent/lib/stb)
  85. set(EMERGENT_INCLUDE_DIRS
  86. ${EMERGENT_INCLUDE_DIR}
  87. ${EMERGENT_GL3W_INCLUDE_DIR}
  88. ${EMERGENT_GLM_INCLUDE_DIR}
  89. ${EMERGENT_STB_INCLUDE_DIR}
  90. )
  91. set(EMERGENT_LIBRARY emergent)
  92. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  93. set(FREETYPE_LIBRARY freetyped)
  94. else()
  95. set(FREETYPE_LIBRARY freetype)
  96. endif()
  97. set(EMERGENT_LIBRARIES
  98. ${EMERGENT_LIBRARY}
  99. ${FREETYPE_LIBRARY}
  100. )
  101. ExternalProject_Add(emergent-project
  102. SOURCE_DIR ${PROJECT_SOURCE_DIR}/lib/emergent
  103. CMAKE_ARGS
  104. "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
  105. "-DCMAKE_INSTALL_PREFIX=${EMERGENT_INSTALL_DIR}"
  106. BINARY_DIR ${EMERGENT_BUILD_DIR}
  107. INSTALL_DIR ${EMERGENT_INSTALL_DIR})
  108. # Configure, build and install SDL2
  109. set(SDL2_BUILD_DIR ${PROJECT_BINARY_DIR}/deps/build/SDL2)
  110. set(SDL2_INSTALL_DIR ${PROJECT_BINARY_DIR}/deps/install/SDL2)
  111. set(SDL2_INCLUDE_DIR ${SDL2_INSTALL_DIR}/include)
  112. set(SDL2_LIBRARY SDL2)
  113. set(SDL2main_LIBRARY SDL2main)
  114. set(SDL2_LIBRARIES
  115. ${SDL2main_LIBRARY}
  116. ${SDL2_LIBRARY}
  117. dinput8
  118. dxguid
  119. user32
  120. gdi32
  121. winmm
  122. imm32
  123. ole32
  124. oleaut32
  125. shell32
  126. version
  127. uuid
  128. )
  129. ExternalProject_Add(SDL2-project
  130. SOURCE_DIR ${PROJECT_SOURCE_DIR}/lib/SDL2
  131. CMAKE_ARGS
  132. "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
  133. "-DCMAKE_INSTALL_PREFIX=${SDL2_INSTALL_DIR}"
  134. "-DSDL_STATIC=true"
  135. "-DSDL_SHARED=false"
  136. BINARY_DIR ${SDL2_BUILD_DIR}
  137. INSTALL_DIR ${SDL2_INSTALL_DIR})
  138. # Configure, build and install dirent
  139. set(DIRENT_BUILD_DIR ${PROJECT_BINARY_DIR}/deps/build/dirent)
  140. set(DIRENT_INSTALL_DIR ${PROJECT_BINARY_DIR}/deps/install/dirent)
  141. set(DIRENT_INCLUDE_DIR ${DIRENT_INSTALL_DIR}/include)
  142. ExternalProject_Add(dirent-project
  143. SOURCE_DIR ${PROJECT_SOURCE_DIR}/lib/dirent
  144. CMAKE_ARGS
  145. "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
  146. "-DCMAKE_INSTALL_PREFIX=${DIRENT_INSTALL_DIR}"
  147. BINARY_DIR ${DIRENT_BUILD_DIR}
  148. INSTALL_DIR ${DIRENT_INSTALL_DIR})
  149. # Find OpenGL
  150. find_package(OpenGL REQUIRED)
  151. # Find executable source directory
  152. set(EXECUTABLE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
  153. # Generate C++ configuration file
  154. configure_file(${EXECUTABLE_SOURCE_DIR}/configuration.hpp.in ${EXECUTABLE_SOURCE_DIR}/configuration.hpp)
  155. # Collect executable source files
  156. set(EXECUTABLE_SOURCES
  157. ${EXECUTABLE_SOURCE_DIR}/configuration.hpp
  158. ${EXECUTABLE_SOURCE_DIR}/controls.cpp
  159. ${EXECUTABLE_SOURCE_DIR}/render-passes.hpp
  160. ${EXECUTABLE_SOURCE_DIR}/settings.cpp
  161. ${EXECUTABLE_SOURCE_DIR}/settings.hpp
  162. ${EXECUTABLE_SOURCE_DIR}/game/terrain.cpp
  163. ${EXECUTABLE_SOURCE_DIR}/game/terrain.hpp
  164. ${EXECUTABLE_SOURCE_DIR}/controls.hpp
  165. ${EXECUTABLE_SOURCE_DIR}/input.cpp
  166. ${EXECUTABLE_SOURCE_DIR}/input.hpp
  167. ${EXECUTABLE_SOURCE_DIR}/main.cpp
  168. ${EXECUTABLE_SOURCE_DIR}/mesh.cpp
  169. ${EXECUTABLE_SOURCE_DIR}/mesh.hpp
  170. ${EXECUTABLE_SOURCE_DIR}/application-state.hpp
  171. ${EXECUTABLE_SOURCE_DIR}/application-state.cpp
  172. ${EXECUTABLE_SOURCE_DIR}/application.hpp
  173. ${EXECUTABLE_SOURCE_DIR}/application.cpp
  174. ${EXECUTABLE_SOURCE_DIR}/states/loading-state.hpp
  175. ${EXECUTABLE_SOURCE_DIR}/states/loading-state.cpp
  176. ${EXECUTABLE_SOURCE_DIR}/states/splash-state.hpp
  177. ${EXECUTABLE_SOURCE_DIR}/states/splash-state.cpp
  178. ${EXECUTABLE_SOURCE_DIR}/states/title-state.hpp
  179. ${EXECUTABLE_SOURCE_DIR}/states/title-state.cpp
  180. ${EXECUTABLE_SOURCE_DIR}/states/game-state.hpp
  181. ${EXECUTABLE_SOURCE_DIR}/states/game-state.cpp
  182. ${EXECUTABLE_SOURCE_DIR}/ui/ui.hpp
  183. ${EXECUTABLE_SOURCE_DIR}/ui/ui.cpp
  184. ${EXECUTABLE_SOURCE_DIR}/ui/menu.hpp
  185. ${EXECUTABLE_SOURCE_DIR}/ui/menu.cpp
  186. ${EXECUTABLE_SOURCE_DIR}/ui/tween.hpp
  187. ${EXECUTABLE_SOURCE_DIR}/ui/tween.cpp
  188. ${EXECUTABLE_SOURCE_DIR}/ui/toolbar.hpp
  189. ${EXECUTABLE_SOURCE_DIR}/ui/toolbar.cpp
  190. ${EXECUTABLE_SOURCE_DIR}/ui/pie-menu.hpp
  191. ${EXECUTABLE_SOURCE_DIR}/ui/pie-menu.cpp
  192. ${EXECUTABLE_SOURCE_DIR}/render-passes.cpp
  193. ${EXECUTABLE_SOURCE_DIR}/game/ant.hpp
  194. ${EXECUTABLE_SOURCE_DIR}/game/ant.cpp
  195. ${EXECUTABLE_SOURCE_DIR}/game/agent.hpp
  196. ${EXECUTABLE_SOURCE_DIR}/game/agent.cpp
  197. ${EXECUTABLE_SOURCE_DIR}/game/colony.hpp
  198. ${EXECUTABLE_SOURCE_DIR}/game/colony.cpp
  199. ${EXECUTABLE_SOURCE_DIR}/game/habitat.hpp
  200. ${EXECUTABLE_SOURCE_DIR}/game/habitat.cpp
  201. ${EXECUTABLE_SOURCE_DIR}/game/nest.hpp
  202. ${EXECUTABLE_SOURCE_DIR}/game/nest.cpp
  203. ${EXECUTABLE_SOURCE_DIR}/game/navmesh.hpp
  204. ${EXECUTABLE_SOURCE_DIR}/game/navmesh.cpp
  205. ${EXECUTABLE_SOURCE_DIR}/game/pheromone-matrix.hpp
  206. ${EXECUTABLE_SOURCE_DIR}/game/pheromone-matrix.cpp
  207. ${EXECUTABLE_SOURCE_DIR}/game/level.hpp
  208. ${EXECUTABLE_SOURCE_DIR}/game/level.cpp
  209. ${EXECUTABLE_SOURCE_DIR}/game/biome.hpp
  210. ${EXECUTABLE_SOURCE_DIR}/game/biome.cpp
  211. ${EXECUTABLE_SOURCE_DIR}/game/tool.cpp
  212. ${EXECUTABLE_SOURCE_DIR}/debug.hpp
  213. ${EXECUTABLE_SOURCE_DIR}/debug.cpp
  214. ${EXECUTABLE_SOURCE_DIR}/camera-rig.hpp
  215. ${EXECUTABLE_SOURCE_DIR}/camera-rig.cpp
  216. ${EXECUTABLE_SOURCE_DIR}/model-loader.hpp
  217. ${EXECUTABLE_SOURCE_DIR}/model-loader.cpp
  218. ${EXECUTABLE_SOURCE_DIR}/material-loader.hpp
  219. ${EXECUTABLE_SOURCE_DIR}/material-loader.cpp
  220. )
  221. # Setup manifest and exe icon for windows
  222. if(${PLATFORM} STREQUAL "win32" OR ${PLATFORM} STREQUAL "win64")
  223. list(APPEND EXECUTABLE_SOURCES "${EXECUTABLE_SOURCE_DIR}/dpi-aware.manifest")
  224. if(EXISTS ${PROJECT_SOURCE_DIR}/data)
  225. set(RC_FILES "${PROJECT_SOURCE_DIR}/data/icons/icon.rc")
  226. #set(CMAKE_RC_COMPILER_INIT windres)
  227. #enable_language(RC)
  228. #set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <SOURCE> <OBJECT>")
  229. set(EXECUTABLE_SOURCES "${EXECUTABLE_SOURCES};${RC_FILES}")
  230. endif()
  231. endif()
  232. # Set link directories
  233. link_directories(${EMERGENT_INSTALL_DIR}/lib
  234. ${SDL2_INSTALL_DIR}/lib
  235. )
  236. # Set executable and library output directories
  237. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PLATFORM_PACKAGE_DIR})
  238. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PLATFORM_PACKAGE_DIR})
  239. # Add executable
  240. set(EXECUTABLE_NAME antkeeper)
  241. set(EXECUTABLE_TARGET ${PACKAGE_BUILD_NAME}-executable)
  242. add_executable(${EXECUTABLE_TARGET} ${EXECUTABLE_SOURCES})
  243. add_dependencies(${EXECUTABLE_TARGET} emergent-project SDL2-project dirent-project)
  244. set_target_properties(${EXECUTABLE_TARGET} PROPERTIES OUTPUT_NAME ${EXECUTABLE_NAME})
  245. # Set include directories
  246. target_include_directories(${EXECUTABLE_TARGET} PUBLIC
  247. ${EMERGENT_INCLUDE_DIRS}
  248. ${SDL2_INCLUDE_DIR}
  249. ${DIRENT_INCLUDE_DIR}
  250. ${OPENGL_INCLUDE_DIRS}
  251. )
  252. # Build library list
  253. list(APPEND EXECUTABLE_LIBRARIES
  254. ${SDL2_LIBRARIES}
  255. ${EMERGENT_LIBRARIES}
  256. ${OPENGL_gl_LIBRARY}
  257. ${FREETYPE_LIBRARY}
  258. #m
  259. #stdc++
  260. )
  261. # Link libraries
  262. target_link_libraries(${EXECUTABLE_TARGET} ${EXECUTABLE_LIBRARIES})
  263. # Add run target
  264. add_custom_target(run
  265. COMMAND ${EXECUTABLE_TARGET}
  266. DEPENDS ${EXECUTABLE_TARGET}
  267. WORKING_DIRECTORY ${PLATFORM_PACKAGE_DIR}
  268. )
  269. # Add dist target
  270. add_custom_target(dist
  271. COMMAND
  272. ${CMAKE_COMMAND} -E tar "cfvz" "${PROJECT_SOURCE_DIR}/dist/${PACKAGE_NAME}.zip" --format=zip
  273. "${PLATFORM_PACKAGE_DIR}")
  274. # Add clean targets
  275. add_custom_target(clean-build
  276. COMMAND git clean -fdX
  277. WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/build"
  278. )
  279. # Add data subdirectory (if it exists)
  280. if(EXISTS ${PROJECT_SOURCE_DIR}/data)
  281. add_subdirectory(${PROJECT_SOURCE_DIR}/data)
  282. endif()
  283. # Distribution
  284. install(TARGETS ${EXECUTABLE_TARGET} DESTINATION ".")
  285. install(DIRECTORY ${PLATFORM_PACKAGE_DIR}/data
  286. DESTINATION .)
  287. set(CPACK_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")
  288. set(CPACK_PACKAGE_VENDOR "C. J. Howard")
  289. set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
  290. set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
  291. set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
  292. set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
  293. set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${PLATFORM})
  294. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  295. set(CPACK_OUTPUT_FILE_PREFIX "${PROJECT_SOURCE_DIR}/dist/debug")
  296. else()
  297. set(CPACK_OUTPUT_FILE_PREFIX "${PROJECT_SOURCE_DIR}/dist/release")
  298. endif()
  299. if(${PLATFORM} STREQUAL "win32" OR ${PLATFORM} STREQUAL "win64")
  300. set(CPACK_GENERATOR "ZIP")
  301. if (NOT STANDALONE)
  302. set(CPACK_GENERATOR "NSIS")
  303. set(CPACK_MONOLITHIC_INSTALL 1)
  304. set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_FILE_NAME}-installer)
  305. set(CPACK_NSIS_MUI_ICON "${PROJECT_SOURCE_DIR}/data/icons/antkeeper-icon.ico")
  306. set(CPACK_PACKAGE_INSTALL_DIRECTORY "Antkeeper")
  307. get_target_property(EXECUTABLE_OUTPUT_NAME ${EXECUTABLE_TARGET} OUTPUT_NAME)
  308. set(CPACK_NSIS_MENU_LINKS "${EXECUTABLE_OUTPUT_NAME}" "Antkeeper")
  309. endif()
  310. elseif(${PLATFORM} STREQUAL "linux32" OR ${PLATFORM} STREQUAL "linux64")
  311. set(CPACK_GENERATOR "TGZ")
  312. if(NOT STANDALONE)
  313. set(CPACK_GENERATOR "DEB")
  314. set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${CPACK_PACKAGE_VENDOR}")
  315. endif()
  316. endif()
  317. include(CPack)