MSYS2: automatically copy dlls as part of build
[xonotic/netradiant.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8.11 FATAL_ERROR)
2 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
3 # Enable Debug by default, can be changed with -DCMAKE_BUILD_TYPE=Release
4 if (NOT CMAKE_BUILD_TYPE)
5     set(CMAKE_BUILD_TYPE Debug)
6 endif ()
7
8 project(NetRadiant C CXX)
9 option(BUILD_RADIANT "Build the gui" ON)
10
11 #-----------------------------------------------------------------------
12 # Version
13 #-----------------------------------------------------------------------
14
15 # CMake 3.0+ would allow this in project()
16 set(NetRadiant_VERSION_MAJOR 1)
17 set(NetRadiant_VERSION_MINOR 5)
18 set(NetRadiant_VERSION_PATCH 0)
19 set(NetRadiant_VERSION "${NetRadiant_VERSION_MAJOR}.${NetRadiant_VERSION_MINOR}.${NetRadiant_VERSION_PATCH}")
20
21 file(WRITE "${PROJECT_BINARY_DIR}/RADIANT_MAJOR" ${NetRadiant_VERSION_MAJOR})
22 file(WRITE "${PROJECT_BINARY_DIR}/RADIANT_MINOR" ${NetRadiant_VERSION_MINOR})
23
24 set(RADIANT_ABOUTMSG "Custom build" CACHE STRING "About message")
25
26 find_package(Git REQUIRED)
27 execute_process(
28         COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
29         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
30         OUTPUT_VARIABLE GIT_VERSION
31         OUTPUT_STRIP_TRAILING_WHITESPACE
32 )
33 set(RADIANT_VERSION_STRING "${NetRadiant_VERSION}n")
34 if (GIT_VERSION)
35     set(RADIANT_VERSION_STRING "${RADIANT_VERSION_STRING}-git-${GIT_VERSION}")
36 endif ()
37
38 message(STATUS "Building ${PROJECT_NAME} ${RADIANT_VERSION_STRING} ${RADIANT_ABOUTMSG}")
39
40 #-----------------------------------------------------------------------
41 # Language standard
42 #-----------------------------------------------------------------------
43
44 set(CMAKE_CXX_STANDARD 11)
45 set(CMAKE_CXX_STANDARD_REQUIRED ON)
46 # For some reason the above flags don't really work...
47 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
48     include(CheckCXXCompilerFlag)
49     check_cxx_compiler_flag(--std=c++${CMAKE_CXX_STANDARD} STD_CXX)
50     if (STD_CXX)
51         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++${CMAKE_CXX_STANDARD}")
52     else ()
53         message(SEND_ERROR "Requires C++${CMAKE_CXX_STANDARD} or better")
54     endif ()
55 else ()
56     message(WARNING "Unrecognized compiler: ${CMAKE_CXX_COMPILER_ID}, make sure it supports C++${CMAKE_CXX_STANDARD}")
57 endif ()
58
59 #-----------------------------------------------------------------------
60 # Flags
61 #-----------------------------------------------------------------------
62
63 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}   -fno-strict-aliasing")
64 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -fno-exceptions -fno-rtti")
65 set(CMAKE_POSITION_INDEPENDENT_CODE 1)
66 set(GTK_NS "GTK2" CACHE STRING "GTK variable prefix")
67
68 #-----------------------------------------------------------------------
69 # Defs
70 #-----------------------------------------------------------------------
71
72 add_definitions(-DRADIANT_VERSION="${NetRadiant_VERSION}")
73 add_definitions(-DRADIANT_MAJOR_VERSION="${NetRadiant_VERSION_MAJOR}")
74 add_definitions(-DRADIANT_MINOR_VERSION="${NetRadiant_VERSION_MINOR}")
75
76 add_definitions(-DRADIANT_ABOUTMSG="${NetRadiant_ABOUT}")
77
78 if (CMAKE_BUILD_TYPE MATCHES Debug)
79     add_definitions(-D_DEBUG=1)
80 endif ()
81
82 if (APPLE)
83     option(XWINDOWS "Build against X11" ON)
84     add_definitions(
85             -DPOSIX=1
86     )
87 elseif (WIN32)
88     add_definitions(
89             -DWIN32=1
90             -D_WIN32=1
91     )
92 else ()
93     set(XWINDOWS ON)
94     add_definitions(
95             -DPOSIX=1
96     )
97 endif ()
98
99 if (XWINDOWS)
100     find_package(X11 REQUIRED)
101     include_directories(${X11_INCLUDE_DIR})
102     add_definitions(-DXWINDOWS=1)
103 endif ()
104
105 include_directories("${PROJECT_SOURCE_DIR}/include")
106 include_directories("${PROJECT_SOURCE_DIR}/libs")
107
108 macro(copy_dlls target)
109     if (WIN32)
110         add_custom_command(TARGET ${target} POST_BUILD
111             COMMAND bash
112             ARGS -c "ldd '$<TARGET_FILE:${target}>' | grep -v /c/Windows | awk '{ print $1 }' | while read dll; do cp \"$(which $dll)\" '${PROJECT_BINARY_DIR}'; done"
113             VERBATIM
114         )
115     endif ()
116 endmacro()
117
118 #-----------------------------------------------------------------------
119 # Libraries
120 #-----------------------------------------------------------------------
121
122 add_subdirectory(libs)
123 add_subdirectory(include)
124
125 #-----------------------------------------------------------------------
126 # Plugins
127 #-----------------------------------------------------------------------
128
129 if (BUILD_RADIANT)
130     add_subdirectory(contrib)
131 endif ()
132
133 #-----------------------------------------------------------------------
134 # Modules
135 #-----------------------------------------------------------------------
136
137 if (BUILD_RADIANT)
138     add_subdirectory(plugins)
139 endif ()
140
141 #-----------------------------------------------------------------------
142 # Radiant
143 #-----------------------------------------------------------------------
144
145 if (BUILD_RADIANT)
146     add_subdirectory(radiant build)
147 endif ()
148
149 if (CMAKE_EXECUTABLE_SUFFIX)
150     set(RADIANT_EXECUTABLE ${CMAKE_EXECUTABLE_SUFFIX})
151 else ()
152     execute_process(
153             COMMAND uname -m
154             OUTPUT_VARIABLE RADIANT_EXECUTABLE
155             OUTPUT_STRIP_TRAILING_WHITESPACE
156     )
157 endif ()
158
159 if (BUILD_RADIANT)
160     set_target_properties(radiant PROPERTIES
161             COMPILE_DEFINITIONS RADIANT_EXECUTABLE="${RADIANT_EXECUTABLE}"
162             )
163 endif ()
164
165 #-----------------------------------------------------------------------
166 # Tools
167 #-----------------------------------------------------------------------
168
169 add_subdirectory(tools)
170
171 if (NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR))
172     # Copy data files from sources to the build directory
173     message(STATUS "Copying data files")
174     file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/setup/data/tools/*")
175     file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}")
176     file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/docs/*")
177     file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}/docs")
178 endif ()
179
180 #-----------------------------------------------------------------------
181 # Game packs
182 #-----------------------------------------------------------------------
183
184 option(DOWNLOAD_GAMEPACKS "Download game packs" ON)
185 add_custom_target(game_packs
186         COMMAND ${CMAKE_COMMAND} -E make_directory games
187         COMMAND DOWNLOAD_GAMEPACKS=yes SOURCE_DIR="${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/install-gamepacks.sh" "${PROJECT_BINARY_DIR}"
188         COMMENT "Downloading game packs"
189         )
190 if (DOWNLOAD_GAMEPACKS)
191     add_custom_target(game_packs_all ALL DEPENDS game_packs)
192 endif ()