1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/CMakeLists.txt
Arseniy Shestakov 40af43c46e CMake: massive rework for Mac build and other improvements
- macOS: RPATH-related code all removed or disabled
- macOS: new osx/CMakeLists.txt to run some install-code running after all subdirectories
- Assets copying into the runtime output directory implemented for Mac and Linux development
2017-08-16 16:10:07 +03:00

328 lines
11 KiB
CMake

project(VCMI)
# TODO
# - macOS: there is problem with running fixup_bundle in main project after subdirectories.
# Cmake put them after all install code of main CMakelists in cmake_install.cmake
# Currently I just added extra add_subdirectory and CMakeLists.txt in osx directory to bypass that.
# - macOS: fix install with built-in minizip
# - macOS: try to fix build with RPATH.
# Currently if CMAKE_MACOSX_RPATH=1 then AI libs unable to find @rpath/libvcmi.dylib
# I tried to set few different INSTALL_RPATH for all targets in AI directory, but nothing worked.
# - build: cleanup remove_directory copy_directory mess.
# When I was trying to fix it copy_if_different failed to work for me.
# - build: and find a way to move add_custom_command for assets deploy out of "lib/CMakeLists.txt"
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo. Default is RelWithDebInfo."
FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release RelWithDebInfo)
endif()
set(CMAKE_MODULE_PATH ${CMAKE_HOME_DIRECTORY}/cmake_modules)
include(VCMIUtils)
set(VCMI_VERSION_MAJOR 0)
set(VCMI_VERSION_MINOR 99)
set(VCMI_VERSION_PATCH 0)
option(ENABLE_ERM "Enable compilation of ERM scripting module" OFF)
option(ENABLE_LAUNCHER "Enable compilation of launcher" ON)
option(ENABLE_TEST "Enable compilation of unit tests" ON)
option(ENABLE_PCH "Enable compilation using precompiled headers" ON)
# Useful for debugging
option(ENABLE_MONOLITHIC_INSTALL "Install everything in single directory on Linux and Mac" OFF)
# Allow to pass package name from Travis CI
set(PACKAGE_NAME_SUFFIX "" CACHE STRING "Suffix for CPack package name")
set(PACKAGE_FILE_NAME "" CACHE STRING "Override for CPack package filename")
if(ENABLE_PCH)
# Cotire require CMake 2.8.12
cmake_minimum_required(VERSION 2.8.12)
elseif(ENABLE_LAUNCHER)
# Some of Qt5 linking policies require 2.8.11
cmake_minimum_required(VERSION 2.8.11)
elseif(ENABLE_TEST)
# For whatever reason test requirement is 2.8.7
cmake_minimum_required(VERSION 2.8.7)
else()
cmake_minimum_required(VERSION 2.6)
endif()
# Find better place for this
if(APPLE)
set(CMAKE_MACOSX_RPATH 0)
endif()
############################################
# Documentation section #
############################################
include(UseDoxygen OPTIONAL)
############################################
# Building section #
############################################
if(WIN32)
add_definitions(-DBOOST_THREAD_USE_LIB)
# Windows Vista or newer for FuzzyLite 6 to compile
add_definitions(-D_WIN32_WINNT=0x0600)
set(SYSTEM_LIBS ${SYSTEM_LIBS} ole32 oleaut32 ws2_32 mswsock dbghelp)
#delete lib prefix for dlls (libvcmi -> vcmi)
set(CMAKE_SHARED_LIBRARY_PREFIX "")
if(MINGW)
#MinGW: check for iconv (may be needed for boost.locale)
#include(CheckLibraryExists)
#check_library_exists(iconv libiconv_open "" ICONV_FOUND)
#if(ICONV_FOUND)
# set(SYSTEM_LIBS ${SYSTEM_LIBS} iconv)
#endif()
#MinGW: use O1 to prevent compiler crash in some cases
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1")
endif()
if(MSVC)
#MSVC: Fix problems with linking
add_definitions(-DBOOST_ALL_NO_LIB)
set(Boost_USE_STATIC_LIBS OFF)
#MSVC: Don't link with SDLMain
#set(SDL2_BUILDING_LIBRARY ON)
#MSVC: Suppress warnings
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /wd4251")
endif()
endif(WIN32)
if(NOT WIN32)
include(CheckLibraryExists)
#check if some platform-specific libraries are needed for linking
CHECK_LIBRARY_EXISTS(rt shm_open "" HAVE_RT_LIB)
if(HAVE_RT_LIB)
set(SYSTEM_LIBS ${SYSTEM_LIBS} rt)
endif()
endif()
set(SYSTEM_LIBS ${SYSTEM_LIBS} ${CMAKE_DL_LIBS})
set(FFmpeg_FIND_COMPONENTS AVFORMAT SWSCALE)
find_package(Boost 1.48.0 COMPONENTS date_time filesystem locale program_options system thread REQUIRED)
find_package(ZLIB REQUIRED)
find_package(FFmpeg REQUIRED)
find_package(Minizip)
if(MINIZIP_FOUND)
add_definitions(-DUSE_SYSTEM_MINIZIP)
endif()
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_mixer REQUIRED)
find_package(SDL2_ttf REQUIRED)
set(SDL_INCLUDE_DIR "${SDL2_INCLUDE_DIR}")
set(SDLTTF_INCLUDE_DIR "${SDL2_TTF_INCLUDE_DIR}")
set(SDLIMAGE_INCLUDE_DIR "${SDL2_IMAGE_INCLUDE_DIR}")
set(SDLMIXER_INCLUDE_DIR "${SDL2_MIXER_INCLUDE_DIR}")
set(SDL_LIBRARY "${SDL2_LIBRARY}")
set(SDLTTF_LIBRARY "${SDL2_TTF_LIBRARY}")
set(SDLIMAGE_LIBRARY "${SDL2_IMAGE_LIBRARY}")
set(SDLMIXER_LIBRARY "${SDL2_MIXER_LIBRARY}")
include(cotire)
if(ENABLE_LAUNCHER)
# Widgets finds its own dependencies (QtGui and QtCore).
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR NOT WIN32) #so far all *nix compilers support such parameters
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CLANG_SPECIFIC_FLAGS "-Wno-mismatched-tags -Wno-unknown-warning-option")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -Wextra -Wpointer-arith -Wno-strict-aliasing -Wno-switch -Wno-sign-compare -Wno-unused-local-typedefs -Wno-unused-parameter -Wuninitialized -Wno-overloaded-virtual -Wno-type-limits -Wno-unknown-pragmas -Wno-reorder ${CLANG_SPECIFIC_FLAGS}")
if(UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
endif()
endif()
if(WIN32) # on Win everything goes into H3 root directory
set(BIN_DIR "." CACHE STRING "Where to install binaries")
set(LIB_DIR "." CACHE STRING "Where to install main library")
set(DATA_DIR "." CACHE STRING "Where to install data files")
elseif(APPLE)
# includes lib path which determines where to install shared libraries (either /lib or /lib64)
include(GNUInstallDirs)
if(ENABLE_MONOLITHIC_INSTALL)
set(BIN_DIR "." CACHE STRING "Where to install binaries")
set(LIB_DIR "." CACHE STRING "Where to install main library")
set(DATA_DIR "." CACHE STRING "Where to install data files")
else()
set(APP_BUNDLE_DIR "${CMAKE_PROJECT_NAME}.app")
set(APP_BUNDLE_CONTENTS_DIR "${APP_BUNDLE_DIR}/Contents")
set(APP_BUNDLE_BINARY_DIR "${APP_BUNDLE_CONTENTS_DIR}/MacOS")
set(APP_BUNDLE_RESOURCES_DIR "${APP_BUNDLE_CONTENTS_DIR}/Resources")
set(BIN_DIR "${APP_BUNDLE_BINARY_DIR}" CACHE STRING "Where to install binaries")
set(LIB_DIR "${APP_BUNDLE_BINARY_DIR}" CACHE STRING "Where to install main library")
set(DATA_DIR "${APP_BUNDLE_RESOURCES_DIR}/Data" CACHE STRING "Where to install data files")
endif()
else()
# includes lib path which determines where to install shared libraries (either /lib or /lib64)
include(GNUInstallDirs)
if(ENABLE_MONOLITHIC_INSTALL)
set(BIN_DIR "." CACHE STRING "Where to install binaries")
set(LIB_DIR "." CACHE STRING "Where to install main library")
set(DATA_DIR "." CACHE STRING "Where to install data files")
else()
if(NOT BIN_DIR)
set(BIN_DIR "bin" CACHE STRING "Where to install binaries")
endif()
if(NOT LIB_DIR)
set(LIB_DIR "${CMAKE_INSTALL_LIBDIR}/vcmi" CACHE STRING "Where to install main library")
endif()
if(NOT DATA_DIR)
set(DATA_DIR "share/vcmi" CACHE STRING "Where to install data files")
endif()
endif()
endif()
set(AI_LIB_DIR "${LIB_DIR}/AI")
set(SCRIPTING_LIB_DIR "${LIB_DIR}/scripting")
#define required constants
add_definitions(-DM_DATA_DIR="${CMAKE_INSTALL_PREFIX}/${DATA_DIR}")
add_definitions(-DM_BIN_DIR="${CMAKE_INSTALL_PREFIX}/${BIN_DIR}")
add_definitions(-DM_LIB_DIR="${CMAKE_INSTALL_PREFIX}/${LIB_DIR}")
# precompiled header configuration
set(PCH_PROPERTIES
COTIRE_ENABLE_PRECOMPILED_HEADER ${ENABLE_PCH}
COTIRE_ADD_UNITY_BUILD FALSE
COTIRE_CXX_PREFIX_HEADER_INIT "StdInc.h"
)
if(ENABLE_ERM)
add_subdirectory(scripting/erm)
endif()
if(NOT MINIZIP_FOUND)
add_subdirectory_with_folder("3rdparty" lib/minizip)
set(MINIZIP_LIBRARIES minizip)
endif()
add_subdirectory(lib)
add_subdirectory(client)
add_subdirectory(server)
add_subdirectory_with_folder("AI" AI)
if(ENABLE_LAUNCHER)
add_subdirectory(launcher)
endif()
if(ENABLE_TEST)
add_subdirectory(test)
endif()
#######################################
# Installation section #
#######################################
install(DIRECTORY config DESTINATION ${DATA_DIR})
install(DIRECTORY Mods DESTINATION ${DATA_DIR})
# that script is useless for Windows
if(NOT WIN32)
install(FILES vcmibuilder DESTINATION ${BIN_DIR} PERMISSIONS
OWNER_WRITE OWNER_READ OWNER_EXECUTE
GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE)
endif()
#######################################
# Packaging section #
#######################################
set(CPACK_PACKAGE_VERSION_MAJOR ${VCMI_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${VCMI_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${VCMI_VERSION_PATCH})
# vcmi does not have "patch version" in version string
set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}")
#TODO: remove version from Global.h and use this one as define?
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY 0)
if("${PACKAGE_NAME_SUFFIX}" STREQUAL "")
set(CPACK_PACKAGE_NAME "VCMI")
else()
set(CPACK_PACKAGE_NAME "VCMI ${PACKAGE_NAME_SUFFIX}")
endif()
if("${PACKAGE_FILE_NAME}" STREQUAL "")
set(CPACK_PACKAGE_FILE_NAME "vcmi-${CPACK_PACKAGE_VERSION}")
else()
set(CPACK_PACKAGE_FILE_NAME "${PACKAGE_FILE_NAME}")
endif()
set(CPACK_PACKAGE_VENDOR "VCMI team")
if(WIN32)
set(CPACK_MONOLITHIC_INSTALL 1)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/license.txt")
set(CPACK_PACKAGE_EXECUTABLES "VCMI_launcher;VCMI")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "${CPACK_PACKAGE_NAME}")
if("${PACKAGE_NAME_SUFFIX}" STREQUAL "")
set(CPACK_NSIS_PACKAGE_NAME "VCMI ${CPACK_PACKAGE_VERSION}")
else()
set(CPACK_NSIS_PACKAGE_NAME "VCMI ${CPACK_PACKAGE_VERSION} ${PACKAGE_NAME_SUFFIX} ")
endif()
set(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES")
if(ENABLE_LAUNCHER)
set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS " CreateShortCut \\\"$DESKTOP\\\\VCMI.lnk\\\" \\\"$INSTDIR\\\\VCMI_launcher.exe\\\"")
else()
set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS " CreateShortCut \\\"$DESKTOP\\\\VCMI.lnk\\\" \\\"$INSTDIR\\\\VCMI_client.exe\\\"")
endif()
set(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS " Delete \\\"$DESKTOP\\\\VCMI.lnk\\\" ")
configure_file("${CMAKE_SOURCE_DIR}/cmake_modules/CMakeCPackOptions.cmake.in" "${CMAKE_BINARY_DIR}/CMakeCPackOptions.cmake" @ONLY)
set(CPACK_PROJECT_CONFIG_FILE "${CMAKE_BINARY_DIR}/CMakeCPackOptions.cmake")
elseif(APPLE AND NOT ENABLE_MONOLITHIC_INSTALL)
set(CPACK_MONOLITHIC_INSTALL 1)
set(CPACK_GENERATOR "DragNDrop")
set(CPACK_DMG_BACKGROUND_IMAGE "${CMAKE_SOURCE_DIR}/osx/dmg_background.png")
set(CPACK_DMG_DS_STORE "${CMAKE_SOURCE_DIR}/osx/dmg_DS_Store")
set(MACOSX_BUNDLE_NAME "${CMAKE_PROJECT_NAME}")
set(MACOSX_BUNDLE_BUNDLE_NAME "${CMAKE_PROJECT_NAME}")
if(ENABLE_LAUNCHER)
set(MACOSX_BUNDLE_EXECUTABLE_NAME "vcmilauncher")
else()
set(MACOSX_BUNDLE_EXECUTABLE_NAME "vcmiclient")
endif()
set(MACOSX_BUNDLE_ICON_FILE "vcmi.icns")
install(FILES "${CMAKE_SOURCE_DIR}/osx/vcmi.icns" DESTINATION ${APP_BUNDLE_RESOURCES_DIR})
configure_file("${CMAKE_SOURCE_DIR}/osx/Info.plist.in" "${CMAKE_BINARY_DIR}/Info.plist")
install(FILES "${CMAKE_BINARY_DIR}/Info.plist" DESTINATION ${APP_BUNDLE_CONTENTS_DIR})
# Bundle fixing code must be in separate directory to be executed after all other install code
add_subdirectory(osx)
else()
set(CPACK_GENERATOR TGZ)
endif()
include(GetGitRevisionDescription)
get_git_head_revision(GIT_REFSPEC GIT_SHA1)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/Version.cpp.in" "${CMAKE_BINARY_DIR}/Version.cpp" @ONLY)
include(CPack)