cmake: fix whitespace
[xonotic/netradiant.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
2
3 project(NetRadiant C CXX)
4
5 #-----------------------------------------------------------------------
6 # Builtins: configuration
7 #-----------------------------------------------------------------------
8
9 set(BUILTINS_INSTALL_DIR "${CMAKE_BINARY_DIR}/builtins")
10 set(BUILTINS_INCLUDE_DIR "${BUILTINS_INSTALL_DIR}/include")
11 set(BUILTINS_LIB_DIR "${BUILTINS_INSTALL_DIR}/lib")
12 set(BUILTINS_PKGCONFIG_DIR "${BUILTINS_INSTALL_DIR}/lib/pkgconfig")
13
14 option(BUILTIN_GTKGLEXT "Builtin GtkGLExt" OFF)
15 option(BUILTIN_GTKTHEME_MOJAVE "Builtin Mojave GTK theme" OFF)
16
17 if (APPLE)
18     set(BUILTIN_GTKGLEXT ON)
19     set(BUILTIN_GTKTHEME_MOJAVE ON)
20 endif ()
21
22 set(BUILTIN_GTKGLEXT_BUILT OFF CACHE INTERNAL "...")
23 if (EXISTS "${BUILTINS_INSTALL_DIR}/lib/pkgconfig/gtkglext-1.0.pc")
24     set(BUILTIN_GTKGLEXT_BUILT ON)
25 endif ()
26
27 if (EXISTS "${BUILTINS_INCLUDE_DIR}")
28     include_directories("${BUILTINS_INCLUDE_DIR}")
29 endif ()
30
31 if (EXISTS "${BUILTINS_LIB_DIR}")
32     set(ENV{LD_LIBRARY_PATH} "$ENV{LD_LIBRARY_PATH}:${BUILTINS_LIB_DIR}")
33     link_directories("${BUILTINS_LIB_DIR}")
34 endif ()
35
36 if (EXISTS "${BUILTINS_PKGCONFIG_DIR}")
37     set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:${BUILTINS_PKGCONFIG_DIR}")
38 endif ()
39
40 #-----------------------------------------------------------------------
41 # CMake modules
42 #-----------------------------------------------------------------------
43
44 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
45
46 #-----------------------------------------------------------------------
47 # Submodules
48 #-----------------------------------------------------------------------
49
50 if (EXISTS "${PROJECT_SOURCE_DIR}/libs/crunch/inc/crn_decomp.h")
51     set(DEFAULT_BUILD_CRUNCH ON CACHE INTERNAL "...")
52 else ()
53     set(DEFAULT_BUILD_CRUNCH OFF CACHE INTERNAL "...")
54 endif ()
55
56 if (EXISTS "${PROJECT_SOURCE_DIR}/tools/unvanquished/daemonmap/tools/quake3/q3map2/main.c")
57     set(DEFAULT_BUILD_DAEMONMAP ON CACHE INTERNAL "...")
58 else ()
59     set(DEFAULT_BUILD_DAEMONMAP OFF CACHE INTERNAL "...")
60 endif ()
61
62 #-----------------------------------------------------------------------
63 # Build options
64 #-----------------------------------------------------------------------
65
66 option(BUILD_RADIANT "Build the GUI" ON)
67 option(BUILD_TOOLS "Build the tools" ON)
68 option(BUILD_CRUNCH "Build Crunch image support" ${DEFAULT_BUILD_CRUNCH})
69 option(BUILD_DAEMONMAP "Build daemonmap navigation mesh generator" ${DEFAULT_BUILD_DAEMONMAP})
70 option(DOWNLOAD_GAMEPACKS "Download game packs" ON)
71 option(USE_WERROR "Build with -Werror -pedantic-errors" OFF)
72 option(FHS_INSTALL "Install according to Filesystem Hierarchy Standard" OFF)
73
74 set(BUILD_BINARIES OFF CACHE INTERNAL "...")
75 if (BUILD_RADIANT OR BUILD_TOOLS)
76     set(BUILD_BINARIES ON)
77 endif ()
78
79 if (BUILD_BINARIES)
80     if (WIN32 OR APPLE OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
81         set(BUNDLING_SUPPORTED ON)
82     endif()
83
84     if (BUNDLING_SUPPORTED)
85         if(WIN32 AND NOT CMAKE_CROSSCOMPILING)
86             set(BUNDLE_LIBRARIES_DEFAULT ON)
87         else ()
88             set(BUNDLE_LIBRARIES_DEFAULT OFF)
89         endif ()
90
91         option(BUNDLE_LIBRARIES "Bundle libraries" ${BUNDLE_LIBRARIES_DEFAULT})
92     endif ()
93
94     if (BUNDLE_LIBRARIES AND BUNDLING_SUPPORTED)
95         set(FHS_INSTALL OFF CACHE PATH "Disabled because of BUNDLE_LIBRARIES" FORCE)
96     endif ()
97 endif ()
98
99 #-----------------------------------------------------------------------
100 # Paths
101 #-----------------------------------------------------------------------
102
103 if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
104     set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/install" CACHE PATH "..." FORCE)
105 endif ()
106
107 if (APPLE)
108     set(APPLE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/NetRadiant.app)
109     set(FINAL_INSTALL_PREFIX ${APPLE_INSTALL_PREFIX}/Contents/Prefix)
110 else ()
111     set(FINAL_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
112 endif ()
113
114 set(RADIANT_NAME "NetRadiant" CACHE INTERNAL "...")
115 add_definitions(-DRADIANT_NAME="${RADIANT_NAME}")
116 set(RADIANT_BASENAME "netradiant" CACHE INTERNAL "...")
117 add_definitions(-DRADIANT_BASENAME="${RADIANT_BASENAME}")
118
119 set(RADIANT_BIN_DIR ${FINAL_INSTALL_PREFIX} CACHE INTERNAL "...")
120 set(RADIANT_LIB_DIR ${FINAL_INSTALL_PREFIX}/lib CACHE INTERNAL "...")
121 set(RADIANT_ADDONS_DIR ${FINAL_INSTALL_PREFIX} CACHE INTERNAL "...")
122 set(RADIANT_ETC_DIR ${FINAL_INSTALL_PREFIX}/etc CACHE INTERNAL "...")
123 set(RADIANT_SHARE_DIR ${FINAL_INSTALL_PREFIX}/share CACHE INTERNAL "...")
124 set(RADIANT_DATA_DIR ${FINAL_INSTALL_PREFIX} CACHE INTERNAL "...")
125
126 if (FHS_INSTALL AND NOT BUNDLE_LIBRARIES)
127     set(RADIANT_BIN_DIR ${FINAL_INSTALL_PREFIX}/bin)
128     set(RADIANT_LIB_DIR ${FINAL_INSTALL_PREFIX}/lib)
129     set(RADIANT_ADDONS_DIR ${RADIANT_LIB_DIR}/${RADIANT_BASENAME})
130     set(RADIANT_ETC_DIR ${FINAL_INSTALL_PREFIX}/etc)
131     set(RADIANT_SHARE_DIR ${FINAL_INSTALL_PREFIX}/share)
132     set(RADIANT_DATA_DIR ${RADIANT_SHARE_DIR}/${RADIANT_BASENAME})
133 endif ()
134
135 set(GAMEPACKS_DOWNLOAD_DIR ${PROJECT_BINARY_DIR}/download CACHE PATH "Where to store downloaded game packs")
136
137 #-----------------------------------------------------------------------
138 # Version
139 #-----------------------------------------------------------------------
140
141 # CMake 3.0+ would allow this in project()
142 set(RADIANT_VERSION_MAJOR 1)
143 set(RADIANT_VERSION_MINOR 5)
144 set(RADIANT_VERSION_PATCH 0)
145 set(RADIANT_VERSION "${RADIANT_VERSION_MAJOR}.${RADIANT_VERSION_MINOR}.${RADIANT_VERSION_PATCH}")
146
147 set(RADIANT_ABOUTMSG "Custom build" CACHE STRING "About message")
148
149 find_package(Git REQUIRED)
150
151 execute_process(
152      COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
153      WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
154      OUTPUT_VARIABLE GIT_VERSION
155      OUTPUT_STRIP_TRAILING_WHITESPACE
156 )
157
158 set(RADIANT_VERSION_STRING "${RADIANT_VERSION}n")
159
160 if (GIT_VERSION)
161     set(RADIANT_VERSION_STRING "${RADIANT_VERSION_STRING}-git-${GIT_VERSION}")
162 endif ()
163
164 message(STATUS "Building ${PROJECT_NAME} ${RADIANT_VERSION_STRING} ${RADIANT_ABOUTMSG}")
165
166 #-----------------------------------------------------------------------
167 # Language standard
168 #-----------------------------------------------------------------------
169
170 if (BUILD_BINARIES)
171     set(CMAKE_CXX_STANDARD 11)
172     set(CMAKE_CXX_STANDARD_REQUIRED ON)
173     set(CMAKE_CXX_EXTENSIONS OFF)
174
175     if (CMAKE_VERSION VERSION_LESS "3.1")
176         if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
177             include(CheckCXXCompilerFlag)
178             check_cxx_compiler_flag(--std=c++${CMAKE_CXX_STANDARD} STD_CXX)
179             if (STD_CXX)
180                 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++${CMAKE_CXX_STANDARD}")
181             else ()
182                 message(SEND_ERROR "Requires C++${CMAKE_CXX_STANDARD} or better")
183             endif ()
184         else ()
185             message(WARNING "Unrecognized compiler: ${CMAKE_CXX_COMPILER_ID}, make sure it supports C++${CMAKE_CXX_STANDARD}")
186         endif ()
187     endif ()
188 endif ()
189
190 #-----------------------------------------------------------------------
191 # Flags
192 #-----------------------------------------------------------------------
193
194 if (BUILD_BINARIES)
195     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
196
197     macro (addflags_c args)
198         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${args}")
199     endmacro ()
200
201     macro (addflags_cxx args)
202         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${args}")
203     endmacro ()
204
205     macro (addflags args)
206         addflags_c("${args}")
207         addflags_cxx("${args}")
208     endmacro ()
209
210     addflags("-fno-strict-aliasing")
211     if (NOT WIN32)
212         addflags("-fvisibility=hidden")
213     endif ()
214
215     if (USE_WERROR)
216         addflags("-Werror")
217         addflags("-pedantic-errors")
218     endif ()
219
220     addflags("-Wall")
221     addflags("-Wextra")
222     addflags("-pedantic")
223
224     addflags_c("-Wno-deprecated-declarations") # vfs.c: g_strdown
225
226     addflags("-Wno-unused-function")
227     addflags("-Wno-unused-variable")
228     addflags("-Wno-unused-parameter")
229
230     set(CMAKE_POSITION_INDEPENDENT_CODE 1)
231 endif ()
232
233 if (BUILD_RADIANT)
234     set(GTK_TARGET 2 CACHE STRING "GTK target")
235     add_definitions(-DGTK_TARGET=${GTK_TARGET})
236
237     if (WIN32)
238         add_definitions(-DWORKAROUND_WINDOWS_FLOATING_WINDOW=1)
239
240         if (GTK_TARGET EQUAL 2)
241             add_definitions(-DWORKAROUND_WINDOWS_GTK2_GLWIDGET=1)
242         endif ()
243     endif ()
244
245     if (APPLE)
246         if (GTK_TARGET EQUAL 2)
247             add_definitions(-DWORKAROUND_MACOS_GTK2_DESTROY=1)
248             add_definitions(-DWORKAROUND_MACOS_GTK2_GLWIDGET=1)
249         endif ()
250     endif ()
251 endif ()
252
253 #-----------------------------------------------------------------------
254 # Defs
255 #-----------------------------------------------------------------------
256
257 if (BUILD_BINARIES)
258     add_definitions(-DRADIANT_VERSION="${RADIANT_VERSION}")
259     add_definitions(-DRADIANT_VERSION_STRING="${RADIANT_VERSION_STRING}")
260
261     if (NOT CMAKE_BUILD_TYPE MATCHES Release)
262         add_definitions(-D_DEBUG=1)
263     endif ()
264 endif ()
265
266 if (BUILD_RADIANT)
267     add_definitions(-DRADIANT_ABOUTMSG="${RADIANT_ABOUTMSG}")
268
269     macro (disable_deprecated name gtk2only)
270         add_definitions(-D${name}_DISABLE_SINGLE_INCLUDES)
271         if ((${gtk2only} EQUAL 0) OR (GTK_TARGET EQUAL 2))
272             add_definitions(-D${name}_DISABLE_DEPRECATED)
273         endif ()
274     endmacro ()
275
276     disable_deprecated(ATK 0)
277     disable_deprecated(G 0)
278     disable_deprecated(GDK 0)
279     disable_deprecated(GDK_PIXBUF 0)
280     disable_deprecated(GTK 1)
281     disable_deprecated(PANGO 0)
282 endif ()
283
284 if (BUILD_BINARIES)
285     add_custom_target(binaries ALL)
286
287     if(APPLE OR ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
288         link_directories(
289             /usr/local/lib
290         )
291     endif ()
292
293     if (WIN32)
294         add_definitions(
295             -DWIN32=1
296             -D_WIN32=1
297         )
298     else ()
299         add_definitions(
300             -DPOSIX=1
301         )
302     endif ()
303 endif ()
304
305 if (BUILD_RADIANT AND NOT APPLE AND NOT WIN32)
306     set(XWINDOWS ON)
307     find_package(X11 REQUIRED)
308     include_directories(${X11_INCLUDE_DIR})
309     add_definitions(-DXWINDOWS=1)
310 endif ()
311
312 if (BUILD_BINARIES)
313     include_directories("${PROJECT_SOURCE_DIR}/include")
314     include_directories("${PROJECT_SOURCE_DIR}/libs")
315
316     macro (copy_dlls target)
317         if (BUNDLE_LIBRARIES AND BUNDLING_SUPPORTED)
318             add_custom_command(TARGET ${target} POST_BUILD
319                 COMMAND "${PROJECT_SOURCE_DIR}/library-bundler"
320                 "register" "${PROJECT_BINARY_DIR}" "$<TARGET_FILE:${target}>"
321                 VERBATIM
322             )
323             add_dependencies(bundle ${target})
324         endif ()
325     endmacro ()
326 endif ()
327
328 #-----------------------------------------------------------------------
329 # Bundle
330 #-----------------------------------------------------------------------
331
332 if (BUILD_BINARIES)
333     if (BUNDLE_LIBRARIES AND BUNDLING_SUPPORTED)
334         add_subdirectory(bundle)
335     endif ()
336 endif ()
337
338 #-----------------------------------------------------------------------
339 # Builtins: targets
340 #-----------------------------------------------------------------------
341
342 add_custom_target(builtins)
343
344 if (BUILTIN_GTKGLEXT OR BUILTIN_GTKTHEME_MOJAVE)
345     include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
346 endif ()
347
348 if (BUILTIN_GTKGLEXT)
349     execute_process(COMMAND pkg-config --libs gdk-2.0 gtk+-2.0 glib-2.0 gl
350         OUTPUT_VARIABLE GTKGLEXT_LDFLAGS
351         OUTPUT_STRIP_TRAILING_WHITESPACE
352     )
353     set(GTKGLEXT_LDFLAGS "$ENV{LDFLAGS} -lm ${GTKGLEXT_LDFLAGS}")
354
355     if (NOT CMAKE_BUILD_TYPE MATCHES Release)
356         set(GTKGLEXT_CFLAGS "$ENV{CFLAGS} -g")
357     else ()
358         set(GTKGLEXT_CFLAGS "$ENV{CFLAGS}")
359     endif()
360
361     set(CONFIGURE_OPTIONS --disable-gtk-doc --disable-gtk-doc-html --disable-gtk-doc-pdf)
362
363     if (APPLE)
364         set(GTKGLEXT_GIT_TAG macos)
365     else ()
366         set(GTKGLEXT_GIT_TAG fixes)
367     endif ()
368
369     ExternalProject_Add(gtkglext
370         GIT_REPOSITORY https://gitlab.gnome.org/illwieckz/gtkglext.git
371         GIT_TAG ${GTKGLEXT_GIT_TAG}
372         BUILD_IN_SOURCE ON
373         CONFIGURE_COMMAND export LDFLAGS=${GTKGLEXT_LDFLAGS}
374         CONFIGURE_COMMAND && NOCONFIGURE=1 ./autogen.sh
375         CONFIGURE_COMMAND && ./configure --prefix "${BUILTINS_INSTALL_DIR}" ${CONFIGURE_OPTIONS}
376         BUILD_COMMAND GL_SILENCE_DEPRECATION=1
377         BUILD_COMMAND && $(MAKE) CFLAGS=${GTKGLEXT_CFLAGS} LDFLAGS=${GTKGLEXT_LDFLAGS}
378         INSTALL_COMMAND $(MAKE) install
379     )
380
381     include_directories("${BUILTINS_INSTALL_DIR}/lib/gtkglext-1.0/include")
382     include_directories("${BUILTINS_INSTALL_DIR}/include/gtkglext-1.0")
383
384     add_dependencies(builtins gtkglext)
385     set_target_properties(gtkglext PROPERTIES EXCLUDE_FROM_ALL ON)
386 endif ()
387
388 if (BUNDLE_LIBRARIES AND EXISTS ${BUILTINS_LIB_DIR})
389     set(CMAKE_SKIP_BUILD_RPATH ON)
390 endif()
391
392 if (BUILTIN_GTKTHEME_MOJAVE)
393     ExternalProject_Add(gtktheme-mojave
394         GIT_REPOSITORY https://github.com/vinceliuice/Mojave-gtk-theme.git
395         GIT_TAG master
396         BUILD_IN_SOURCE ON
397         CONFIGURE_COMMAND echo
398         BUILD_COMMAND echo
399         INSTALL_COMMAND ./install.sh -o standard -a standard -s standard -i standard -d "${PROJECT_BINARY_DIR}/themes"
400     )
401     add_dependencies(builtins gtktheme-mojave)
402     set_target_properties(gtktheme-mojave PROPERTIES EXCLUDE_FROM_ALL ON)
403 endif ()
404
405 #-----------------------------------------------------------------------
406 # Libraries
407 #-----------------------------------------------------------------------
408
409 if (BUILD_BINARIES)
410     add_subdirectory(libs)
411 endif ()
412
413 if (BUILD_RADIANT)
414     add_subdirectory(include)
415 endif ()
416
417 #-----------------------------------------------------------------------
418 # Plugins
419 #-----------------------------------------------------------------------
420
421 if (BUILD_RADIANT)
422     add_subdirectory(contrib)
423 endif ()
424
425 #-----------------------------------------------------------------------
426 # Modules
427 #-----------------------------------------------------------------------
428
429 if (BUILD_RADIANT)
430     add_subdirectory(plugins)
431 endif ()
432
433 #-----------------------------------------------------------------------
434 # Radiant
435 #-----------------------------------------------------------------------
436
437 if (BUILD_BINARIES)
438     macro (radiant_tool name)
439         add_dependencies(binaries ${name})
440
441         if (WIN32)
442             add_executable(${name} ${ARGN} ${PROJECT_SOURCE_DIR}/include/lib.rc)
443         else ()
444             add_executable(${name} ${ARGN})
445         endif()
446
447         copy_dlls(${name})
448
449         install(
450             TARGETS ${name}
451             RUNTIME DESTINATION ${RADIANT_BIN_DIR}
452         )
453     endmacro ()
454
455     if (BUILD_RADIANT)
456         add_subdirectory(radiant)
457     endif ()
458 endif ()
459
460 #-----------------------------------------------------------------------
461 # Tools
462 #-----------------------------------------------------------------------
463
464 if (BUILD_TOOLS)
465     add_subdirectory(tools)
466
467     file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/setup/data/tools/*")
468
469     if (NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR))
470         # Copy data files from sources to the build directory
471         message(STATUS "Copying data files")
472         file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}")
473     endif ()
474 endif ()
475
476 #-----------------------------------------------------------------------
477 # Game packs
478 #-----------------------------------------------------------------------
479
480 set(GAMEPACKS_LICENSE_LIST free CACHE STRING "Download game packs by license")
481 set(GAMEPACKS_NAME_LIST none CACHE STRING "Download game packs by name")
482
483 if (DOWNLOAD_GAMEPACKS)
484     add_custom_target(gamepacks ALL
485        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
486        COMMENT "Downloading game packs"
487     )
488 endif ()
489
490 #-----------------------------------------------------------------------
491 # Install
492 #-----------------------------------------------------------------------
493
494 if (BUILD_BINARIES)
495     install(DIRECTORY
496         setup/data/tools/base
497         setup/data/tools/bitmaps
498         setup/data/tools/gl
499         setup/data/tools/plugins
500         docs
501         DESTINATION ${RADIANT_DATA_DIR}
502     )
503
504     install(FILES
505         setup/data/tools/global.xlink
506         DESTINATION ${RADIANT_DATA_DIR}
507     )
508 endif ()
509
510 if (DOWNLOAD_GAMEPACKS)
511     install(DIRECTORY
512         ${PROJECT_BINARY_DIR}/gamepacks
513         DESTINATION ${RADIANT_DATA_DIR}
514     )
515 endif ()
516
517 if (BUILD_BINARIES AND FHS_INSTALL AND NOT WIN32 AND NOT APPLE)
518     install(FILES
519         icons/netradiant.png
520         DESTINATION ${RADIANT_SHARE_DIR}/pixmaps
521     )
522     install(FILES
523         icons/netradiant.desktop
524         DESTINATION ${RADIANT_SHARE_DIR}/applications
525     )
526     install(FILES
527         icons/mime/map.xml
528         DESTINATION ${RADIANT_SHARE_DIR}/mime/model
529     )
530 endif ()
531
532 if (APPLE)
533     install(DIRECTORY
534         setup/macos/Contents
535         DESTINATION ${APPLE_INSTALL_PREFIX}
536     )
537     install(DIRECTORY
538     ${PROJECT_BINARY_DIR}/themes
539         DESTINATION ${APPLE_INSTALL_PREFIX}/Contents/Prefix/share
540     )
541     install(PROGRAMS
542         setup/macos/NetRadiant
543         DESTINATION ${APPLE_INSTALL_PREFIX}/Contents/MacOS
544     )
545 endif ()
546
547 include(cmake/scripts/package.cmake)