CMakeLists.txt 26 KB

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