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