q3map2: use ~/Library/Application Support on Mac
[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_SUBDIR . CACHE INTERNAL "...")
29 set(RADIANT_BIN_SUBDIR . CACHE INTERNAL "...")
30 set(RADIANT_DATA_SUBDIR . CACHE INTERNAL "...")
31
32 if (FHS_INSTALL)
33     set(RADIANT_LIB_SUBDIR lib/${RADIANT_BASENAME})
34     set(RADIANT_BIN_SUBDIR bin)
35     set(RADIANT_DATA_SUBDIR 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     elseif (WIN32)
182         add_definitions(
183                 -DWIN32=1
184                 -D_WIN32=1
185         )
186     else ()
187         if (BUILD_RADIANT)
188         set(XWINDOWS ON)
189         endif()
190
191         add_definitions(
192                 -DPOSIX=1
193         )
194     endif ()
195 endif ()
196
197 if (BUILD_RADIANT AND XWINDOWS)
198     find_package(X11 REQUIRED)
199     include_directories(${X11_INCLUDE_DIR})
200     add_definitions(-DXWINDOWS=1)
201 endif ()
202
203 if (BUILD_BINARIES)
204     include_directories("${PROJECT_SOURCE_DIR}/include")
205     include_directories("${PROJECT_SOURCE_DIR}/libs")
206
207     if (WIN32 AND NOT CMAKE_CROSSCOMPILING)
208         set(BUNDLE_LIBRARIES_DEFAULT ON)
209     else ()
210         set(BUNDLE_LIBRARIES_DEFAULT OFF)
211     endif ()
212
213     option(BUNDLE_LIBRARIES "Bundle libraries" ${BUNDLE_LIBRARIES_DEFAULT})
214
215     macro(copy_dlls target)
216         if (BUNDLE_LIBRARIES)
217             add_custom_command(TARGET ${target} POST_BUILD
218                     COMMAND bash
219                     ARGS -c "ldd '$<TARGET_FILE:${target}>' | grep -v /c/Windows | awk '{ print $1 }' | while read dll; do cp \"$(which $dll)\" '${PROJECT_BINARY_DIR}'; done"
220                     VERBATIM
221                     )
222         endif ()
223     endmacro()
224 endif ()
225
226 #-----------------------------------------------------------------------
227 # Libraries
228 #-----------------------------------------------------------------------
229
230 if (BUILD_BINARIES)
231     add_subdirectory(libs)
232 endif ()
233
234 if (BUILD_RADIANT)
235     add_subdirectory(include)
236 endif ()
237
238 #-----------------------------------------------------------------------
239 # Plugins
240 #-----------------------------------------------------------------------
241
242 if (BUILD_RADIANT)
243     add_subdirectory(contrib)
244 endif ()
245
246 #-----------------------------------------------------------------------
247 # Modules
248 #-----------------------------------------------------------------------
249
250 if (BUILD_RADIANT)
251     add_subdirectory(plugins)
252 endif ()
253
254 #-----------------------------------------------------------------------
255 # Radiant
256 #-----------------------------------------------------------------------
257
258 if (BUILD_BINARIES)
259     if (CMAKE_EXECUTABLE_SUFFIX)
260         string(REGEX REPLACE "^[.]" "" RADIANT_EXECUTABLE ${CMAKE_EXECUTABLE_SUFFIX})
261     endif ()
262
263     macro(radiant_tool name)
264         add_executable(${name} ${ARGN})
265         install(
266                 TARGETS ${name}
267                 RUNTIME DESTINATION ${RADIANT_BIN_SUBDIR}/.
268         )
269     endmacro()
270
271     if (BUILD_RADIANT)
272         add_subdirectory(radiant _radiant)
273         set_target_properties(${RADIANT_BASENAME} PROPERTIES
274                 COMPILE_DEFINITIONS RADIANT_EXECUTABLE="${RADIANT_EXECUTABLE}"
275                 )
276     endif ()
277 endif ()
278
279 #-----------------------------------------------------------------------
280 # Tools
281 #-----------------------------------------------------------------------
282
283 if (BUILD_TOOLS)
284     add_subdirectory(tools)
285
286     file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/setup/data/tools/*")
287
288     if (NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR))
289         # Copy data files from sources to the build directory
290         message(STATUS "Copying data files")
291         file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}")
292     endif ()
293 endif ()
294
295 #-----------------------------------------------------------------------
296 # Game packs
297 #-----------------------------------------------------------------------
298
299 set(GAMEPACKS_LICENSE_LIST free CACHE STRING "Download game packs by license")
300 set(GAMEPACKS_NAME_LIST none CACHE STRING "Download game packs by name")
301
302 if (DOWNLOAD_GAMEPACKS)
303    add_custom_target(game_packs ALL
304       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
305       COMMENT "Downloading ${GAMEPACKS_LICENSE_LIST} game packs"
306    )
307 endif()
308
309 #-----------------------------------------------------------------------
310 # Install
311 #-----------------------------------------------------------------------
312
313 if (BUILD_BINARIES)
314     install(DIRECTORY
315             setup/data/tools/
316             docs
317             DESTINATION ${RADIANT_DATA_SUBDIR}/.
318     )
319 endif ()
320
321 if (DOWNLOAD_GAMEPACKS)
322     if (FHS_INSTALL)
323        set(GAME_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/share/netradiant")
324     else ()
325        set(GAME_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
326     endif ()
327     install(CODE
328             "execute_process(COMMAND \"${PROJECT_SOURCE_DIR}/gamepack-manager\" --license ${GAMEPACKS_LICENSE_LIST} --name ${GAMEPACKS_NAME_LIST} --download-dir \"${PROJECT_BINARY_DIR}/download\" --install-dir \"${GAME_INSTALL_PREFIX}\" --install)"
329         )
330 endif()
331
332 if(BUILD_BINARIES AND FHS_INSTALL AND NOT WIN32 AND NOT APPLE)
333     install(FILES
334             ${PROJECT_SOURCE_DIR}/icons/netradiant.png
335             DESTINATION ${CMAKE_INSTALL_PREFIX}/share/pixmaps
336     )
337     install(FILES
338             ${PROJECT_SOURCE_DIR}/icons/netradiant.desktop
339             DESTINATION ${CMAKE_INSTALL_PREFIX}/share/applications
340     )
341     install(FILES
342             ${PROJECT_SOURCE_DIR}/icons/mime/map.xml
343             DESTINATION ${CMAKE_INSTALL_PREFIX}/share/mime/model
344     )
345 endif()
346
347 include(cmake/scripts/package.cmake)