crn_rgba: use “algorithm” header to avoid std::min/max being unknown
[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 option(BUILD_TOOLS "Build the tools" ON)
7 option(BUILD_CRUNCH "Build Crunch image support" OFF)
8 option(DOWNLOAD_GAMEPACKS "Download game packs" ON)
9 option(USE_WERROR "Build with -Werror -pedantic-errors" OFF)
10 option(FHS_INSTALL "Install according to Filesystem Hierarchy Standard" OFF)
11
12 set(BUILD_BINARIES OFF CACHE INTERNAL "...")
13 if (BUILD_RADIANT OR BUILD_TOOLS)
14     set(BUILD_BINARIES ON)
15 endif ()
16
17 #-----------------------------------------------------------------------
18 # Paths
19 #-----------------------------------------------------------------------
20
21 if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
22     set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/install" CACHE PATH "..." FORCE)
23 endif ()
24
25 set(RADIANT_NAME "NetRadiant" CACHE INTERNAL "...")
26 add_definitions(-DRADIANT_NAME="${RADIANT_NAME}")
27 set(RADIANT_BASENAME "netradiant" CACHE INTERNAL "...")
28 add_definitions(-DRADIANT_BASENAME="${RADIANT_BASENAME}")
29
30 set(RADIANT_LIB_DIR ${CMAKE_INSTALL_PREFIX} CACHE INTERNAL "...")
31 set(RADIANT_BIN_DIR ${CMAKE_INSTALL_PREFIX} CACHE INTERNAL "...")
32 set(RADIANT_DATA_DIR ${CMAKE_INSTALL_PREFIX} CACHE INTERNAL "...")
33
34 if (FHS_INSTALL AND NOT BUNDLE_LIBRARIES)
35     set(RADIANT_LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib/${RADIANT_BASENAME})
36     set(RADIANT_BIN_DIR ${CMAKE_INSTALL_PREFIX}/bin)
37     set(RADIANT_DATA_DIR ${CMAKE_INSTALL_PREFIX}/share/${RADIANT_BASENAME})
38 endif ()
39
40 set(GAMEPACKS_DOWNLOAD_DIR ${PROJECT_BINARY_DIR}/download CACHE PATH "Where to store downloaded game packs")
41
42 #-----------------------------------------------------------------------
43 # Version
44 #-----------------------------------------------------------------------
45
46 # CMake 3.0+ would allow this in project()
47 set(RADIANT_VERSION_MAJOR 1)
48 set(RADIANT_VERSION_MINOR 5)
49 set(RADIANT_VERSION_PATCH 0)
50 set(RADIANT_VERSION "${RADIANT_VERSION_MAJOR}.${RADIANT_VERSION_MINOR}.${RADIANT_VERSION_PATCH}")
51
52 set(RADIANT_ABOUTMSG "Custom build" CACHE STRING "About message")
53
54 find_package(Git REQUIRED)
55
56 execute_process(
57      COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
58      WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
59      OUTPUT_VARIABLE GIT_VERSION
60      OUTPUT_STRIP_TRAILING_WHITESPACE
61 )
62
63 set(RADIANT_VERSION_STRING "${RADIANT_VERSION}n")
64
65 if (GIT_VERSION)
66     set(RADIANT_VERSION_STRING "${RADIANT_VERSION_STRING}-git-${GIT_VERSION}")
67 endif ()
68
69 message(STATUS "Building ${PROJECT_NAME} ${RADIANT_VERSION_STRING} ${RADIANT_ABOUTMSG}")
70
71 #-----------------------------------------------------------------------
72 # Language standard
73 #-----------------------------------------------------------------------
74
75 if (BUILD_BINARIES)
76     set(CMAKE_CXX_STANDARD 11)
77     set(CMAKE_CXX_STANDARD_REQUIRED ON)
78     set(CMAKE_CXX_EXTENSIONS OFF)
79
80     if (CMAKE_VERSION VERSION_LESS "3.1")
81         if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
82             include(CheckCXXCompilerFlag)
83             check_cxx_compiler_flag(--std=c++${CMAKE_CXX_STANDARD} STD_CXX)
84             if (STD_CXX)
85                 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++${CMAKE_CXX_STANDARD}")
86             else ()
87                 message(SEND_ERROR "Requires C++${CMAKE_CXX_STANDARD} or better")
88             endif ()
89         else ()
90             message(WARNING "Unrecognized compiler: ${CMAKE_CXX_COMPILER_ID}, make sure it supports C++${CMAKE_CXX_STANDARD}")
91         endif ()
92     endif ()
93 endif ()
94
95 #-----------------------------------------------------------------------
96 # Flags
97 #-----------------------------------------------------------------------
98
99 if (BUILD_BINARIES)
100     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
101
102     macro (addflags_c args)
103         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${args}")
104     endmacro ()
105
106     macro (addflags_cxx args)
107         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${args}")
108     endmacro ()
109
110     macro (addflags args)
111         addflags_c("${args}")
112         addflags_cxx("${args}")
113     endmacro ()
114
115     addflags("-fno-strict-aliasing")
116     if (NOT WIN32)
117         addflags("-fvisibility=hidden")
118     endif ()
119
120     if (USE_WERROR)
121         addflags("-Werror")
122         addflags("-pedantic-errors")
123     endif ()
124
125     addflags("-Wall")
126     addflags("-Wextra")
127     addflags("-pedantic")
128
129     addflags_c("-Wno-deprecated-declarations") # vfs.c: g_strdown
130
131     addflags("-Wno-unused-function")
132     addflags("-Wno-unused-variable")
133     addflags("-Wno-unused-parameter")
134
135     set(CMAKE_POSITION_INDEPENDENT_CODE 1)
136 endif ()
137
138 if (BUILD_RADIANT)
139     set(GTK_TARGET 2 CACHE STRING "GTK target")
140     add_definitions(-DGTK_TARGET=${GTK_TARGET})
141
142     if (WIN32)
143         add_definitions(-DWORKAROUND_WINDOWS_FLOATING_WINDOW=1)
144
145         if (GTK_TARGET EQUAL 2)
146             add_definitions(-DWORKAROUND_WINDOWS_GTK2_GLWIDGET=1)
147         endif ()
148     endif ()
149 endif ()
150
151 #-----------------------------------------------------------------------
152 # Defs
153 #-----------------------------------------------------------------------
154
155 if (BUILD_BINARIES)
156     add_definitions(-DRADIANT_VERSION="${RADIANT_VERSION}")
157     add_definitions(-DRADIANT_VERSION_STRING="${RADIANT_VERSION_STRING}")
158
159     if (NOT CMAKE_BUILD_TYPE MATCHES Release)
160         add_definitions(-D_DEBUG=1)
161     endif ()
162 endif ()
163
164 if (BUILD_RADIANT)
165     add_definitions(-DRADIANT_ABOUTMSG="${RADIANT_ABOUTMSG}")
166
167     macro (disable_deprecated name gtk2only)
168         add_definitions(-D${name}_DISABLE_SINGLE_INCLUDES)
169         if ((${gtk2only} EQUAL 0) OR (GTK_TARGET EQUAL 2))
170             add_definitions(-D${name}_DISABLE_DEPRECATED)
171         endif ()
172     endmacro ()
173
174     disable_deprecated(ATK 0)
175     disable_deprecated(G 0)
176     disable_deprecated(GDK 0)
177     disable_deprecated(GDK_PIXBUF 0)
178     disable_deprecated(GTK 1)
179     disable_deprecated(PANGO 0)
180 endif ()
181
182 if (BUILD_BINARIES)
183     add_custom_target(binaries ALL)
184
185     if(APPLE OR ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
186         link_directories(
187             /usr/local/lib
188         )
189     endif ()
190
191     if (WIN32)
192         add_definitions(
193             -DWIN32=1
194             -D_WIN32=1
195         )
196     else ()
197         add_definitions(
198             -DPOSIX=1
199         )
200     endif ()
201 endif ()
202
203 if (BUILD_RADIANT AND NOT APPLE AND NOT WIN32)
204     set(XWINDOWS ON)
205     find_package(X11 REQUIRED)
206     include_directories(${X11_INCLUDE_DIR})
207     add_definitions(-DXWINDOWS=1)
208 endif ()
209
210 if (BUILD_BINARIES)
211     include_directories("${PROJECT_SOURCE_DIR}/include")
212     include_directories("${PROJECT_SOURCE_DIR}/libs")
213
214     if (WIN32 OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
215         set(BUNDLING_SUPPORTED ON)
216     endif()
217
218     if (BUNDLING_SUPPORTED)
219         if(WIN32 AND NOT CMAKE_CROSSCOMPILING)
220             set(BUNDLE_LIBRARIES_DEFAULT ON)
221         else ()
222             set(BUNDLE_LIBRARIES_DEFAULT OFF)
223         endif ()
224
225         option(BUNDLE_LIBRARIES "Bundle libraries" ${BUNDLE_LIBRARIES_DEFAULT})
226     endif ()
227
228     if (BUNDLE_LIBRARIES AND BUNDLING_SUPPORTED)
229         add_subdirectory(bundle)
230
231         set(FHS_INSTALL OFF CACHE PATH "Disabled because of BUNDLE_LIBRARIES" FORCE)
232     endif ()
233
234     macro (copy_dlls target)
235         if (BUNDLE_LIBRARIES AND BUNDLING_SUPPORTED)
236             add_custom_command(TARGET ${target} POST_BUILD
237                 COMMAND "${PROJECT_SOURCE_DIR}/library-bundler"
238                 "register" "${PROJECT_BINARY_DIR}" "$<TARGET_FILE:${target}>"
239                 VERBATIM
240             )
241             add_dependencies(bundle ${target})
242         endif ()
243     endmacro ()
244 endif ()
245
246 #-----------------------------------------------------------------------
247 # Libraries
248 #-----------------------------------------------------------------------
249
250 if (BUILD_BINARIES)
251     add_subdirectory(libs)
252 endif ()
253
254 if (BUILD_RADIANT)
255     add_subdirectory(include)
256 endif ()
257
258 #-----------------------------------------------------------------------
259 # Plugins
260 #-----------------------------------------------------------------------
261
262 if (BUILD_RADIANT)
263     add_subdirectory(contrib)
264 endif ()
265
266 #-----------------------------------------------------------------------
267 # Modules
268 #-----------------------------------------------------------------------
269
270 if (BUILD_RADIANT)
271     add_subdirectory(plugins)
272 endif ()
273
274 #-----------------------------------------------------------------------
275 # Radiant
276 #-----------------------------------------------------------------------
277
278 if (BUILD_BINARIES)
279     if (CMAKE_EXECUTABLE_SUFFIX)
280         string(REGEX REPLACE "^[.]" "" RADIANT_EXECUTABLE ${CMAKE_EXECUTABLE_SUFFIX})
281     endif ()
282
283     macro (radiant_tool name)
284         add_dependencies(binaries ${name})
285
286         if (WIN32)
287             add_executable(${name} ${ARGN} ${PROJECT_SOURCE_DIR}/include/lib.rc)
288         else ()
289             add_executable(${name} ${ARGN})
290         endif()
291
292         copy_dlls(${name})
293
294         install(
295             TARGETS ${name}
296             RUNTIME DESTINATION ${RADIANT_BIN_DIR}
297         )
298     endmacro ()
299
300     if (BUILD_RADIANT)
301         add_subdirectory(radiant)
302         set_target_properties(${RADIANT_BASENAME} PROPERTIES
303         COMPILE_DEFINITIONS RADIANT_EXECUTABLE="${RADIANT_EXECUTABLE}"
304     )
305     endif ()
306 endif ()
307
308 #-----------------------------------------------------------------------
309 # Tools
310 #-----------------------------------------------------------------------
311
312 if (BUILD_TOOLS)
313     add_subdirectory(tools)
314
315     file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/setup/data/tools/*")
316
317     if (NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR))
318         # Copy data files from sources to the build directory
319         message(STATUS "Copying data files")
320         file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}")
321     endif ()
322 endif ()
323
324 #-----------------------------------------------------------------------
325 # Game packs
326 #-----------------------------------------------------------------------
327
328 set(GAMEPACKS_LICENSE_LIST free CACHE STRING "Download game packs by license")
329 set(GAMEPACKS_NAME_LIST none CACHE STRING "Download game packs by name")
330
331 if (DOWNLOAD_GAMEPACKS)
332     add_custom_target(gamepacks ALL
333        COMMAND "${PROJECT_SOURCE_DIR}/gamepack-manager" --license ${GAMEPACKS_LICENSE_LIST} --name ${GAMEPACKS_NAME_LIST} --download-dir "${GAMEPACKS_DOWNLOAD_DIR}" --install-dir "${PROJECT_BINARY_DIR}/gamepacks" --download --install
334        COMMENT "Downloading game packs"
335     )
336 endif ()
337
338 #-----------------------------------------------------------------------
339 # Install
340 #-----------------------------------------------------------------------
341
342 if (BUILD_BINARIES)
343     install(DIRECTORY
344         setup/data/tools/base
345         setup/data/tools/bitmaps
346         setup/data/tools/gl
347         setup/data/tools/plugins
348         docs
349         DESTINATION ${RADIANT_DATA_DIR}
350     )
351 endif ()
352
353 if (DOWNLOAD_GAMEPACKS)
354     if (FHS_INSTALL)
355        set(GAME_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/share/netradiant")
356     else ()
357        set(GAME_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
358     endif ()
359
360     install(DIRECTORY
361         ${PROJECT_BINARY_DIR}/gamepacks
362         DESTINATION ${RADIANT_DATA_DIR}
363     )
364 endif ()
365
366 if(BUILD_BINARIES AND FHS_INSTALL AND NOT WIN32 AND NOT APPLE)
367     install(FILES
368         ${PROJECT_SOURCE_DIR}/icons/netradiant.png
369         DESTINATION ${CMAKE_INSTALL_PREFIX}/share/pixmaps
370     )
371     install(FILES
372         ${PROJECT_SOURCE_DIR}/icons/netradiant.desktop
373         DESTINATION ${CMAKE_INSTALL_PREFIX}/share/applications
374     )
375     install(FILES
376         ${PROJECT_SOURCE_DIR}/icons/mime/map.xml
377         DESTINATION ${CMAKE_INSTALL_PREFIX}/share/mime/model
378     )
379 endif ()
380
381 include(cmake/scripts/package.cmake)