CMakeLists.txt 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. # Minimum required version greatly affect CMake behavior
  2. # So cmake_minimum_required must be called before the project()
  3. # 3.10.0 is used since it's minimal in MXE dependencies for now
  4. cmake_minimum_required(VERSION 3.10.0)
  5. project(VCMI)
  6. # TODO
  7. # macOS:
  8. # - There is problem with running fixup_bundle in main project after subdirectories.
  9. # Cmake put them after all install code of main CMakelists in cmake_install.cmake
  10. # Currently I just added extra add_subdirectory and CMakeLists.txt in osx directory to bypass that.
  11. #
  12. # MXE:
  13. # - Try to implement MXE support into BundleUtilities so we can deploy deps automatically
  14. #
  15. # Vckpg:
  16. # - Improve install code once there is better way to deploy DLLs and Qt plugins
  17. #
  18. # Other:
  19. # - Cleanup remove_directory copy_directory if performance will be a problem.
  20. # We can use some macro over copy_if_different since it's only work for single file.
  21. # - Find a way to move add_custom_command for assets deploy out of "lib/CMakeLists.txt"
  22. # - Consider to remove M_DATA_DIR, DM_BIN_DIR, DM_LIB_DIR and not use them in code as well
  23. # - Try to get rid of FOLDER override with define_property
  24. # It's used currently to make sure that 3rd-party dependencies in git submodules get proper FOLDER property
  25. # - Make FindFuzzyLite check for the right version and disable FORCE_BUNDLED_FL by default
  26. if(APPLE)
  27. if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
  28. set(APPLE_MACOS 1)
  29. else()
  30. set(APPLE_IOS 1)
  31. endif()
  32. endif()
  33. ############################################
  34. # User-provided options #
  35. ############################################
  36. if(NOT CMAKE_BUILD_TYPE)
  37. set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
  38. "Choose the type of build, options are: Debug Release RelWithDebInfo. Default is RelWithDebInfo."
  39. FORCE)
  40. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release RelWithDebInfo)
  41. endif()
  42. option(ENABLE_ERM "Enable compilation of ERM scripting module" OFF)
  43. option(ENABLE_LUA "Enable compilation of LUA scripting module" OFF)
  44. option(ENABLE_LAUNCHER "Enable compilation of launcher" ON)
  45. option(ENABLE_EDITOR "Enable compilation of map editor" ON)
  46. option(ENABLE_TRANSLATIONS "Enable generation of translations for launcher and editor" ON)
  47. option(ENABLE_NULLKILLER_AI "Enable compilation of Nullkiller AI library" ON)
  48. if(APPLE_IOS)
  49. set(BUNDLE_IDENTIFIER_PREFIX "" CACHE STRING "Bundle identifier prefix")
  50. set(APP_DISPLAY_NAME "VCMI" CACHE STRING "App name on the home screen")
  51. else()
  52. option(ENABLE_TEST "Enable compilation of unit tests" OFF)
  53. endif()
  54. if(NOT ${CMAKE_VERSION} VERSION_LESS "3.16.0")
  55. option(ENABLE_PCH "Enable compilation using precompiled headers" ON)
  56. endif(NOT ${CMAKE_VERSION} VERSION_LESS "3.16.0")
  57. option(ENABLE_GITVERSION "Enable Version.cpp with Git commit hash" ON)
  58. option(ENABLE_DEBUG_CONSOLE "Enable debug console for Windows builds" ON)
  59. option(ENABLE_STRICT_COMPILATION "Treat all compiler warnings as errors" OFF)
  60. option(ENABLE_MULTI_PROCESS_BUILDS "Enable /MP flag for MSVS solution" ON)
  61. option(ENABLE_SINGLE_APP_BUILD "Builds client and server as single executable" OFF)
  62. option(COPY_CONFIG_ON_BUILD "Copies config folder into output directory at building phase" ON)
  63. # Used for Snap packages and also useful for debugging
  64. if(NOT APPLE_IOS)
  65. option(ENABLE_MONOLITHIC_INSTALL "Install everything in single directory on Linux and Mac" OFF)
  66. endif()
  67. # Allow to pass package name from Travis CI
  68. set(PACKAGE_NAME_SUFFIX "" CACHE STRING "Suffix for CPack package name")
  69. set(PACKAGE_FILE_NAME "" CACHE STRING "Override for CPack package filename")
  70. if(APPLE_IOS AND NOT ENABLE_SINGLE_APP_BUILD)
  71. set(ENABLE_SINGLE_APP_BUILD ON)
  72. endif()
  73. # ERM depends on LUA implicitly
  74. if(ENABLE_ERM AND NOT ENABLE_LUA)
  75. set(ENABLE_LUA ON)
  76. endif()
  77. # We don't want to deploy assets into build directory for iOS build
  78. if(APPLE_IOS AND COPY_CONFIG_ON_BUILD)
  79. set(COPY_CONFIG_ON_BUILD OFF)
  80. endif()
  81. ############################################
  82. # Miscellaneous options #
  83. ############################################
  84. set(CMAKE_MODULE_PATH ${CMAKE_HOME_DIRECTORY}/cmake_modules ${PROJECT_SOURCE_DIR}/CI)
  85. # Contains custom functions and macros, but don't altering any options
  86. include(VCMIUtils)
  87. include(VersionDefinition)
  88. vcmi_print_important_variables()
  89. # Options to enable folders in CMake generated projects for Visual Studio, Xcode, etc
  90. # Very useful to put 3rd-party libraries such as Minizip, GoogleTest and FuzzyLite in their own folders
  91. set_property(GLOBAL PROPERTY USE_FOLDERS TRUE)
  92. # Make FOLDER property inheritable
  93. # So when we set FOLDER property on AI directory all and targets inside will inherit it
  94. #
  95. # Important! This trick depend on undefined behavior since we override CMake own property.
  96. # In same time define_property documentation states it's function for custom properties.
  97. define_property(
  98. TARGET
  99. PROPERTY FOLDER
  100. INHERITED
  101. BRIEF_DOCS "Set the folder name."
  102. FULL_DOCS "Use to organize targets in an IDE."
  103. )
  104. # Generate Version.cpp
  105. if(ENABLE_GITVERSION)
  106. add_custom_target(update_version ALL
  107. BYPRODUCTS "Version.cpp"
  108. COMMAND ${CMAKE_COMMAND} -DGIT_SHA1="${GIT_SHA1}" -P "${PROJECT_SOURCE_DIR}/cmake_modules/Version.cmake"
  109. )
  110. else()
  111. add_definitions(-DVCMI_NO_EXTRA_VERSION)
  112. endif(ENABLE_GITVERSION)
  113. # Precompiled header configuration
  114. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0 )
  115. set(ENABLE_PCH OFF) # broken
  116. endif()
  117. if( ${CMAKE_VERSION} VERSION_LESS "3.16.0")
  118. set(ENABLE_PCH OFF) #not supported
  119. endif()
  120. if(ENABLE_PCH)
  121. macro(enable_pch name)
  122. target_precompile_headers(${name} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:<StdInc.h$<ANGLE-R>>)
  123. endmacro(enable_pch)
  124. else()
  125. macro(enable_pch ignore)
  126. endmacro(enable_pch)
  127. endif()
  128. ############################################
  129. # Documentation section #
  130. ############################################
  131. include(UseDoxygen OPTIONAL)
  132. ############################################
  133. # Compile and linking options #
  134. ############################################
  135. #Enable C++14 Globally
  136. set (CMAKE_CXX_STANDARD 14)
  137. #General visibility options
  138. set(CMAKE_CXX_VISIBILITY_PRESET hidden)
  139. set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
  140. #Global fallback mapping
  141. # RelWithDebInfo falls back to Release, then MinSizeRel
  142. set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO RelWithDebInfo Release MinSizeRel "")
  143. # MinSizeRel falls back to Release, then RelWithDebInfo
  144. set(CMAKE_MAP_IMPORTED_CONFIG_MINSIZEREL MinSizeRel Release RelWithDebInfo "")
  145. # Release falls back to RelWithDebInfo, then MinSizeRel
  146. set(CMAKE_MAP_IMPORTED_CONFIG_RELEASE Release RelWithDebInfo MinSizeRel "")
  147. set(CMAKE_XCODE_ATTRIBUTE_APP_DISPLAY_NAME ${APP_DISPLAY_NAME})
  148. set(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES)
  149. set(CMAKE_XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT[variant=Debug] dwarf)
  150. set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE NO)
  151. set(CMAKE_XCODE_ATTRIBUTE_ENABLE_NS_ASSERTIONS NO)
  152. set(CMAKE_XCODE_ATTRIBUTE_ENABLE_NS_ASSERTIONS[variant=Debug] YES)
  153. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_64_TO_32_BIT_CONVERSION NO)
  154. set(CMAKE_XCODE_ATTRIBUTE_MARKETING_VERSION ${APP_SHORT_VERSION})
  155. set(CMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH NO)
  156. set(CMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH[variant=Debug] YES)
  157. if(ENABLE_SINGLE_APP_BUILD)
  158. add_definitions(-DSINGLE_PROCESS_APP=1)
  159. endif()
  160. if(APPLE_IOS)
  161. set(CMAKE_MACOSX_RPATH 1)
  162. set(CMAKE_OSX_DEPLOYMENT_TARGET 12.0)
  163. if(NOT USING_CONAN)
  164. list(APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_PREFIX_PATH}") # required for Boost
  165. set(CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH FALSE)
  166. set(CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH FALSE)
  167. endif()
  168. set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED NO)
  169. set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED_FOR_APPS YES)
  170. set(CMAKE_XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "${BUNDLE_IDENTIFIER_PREFIX}.$(PRODUCT_NAME)")
  171. set(CMAKE_XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2")
  172. endif()
  173. if(MINGW OR MSVC)
  174. # Windows Vista or newer for FuzzyLite 6 to compile
  175. add_definitions(-D_WIN32_WINNT=0x0600)
  176. #delete lib prefix for dlls (libvcmi -> vcmi)
  177. set(CMAKE_SHARED_LIBRARY_PREFIX "")
  178. if(MSVC)
  179. add_definitions(-DBOOST_ALL_NO_LIB)
  180. add_definitions(-DBOOST_ALL_DYN_LINK)
  181. set(Boost_USE_STATIC_LIBS OFF)
  182. # Don't link with SDLMain
  183. if(ENABLE_DEBUG_CONSOLE)
  184. set(SDL2_BUILDING_LIBRARY ON)
  185. add_definitions(-DVCMI_WITH_DEBUG_CONSOLE)
  186. endif()
  187. # Suppress warnings
  188. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  189. add_definitions(-D_SCL_SECURE_NO_WARNINGS)
  190. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
  191. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4250") # 4250: 'class1' : inherits 'class2::member' via dominance
  192. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") # 4251: class 'xxx' needs to have dll-interface to be used by clients of class 'yyy'
  193. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4244") # 4244: conversion from 'xxx' to 'yyy', possible loss of data
  194. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4267") # 4267: conversion from 'xxx' to 'yyy', possible loss of data
  195. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4275") # 4275: non dll-interface class 'xxx' used as base for dll-interface class
  196. #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4800") # 4800: implicit conversion from 'xxx' to bool. Possible information loss
  197. if(ENABLE_STRICT_COMPILATION)
  198. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX") # Treats all compiler warnings as errors
  199. endif()
  200. if(ENABLE_MULTI_PROCESS_BUILDS)
  201. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
  202. endif()
  203. # Workaround: Visual Studio has issues with exports of classes that inherit templates
  204. # https://stackoverflow.com/questions/44960760/msvc-dll-exporting-class-that-inherits-from-template-cause-lnk2005-already-defin
  205. # Reported to Microsoft here:
  206. # https://developercommunity.visualstudio.com/content/problem/224597/linker-failing-because-of-multiple-definitions-of.html
  207. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /FORCE:MULTIPLE")
  208. # Required at least for compatibility with Boost 1.68 on Vcpkg
  209. set(SYSTEM_LIBS ${SYSTEM_LIBS} bcrypt)
  210. endif(MSVC)
  211. if(MINGW)
  212. set(SYSTEM_LIBS ${SYSTEM_LIBS} ole32 oleaut32 ws2_32 mswsock dbghelp bcrypt)
  213. # Check for iconv (may be needed for Boost.Locale)
  214. include(CheckLibraryExists)
  215. check_library_exists(iconv libiconv_open "" ICONV_FOUND)
  216. if(ICONV_FOUND)
  217. set(SYSTEM_LIBS ${SYSTEM_LIBS} iconv)
  218. endif()
  219. # Prevent compiler issues when building Debug
  220. # Assembler might fail with "too many sections"
  221. # With big-obj or 64-bit build will take hours
  222. if(CMAKE_BUILD_TYPE MATCHES Debug)
  223. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og")
  224. endif()
  225. endif(MINGW)
  226. endif(MINGW OR MSVC)
  227. if(CMAKE_COMPILER_IS_GNUCXX OR NOT WIN32)
  228. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
  229. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpointer-arith")
  230. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wuninitialized")
  231. if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0 OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
  232. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmismatched-tags")
  233. endif()
  234. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter") # low chance of valid reports, a lot of emitted warnings
  235. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-switch") # large number of false-positives, disabled
  236. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-reorder") # large number of noise, low chance of any significant issues
  237. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare") # low chance of any significant issues
  238. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-varargs") # emitted in fuzzylite headers, disabled
  239. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
  240. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas") # emitted only by ancient gcc 5.5 in MXE build, remove after upgrade
  241. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-variable") # emitted only by ancient gcc 5.5 in MXE build, remove after upgrade
  242. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-maybe-uninitialized") # emitted only by ancient gcc 5.5 in MXE build, remove after upgrade
  243. endif()
  244. if(ENABLE_STRICT_COMPILATION)
  245. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
  246. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=array-bounds") # false positives in boost::multiarray during release build, keep as warning-only
  247. endif()
  248. if(UNIX)
  249. set(SYSTEM_LIBS ${SYSTEM_LIBS} ${CMAKE_DL_LIBS})
  250. endif()
  251. endif()
  252. # Check if some platform-specific libraries are needed for linking
  253. if(NOT WIN32 AND NOT APPLE_IOS)
  254. include(CheckLibraryExists)
  255. # Shared memory functions used by Boost.Interprocess
  256. # FindBoost handle linking with pthreads, but doesn't handle this
  257. CHECK_LIBRARY_EXISTS(rt shm_open "" HAVE_RT_LIB)
  258. if(HAVE_RT_LIB)
  259. set(SYSTEM_LIBS ${SYSTEM_LIBS} rt)
  260. endif()
  261. endif()
  262. if(ENABLE_LUA)
  263. add_definitions(-DSCRIPTING_ENABLED=1)
  264. endif()
  265. ############################################
  266. # Finding packages #
  267. ############################################
  268. find_package(Boost 1.48.0 REQUIRED COMPONENTS date_time filesystem locale program_options system thread)
  269. find_package(ZLIB REQUIRED)
  270. # Conan compatibility
  271. if(TARGET zlib::zlib)
  272. add_library(ZLIB::ZLIB ALIAS zlib::zlib)
  273. endif()
  274. set(FFMPEG_COMPONENTS avutil swscale avformat avcodec)
  275. if(APPLE_IOS AND NOT USING_CONAN)
  276. list(APPEND FFMPEG_COMPONENTS swresample)
  277. endif()
  278. find_package(ffmpeg COMPONENTS ${FFMPEG_COMPONENTS})
  279. option(FORCE_BUNDLED_MINIZIP "Force bundled Minizip library" OFF)
  280. if(NOT FORCE_BUNDLED_MINIZIP)
  281. find_package(minizip)
  282. if(TARGET minizip::minizip)
  283. add_definitions(-DUSE_SYSTEM_MINIZIP)
  284. endif()
  285. endif()
  286. find_package(SDL2 REQUIRED)
  287. find_package(SDL2_image REQUIRED)
  288. if(TARGET SDL2_image::SDL2_image)
  289. add_library(SDL2::Image ALIAS SDL2_image::SDL2_image)
  290. endif()
  291. find_package(SDL2_mixer REQUIRED)
  292. if(TARGET SDL2_mixer::SDL2_mixer)
  293. add_library(SDL2::Mixer ALIAS SDL2_mixer::SDL2_mixer)
  294. endif()
  295. find_package(SDL2_ttf REQUIRED)
  296. if(TARGET SDL2_ttf::SDL2_ttf)
  297. add_library(SDL2::TTF ALIAS SDL2_ttf::SDL2_ttf)
  298. endif()
  299. if(ENABLE_LAUNCHER OR ENABLE_EDITOR)
  300. # Widgets finds its own dependencies (QtGui and QtCore).
  301. find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Network)
  302. find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Network)
  303. find_package(QT NAMES Qt6 Qt5 COMPONENTS LinguistTools)
  304. find_package(Qt${QT_VERSION_MAJOR} COMPONENTS LinguistTools)
  305. if(NOT Qt${QT_VERSION_MAJOR}LinguistTools_DIR)
  306. set(ENABLE_TRANSLATIONS OFF)
  307. endif()
  308. if(ENABLE_TRANSLATIONS)
  309. add_definitions(-DENABLE_QT_TRANSLATIONS)
  310. endif()
  311. endif()
  312. if(ENABLE_NULLKILLER_AI)
  313. find_package(TBB REQUIRED)
  314. endif()
  315. if(ENABLE_LUA)
  316. find_package(luajit)
  317. # MXE paths hardcoded for current dependencies pack - tried and could not make it work another way
  318. if((MINGW) AND (${CMAKE_CROSSCOMPILING}) AND (DEFINED MSYS) AND (NOT TARGET luajit::luajit))
  319. add_library(luajit::luajit STATIC IMPORTED)
  320. set_target_properties(luajit::luajit PROPERTIES
  321. INTERFACE_INCLUDE_DIRECTORIES "/usr/lib/mxe/usr/i686-w64-mingw32.static/include/luajit-2.0")
  322. set_target_properties(luajit::luajit PROPERTIES
  323. IMPORTED_LOCATION "/usr/lib/mxe/usr/i686-w64-mingw32.static/lib/libluajit-5.1.a")
  324. endif()
  325. if(TARGET luajit::luajit)
  326. message(STATUS "Using LuaJIT provided by system")
  327. else()
  328. message(STATUS "Cannot find LuaJIT! Fallback to using usual Lua.")
  329. find_package(Lua REQUIRED)
  330. if(Lua_FOUND)
  331. add_library(luajit::luajit UNKNOWN IMPORTED)
  332. set_target_properties(luajit::luajit PROPERTIES
  333. INTERFACE_INCLUDE_DIRECTORIES "${LUA_INCLUDE_DIR}")
  334. set_target_properties(luajit::luajit PROPERTIES
  335. IMPORTED_LOCATION "${LUA_LIBRARIES}")
  336. endif()
  337. endif()
  338. endif()
  339. ############################################
  340. # Output directories #
  341. ############################################
  342. if(WIN32) # on Win everything goes into H3 root directory
  343. set(BIN_DIR "." CACHE STRING "Where to install binaries")
  344. set(LIB_DIR "." CACHE STRING "Where to install main library")
  345. set(DATA_DIR "." CACHE STRING "Where to install data files")
  346. elseif(APPLE)
  347. # includes lib path which determines where to install shared libraries (either /lib or /lib64)
  348. include(GNUInstallDirs)
  349. set(CMAKE_INSTALL_RPATH "@executable_path/../Frameworks")
  350. if(ENABLE_MONOLITHIC_INSTALL)
  351. set(BIN_DIR "." CACHE STRING "Where to install binaries")
  352. set(LIB_DIR "." CACHE STRING "Where to install main library")
  353. set(DATA_DIR "." CACHE STRING "Where to install data files")
  354. else()
  355. if(APPLE_MACOS)
  356. set(APP_BUNDLE_DIR "${CMAKE_PROJECT_NAME}.app")
  357. set(APP_BUNDLE_CONTENTS_DIR "${APP_BUNDLE_DIR}/Contents")
  358. set(APP_BUNDLE_BINARY_DIR "${APP_BUNDLE_CONTENTS_DIR}/MacOS")
  359. set(APP_BUNDLE_RESOURCES_DIR "${APP_BUNDLE_CONTENTS_DIR}/Resources")
  360. set(BIN_DIR "${APP_BUNDLE_BINARY_DIR}" CACHE STRING "Where to install binaries")
  361. set(LIB_DIR "${APP_BUNDLE_CONTENTS_DIR}/Frameworks" CACHE STRING "Where to install main library")
  362. set(DATA_DIR "${APP_BUNDLE_RESOURCES_DIR}/Data" CACHE STRING "Where to install data files")
  363. else()
  364. set(LIB_DIR "Frameworks")
  365. set(DATA_DIR ".")
  366. endif()
  367. endif()
  368. else()
  369. # includes lib path which determines where to install shared libraries (either /lib or /lib64)
  370. include(GNUInstallDirs)
  371. if(ENABLE_MONOLITHIC_INSTALL)
  372. set(BIN_DIR "." CACHE STRING "Where to install binaries")
  373. set(LIB_DIR "." CACHE STRING "Where to install main library")
  374. set(DATA_DIR "." CACHE STRING "Where to install data files")
  375. # following constants only used for platforms using XDG (Linux, BSD, etc)
  376. add_definitions(-DM_DATA_DIR="${DATA_DIR}")
  377. add_definitions(-DM_BIN_DIR="${BIN_DIR}")
  378. add_definitions(-DM_LIB_DIR="${LIB_DIR}")
  379. set(CMAKE_INSTALL_RPATH "$ORIGIN/")
  380. else()
  381. if(NOT BIN_DIR)
  382. set(BIN_DIR "${CMAKE_INSTALL_BINDIR}" CACHE STRING "Where to install binaries")
  383. endif()
  384. if(NOT LIB_DIR)
  385. set(LIB_DIR "${CMAKE_INSTALL_LIBDIR}/vcmi" CACHE STRING "Where to install main library")
  386. endif()
  387. if(NOT DATA_DIR)
  388. set(DATA_DIR "${CMAKE_INSTALL_DATAROOTDIR}/vcmi" CACHE STRING "Where to install data files")
  389. endif()
  390. # following constants only used for platforms using XDG (Linux, BSD, etc)
  391. add_definitions(-DM_DATA_DIR="${CMAKE_INSTALL_PREFIX}/${DATA_DIR}")
  392. add_definitions(-DM_BIN_DIR="${CMAKE_INSTALL_PREFIX}/${BIN_DIR}")
  393. add_definitions(-DM_LIB_DIR="${CMAKE_INSTALL_PREFIX}/${LIB_DIR}")
  394. set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${LIB_DIR}")
  395. endif()
  396. endif()
  397. # iOS has flat libs directory structure
  398. if(APPLE_IOS)
  399. set(AI_LIB_DIR "${LIB_DIR}")
  400. set(SCRIPTING_LIB_DIR "${LIB_DIR}")
  401. else()
  402. set(AI_LIB_DIR "${LIB_DIR}/AI")
  403. set(SCRIPTING_LIB_DIR "${LIB_DIR}/scripting")
  404. endif()
  405. #######################################
  406. # Add subdirectories #
  407. #######################################
  408. if(APPLE_IOS)
  409. add_subdirectory(ios)
  410. endif()
  411. include(VCMI_lib)
  412. add_subdirectory(lib)
  413. set(VCMI_LIB_TARGET vcmi)
  414. if(ENABLE_SINGLE_APP_BUILD)
  415. add_subdirectory(lib_server)
  416. endif()
  417. if(ENABLE_ERM)
  418. add_subdirectory(scripting/erm)
  419. endif()
  420. if(ENABLE_LUA)
  421. add_subdirectory(scripting/lua)
  422. endif()
  423. if(NOT TARGET minizip::minizip)
  424. add_subdirectory_with_folder("3rdparty" lib/minizip)
  425. add_library(minizip::minizip ALIAS minizip)
  426. endif()
  427. if(ENABLE_LAUNCHER)
  428. add_subdirectory(launcher)
  429. endif()
  430. if(ENABLE_EDITOR)
  431. add_subdirectory(mapeditor)
  432. endif()
  433. add_subdirectory(client)
  434. add_subdirectory(server)
  435. add_subdirectory_with_folder("AI" AI)
  436. if(ENABLE_TEST)
  437. enable_testing()
  438. add_subdirectory(test)
  439. endif()
  440. #######################################
  441. # Installation section #
  442. #######################################
  443. install(DIRECTORY config DESTINATION ${DATA_DIR})
  444. install(DIRECTORY Mods DESTINATION ${DATA_DIR})
  445. if(ENABLE_LUA)
  446. install(DIRECTORY scripts DESTINATION ${DATA_DIR})
  447. endif()
  448. # that script is useless for Windows and iOS
  449. if(NOT WIN32 AND NOT APPLE_IOS)
  450. install(FILES vcmibuilder DESTINATION ${BIN_DIR} PERMISSIONS
  451. OWNER_WRITE OWNER_READ OWNER_EXECUTE
  452. GROUP_READ GROUP_EXECUTE
  453. WORLD_READ WORLD_EXECUTE)
  454. endif()
  455. if(WIN32)
  456. file(GLOB dep_files
  457. ${dep_files}
  458. "${CMAKE_FIND_ROOT_PATH}/bin/*.dll")
  459. if((${CMAKE_CROSSCOMPILING}) AND (DEFINED MSYS))
  460. message(STATUS "Detected MXE build")
  461. elseif(CMAKE_BUILD_TYPE MATCHES Debug)
  462. # Copy debug versions of libraries if build type is debug
  463. set(debug_postfix d)
  464. endif()
  465. if(ENABLE_LAUNCHER OR ENABLE_EDITOR)
  466. get_target_property(QtCore_location Qt${QT_VERSION_MAJOR}::Core LOCATION)
  467. get_filename_component(Qtbin_folder ${QtCore_location} PATH)
  468. file(GLOB dep_files
  469. ${dep_files}
  470. ${Qtbin_folder}/Qt5Core${debug_postfix}.dll
  471. ${Qtbin_folder}/Qt5Gui${debug_postfix}.dll
  472. ${Qtbin_folder}/Qt5Widgets${debug_postfix}.dll
  473. ${Qtbin_folder}/Qt5Network${debug_postfix}.dll
  474. ${Qtbin_folder}/icu*.dll)
  475. get_target_property(integration_type Qt${QT_VERSION_MAJOR}::QWindowsIntegrationPlugin TYPE)
  476. if(NOT(integration_type STREQUAL "INTERFACE_LIBRARY"))
  477. get_target_property(integration_loc Qt${QT_VERSION_MAJOR}::QWindowsIntegrationPlugin LOCATION)
  478. install(
  479. FILES ${integration_loc}
  480. DESTINATION ${BIN_DIR}/platforms
  481. )
  482. install(
  483. FILES "$<TARGET_FILE:Qt${QT_VERSION_MAJOR}::QWindowsVistaStylePlugin>"
  484. DESTINATION ${BIN_DIR}/styles)
  485. endif()
  486. endif()
  487. install(FILES ${dep_files} DESTINATION ${BIN_DIR})
  488. endif(WIN32)
  489. #######################################
  490. # Packaging section #
  491. #######################################
  492. set(CPACK_PACKAGE_VERSION_MAJOR ${VCMI_VERSION_MAJOR})
  493. set(CPACK_PACKAGE_VERSION_MINOR ${VCMI_VERSION_MINOR})
  494. set(CPACK_PACKAGE_VERSION_PATCH ${VCMI_VERSION_PATCH})
  495. # vcmi does not have "patch version" in version string
  496. set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}")
  497. #TODO: remove version from Global.h and use this one as define?
  498. set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY 0)
  499. if("${PACKAGE_NAME_SUFFIX}" STREQUAL "")
  500. set(CPACK_PACKAGE_NAME "VCMI")
  501. else()
  502. set(CPACK_PACKAGE_NAME "VCMI ${PACKAGE_NAME_SUFFIX}")
  503. endif()
  504. if("${PACKAGE_FILE_NAME}" STREQUAL "")
  505. set(CPACK_PACKAGE_FILE_NAME "vcmi-${CPACK_PACKAGE_VERSION}")
  506. else()
  507. set(CPACK_PACKAGE_FILE_NAME "${PACKAGE_FILE_NAME}")
  508. endif()
  509. set(CPACK_PACKAGE_VENDOR "VCMI team")
  510. if(WIN32)
  511. # Note: due to NSI script generation process all of the backward slashes here are useful
  512. set(CPACK_MONOLITHIC_INSTALL 1)
  513. set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/license.txt")
  514. set(CPACK_PACKAGE_INSTALL_DIRECTORY "${CPACK_PACKAGE_NAME}")
  515. if("${PACKAGE_NAME_SUFFIX}" STREQUAL "")
  516. set(CPACK_NSIS_PACKAGE_NAME "VCMI ${CPACK_PACKAGE_VERSION}")
  517. else()
  518. set(CPACK_NSIS_PACKAGE_NAME "VCMI ${CPACK_PACKAGE_VERSION} ${PACKAGE_NAME_SUFFIX} ")
  519. endif()
  520. set(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES")
  521. if(ENABLE_LAUNCHER)
  522. set(CPACK_PACKAGE_EXECUTABLES "VCMI_launcher;VCMI")
  523. set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS " CreateShortCut \\\"$DESKTOP\\\\VCMI.lnk\\\" \\\"$INSTDIR\\\\VCMI_launcher.exe\\\"")
  524. else()
  525. set(CPACK_PACKAGE_EXECUTABLES "VCMI_client;VCMI")
  526. set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS " CreateShortCut \\\"$DESKTOP\\\\VCMI.lnk\\\" \\\"$INSTDIR\\\\VCMI_client.exe\\\"")
  527. endif()
  528. set(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS " Delete \\\"$DESKTOP\\\\VCMI.lnk\\\" ")
  529. # set the install/unistall icon used for the installer itself
  530. # There is a bug in NSI that does not handle full unix paths properly.
  531. set(CPACK_NSIS_MUI_ICON "${CMAKE_CURRENT_SOURCE_DIR}/client\\\\vcmi.ico")
  532. set(CPACK_NSIS_MUI_UNIICON "${CMAKE_CURRENT_SOURCE_DIR}/client\\\\vcmi.ico")
  533. # set the package header icon for MUI
  534. set(CPACK_PACKAGE_ICON "${CMAKE_SOURCE_DIR}/client\\\\vcmi.ico")
  535. set(CPACK_NSIS_MENU_LINKS "http://vcmi.eu/" "VCMI Web Site")
  536. set(CPACK_NSIS_INSTALLED_ICON_NAME "VCMI_client.exe")
  537. set(CPACK_NSIS_COMPRESSOR "/SOLID lzma")
  538. set(CPACK_NSIS_DISPLAY_NAME "${CPACK_NSIS_PACKAGE_NAME}, open-source engine for Heroes of Might and Magic III ")
  539. set(CPACK_NSIS_HELP_LINK "http://vcmi.eu/")
  540. set(CPACK_NSIS_URL_INFO_ABOUT "http://vcmi.eu/")
  541. set(CPACK_NSIS_CONTACT @CPACK_PACKAGE_CONTACT@)
  542. set(CPACK_NSIS_EXECUTABLES_DIRECTORY ".")
  543. # Use BundleUtilities to fix build when Vcpkg is used and disable it for MXE
  544. if(NOT (${CMAKE_CROSSCOMPILING}))
  545. add_subdirectory(win)
  546. endif()
  547. elseif(APPLE_MACOS AND NOT ENABLE_MONOLITHIC_INSTALL)
  548. set(CPACK_MONOLITHIC_INSTALL 1)
  549. set(CPACK_GENERATOR "DragNDrop")
  550. set(CPACK_DMG_BACKGROUND_IMAGE "${CMAKE_SOURCE_DIR}/osx/dmg_background.png")
  551. # CMake code for CPACK_DMG_DS_STORE executed before DS_STORE_SETUP_SCRIPT
  552. # So we can keep both enabled and this shouldn't hurt
  553. # set(CPACK_DMG_DS_STORE "${CMAKE_SOURCE_DIR}/osx/dmg_DS_Store")
  554. set(CPACK_DMG_DS_STORE_SETUP_SCRIPT "${CMAKE_SOURCE_DIR}/osx/DS_Store_Setup.scpt")
  555. # Always use "VCMI" as volume name so full path will be /Volumes/VCMI/
  556. # Otherwise DMG background image wouldn't work
  557. # Pre-generated DS_Store use absolute path to background image
  558. set(CPACK_DMG_VOLUME_NAME "${CMAKE_PROJECT_NAME}")
  559. include(GetGitRevisionDescription)
  560. get_git_head_revision(GIT_REFSPEC GIT_SHA1)
  561. string(TIMESTAMP CURRENT_YEAR "%Y")
  562. set(MACOSX_BUNDLE_NAME "${CMAKE_PROJECT_NAME}")
  563. set(MACOSX_BUNDLE_BUNDLE_NAME "${CMAKE_PROJECT_NAME}")
  564. set(MACOSX_BUNDLE_COPYRIGHT "Copyright © 2007-${CURRENT_YEAR} VCMI team")
  565. set(MACOSX_BUNDLE_GUI_IDENTIFIER "eu.vcmi.vcmi")
  566. set(MACOSX_BUNDLE_BUNDLE_VERSION ${GIT_SHA1})
  567. set(MACOSX_BUNDLE_SHORT_VERSION_STRING ${APP_SHORT_VERSION})
  568. if(ENABLE_LAUNCHER)
  569. set(MACOSX_BUNDLE_EXECUTABLE_NAME "vcmilauncher")
  570. else()
  571. set(MACOSX_BUNDLE_EXECUTABLE_NAME "vcmiclient")
  572. endif()
  573. set(MACOSX_BUNDLE_ICON_FILE "vcmi.icns")
  574. install(FILES "${CMAKE_SOURCE_DIR}/osx/vcmi.icns" DESTINATION ${APP_BUNDLE_RESOURCES_DIR})
  575. configure_file("${CMAKE_SOURCE_DIR}/osx/Info.plist.in" "${CMAKE_BINARY_DIR}/Info.plist")
  576. install(FILES "${CMAKE_BINARY_DIR}/Info.plist" DESTINATION ${APP_BUNDLE_CONTENTS_DIR})
  577. # Bundle fixing code must be in separate directory to be executed after all other install code
  578. add_subdirectory(osx)
  579. elseif(APPLE_IOS)
  580. set(CPACK_GENERATOR ZIP)
  581. set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)
  582. set(CPACK_INSTALL_CMAKE_PROJECTS "${CMAKE_CURRENT_BINARY_DIR};${CMAKE_PROJECT_NAME};app;/")
  583. else()
  584. set(CPACK_GENERATOR TGZ)
  585. endif()
  586. include(CPack)