CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #
  2. # SuperTux - root build script
  3. # Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. #
  19. #
  20. # INSTRUCTIONS:
  21. # -------------
  22. #
  23. # Create a directory build/ and change to it. Run
  24. #
  25. # cmake ..
  26. #
  27. # This creates a set of Makefiles to build the project. Run
  28. #
  29. # make
  30. #
  31. cmake_minimum_required(VERSION 3.1)
  32. ## Project name to use as command prefix.
  33. project(SUPERTUX)
  34. ### CMake configuration
  35. if(COMMAND cmake_policy)
  36. cmake_policy(SET CMP0003 NEW)
  37. cmake_policy(SET CMP0008 NEW)
  38. cmake_policy(SET CMP0023 NEW)
  39. endif()
  40. set(CMAKE_CXX_STANDARD 17)
  41. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  42. set(CMAKE_CXX_EXTENSIONS OFF)
  43. # This should be set only with windows Visual Studio generator builds
  44. string(TOLOWER "${CMAKE_GENERATOR_PLATFORM}" PLATFORM)
  45. if(${PLATFORM} MATCHES "arm64")
  46. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_ARM64_ /DMY_CPU_LE")
  47. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D_ARM64_ /DMY_CPU_LE")
  48. endif()
  49. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/mk/cmake)
  50. include(ConfigureFiles)
  51. include(ExternalProject)
  52. include(CheckCXXCompilerFlag)
  53. include(CheckSymbolExists)
  54. ## For autopackage
  55. set(APPDATADIR "${CMAKE_INSTALL_PREFIX}/share/games/supertux2")
  56. set(BUILD_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/data")
  57. set(BUILD_CONFIG_DATA_DIR "${CMAKE_BINARY_DIR}/data")
  58. ## Check endianess
  59. if(NOT EMSCRIPTEN)
  60. # FIXME: Any reason why we need this?
  61. include(TestBigEndian)
  62. test_big_endian(WORDS_BIGENDIAN)
  63. endif()
  64. ## Add definitions
  65. if(CMAKE_BUILD_TYPE MATCHES "Release|RelWithDebInfo")
  66. add_definitions(-DRELEASE)
  67. elseif(CMAKE_BUILD_TYPE MATCHES Debug)
  68. add_definitions(-DDEBUG)
  69. endif()
  70. # Options for install
  71. if(WIN32 AND NOT UNIX)
  72. set(INSTALL_SUBDIR_BIN "bin" CACHE STRING "Installation subdir for binaries")
  73. set(INSTALL_SUBDIR_SHARE "data" CACHE STRING "Installation subdir for data")
  74. set(INSTALL_SUBDIR_DOC "doc" CACHE STRING "Installation subdir for docs")
  75. else()
  76. if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND DISABLE_CPACK_BUNDLING)
  77. set(INSTALL_SUBDIR_BIN "SuperTux.app/Contents/MacOS" CACHE STRING "Installation subdir for binaries")
  78. set(INSTALL_SUBDIR_SHARE "SuperTux.app/Contents/Resources/data" CACHE STRING "Installation subdir for data")
  79. set(INSTALL_SUBDIR_DOC "SuperTux.app/Contents/Resources" CACHE STRING "Installation subdir for docs")
  80. else()
  81. set(INSTALL_SUBDIR_BIN "games" CACHE STRING "Installation subdir for binaries")
  82. set(INSTALL_SUBDIR_SHARE "share/games/supertux2" CACHE STRING "Installation subdir for data")
  83. set(INSTALL_SUBDIR_DOC "share/doc/supertux2" CACHE STRING "Installation subdir for docs")
  84. endif()
  85. endif()
  86. if(EMSCRIPTEN)
  87. set(CMAKE_EXECUTABLE_SUFFIX .html)
  88. set(IS_EMSCRIPTEN_BUILD ON)
  89. set(EM_USE_FLAGS "-sDISABLE_EXCEPTION_CATCHING=0")
  90. set(EM_LINK_FLAGS " -sINITIAL_MEMORY=134217728 -sALLOW_MEMORY_GROWTH=1 -sMAXIMUM_MEMORY=536870912 -sERROR_ON_UNDEFINED_SYMBOLS=0 --preload-file ${BUILD_CONFIG_DATA_DIR} --use-preload-plugins -lidbfs.js")
  91. if(CMAKE_BUILD_TYPE MATCHES Debug)
  92. set(EM_USE_FLAGS "${EM_USE_FLAGS} -fsanitize=undefined")
  93. set(EM_LINK_FLAGS "${EM_LINK_FLAGS} -fsanitize=undefined -sSAFE_HEAP=1 -sASSERTIONS=1 -sDEMANGLE_SUPPORT=1")
  94. endif()
  95. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EM_USE_FLAGS}")
  96. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EM_USE_FLAGS}")
  97. set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${EM_USE_FLAGS} ${EM_LINK_FLAGS}")
  98. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${EM_USE_FLAGS} ${EM_LINK_FLAGS}")
  99. endif()
  100. # TODO: Add " OR ANDROID OR IOS" to this
  101. if(EMSCRIPTEN OR UBUNTU_TOUCH OR ANDROID)
  102. option(REMOVE_QUIT_BUTTON "Remove the option to quit the game (useful on mobile devices)" ON)
  103. else()
  104. option(REMOVE_QUIT_BUTTON "Remove the option to quit the game (useful on mobile devices)" OFF)
  105. endif()
  106. option(STEAM_BUILD "Prepare build for Steam" OFF)
  107. option(IS_SUPERTUX_RELEASE "Build as official SuperTux release" OFF)
  108. set(SUPERTUX_SYSTEM_NAME ${CMAKE_SYSTEM_NAME})
  109. set(VCPKG_BUILD OFF CACHE BOOL "Use dependencies installed via vcpkg (not dependency package)")
  110. set(VCPKG_APPLOCAL_DEPS ${VCPKG_BUILD} BOOL)
  111. # Detect mobile builds
  112. option(UBUNTU_TOUCH "Compile the project for an Ubuntu Touch target" OFF)
  113. # Mobile builds
  114. if(UBUNTU_TOUCH OR ANDROID)
  115. option(HIDE_NONMOBILE_OPTIONS "Hide options that are impractical on mobile devices (e. g. changing screen resolution)" ON)
  116. else()
  117. option(HIDE_NONMOBILE_OPTIONS "Hide options that are impractical on mobile devices (e. g. changing screen resolution)" OFF)
  118. endif()
  119. # Configure main menu logo
  120. if(("${SUPERTUX_VERSION_STRING}" MATCHES "^v[0-9]+\\.[0-9]+\\.[0-9]+$") OR IS_SUPERTUX_RELEASE OR STEAM_BUILD)
  121. set(LOGO_FILE "logo.png")
  122. else()
  123. set(LOGO_FILE "logo_dev.png")
  124. endif()
  125. if(WIN32)
  126. include(SuperTux/Win32)
  127. endif()
  128. ## Check platform-dependent build options
  129. include(ConfigureChecks)
  130. ## Some additional compiler switches
  131. include(SuperTux/ClangTidy)
  132. include(SuperTux/WarningFlags)
  133. if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  134. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
  135. endif()
  136. if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  137. add_definitions(-DMACOSX)
  138. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib")
  139. endif()
  140. ## Add lots of dependencies to compiler switches
  141. include(SuperTux/ProvideGlm)
  142. include(SuperTux/ProvideFmt)
  143. include(SuperTux/ProvideSDL2)
  144. include(SuperTux/ProvideOpenAL)
  145. include(SuperTux/ProvideOggVorbis)
  146. include(SuperTux/ProvidePhysfs)
  147. include(SuperTux/ProvideCurl)
  148. include(SuperTux/ProvideSquirrel)
  149. include(SuperTux/ProvideTinygettext)
  150. include(SuperTux/ProvideSDL2_ttf)
  151. include(SuperTux/ProvideDiscord)
  152. include(SuperTux/ProvideSexpcpp)
  153. include(SuperTux/ProvideSavePNG)
  154. include(SuperTux/ProvidePartioZip)
  155. include(SuperTux/ProvideOpenGL)
  156. include(SuperTux/ProvideENet)
  157. ## Build stuff
  158. include(SuperTux/BuildVersion)
  159. include(SuperTux/BuildDocumentation)
  160. include(SuperTux/BuildMessagePot)
  161. include(SuperTux/BuildMiniswigWrapper)
  162. ## Build list of sources for supertux binary
  163. file(GLOB SUPERTUX_SOURCES_C RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} external/obstack/*.c external/findlocale/findlocale.c)
  164. file(GLOB SUPERTUX_SOURCES_CXX RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/*/*.cpp src/supertux/menu/*.cpp src/video/sdl/*.cpp src/video/null/*.cpp)
  165. file(GLOB SUPERTUX_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${PROJECT_BINARY_DIR}/tmp/*.rc")
  166. if(HAVE_OPENGL)
  167. file(GLOB SUPERTUX_OPENGL_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/video/gl/*.cpp)
  168. set(SUPERTUX_SOURCES_CXX ${SUPERTUX_SOURCES_CXX} ${SUPERTUX_OPENGL_SOURCES})
  169. endif()
  170. ## Sort source lists to have deterministic linking order
  171. list(SORT SUPERTUX_SOURCES_C)
  172. list(SORT SUPERTUX_SOURCES_CXX)
  173. list(SORT SUPERTUX_RESOURCES)
  174. if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/scripting/wrapper.cpp)
  175. set(SUPERTUX_SOURCES_CXX ${SUPERTUX_SOURCES_CXX} ${CMAKE_CURRENT_SOURCE_DIR}/src/scripting/wrapper.cpp)
  176. endif()
  177. ## On Windows, add an icon
  178. if(WIN32)
  179. if(MINGW)
  180. add_custom_command(
  181. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/supertux_rc.o
  182. COMMAND ${CMAKE_RC_COMPILER} -I${CMAKE_CURRENT_SOURCE_DIR}/data/images/engine/icons -i${CMAKE_CURRENT_SOURCE_DIR}/data/images/engine/icons/supertux.rc -o ${CMAKE_CURRENT_BINARY_DIR}/supertux_rc.o)
  183. set(SUPERTUX_SOURCES_C ${SUPERTUX_SOURCES_C} ${CMAKE_CURRENT_BINARY_DIR}/supertux_rc.o)
  184. endif()
  185. endif()
  186. include(SuperTux/CompileAmalgation)
  187. ## Generate supertux executable in the right place
  188. #set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
  189. set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
  190. ## Add target for supertux binary
  191. add_library(supertux2_c OBJECT ${SUPERTUX_SOURCES_C})
  192. add_library(supertux2_lib STATIC ${CMAKE_BINARY_DIR}/version.h ${SUPERTUX_SOURCES_CXX} ${SUPERTUX_RESOURCES} $<TARGET_OBJECTS:supertux2_c>)
  193. target_include_directories(supertux2_lib PUBLIC ${CMAKE_BINARY_DIR} src/)
  194. if(WIN32)
  195. add_executable(supertux2 WIN32 src/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/data/images/engine/icons/supertux.rc)
  196. target_link_libraries(supertux2 LibSDL2main)
  197. else()
  198. add_executable(supertux2 src/main.cpp)
  199. endif()
  200. target_link_libraries(supertux2 supertux2_lib)
  201. set_target_properties(supertux2_lib PROPERTIES OUTPUT_NAME supertux2_lib)
  202. set_target_properties(supertux2_lib PROPERTIES COMPILE_FLAGS "${SUPERTUX2_EXTRA_WARNING_FLAGS}")
  203. if(EMSCRIPTEN)
  204. target_link_options(supertux2 PUBLIC -sEXPORTED_FUNCTIONS=['_main','_set_resolution','_save_config','_onDownloadProgress','_onDownloadFinished','_onDownloadError','_onDownloadAborted','_getExceptionMessage'] PUBLIC -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap'])
  205. endif()
  206. if(WIN32 AND NOT VCPKG_BUILD)
  207. ## Copy dlls on windows
  208. add_custom_command(TARGET supertux2_lib POST_BUILD
  209. COMMAND ${CMAKE_COMMAND} -E copy_directory
  210. "${DEPENDENCY_FOLDER}/dll"
  211. $<TARGET_FILE_DIR:supertux2_lib>)
  212. endif()
  213. ## Some additional include paths
  214. target_include_directories(supertux2_lib SYSTEM PUBLIC
  215. external/findlocale/
  216. external/obstack/
  217. )
  218. # Include altivec wrapper on ppc
  219. if(CMAKE_SYSTEM_PROCESSOR MATCHES "^ppc.*")
  220. target_include_directories(supertux2_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/ppc)
  221. endif()
  222. ## Link supertux binary with squirrel and other libraries
  223. if(NOT EMSCRIPTEN)
  224. target_link_libraries(supertux2_lib PUBLIC LibSDL2 LibSDL2_image)
  225. endif()
  226. target_link_libraries(supertux2_lib PUBLIC LibSquirrel)
  227. target_link_libraries(supertux2_lib PUBLIC LibSqstdlib)
  228. target_link_libraries(supertux2_lib PUBLIC LibTinygettext)
  229. target_link_libraries(supertux2_lib PUBLIC LibSexp)
  230. target_link_libraries(supertux2_lib PUBLIC LibSavePNG)
  231. target_link_libraries(supertux2_lib PUBLIC LibPartioZip)
  232. target_link_libraries(supertux2_lib PUBLIC LibOpenAL)
  233. target_link_libraries(supertux2_lib PUBLIC LibGlm)
  234. target_link_libraries(supertux2_lib PUBLIC LibFmt)
  235. target_link_libraries(supertux2_lib PUBLIC LibPhysfs)
  236. target_link_libraries(supertux2_lib PUBLIC LibENet)
  237. if(NOT EMSCRIPTEN)
  238. target_link_libraries(supertux2_lib PUBLIC LibSDL2_ttf)
  239. target_link_libraries(supertux2_lib PUBLIC LibOggVorbis)
  240. endif()
  241. if(HAVE_OPENGL)
  242. target_link_libraries(supertux2_lib PUBLIC LibOpenGL)
  243. endif()
  244. if(ENABLE_DISCORD)
  245. target_link_libraries(supertux2_lib PUBLIC LibDiscord)
  246. endif()
  247. if(NOT EMSCRIPTEN)
  248. target_link_libraries(supertux2_lib PUBLIC LibCurl)
  249. endif()
  250. ## Install stuff
  251. include(SuperTux/BuildInstall)
  252. ## Create config.h now that INSTALL_SUBDIR_* have been set.
  253. configure_file(config.h.cmake ${CMAKE_BINARY_DIR}/config.h )
  254. ## Build tests
  255. include(SuperTux/BuildTests)
  256. ## CPack/Installation-specific stuff
  257. include(SuperTux/BuildCPack)
  258. # move some config clutter to the advanced section
  259. mark_as_advanced(
  260. INSTALL_SUBDIR_BIN
  261. INSTALL_SUBDIR_SHARE
  262. INSTALL_SUBDIR_DOC
  263. )
  264. mark_as_advanced(
  265. CMAKE_BACKWARDS_COMPATIBILITY
  266. CMAKE_BUILD_TYPE
  267. CMAKE_INSTALL_PREFIX
  268. EXECUTABLE_OUTPUT_PATH
  269. LIBRARY_OUTPUT_PATH
  270. CMAKE_OSX_ARCHITECTURES
  271. CMAKE_OSX_SYSROOT
  272. )
  273. mark_as_advanced(
  274. APPDATADIR
  275. )
  276. # EOF #