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