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