uncrustify iqmmodel code
[xonotic/netradiant.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
2 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
3 project(NetRadiant C CXX)
4
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 set(CMAKE_CXX_EXTENSIONS OFF)
48 if (CMAKE_VERSION VERSION_LESS "3.1")
49     if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
50         include(CheckCXXCompilerFlag)
51         check_cxx_compiler_flag(--std=c++${CMAKE_CXX_STANDARD} STD_CXX)
52         if (STD_CXX)
53             set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++${CMAKE_CXX_STANDARD}")
54         else ()
55             message(SEND_ERROR "Requires C++${CMAKE_CXX_STANDARD} or better")
56         endif ()
57     else ()
58         message(WARNING "Unrecognized compiler: ${CMAKE_CXX_COMPILER_ID}, make sure it supports C++${CMAKE_CXX_STANDARD}")
59     endif ()
60 endif ()
61
62 #-----------------------------------------------------------------------
63 # Flags
64 #-----------------------------------------------------------------------
65
66 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
67 macro(addflags_c args)
68     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${args}")
69 endmacro()
70 macro(addflags_cxx args)
71     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${args}")
72 endmacro()
73 macro(addflags args)
74     addflags_c("${args}")
75     addflags_cxx("${args}")
76 endmacro()
77 addflags("-fno-strict-aliasing")
78 if (NOT WIN32)
79     addflags("-fvisibility=hidden")
80 endif ()
81
82 if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
83     addflags("-Werror")
84     addflags("-pedantic-errors")
85 endif ()
86
87 addflags("-Wall")
88 addflags("-Wextra")
89 addflags("-pedantic")
90
91 addflags_c("-Wno-deprecated-declarations") # vfs.c: g_strdown
92
93 addflags("-Wno-unused-function")
94 addflags("-Wno-unused-variable")
95 addflags("-Wno-unused-parameter")
96
97 set(CMAKE_POSITION_INDEPENDENT_CODE 1)
98 set(GTK_TARGET 2 CACHE STRING "GTK target")
99 add_definitions(-DGTK_TARGET=${GTK_TARGET})
100
101 #-----------------------------------------------------------------------
102 # Defs
103 #-----------------------------------------------------------------------
104
105 add_definitions(-DRADIANT_VERSION="${NetRadiant_VERSION}")
106 add_definitions(-DRADIANT_MAJOR_VERSION="${NetRadiant_VERSION_MAJOR}")
107 add_definitions(-DRADIANT_MINOR_VERSION="${NetRadiant_VERSION_MINOR}")
108 add_definitions(-DRADIANT_PATCH_VERSION="${NetRadiant_VERSION_PATCH}")
109
110 add_definitions(-DRADIANT_ABOUTMSG="${NetRadiant_ABOUT}")
111
112 if (NOT CMAKE_BUILD_TYPE MATCHES Release)
113     add_definitions(-D_DEBUG=1)
114 endif ()
115
116 macro(disable_deprecated name gtk2only)
117     add_definitions(-D${name}_DISABLE_SINGLE_INCLUDES)
118     if ((${gtk2only} EQUAL 0) OR (GTK_TARGET EQUAL 2))
119         add_definitions(-D${name}_DISABLE_DEPRECATED)
120     endif ()
121 endmacro()
122
123 disable_deprecated(ATK 0)
124 disable_deprecated(G 0)
125 disable_deprecated(GDK 0)
126 disable_deprecated(GDK_PIXBUF 0)
127 disable_deprecated(GTK 1)
128 disable_deprecated(PANGO 0)
129
130 if (APPLE)
131     option(XWINDOWS "Build against X11" ON)
132     add_definitions(
133             -DPOSIX=1
134     )
135 elseif (WIN32)
136     add_definitions(
137             -DWIN32=1
138             -D_WIN32=1
139     )
140 else ()
141     set(XWINDOWS ON)
142     add_definitions(
143             -DPOSIX=1
144     )
145 endif ()
146
147 if (XWINDOWS)
148     find_package(X11 REQUIRED)
149     include_directories(${X11_INCLUDE_DIR})
150     add_definitions(-DXWINDOWS=1)
151 endif ()
152
153 include_directories("${PROJECT_SOURCE_DIR}/include")
154 include_directories("${PROJECT_SOURCE_DIR}/libs")
155
156 if (WIN32 AND NOT CMAKE_CROSSCOMPILING)
157     set(BUNDLE_LIBRARIES_DEFAULT ON)
158 else ()
159     set(BUNDLE_LIBRARIES_DEFAULT OFF)
160 endif ()
161 option(BUNDLE_LIBRARIES "Bundle libraries" ${BUNDLE_LIBRARIES_DEFAULT})
162
163 macro(copy_dlls target)
164     if (BUNDLE_LIBRARIES)
165         add_custom_command(TARGET ${target} POST_BUILD
166                 COMMAND bash
167                 ARGS -c "ldd '$<TARGET_FILE:${target}>' | grep -v /c/Windows | awk '{ print $1 }' | while read dll; do cp \"$(which $dll)\" '${PROJECT_BINARY_DIR}'; done"
168                 VERBATIM
169                 )
170     endif ()
171 endmacro()
172
173 #-----------------------------------------------------------------------
174 # Libraries
175 #-----------------------------------------------------------------------
176
177 add_subdirectory(libs)
178 add_subdirectory(include)
179
180 #-----------------------------------------------------------------------
181 # Plugins
182 #-----------------------------------------------------------------------
183
184 if (BUILD_RADIANT)
185     add_subdirectory(contrib)
186 endif ()
187
188 #-----------------------------------------------------------------------
189 # Modules
190 #-----------------------------------------------------------------------
191
192 if (BUILD_RADIANT)
193     add_subdirectory(plugins)
194 endif ()
195
196 #-----------------------------------------------------------------------
197 # Radiant
198 #-----------------------------------------------------------------------
199
200 if (CMAKE_EXECUTABLE_SUFFIX)
201     string(REGEX REPLACE "^[.]" "" RADIANT_EXECUTABLE ${CMAKE_EXECUTABLE_SUFFIX})
202 else ()
203     execute_process(
204             COMMAND uname -m
205             OUTPUT_VARIABLE RADIANT_EXECUTABLE
206             OUTPUT_STRIP_TRAILING_WHITESPACE
207     )
208 endif ()
209
210 macro(radiant_tool name)
211     add_executable(${name} ${ARGN})
212     install(
213             TARGETS ${name}
214             RUNTIME DESTINATION .
215     )
216     if (NOT (CMAKE_EXECUTABLE_SUFFIX STREQUAL ".${RADIANT_EXECUTABLE}"))
217         add_custom_command(TARGET ${name} POST_BUILD
218                 COMMAND ln -f -s "$<TARGET_FILE_NAME:${name}>" "${PROJECT_BINARY_DIR}/${name}.${RADIANT_EXECUTABLE}"
219                 VERBATIM
220                 )
221         install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
222                 ${name}${CMAKE_EXECUTABLE_SUFFIX} ${CMAKE_INSTALL_PREFIX}/${name}.${RADIANT_EXECUTABLE})
223                 ")
224     endif ()
225 endmacro()
226
227 if (BUILD_RADIANT)
228     add_subdirectory(radiant _radiant)
229     set_target_properties(radiant PROPERTIES
230             COMPILE_DEFINITIONS RADIANT_EXECUTABLE="${RADIANT_EXECUTABLE}"
231             )
232 endif ()
233
234 #-----------------------------------------------------------------------
235 # Tools
236 #-----------------------------------------------------------------------
237
238 add_subdirectory(tools)
239
240 file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/setup/data/tools/*")
241
242 if (NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR))
243     # Copy data files from sources to the build directory
244     message(STATUS "Copying data files")
245     file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}")
246 endif ()
247
248 #-----------------------------------------------------------------------
249 # Game packs
250 #-----------------------------------------------------------------------
251
252 option(DOWNLOAD_GAMEPACKS "Download game packs" ON)
253 add_custom_target(game_packs_free
254         COMMAND ${CMAKE_COMMAND} -E make_directory games
255         COMMAND DOWNLOAD_GAMEPACKS=yes SOURCE_DIR="${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/install-gamepacks.sh" "${PROJECT_BINARY_DIR}"
256         COMMENT "Downloading free game packs"
257         )
258 add_custom_target(game_packs_all
259         COMMAND ${CMAKE_COMMAND} -E make_directory games
260         COMMAND DOWNLOAD_GAMEPACKS=all SOURCE_DIR="${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/install-gamepacks.sh" "${PROJECT_BINARY_DIR}"
261         COMMENT "Downloading all game packs"
262         )
263
264 if (DOWNLOAD_GAMEPACKS)
265     set(DOWNLOAD_GAMEPACKS "all")
266 endif()
267 if ("${DOWNLOAD_GAMEPACKS}" STREQUAL "free")
268     add_custom_target(game_packs_go ALL DEPENDS game_packs_free)
269 elseif ("${DOWNLOAD_GAMEPACKS}" STREQUAL "all")
270     add_custom_target(game_packs_go ALL DEPENDS game_packs_all)
271 endif ()
272
273 #-----------------------------------------------------------------------
274 # Install
275 #-----------------------------------------------------------------------
276
277 install(
278         FILES
279         "${PROJECT_BINARY_DIR}/RADIANT_MAJOR"
280         "${PROJECT_BINARY_DIR}/RADIANT_MINOR"
281         "${PROJECT_BINARY_DIR}/RADIANT_PATCH"
282         DESTINATION .
283 )
284
285 install(
286         DIRECTORY
287         setup/data/tools/
288         docs
289         DESTINATION .
290 )
291
292 set(GAME_FILES
293         DarkPlaces
294         Nexuiz
295         OpenArena
296         Osirion
297         Q3
298         Quake2
299         Quake
300         Quetoo
301         Tremulous
302         UFOAI
303         Unvanquished
304         Warsow
305         Xonotic
306         )
307 unset(_tmp)
308 foreach (it ${GAME_FILES})
309     set(dir "${PROJECT_BINARY_DIR}/games/${it}Pack")
310     string(TOLOWER "${it}" it)
311     list(APPEND _tmp "${dir}/games")
312     list(APPEND _tmp "${dir}/${it}.game")
313 endforeach ()
314 set(GAME_FILES ${_tmp})
315
316 install(
317         DIRECTORY
318         ${GAME_FILES}
319         DESTINATION .
320         OPTIONAL
321 )
322
323 include(cmake/scripts/package.cmake)