CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. ## Project name to use as command prefix.
  32. project(SUPERTUX)
  33. ### CMake configuration
  34. cmake_minimum_required(VERSION 3.1)
  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 14)
  41. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  42. set(CMAKE_CXX_EXTENSIONS OFF)
  43. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/mk/cmake)
  44. include(ConfigureFiles)
  45. include(ExternalProject)
  46. include(CheckCXXCompilerFlag)
  47. include(CheckSymbolExists)
  48. ## For autopackage
  49. set(APPDATADIR "${CMAKE_INSTALL_PREFIX}/share/games/supertux2")
  50. set(BUILD_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/data")
  51. set(BUILD_CONFIG_DATA_DIR "${CMAKE_BINARY_DIR}/data")
  52. ## Check endianess
  53. if(NOT EMSCRIPTEN)
  54. # FIXME: Any reason why we need this?
  55. include(TestBigEndian)
  56. test_big_endian(WORDS_BIGENDIAN)
  57. endif()
  58. ## Add definitions
  59. if(CMAKE_BUILD_TYPE MATCHES "Release|RelWithDebInfo")
  60. add_definitions(-DRELEASE)
  61. elseif(CMAKE_BUILD_TYPE MATCHES Debug)
  62. add_definitions(-DDEBUG)
  63. endif()
  64. # Options for install
  65. if(WIN32 AND NOT UNIX)
  66. set(INSTALL_SUBDIR_BIN "bin" CACHE STRING "Installation subdir for binaries")
  67. set(INSTALL_SUBDIR_SHARE "data" CACHE STRING "Installation subdir for data")
  68. set(INSTALL_SUBDIR_DOC "doc" CACHE STRING "Installation subdir for docs")
  69. else()
  70. if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND DISABLE_CPACK_BUNDLING)
  71. set(INSTALL_SUBDIR_BIN "SuperTux.app/Contents/MacOS" CACHE STRING "Installation subdir for binaries")
  72. set(INSTALL_SUBDIR_SHARE "SuperTux.app/Contents/Resources/data" CACHE STRING "Installation subdir for data")
  73. set(INSTALL_SUBDIR_DOC "SuperTux.app/Contents/Resources" CACHE STRING "Installation subdir for docs")
  74. else()
  75. set(INSTALL_SUBDIR_BIN "games" CACHE STRING "Installation subdir for binaries")
  76. set(INSTALL_SUBDIR_SHARE "share/games/supertux2" CACHE STRING "Installation subdir for data")
  77. set(INSTALL_SUBDIR_DOC "share/doc/supertux2" CACHE STRING "Installation subdir for docs")
  78. endif()
  79. endif()
  80. if(EMSCRIPTEN)
  81. set(CMAKE_EXECUTABLE_SUFFIX .html)
  82. set(IS_EMSCRIPTEN_BUILD ON)
  83. set(USE_FLAGS " -sUSE_SDL=2 -sFULL_ES2=1 -sUSE_SDL_IMAGE=2 -sUSE_FREETYPE=2 -sUSE_PTHREADS=1 -sDISABLE_EXCEPTION_CATCHING=0 --preload-file ${BUILD_CONFIG_DATA_DIR} -sTOTAL_MEMORY=67108864 -sALLOW_MEMORY_GROWTH=1 -sSDL2_IMAGE_FORMATS='[\"png\",\"jpg\"]' --use-preload-plugins")
  84. if(CMAKE_BUILD_TYPE MATCHES Debug)
  85. set(USE_FLAGS "${USE_FLAGS} -fsanitize=undefined -sSAFE_HEAP=1 -sASSERTIONS=1 -sDEMANGLE_SUPPORT=1")
  86. endif()
  87. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${USE_FLAGS} -sERROR_ON_UNDEFINED_SYMBOLS=0")
  88. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${USE_FLAGS} -sERROR_ON_UNDEFINED_SYMBOLS=0")
  89. set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${USE_FLAGS} -sERROR_ON_UNDEFINED_SYMBOLS=0 -lidbfs.js")
  90. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${USE_FLAGS} -sERROR_ON_UNDEFINED_SYMBOLS=0 -lidbfs.js")
  91. endif()
  92. # TODO: Add " OR ANDROID OR IOS" to this
  93. if(EMSCRIPTEN OR UBUNTU_TOUCH OR ANDROID)
  94. option(REMOVE_QUIT_BUTTON "Remove the option to quit the game (useful on mobile devices)" ON)
  95. else()
  96. option(REMOVE_QUIT_BUTTON "Remove the option to quit the game (useful on mobile devices)" OFF)
  97. endif()
  98. option(STEAM_BUILD "Prepare build for Steam" OFF)
  99. option(IS_SUPERTUX_RELEASE "Build as official SuperTux release" OFF)
  100. set(SUPERTUX_SYSTEM_NAME ${CMAKE_SYSTEM_NAME})
  101. set(VCPKG_BUILD OFF CACHE BOOL "Use dependencies installed via vcpkg (not dependency package)")
  102. set(VCPKG_APPLOCAL_DEPS ${VCPKG_BUILD} BOOL)
  103. # Detect mobile builds
  104. option(UBUNTU_TOUCH "Compile the project for an Ubuntu Touch target" OFF)
  105. # Mobile builds
  106. if(UBUNTU_TOUCH OR ANDROID)
  107. option(HIDE_NONMOBILE_OPTIONS "Hide options that are impractical on mobile devices (e. g. changing screen resolution)" ON)
  108. else()
  109. option(HIDE_NONMOBILE_OPTIONS "Hide options that are impractical on mobile devices (e. g. changing screen resolution)" OFF)
  110. endif()
  111. if(WIN32)
  112. include(SuperTux/Win32)
  113. endif()
  114. ## Check platform-dependent build options
  115. include(ConfigureChecks)
  116. ## Some additional compiler switches
  117. include(SuperTux/ClangTidy)
  118. include(SuperTux/WarningFlags)
  119. if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  120. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
  121. endif()
  122. if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  123. add_definitions(-DMACOSX)
  124. endif()
  125. ## Add lots of dependencies to compiler switches
  126. include(SuperTux/ProvideBoost)
  127. include(SuperTux/ProvideGlm)
  128. include(SuperTux/ProvideFmt)
  129. include(SuperTux/ProvideSDL2)
  130. include(SuperTux/ProvideOpenAL)
  131. include(SuperTux/ProvideOggVorbis)
  132. include(SuperTux/ProvidePhysfs)
  133. include(SuperTux/ProvideCurl)
  134. include(SuperTux/ProvideSquirrel)
  135. include(SuperTux/ProvideTinygettext)
  136. include(SuperTux/ProvideSDL2_ttf)
  137. include(SuperTux/ProvideDiscord)
  138. include(SuperTux/ProvideSexpcpp)
  139. include(SuperTux/ProvideSavePNG)
  140. include(SuperTux/ProvidePartioZip)
  141. include(SuperTux/ProvideOpenGL)
  142. ## Build stuff
  143. include(SuperTux/BuildVersion)
  144. include(SuperTux/BuildDocumentation)
  145. include(SuperTux/BuildMessagePot)
  146. include(SuperTux/BuildMiniswigWrapper)
  147. ## Build list of sources for supertux binary
  148. file(GLOB SUPERTUX_SOURCES_C RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} external/obstack/*.c external/findlocale/findlocale.c)
  149. file(GLOB SUPERTUX_SOURCES_CXX RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/*/*.cpp src/supertux/menu/*.cpp src/video/sdl/*.cpp src/video/null/*.cpp)
  150. file(GLOB SUPERTUX_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${PROJECT_BINARY_DIR}/tmp/*.rc")
  151. if(HAVE_OPENGL)
  152. file(GLOB SUPERTUX_OPENGL_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/video/gl/*.cpp)
  153. set(SUPERTUX_SOURCES_CXX ${SUPERTUX_SOURCES_CXX} ${SUPERTUX_OPENGL_SOURCES})
  154. endif()
  155. ## Sort source lists to have deterministic linking order
  156. list(SORT SUPERTUX_SOURCES_C)
  157. list(SORT SUPERTUX_SOURCES_CXX)
  158. list(SORT SUPERTUX_RESOURCES)
  159. if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/scripting/wrapper.cpp)
  160. set(SUPERTUX_SOURCES_CXX ${SUPERTUX_SOURCES_CXX} ${CMAKE_CURRENT_SOURCE_DIR}/src/scripting/wrapper.cpp)
  161. endif()
  162. ## On Windows, add an icon
  163. if(WIN32)
  164. if(MINGW)
  165. add_custom_command(
  166. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/supertux_rc.o
  167. 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)
  168. set(SUPERTUX_SOURCES_C ${SUPERTUX_SOURCES_C} ${CMAKE_CURRENT_BINARY_DIR}/supertux_rc.o)
  169. endif()
  170. endif()
  171. include(SuperTux/CompileAmalgation)
  172. ## Generate supertux executable in the right place
  173. #set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
  174. set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
  175. ## Add target for supertux binary
  176. add_library(supertux2_c OBJECT ${SUPERTUX_SOURCES_C})
  177. add_library(supertux2_lib STATIC ${CMAKE_BINARY_DIR}/version.h ${SUPERTUX_SOURCES_CXX} ${SUPERTUX_RESOURCES} $<TARGET_OBJECTS:supertux2_c>)
  178. target_include_directories(supertux2_lib PUBLIC ${CMAKE_BINARY_DIR} src/)
  179. if(WIN32)
  180. add_executable(supertux2 WIN32 src/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/data/images/engine/icons/supertux.rc)
  181. target_link_libraries(supertux2 LibSDL2main)
  182. else()
  183. add_executable(supertux2 src/main.cpp)
  184. endif()
  185. target_link_libraries(supertux2 supertux2_lib)
  186. set_target_properties(supertux2_lib PROPERTIES OUTPUT_NAME supertux2_lib)
  187. set_target_properties(supertux2_lib PROPERTIES COMPILE_FLAGS "${SUPERTUX2_EXTRA_WARNING_FLAGS}")
  188. if(EMSCRIPTEN)
  189. target_link_options(supertux2 PUBLIC -sEXPORTED_FUNCTIONS=['_main','_set_resolution','_save_config','_onDownloadProgress','_onDownloadFinished','_onDownloadError','_onDownloadAborted','_getExceptionMessage'] PUBLIC -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap'] PUBLIC -sEXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap'])
  190. endif()
  191. if(WIN32 AND NOT VCPKG_BUILD)
  192. ## Copy dlls on windows
  193. add_custom_command(TARGET supertux2_lib POST_BUILD
  194. COMMAND ${CMAKE_COMMAND} -E copy_directory
  195. "${DEPENDENCY_FOLDER}/dll"
  196. $<TARGET_FILE_DIR:supertux2_lib>)
  197. endif()
  198. ## Some additional include paths
  199. target_include_directories(supertux2_lib SYSTEM PUBLIC
  200. external/findlocale/
  201. external/obstack/
  202. )
  203. # Include altivec wrapper on ppc
  204. if(CMAKE_SYSTEM_PROCESSOR MATCHES "^ppc.*")
  205. target_include_directories(supertux2_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/ppc)
  206. endif()
  207. ## Link supertux binary with squirrel and other libraries
  208. if(NOT EMSCRIPTEN)
  209. target_link_libraries(supertux2_lib PUBLIC LibSDL2 LibSDL2_image)
  210. endif()
  211. target_link_libraries(supertux2_lib PUBLIC LibSDL2_ttf)
  212. target_link_libraries(supertux2_lib PUBLIC LibSquirrel)
  213. target_link_libraries(supertux2_lib PUBLIC LibSqstdlib)
  214. target_link_libraries(supertux2_lib PUBLIC LibTinygettext)
  215. target_link_libraries(supertux2_lib PUBLIC LibSexp)
  216. target_link_libraries(supertux2_lib PUBLIC LibSavePNG)
  217. target_link_libraries(supertux2_lib PUBLIC LibPartioZip)
  218. target_link_libraries(supertux2_lib PUBLIC LibOpenAL)
  219. target_link_libraries(supertux2_lib PUBLIC LibOggVorbis)
  220. target_link_libraries(supertux2_lib PUBLIC LibBoost)
  221. target_link_libraries(supertux2_lib PUBLIC LibGlm)
  222. target_link_libraries(supertux2_lib PUBLIC LibFmt)
  223. target_link_libraries(supertux2_lib PUBLIC LibPhysfs)
  224. if(HAVE_OPENGL)
  225. target_link_libraries(supertux2_lib PUBLIC LibOpenGL)
  226. endif()
  227. if(ENABLE_DISCORD)
  228. target_link_libraries(supertux2_lib PUBLIC LibDiscord)
  229. endif()
  230. if(NOT EMSCRIPTEN)
  231. target_link_libraries(supertux2_lib PUBLIC LibCurl)
  232. endif()
  233. ## Install stuff
  234. include(SuperTux/BuildInstall)
  235. ## Create config.h now that INSTALL_SUBDIR_* have been set.
  236. configure_file(config.h.cmake ${CMAKE_BINARY_DIR}/config.h )
  237. ## Configure main menu logo
  238. if(("${SUPERTUX_VERSION_STRING}" MATCHES "^v[0-9]+\\.[0-9]+\\.[0-9]+$") OR IS_SUPERTUX_RELEASE OR STEAM_BUILD)
  239. set(LOGO_FILE "logo_final.sprite")
  240. else()
  241. set(LOGO_FILE "logo.sprite")
  242. endif()
  243. configure_file(data/levels/misc/menu.stl.in ${CMAKE_BINARY_DIR}/data/levels/misc/menu.stl )
  244. ## Build tests
  245. include(SuperTux/BuildTests)
  246. ## CPack/Installation-specific stuff
  247. include(SuperTux/BuildCPack)
  248. # move some config clutter to the advanced section
  249. mark_as_advanced(
  250. INSTALL_SUBDIR_BIN
  251. INSTALL_SUBDIR_SHARE
  252. INSTALL_SUBDIR_DOC
  253. )
  254. mark_as_advanced(
  255. CMAKE_BACKWARDS_COMPATIBILITY
  256. CMAKE_BUILD_TYPE
  257. CMAKE_INSTALL_PREFIX
  258. EXECUTABLE_OUTPUT_PATH
  259. LIBRARY_OUTPUT_PATH
  260. CMAKE_OSX_ARCHITECTURES
  261. CMAKE_OSX_SYSROOT
  262. )
  263. mark_as_advanced(
  264. APPDATADIR
  265. )
  266. # EOF #