b01730e8fcd1690b2d081fefb019385719e6d542
[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 $(MAKE) CFLAGS=${GTKGLEXT_CFLAGS} LDFLAGS=${GTKGLEXT_LDFLAGS}
377         INSTALL_COMMAND $(MAKE) install
378     )
379
380     include_directories("${BUILTINS_INSTALL_DIR}/lib/gtkglext-1.0/include")
381     include_directories("${BUILTINS_INSTALL_DIR}/include/gtkglext-1.0")
382
383     add_dependencies(builtins gtkglext)
384     set_target_properties(gtkglext PROPERTIES EXCLUDE_FROM_ALL ON)
385 endif ()
386
387 if (BUNDLE_LIBRARIES AND EXISTS ${BUILTINS_LIB_DIR})
388     set(CMAKE_SKIP_BUILD_RPATH ON)
389 endif()
390
391 if (BUILTIN_GTKTHEME_MOJAVE)
392         ExternalProject_Add(gtktheme-mojave
393         GIT_REPOSITORY https://github.com/vinceliuice/Mojave-gtk-theme.git
394         GIT_TAG master
395         BUILD_IN_SOURCE ON
396         CONFIGURE_COMMAND echo
397         BUILD_COMMAND echo
398         INSTALL_COMMAND ./install.sh -o standard -a standard -s standard -i standard -d "${PROJECT_BINARY_DIR}/themes"
399     )
400     add_dependencies(builtins gtktheme-mojave)
401     set_target_properties(gtktheme-mojave PROPERTIES EXCLUDE_FROM_ALL ON)
402 endif ()
403
404 #-----------------------------------------------------------------------
405 # Libraries
406 #-----------------------------------------------------------------------
407
408 if (BUILD_BINARIES)
409     add_subdirectory(libs)
410 endif ()
411
412 if (BUILD_RADIANT)
413     add_subdirectory(include)
414 endif ()
415
416 #-----------------------------------------------------------------------
417 # Plugins
418 #-----------------------------------------------------------------------
419
420 if (BUILD_RADIANT)
421     add_subdirectory(contrib)
422 endif ()
423
424 #-----------------------------------------------------------------------
425 # Modules
426 #-----------------------------------------------------------------------
427
428 if (BUILD_RADIANT)
429     add_subdirectory(plugins)
430 endif ()
431
432 #-----------------------------------------------------------------------
433 # Radiant
434 #-----------------------------------------------------------------------
435
436 if (BUILD_BINARIES)
437     macro (radiant_tool name)
438         add_dependencies(binaries ${name})
439
440         if (WIN32)
441             add_executable(${name} ${ARGN} ${PROJECT_SOURCE_DIR}/include/lib.rc)
442         else ()
443             add_executable(${name} ${ARGN})
444         endif()
445
446         copy_dlls(${name})
447
448         install(
449             TARGETS ${name}
450             RUNTIME DESTINATION ${RADIANT_BIN_DIR}
451         )
452     endmacro ()
453
454     if (BUILD_RADIANT)
455         add_subdirectory(radiant)
456     endif ()
457 endif ()
458
459 #-----------------------------------------------------------------------
460 # Tools
461 #-----------------------------------------------------------------------
462
463 if (BUILD_TOOLS)
464     add_subdirectory(tools)
465
466     file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/setup/data/tools/*")
467
468     if (NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR))
469         # Copy data files from sources to the build directory
470         message(STATUS "Copying data files")
471         file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}")
472     endif ()
473 endif ()
474
475 #-----------------------------------------------------------------------
476 # Game packs
477 #-----------------------------------------------------------------------
478
479 set(GAMEPACKS_LICENSE_LIST free CACHE STRING "Download game packs by license")
480 set(GAMEPACKS_NAME_LIST none CACHE STRING "Download game packs by name")
481
482 if (DOWNLOAD_GAMEPACKS)
483     add_custom_target(gamepacks ALL
484        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
485        COMMENT "Downloading game packs"
486     )
487 endif ()
488
489 #-----------------------------------------------------------------------
490 # Install
491 #-----------------------------------------------------------------------
492
493 if (BUILD_BINARIES)
494     install(DIRECTORY
495         setup/data/tools/base
496         setup/data/tools/bitmaps
497         setup/data/tools/gl
498         setup/data/tools/plugins
499         docs
500         DESTINATION ${RADIANT_DATA_DIR}
501     )
502
503     install(FILES
504         setup/data/tools/global.xlink
505         DESTINATION ${RADIANT_DATA_DIR}
506     )
507 endif ()
508
509 if (DOWNLOAD_GAMEPACKS)
510     install(DIRECTORY
511         ${PROJECT_BINARY_DIR}/gamepacks
512         DESTINATION ${RADIANT_DATA_DIR}
513     )
514 endif ()
515
516 if (BUILD_BINARIES AND FHS_INSTALL AND NOT WIN32 AND NOT APPLE)
517     install(FILES
518         icons/netradiant.png
519         DESTINATION ${RADIANT_SHARE_DIR}/pixmaps
520     )
521     install(FILES
522         icons/netradiant.desktop
523         DESTINATION ${RADIANT_SHARE_DIR}/applications
524     )
525     install(FILES
526         icons/mime/map.xml
527         DESTINATION ${RADIANT_SHARE_DIR}/mime/model
528     )
529 endif ()
530
531 if (APPLE)
532     install(DIRECTORY
533         setup/macos/Contents
534         DESTINATION ${APPLE_INSTALL_PREFIX}
535     )
536     install(DIRECTORY
537     ${PROJECT_BINARY_DIR}/themes
538         DESTINATION ${APPLE_INSTALL_PREFIX}/Contents/Prefix/share
539     )
540     install(PROGRAMS
541         setup/macos/NetRadiant
542         DESTINATION ${APPLE_INSTALL_PREFIX}/Contents/MacOS
543     )
544 endif ()
545
546 include(cmake/scripts/package.cmake)