Merge branch 'TimePath/gtk++' into 'master'
[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
4 project(NetRadiant C CXX)
5 option(BUILD_RADIANT "Build the gui" ON)
6
7 if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
8     set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/install" CACHE PATH "..." FORCE)
9 endif ()
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 file(WRITE "${PROJECT_BINARY_DIR}/RADIANT_PATCH" ${NetRadiant_VERSION_PATCH})
24
25 set(RADIANT_ABOUTMSG "Custom build" CACHE STRING "About message")
26
27 find_package(Git REQUIRED)
28 execute_process(
29         COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
30         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
31         OUTPUT_VARIABLE GIT_VERSION
32         OUTPUT_STRIP_TRAILING_WHITESPACE
33 )
34 set(RADIANT_VERSION_STRING "${NetRadiant_VERSION}n")
35 if (GIT_VERSION)
36     set(RADIANT_VERSION_STRING "${RADIANT_VERSION_STRING}-git-${GIT_VERSION}")
37 endif ()
38
39 message(STATUS "Building ${PROJECT_NAME} ${RADIANT_VERSION_STRING} ${RADIANT_ABOUTMSG}")
40
41 #-----------------------------------------------------------------------
42 # Language standard
43 #-----------------------------------------------------------------------
44
45 set(CMAKE_CXX_STANDARD 11)
46 set(CMAKE_CXX_STANDARD_REQUIRED ON)
47 # For some reason the above flags don't really work...
48 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
49     include(CheckCXXCompilerFlag)
50     check_cxx_compiler_flag(--std=c++${CMAKE_CXX_STANDARD} STD_CXX)
51     if (STD_CXX)
52         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++${CMAKE_CXX_STANDARD}")
53     else ()
54         message(SEND_ERROR "Requires C++${CMAKE_CXX_STANDARD} or better")
55     endif ()
56 else ()
57     message(WARNING "Unrecognized compiler: ${CMAKE_CXX_COMPILER_ID}, make sure it supports C++${CMAKE_CXX_STANDARD}")
58 endif ()
59
60 #-----------------------------------------------------------------------
61 # Flags
62 #-----------------------------------------------------------------------
63
64 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
65 macro(addflags args)
66     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${args}")
67     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${args}")
68 endmacro()
69 addflags("-fno-strict-aliasing")
70 if (NOT WIN32)
71     addflags("-fvisibility=hidden")
72 endif ()
73
74 set(CMAKE_POSITION_INDEPENDENT_CODE 1)
75 set(GTK_TARGET 2 CACHE STRING "GTK target")
76 add_definitions(-DGTK_TARGET=${GTK_TARGET})
77
78 #-----------------------------------------------------------------------
79 # Defs
80 #-----------------------------------------------------------------------
81
82 add_definitions(-DRADIANT_VERSION="${NetRadiant_VERSION}")
83 add_definitions(-DRADIANT_MAJOR_VERSION="${NetRadiant_VERSION_MAJOR}")
84 add_definitions(-DRADIANT_MINOR_VERSION="${NetRadiant_VERSION_MINOR}")
85 add_definitions(-DRADIANT_PATCH_VERSION="${NetRadiant_VERSION_PATCH}")
86
87 add_definitions(-DRADIANT_ABOUTMSG="${NetRadiant_ABOUT}")
88
89 if (NOT CMAKE_BUILD_TYPE MATCHES Release)
90     add_definitions(-D_DEBUG=1)
91 endif ()
92
93 macro(disable_deprecated name gtk2only)
94     add_definitions(-D${name}_DISABLE_SINGLE_INCLUDES)
95     if ((${gtk2only} EQUAL 0) OR (GTK_TARGET EQUAL 2))
96         add_definitions(-D${name}_DISABLE_DEPRECATED)
97     endif ()
98 endmacro()
99
100 disable_deprecated(ATK 0)
101 disable_deprecated(G 0)
102 disable_deprecated(GDK 0)
103 disable_deprecated(GDK_PIXBUF 0)
104 disable_deprecated(GTK 1)
105 disable_deprecated(PANGO 0)
106
107 if (APPLE)
108     option(XWINDOWS "Build against X11" ON)
109     add_definitions(
110             -DPOSIX=1
111     )
112 elseif (WIN32)
113     add_definitions(
114             -DWIN32=1
115             -D_WIN32=1
116     )
117 else ()
118     set(XWINDOWS ON)
119     add_definitions(
120             -DPOSIX=1
121     )
122 endif ()
123
124 if (XWINDOWS)
125     find_package(X11 REQUIRED)
126     include_directories(${X11_INCLUDE_DIR})
127     add_definitions(-DXWINDOWS=1)
128 endif ()
129
130 include_directories("${PROJECT_SOURCE_DIR}/include")
131 include_directories("${PROJECT_SOURCE_DIR}/libs")
132
133 if (WIN32 AND NOT CMAKE_CROSSCOMPILING)
134     set(BUNDLE_LIBRARIES_DEFAULT ON)
135 else ()
136     set(BUNDLE_LIBRARIES_DEFAULT OFF)
137 endif ()
138 option(BUNDLE_LIBRARIES "Bundle libraries" ${BUNDLE_LIBRARIES_DEFAULT})
139
140 macro(copy_dlls target)
141     if (BUNDLE_LIBRARIES)
142         add_custom_command(TARGET ${target} POST_BUILD
143                 COMMAND bash
144                 ARGS -c "ldd '$<TARGET_FILE:${target}>' | grep -v /c/Windows | awk '{ print $1 }' | while read dll; do cp \"$(which $dll)\" '${PROJECT_BINARY_DIR}'; done"
145                 VERBATIM
146                 )
147     endif ()
148 endmacro()
149
150 #-----------------------------------------------------------------------
151 # Libraries
152 #-----------------------------------------------------------------------
153
154 add_subdirectory(libs)
155 add_subdirectory(include)
156
157 #-----------------------------------------------------------------------
158 # Plugins
159 #-----------------------------------------------------------------------
160
161 if (BUILD_RADIANT)
162     add_subdirectory(contrib)
163 endif ()
164
165 #-----------------------------------------------------------------------
166 # Modules
167 #-----------------------------------------------------------------------
168
169 if (BUILD_RADIANT)
170     add_subdirectory(plugins)
171 endif ()
172
173 #-----------------------------------------------------------------------
174 # Radiant
175 #-----------------------------------------------------------------------
176
177 if (CMAKE_EXECUTABLE_SUFFIX)
178     string(REGEX REPLACE "^[.]" "" RADIANT_EXECUTABLE ${CMAKE_EXECUTABLE_SUFFIX})
179 else ()
180     execute_process(
181             COMMAND uname -m
182             OUTPUT_VARIABLE RADIANT_EXECUTABLE
183             OUTPUT_STRIP_TRAILING_WHITESPACE
184     )
185 endif ()
186
187 macro(radiant_tool name)
188     add_executable(${name} ${ARGN})
189     install(
190             TARGETS ${name}
191             RUNTIME DESTINATION .
192     )
193     if (NOT (CMAKE_EXECUTABLE_SUFFIX STREQUAL ".${RADIANT_EXECUTABLE}"))
194         add_custom_command(TARGET ${name} POST_BUILD
195                 COMMAND ln -f -s "$<TARGET_FILE_NAME:${name}>" "${PROJECT_BINARY_DIR}/${name}.${RADIANT_EXECUTABLE}"
196                 VERBATIM
197                 )
198         install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink \
199             ${name}${CMAKE_EXECUTABLE_SUFFIX} \
200             ${CMAKE_INSTALL_PREFIX}/${name}.${RADIANT_EXECUTABLE} \
201         )")
202     endif ()
203 endmacro()
204
205 if (BUILD_RADIANT)
206     add_subdirectory(radiant build)
207     set_target_properties(radiant PROPERTIES
208             COMPILE_DEFINITIONS RADIANT_EXECUTABLE="${RADIANT_EXECUTABLE}"
209             )
210 endif ()
211
212 #-----------------------------------------------------------------------
213 # Tools
214 #-----------------------------------------------------------------------
215
216 add_subdirectory(tools)
217
218 file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/setup/data/tools/*")
219
220 if (NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR))
221     # Copy data files from sources to the build directory
222     message(STATUS "Copying data files")
223     file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}")
224 endif ()
225
226 #-----------------------------------------------------------------------
227 # Game packs
228 #-----------------------------------------------------------------------
229
230 option(DOWNLOAD_GAMEPACKS "Download game packs" ON)
231 add_custom_target(game_packs
232         COMMAND ${CMAKE_COMMAND} -E make_directory games
233         COMMAND DOWNLOAD_GAMEPACKS=yes SOURCE_DIR="${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/install-gamepacks.sh" "${PROJECT_BINARY_DIR}"
234         COMMENT "Downloading game packs"
235         )
236 if (DOWNLOAD_GAMEPACKS)
237     add_custom_target(game_packs_all ALL DEPENDS game_packs)
238 endif ()
239
240 #-----------------------------------------------------------------------
241 # Install
242 #-----------------------------------------------------------------------
243
244 install(
245         FILES
246         "${PROJECT_BINARY_DIR}/RADIANT_MAJOR"
247         "${PROJECT_BINARY_DIR}/RADIANT_MINOR"
248         "${PROJECT_BINARY_DIR}/RADIANT_PATCH"
249         DESTINATION ${CMAKE_INSTALL_PREFIX}
250 )
251
252 install(
253         DIRECTORY
254         ${PROJECT_SOURCE_DIR}/setup/data/tools/
255         ${PROJECT_SOURCE_DIR}/docs
256         DESTINATION ${CMAKE_INSTALL_PREFIX}
257 )
258
259 set(GAME_FILES
260         DarkPlaces
261         Nexuiz
262         OpenArena
263         Osirion
264         Q3
265         Quake2
266         Quake
267         Quetoo
268         Tremulous
269         UFOAI
270         Unvanquished
271         Warsow
272         Xonotic
273         )
274 unset(_tmp)
275 foreach (it ${GAME_FILES})
276     set(dir "${PROJECT_BINARY_DIR}/games/${it}Pack")
277     string(TOLOWER "${it}" it)
278     list(APPEND _tmp "${dir}/games")
279     list(APPEND _tmp "${dir}/${it}.game")
280 endforeach ()
281 set(GAME_FILES ${_tmp})
282
283 install(
284         DIRECTORY
285         ${GAME_FILES}
286         DESTINATION ${CMAKE_INSTALL_PREFIX}
287         OPTIONAL
288 )