💿🐜 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.

339 lines
11 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
  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-2.0.5
  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. # Find OpenGL
  139. find_package(OpenGL REQUIRED)
  140. # Find executable source directory
  141. set(EXECUTABLE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
  142. # Generate C++ configuration file
  143. configure_file(${EXECUTABLE_SOURCE_DIR}/configuration.hpp.in ${EXECUTABLE_SOURCE_DIR}/configuration.hpp)
  144. # Collect executable source files
  145. set(EXECUTABLE_SOURCES
  146. ${EXECUTABLE_SOURCE_DIR}/configuration.hpp
  147. ${EXECUTABLE_SOURCE_DIR}/windows-dirent.h
  148. ${EXECUTABLE_SOURCE_DIR}/controls.cpp
  149. ${EXECUTABLE_SOURCE_DIR}/render-passes.hpp
  150. ${EXECUTABLE_SOURCE_DIR}/settings.cpp
  151. ${EXECUTABLE_SOURCE_DIR}/settings.hpp
  152. ${EXECUTABLE_SOURCE_DIR}/game/terrain.cpp
  153. ${EXECUTABLE_SOURCE_DIR}/game/terrain.hpp
  154. ${EXECUTABLE_SOURCE_DIR}/controls.hpp
  155. ${EXECUTABLE_SOURCE_DIR}/input.cpp
  156. ${EXECUTABLE_SOURCE_DIR}/input.hpp
  157. ${EXECUTABLE_SOURCE_DIR}/main.cpp
  158. ${EXECUTABLE_SOURCE_DIR}/mesh.cpp
  159. ${EXECUTABLE_SOURCE_DIR}/mesh.hpp
  160. ${EXECUTABLE_SOURCE_DIR}/application-state.hpp
  161. ${EXECUTABLE_SOURCE_DIR}/application-state.cpp
  162. ${EXECUTABLE_SOURCE_DIR}/application.hpp
  163. ${EXECUTABLE_SOURCE_DIR}/application.cpp
  164. ${EXECUTABLE_SOURCE_DIR}/states/loading-state.hpp
  165. ${EXECUTABLE_SOURCE_DIR}/states/loading-state.cpp
  166. ${EXECUTABLE_SOURCE_DIR}/states/splash-state.hpp
  167. ${EXECUTABLE_SOURCE_DIR}/states/splash-state.cpp
  168. ${EXECUTABLE_SOURCE_DIR}/states/title-state.hpp
  169. ${EXECUTABLE_SOURCE_DIR}/states/title-state.cpp
  170. ${EXECUTABLE_SOURCE_DIR}/states/main-menu-state.hpp
  171. ${EXECUTABLE_SOURCE_DIR}/states/main-menu-state.cpp
  172. ${EXECUTABLE_SOURCE_DIR}/states/play-state.hpp
  173. ${EXECUTABLE_SOURCE_DIR}/states/play-state.cpp
  174. ${EXECUTABLE_SOURCE_DIR}/ui/ui.hpp
  175. ${EXECUTABLE_SOURCE_DIR}/ui/ui.cpp
  176. ${EXECUTABLE_SOURCE_DIR}/ui/tween.hpp
  177. ${EXECUTABLE_SOURCE_DIR}/ui/tween.cpp
  178. ${EXECUTABLE_SOURCE_DIR}/ui/toolbar.hpp
  179. ${EXECUTABLE_SOURCE_DIR}/ui/toolbar.cpp
  180. ${EXECUTABLE_SOURCE_DIR}/ui/pie-menu.hpp
  181. ${EXECUTABLE_SOURCE_DIR}/ui/pie-menu.cpp
  182. ${EXECUTABLE_SOURCE_DIR}/render-passes.cpp
  183. ${EXECUTABLE_SOURCE_DIR}/game/ant.hpp
  184. ${EXECUTABLE_SOURCE_DIR}/game/ant.cpp
  185. ${EXECUTABLE_SOURCE_DIR}/game/agent.hpp
  186. ${EXECUTABLE_SOURCE_DIR}/game/agent.cpp
  187. ${EXECUTABLE_SOURCE_DIR}/game/colony.hpp
  188. ${EXECUTABLE_SOURCE_DIR}/game/colony.cpp
  189. ${EXECUTABLE_SOURCE_DIR}/game/habitat.hpp
  190. ${EXECUTABLE_SOURCE_DIR}/game/habitat.cpp
  191. ${EXECUTABLE_SOURCE_DIR}/game/nest.hpp
  192. ${EXECUTABLE_SOURCE_DIR}/game/nest.cpp
  193. ${EXECUTABLE_SOURCE_DIR}/game/navmesh.hpp
  194. ${EXECUTABLE_SOURCE_DIR}/game/navmesh.cpp
  195. ${EXECUTABLE_SOURCE_DIR}/game/pheromone.hpp
  196. ${EXECUTABLE_SOURCE_DIR}/game/pheromone.cpp
  197. ${EXECUTABLE_SOURCE_DIR}/game/level.hpp
  198. ${EXECUTABLE_SOURCE_DIR}/game/level.cpp
  199. ${EXECUTABLE_SOURCE_DIR}/game/biome.hpp
  200. ${EXECUTABLE_SOURCE_DIR}/game/biome.cpp
  201. ${EXECUTABLE_SOURCE_DIR}/debug.hpp
  202. ${EXECUTABLE_SOURCE_DIR}/debug.cpp
  203. ${EXECUTABLE_SOURCE_DIR}/camera-controller.hpp
  204. ${EXECUTABLE_SOURCE_DIR}/camera-controller.cpp
  205. ${EXECUTABLE_SOURCE_DIR}/model-loader.hpp
  206. ${EXECUTABLE_SOURCE_DIR}/model-loader.cpp
  207. ${EXECUTABLE_SOURCE_DIR}/material-loader.hpp
  208. ${EXECUTABLE_SOURCE_DIR}/material-loader.cpp
  209. )
  210. # Setup exe icon for windows
  211. if(${PLATFORM} STREQUAL "win32" OR ${PLATFORM} STREQUAL "win64")
  212. if(EXISTS ${PROJECT_SOURCE_DIR}/data)
  213. set(RC_FILES "${PROJECT_SOURCE_DIR}/data/icons/icon.rc")
  214. #set(CMAKE_RC_COMPILER_INIT windres)
  215. #enable_language(RC)
  216. #set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <SOURCE> <OBJECT>")
  217. set(EXECUTABLE_SOURCES "${EXECUTABLE_SOURCES};${RC_FILES}")
  218. endif()
  219. endif()
  220. # Set link directories
  221. link_directories(${EMERGENT_INSTALL_DIR}/lib
  222. ${SDL2_INSTALL_DIR}/lib
  223. )
  224. # Set executable and library output directories
  225. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PLATFORM_PACKAGE_DIR})
  226. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PLATFORM_PACKAGE_DIR})
  227. # Add executable
  228. set(EXECUTABLE_NAME antkeeper)
  229. set(EXECUTABLE_TARGET ${PACKAGE_BUILD_NAME}-executable)
  230. add_executable(${EXECUTABLE_TARGET} ${EXECUTABLE_SOURCES})
  231. add_dependencies(${EXECUTABLE_TARGET} emergent-project SDL2-project)
  232. set_target_properties(${EXECUTABLE_TARGET} PROPERTIES OUTPUT_NAME ${EXECUTABLE_NAME})
  233. # Set include directories
  234. target_include_directories(${EXECUTABLE_TARGET} PUBLIC
  235. ${EMERGENT_INCLUDE_DIRS}
  236. ${SDL2_INCLUDE_DIR}
  237. ${OPENGL_INCLUDE_DIRS}
  238. )
  239. # Build library list
  240. list(APPEND EXECUTABLE_LIBRARIES
  241. ${SDL2_LIBRARIES}
  242. ${EMERGENT_LIBRARIES}
  243. ${OPENGL_gl_LIBRARY}
  244. ${FREETYPE_LIBRARY}
  245. #m
  246. #stdc++
  247. )
  248. # Link libraries
  249. target_link_libraries(${EXECUTABLE_TARGET} ${EXECUTABLE_LIBRARIES})
  250. # Add run target
  251. add_custom_target(run
  252. COMMAND ${EXECUTABLE_TARGET}
  253. DEPENDS ${EXECUTABLE_TARGET}
  254. WORKING_DIRECTORY ${PLATFORM_PACKAGE_DIR}
  255. )
  256. # Add dist target
  257. add_custom_target(dist
  258. COMMAND
  259. ${CMAKE_COMMAND} -E tar "cfvz" "${PROJECT_SOURCE_DIR}/dist/${PACKAGE_NAME}.zip" --format=zip
  260. "${PLATFORM_PACKAGE_DIR}")
  261. # Add clean targets
  262. add_custom_target(clean-build
  263. COMMAND git clean -fdX
  264. WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/build"
  265. )
  266. # Add data subdirectory (if it exists)
  267. if(EXISTS ${PROJECT_SOURCE_DIR}/data)
  268. add_subdirectory(${PROJECT_SOURCE_DIR}/data)
  269. endif()
  270. # Distribution
  271. install(TARGETS ${EXECUTABLE_TARGET} DESTINATION ".")
  272. install(DIRECTORY ${PLATFORM_PACKAGE_DIR}/data
  273. DESTINATION .)
  274. set(CPACK_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")
  275. set(CPACK_PACKAGE_VENDOR "C. J. Howard")
  276. set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
  277. set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
  278. set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
  279. set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
  280. set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${PLATFORM})
  281. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  282. set(CPACK_OUTPUT_FILE_PREFIX "${PROJECT_SOURCE_DIR}/dist/debug")
  283. else()
  284. set(CPACK_OUTPUT_FILE_PREFIX "${PROJECT_SOURCE_DIR}/dist/release")
  285. endif()
  286. if(${PLATFORM} STREQUAL "win32" OR ${PLATFORM} STREQUAL "win64")
  287. set(CPACK_GENERATOR "ZIP")
  288. if (NOT STANDALONE)
  289. set(CPACK_GENERATOR "NSIS")
  290. set(CPACK_MONOLITHIC_INSTALL 1)
  291. set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_FILE_NAME}-installer)
  292. set(CPACK_NSIS_MUI_ICON "${PROJECT_SOURCE_DIR}/data/icons/antkeeper-icon.ico")
  293. set(CPACK_PACKAGE_INSTALL_DIRECTORY "Antkeeper")
  294. get_target_property(EXECUTABLE_OUTPUT_NAME ${EXECUTABLE_TARGET} OUTPUT_NAME)
  295. set(CPACK_NSIS_MENU_LINKS "${EXECUTABLE_OUTPUT_NAME}" "Antkeeper")
  296. endif()
  297. elseif(${PLATFORM} STREQUAL "linux32" OR ${PLATFORM} STREQUAL "linux64")
  298. set(CPACK_GENERATOR "TGZ")
  299. if(NOT STANDALONE)
  300. set(CPACK_GENERATOR "DEB")
  301. set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${CPACK_PACKAGE_VENDOR}")
  302. endif()
  303. endif()
  304. include(CPack)