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