Remove leading . in CMAKE_EXECUTABLE_SUFFIX if it has one, but don't modify it
[xonotic/netradiant.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8.11 FATAL_ERROR)
2 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
3
4 project(NetRadiant C CXX)
5 option(BUILD_RADIANT "Build the gui" ON)
6
7 #-----------------------------------------------------------------------
8 # Version
9 #-----------------------------------------------------------------------
10
11 # CMake 3.0+ would allow this in project()
12 set(NetRadiant_VERSION_MAJOR 1)
13 set(NetRadiant_VERSION_MINOR 5)
14 set(NetRadiant_VERSION_PATCH 0)
15 set(NetRadiant_VERSION "${NetRadiant_VERSION_MAJOR}.${NetRadiant_VERSION_MINOR}.${NetRadiant_VERSION_PATCH}")
16
17 file(WRITE "${PROJECT_BINARY_DIR}/RADIANT_MAJOR" ${NetRadiant_VERSION_MAJOR})
18 file(WRITE "${PROJECT_BINARY_DIR}/RADIANT_MINOR" ${NetRadiant_VERSION_MINOR})
19
20 set(RADIANT_ABOUTMSG "Custom build" CACHE STRING "About message")
21
22 find_package(Git REQUIRED)
23 execute_process(
24         COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
25         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
26         OUTPUT_VARIABLE GIT_VERSION
27         OUTPUT_STRIP_TRAILING_WHITESPACE
28 )
29 set(RADIANT_VERSION_STRING "${NetRadiant_VERSION}n")
30 if (GIT_VERSION)
31     set(RADIANT_VERSION_STRING "${RADIANT_VERSION_STRING}-git-${GIT_VERSION}")
32 endif ()
33
34 message(STATUS "Building ${PROJECT_NAME} ${RADIANT_VERSION_STRING} ${RADIANT_ABOUTMSG}")
35
36 #-----------------------------------------------------------------------
37 # Language standard
38 #-----------------------------------------------------------------------
39
40 set(CMAKE_CXX_STANDARD 11)
41 set(CMAKE_CXX_STANDARD_REQUIRED ON)
42 # For some reason the above flags don't really work...
43 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
44     include(CheckCXXCompilerFlag)
45     check_cxx_compiler_flag(--std=c++${CMAKE_CXX_STANDARD} STD_CXX)
46     if (STD_CXX)
47         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++${CMAKE_CXX_STANDARD}")
48     else ()
49         message(SEND_ERROR "Requires C++${CMAKE_CXX_STANDARD} or better")
50     endif ()
51 else ()
52     message(WARNING "Unrecognized compiler: ${CMAKE_CXX_COMPILER_ID}, make sure it supports C++${CMAKE_CXX_STANDARD}")
53 endif ()
54
55 #-----------------------------------------------------------------------
56 # Flags
57 #-----------------------------------------------------------------------
58
59 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}   -fno-strict-aliasing")
60 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -fno-exceptions -fno-rtti")
61 set(CMAKE_POSITION_INDEPENDENT_CODE 1)
62 set(GTK_NS "GTK2" CACHE STRING "GTK variable prefix")
63
64 #-----------------------------------------------------------------------
65 # Defs
66 #-----------------------------------------------------------------------
67
68 add_definitions(-DRADIANT_VERSION="${NetRadiant_VERSION}")
69 add_definitions(-DRADIANT_MAJOR_VERSION="${NetRadiant_VERSION_MAJOR}")
70 add_definitions(-DRADIANT_MINOR_VERSION="${NetRadiant_VERSION_MINOR}")
71
72 add_definitions(-DRADIANT_ABOUTMSG="${NetRadiant_ABOUT}")
73
74 if (NOT CMAKE_BUILD_TYPE MATCHES Release)
75     add_definitions(-D_DEBUG=1)
76 endif ()
77
78 if (APPLE)
79     option(XWINDOWS "Build against X11" ON)
80     add_definitions(
81             -DPOSIX=1
82     )
83 elseif (WIN32)
84     add_definitions(
85             -DWIN32=1
86             -D_WIN32=1
87     )
88 else ()
89     set(XWINDOWS ON)
90     add_definitions(
91             -DPOSIX=1
92     )
93 endif ()
94
95 if (XWINDOWS)
96     find_package(X11 REQUIRED)
97     include_directories(${X11_INCLUDE_DIR})
98     add_definitions(-DXWINDOWS=1)
99 endif ()
100
101 include_directories("${PROJECT_SOURCE_DIR}/include")
102 include_directories("${PROJECT_SOURCE_DIR}/libs")
103
104 macro(copy_dlls target)
105     if (WIN32)
106         add_custom_command(TARGET ${target} POST_BUILD
107             COMMAND bash
108             ARGS -c "ldd '$<TARGET_FILE:${target}>' | grep -v /c/Windows | awk '{ print $1 }' | while read dll; do cp \"$(which $dll)\" '${PROJECT_BINARY_DIR}'; done"
109             VERBATIM
110         )
111     endif ()
112 endmacro()
113
114 #-----------------------------------------------------------------------
115 # Libraries
116 #-----------------------------------------------------------------------
117
118 add_subdirectory(libs)
119 add_subdirectory(include)
120
121 #-----------------------------------------------------------------------
122 # Plugins
123 #-----------------------------------------------------------------------
124
125 if (BUILD_RADIANT)
126     add_subdirectory(contrib)
127 endif ()
128
129 #-----------------------------------------------------------------------
130 # Modules
131 #-----------------------------------------------------------------------
132
133 if (BUILD_RADIANT)
134     add_subdirectory(plugins)
135 endif ()
136
137 #-----------------------------------------------------------------------
138 # Radiant
139 #-----------------------------------------------------------------------
140
141 if (BUILD_RADIANT)
142     add_subdirectory(radiant build)
143 endif ()
144
145 if (CMAKE_EXECUTABLE_SUFFIX)
146     string(REGEX REPLACE "^[.]" "" RADIANT_EXECUTABLE ${CMAKE_EXECUTABLE_SUFFIX})
147 else ()
148     execute_process(
149             COMMAND uname -m
150             OUTPUT_VARIABLE RADIANT_EXECUTABLE
151             OUTPUT_STRIP_TRAILING_WHITESPACE
152     )
153 endif ()
154
155 if (BUILD_RADIANT)
156     set_target_properties(radiant PROPERTIES
157             COMPILE_DEFINITIONS RADIANT_EXECUTABLE="${RADIANT_EXECUTABLE}"
158             )
159 endif ()
160
161 #-----------------------------------------------------------------------
162 # Tools
163 #-----------------------------------------------------------------------
164
165 add_subdirectory(tools)
166
167 if (NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR))
168     # Copy data files from sources to the build directory
169     message(STATUS "Copying data files")
170     file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/setup/data/tools/*")
171     file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}")
172     file(GLOB DATA_FILES "${PROJECT_SOURCE_DIR}/docs/*")
173     file(COPY ${DATA_FILES} DESTINATION "${PROJECT_BINARY_DIR}/docs")
174 endif ()
175
176 #-----------------------------------------------------------------------
177 # Game packs
178 #-----------------------------------------------------------------------
179
180 option(DOWNLOAD_GAMEPACKS "Download game packs" ON)
181 add_custom_target(game_packs
182         COMMAND ${CMAKE_COMMAND} -E make_directory games
183         COMMAND DOWNLOAD_GAMEPACKS=yes SOURCE_DIR="${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/install-gamepacks.sh" "${PROJECT_BINARY_DIR}"
184         COMMENT "Downloading game packs"
185         )
186 if (DOWNLOAD_GAMEPACKS)
187     add_custom_target(game_packs_all ALL DEPENDS game_packs)
188 endif ()