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