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