Fix mismatched malloc/delete (found using valgrind).
[gemrb.git] / CMakeLists.txt
blob2e0d492a9652c77e5d7d3c04a6e8249e49be7004
1 cmake_minimum_required(VERSION 2.4.4)
2 if(COMMAND cmake_policy)
3       cmake_policy(SET CMP0003 NEW)
4       cmake_policy(SET CMP0005 NEW)
5 endif(COMMAND cmake_policy)
6 # allow empty else and endif constructs (available by default since 2.6.0)
7 set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
9 # If the user specifies -DCMAKE_BUILD_TYPE on the command line, take their definition
10 # and dump it in the cache along with proper documentation, otherwise set CMAKE_BUILD_TYPE
11 # to Release prior to calling PROJECT()
12 IF(DEFINED CMAKE_BUILD_TYPE)
13    SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
14 ELSE(DEFINED CMAKE_BUILD_TYPE)
15    SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
16 ENDIF(DEFINED CMAKE_BUILD_TYPE)
18 PROJECT(gemrb)
19 # try to extract the version from the source
20 execute_process(
21   COMMAND sed -n "s/\#define VERSION_GEMRB .\\([^\"]*\\).$/\\1/p" ${CMAKE_CURRENT_SOURCE_DIR}/gemrb/includes/globals.h
22   OUTPUT_VARIABLE GEMRB_VERSION
23   RESULT_VARIABLE RC
24   OUTPUT_STRIP_TRAILING_WHITESPACE
26 if(${RC} GREATER 0) # lookup failed
27   set(GEMRB_VERSION "unknown")
28 endif(${RC} GREATER 0)
29 message("Detected version: ${GEMRB_VERSION}")
31 IF(PREFIX)
32   SET(PREFIX CACHE PATH "Abbreviation for CMAKE_INSTALL_PREFIX.")
33   SET(CMAKE_INSTALL_PREFIX ${PREFIX})
34 ENDIF(PREFIX)
36 if (NOT LAYOUT)
37         if (WIN32)
38                 set(LAYOUT "home")
39         else (WIN32)
40                 set(LAYOUT "fhs")
41         endif (WIN32)
42 endif (NOT LAYOUT)
44 SET(LAYOUT "${LAYOUT}" CACHE STRING "Directory layout.")
46 # macro that sets a default (path) if one wasn't specified
47 MACRO(SET_PATH variable default)
48         IF(NOT ${variable})
49                 SET(${variable} ${default})
50         ENDIF(NOT ${variable})
51 ENDMACRO(SET_PATH)
53 if (${LAYOUT} MATCHES "home")
54         SET_PATH( PLUGIN_DIR ${CMAKE_INSTALL_PREFIX}/plugins )
55         SET_PATH( DATA_DIR ${CMAKE_INSTALL_PREFIX} )
56         SET_PATH( MAN_DIR ${CMAKE_INSTALL_PREFIX}/man/man6 )
57         SET_PATH( BIN_DIR ${CMAKE_INSTALL_PREFIX} )
58         SET_PATH( SYSCONF_DIR ${CMAKE_INSTALL_PREFIX} )
59         SET_PATH( LIB_DIR ${CMAKE_INSTALL_PREFIX} )
60         SET_PATH( DOC_DIR ${CMAKE_INSTALL_PREFIX}/doc )
61         SET_PATH( ICON_DIR ${CMAKE_INSTALL_PREFIX} )
62         SET_PATH( MENU_DIR ${CMAKE_INSTALL_PREFIX} )
63 elseif (${LAYOUT} MATCHES "fhs")
64         SET_PATH( LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib/gemrb )
65         SET_PATH( PLUGIN_DIR ${LIB_DIR}/plugins )
66         SET_PATH( DATA_DIR ${CMAKE_INSTALL_PREFIX}/share/gemrb )
67         SET_PATH( MAN_DIR ${CMAKE_INSTALL_PREFIX}/share/man/man6 )
68         SET_PATH( BIN_DIR ${CMAKE_INSTALL_PREFIX}/bin )
69         IF( NOT SYSCONF_DIR )
70                 if ( ${CMAKE_INSTALL_PREFIX} STREQUAL "/usr" )
71                         SET( SYSCONF_DIR /etc/gemrb )
72                 else ()
73                         SET( SYSCONF_DIR ${CMAKE_INSTALL_PREFIX}/etc/gemrb )
74                 endif ()
75         ENDIF( NOT SYSCONF_DIR )
76         SET_PATH( DOC_DIR ${CMAKE_INSTALL_PREFIX}/share/doc/gemrb )
77         SET_PATH( ICON_DIR ${CMAKE_INSTALL_PREFIX}/share/pixmaps )
78         SET_PATH( MENU_DIR ${CMAKE_INSTALL_PREFIX}/share/applications )
79 else (${LAYOUT} MATCHES "opt")
80         SET_PATH( LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib )
81         SET_PATH( PLUGIN_DIR ${LIB_DIR}/plugins )
82         SET_PATH( DATA_DIR ${CMAKE_INSTALL_PREFIX}/share/ )
83         SET_PATH( MAN_DIR ${CMAKE_INSTALL_PREFIX}/man/man6 )
84         SET_PATH( BIN_DIR ${CMAKE_INSTALL_PREFIX}/bin )
85         SET_PATH( SYSCONF_DIR ${CMAKE_INSTALL_PREFIX}/etc )
86         SET_PATH( DOC_DIR ${CMAKE_INSTALL_PREFIX}/share/doc/gemrb )
87         SET_PATH( ICON_DIR ${CMAKE_INSTALL_PREFIX}/share/pixmaps )
88         SET_PATH( MENU_DIR ${CMAKE_INSTALL_PREFIX}/share/applications )
89 endif (${LAYOUT} MATCHES "home")
92 IF(CMAKE_COMPILER_IS_GNUCXX)
93         if ( NOT DISABLE_WERROR )
94                 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
95         endif ( NOT DISABLE_WERROR )
96         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -W -Wpointer-arith -Wcast-align")
97         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-format-y2k -Wno-long-long -fno-strict-aliasing")
98         # only export symbols explicitly marked to be exported.
99         INCLUDE(CheckCXXCompilerFlag)
100         CHECK_CXX_COMPILER_FLAG("-fvisibility=hidden" VISIBILITY_HIDDEN)
101         IF (VISIBILITY_HIDDEN)
102                 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
103         ENDIF (VISIBILITY_HIDDEN)
104         if (WIN32)
105                 # GCC 4.5.0+ has shared libstdc++ without dllimport
106                 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--enable-auto-import")
107                 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--enable-auto-import")
108         endif (WIN32)
109         # Ensure all plugin symbols exist.
110         if (NOT APPLE AND NOT UNSAFE_PLUGIN)
111                 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--no-undefined")
112         endif (NOT APPLE AND NOT UNSAFE_PLUGIN)
113 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
115 # Check for all the required and optional dependencies
116 INCLUDE(FindPythonLibs)
117 IF(PYTHON_LIBRARY)
118         MESSAGE(STATUS "Looking for Python libraries: found")
119 ELSE()
120         MESSAGE(SEND_ERROR "Looking for Python libraries: not found!")
121         MESSAGE(FATAL_ERROR "Please get the development libraries from www.python.org")
122 ENDIF()
124 INCLUDE(FindSDL)
125 IF(SDL_FOUND)
126         MESSAGE(STATUS "Looking for SDL: found")
127 ELSE()
128         MESSAGE(SEND_ERROR "Looking for SDL: not found!")
129         MESSAGE(FATAL_ERROR "Please get it from www.libsdl.org")
130 ENDIF()
132 INCLUDE(FindZLIB)
133 IF(ZLIB_FOUND)
134         MESSAGE(STATUS "Looking for Zlib: found")
135 ELSE()
136         MESSAGE(SEND_ERROR "Looking for Zlib: not found!")
137         MESSAGE(FATAL_ERROR "Please install the Zlib library and headers first!")
138 ENDIF()
140 IF(UNIX)
141         FIND_LIBRARY(DLD_LIBRARY_PATH dl dld)
142         IF(DLD_LIBRARY_PATH)
143                 MESSAGE(STATUS "Looking for dl library: found")
144         ELSE(DLD_LIBRARY_PATH)
145                 MESSAGE(FATAL_ERROR "Looking for dl library: not found!")
146         ENDIF(DLD_LIBRARY_PATH)
148         SET(CMAKE_THREAD_PREFER_PTHREAD true)
149         FIND_PACKAGE(Threads REQUIRED)
150 ENDIF(UNIX)
152 INCLUDE(FindOpenAL)
153 IF(OPENAL_FOUND)
154         MESSAGE(STATUS "Looking for OpenAL: found")
155 ELSE()
156         MESSAGE(WARNING "Looking for OpenAL: not found!")
157         MESSAGE(WARNING "If you want to build the OpenAL plugin, get OpenAL from www.openal.org.")
158         MESSAGE(WARNING "If it just wasn't found, try setting the OPENALDIR environment variable.")
159 ENDIF()
161 INCLUDE(FindSDL_mixer)
162 IF(SDLMIXER_FOUND)
163         MESSAGE(STATUS "Looking for SDL_mixer: found")
164 ELSE()
165         MESSAGE(WARNING "Looking for SDL_mixer: not found!")
166         MESSAGE(WARNING "If you want to build the SDL_mixer plugin, install SDL_mixer first.")
167 ENDIF()
169 INCLUDE(FindPNG)
170 IF(PNG_FOUND)
171         MESSAGE(STATUS "Looking for libPNG: found")
172 ELSE()
173         MESSAGE(WARNING "Looking for libPNG: not found!")
174         MESSAGE(WARNING "GemRB will be built without any PNG support. Get it from www.libpng.org" )
175         MESSAGE(WARNING "While no original game data is in PNG format, some mod data is and will need conversion.")
176 ENDIF()
178 FIND_LIBRARY(VORBIS_LIBRARY vorbisfile)
179 IF(VORBIS_LIBRARY)
180         MESSAGE(STATUS "Looking for Ogg Vorbis support: found")
181 ELSE()
182         MESSAGE(WARNING "Looking for Ogg Vorbis support: not found!")
183         MESSAGE(WARNING "While no original game data is in OGG format, some mod data is and will need conversion.")
184 ENDIF()
186 ADD_DEFINITIONS("-DHAVE_CONFIG_H")
188 # On Release builds cmake automatically defines NDEBUG, so we
189 # explicitly undefine it:
190 if(CMAKE_BUILD_TYPE STREQUAL "Release" AND NOT MSVC)
191         ADD_DEFINITIONS("-UNDEBUG")
192 endif()
194 if (STATIC_LINK)
195         if (NOT WIN32 AND NOT APPLE)
196                 ADD_DEFINITIONS("-DSTATIC_LINK")
197         else (NOT WIN32 AND NOT APPLE)
198                 unset(STATIC_LINK CACHE)
199                 MESSAGE(STATUS "Static linking not (yet) supported on this platform.")
200         endif (NOT WIN32 AND NOT APPLE)
201 endif (STATIC_LINK)
203 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} gemrb/includes gemrb/core)
205 #Platform checks
206 INCLUDE (CheckTypeSize)
207 CHECK_TYPE_SIZE("int" SIZEOF_INT)
208 CHECK_TYPE_SIZE("long int" SIZEOF_LONG_INT)
210 INCLUDE (CheckFunctionExists)
211 CHECK_FUNCTION_EXISTS("snprintf" HAVE_SNPRINTF)
212 CHECK_FUNCTION_EXISTS("strndup" HAVE_STRNDUP)
214 #Unneeded on windows
215 IF(NOT WIN32)
216         INCLUDE (CheckCXXSourceCompiles)
217         CHECK_CXX_SOURCE_COMPILES("typedef void *(* voidvoid)(void);
219                 void *object = 0;
220                 voidvoid function;
221                 function = (voidvoid) object;
222                 " PERMITS_OBJECT_TO_FUNCTION_CAST)
224         IF( NOT PERMITS_OBJECT_TO_FUNCTION_CAST )
225                 SET(HAVE_FORBIDDEN_OBJECT_TO_FUNCTION_CAST 1)
226         ENDIF( NOT PERMITS_OBJECT_TO_FUNCTION_CAST )
227 ENDIF(NOT WIN32)
229 IF(VORBIS_LIBRARY)
230  SET(HAS_VORBIS_SUPPORT 1)
231 ELSE(VORBIS_LIBRARY)
232  SET(VORBIS_LIBRARY "")
233 ENDIF(VORBIS_LIBRARY)
235 IF(APPLE)
236  FIND_LIBRARY(SDL_MAIN_LIBRARY_PATH SDLmain)
237  FIND_LIBRARY(COCOA_LIBRARY_PATH Cocoa)
238 ENDIF(APPLE)
240 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake_config.h.in
241         ${CMAKE_CURRENT_BINARY_DIR}/config.h ESCAPE_QUOTES)
243 #Plugin addition macro
244 MACRO(ADD_GEMRB_PLUGIN plugin_name)
245         if (STATIC_LINK)
246                 ADD_LIBRARY(${plugin_name} STATIC ${ARGN})
247                 set(plugins "${plugins};${plugin_name}" PARENT_SCOPE)
248         else (STATIC_LINK)
249                 ADD_LIBRARY(${plugin_name} MODULE ${ARGN})
250                 if (NOT UNSAFE_PLUGIN)
251                         TARGET_LINK_LIBRARIES(${plugin_name} gemrb_core ${CMAKE_THREAD_LIBS_INIT})
252                 endif (NOT UNSAFE_PLUGIN)
253                 INSTALL(TARGETS ${plugin_name} DESTINATION ${PLUGIN_DIR})
254         endif (STATIC_LINK)
255         SET_TARGET_PROPERTIES(${plugin_name} PROPERTIES PREFIX ""
256                 INSTALL_RPATH ${LIB_DIR}
257                 LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/gemrb/plugins)
258 ENDMACRO(ADD_GEMRB_PLUGIN)
260 #gemrb overrides macro
261 MACRO(ADD_GEMRB_OVERRIDE game_name)
262         FILE( GLOB FILES_TO_INSTALL *.2da *.bmp *.ini *.chu *.ids *.bcs *.vvc *.mos *.spl *.wav *.pro)
263         INSTALL( FILES ${FILES_TO_INSTALL} DESTINATION ${DATA_DIR}/override/${game_name} )
264 ENDMACRO(ADD_GEMRB_OVERRIDE )
266 ADD_SUBDIRECTORY( gemrb )
267 INSTALL( FILES gemrb.6 DESTINATION ${MAN_DIR} )
268 INSTALL( FILES artwork/gemrb-logo.png DESTINATION ${ICON_DIR} )
269 INSTALL( FILES gemrb.desktop DESTINATION ${MENU_DIR} )
270 INSTALL( FILES README INSTALL COPYING NEWS AUTHORS DESTINATION ${DOC_DIR} )
272 CONFIGURE_FILE(
273   "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
274   "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
275   IMMEDIATE @ONLY
278 # copy the variable, since the file uses @VERSION@
279 set(VERSION ${GEMRB_VERSION})
280 CONFIGURE_FILE(
281   "${CMAKE_CURRENT_SOURCE_DIR}/gemrb.spec.in"
282   "${CMAKE_CURRENT_BINARY_DIR}/gemrb.spec"
283   IMMEDIATE @ONLY
286 ADD_CUSTOM_TARGET( uninstall
287   "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" )
289 # make dist for a gzipped tarball of current HEAD
290 set(ARCHIVE_NAME ${CMAKE_PROJECT_NAME}-${GEMRB_VERSION})
291 add_custom_target( dist
292   COMMAND git archive --prefix=${ARCHIVE_NAME}/ HEAD
293     | gzip --best > ${CMAKE_BINARY_DIR}/${ARCHIVE_NAME}.tar.gz
294   WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
297 message(STATUS "")
298 message(STATUS "These are the configured paths:")
299 message(STATUS "  PREFIX: ${CMAKE_INSTALL_PREFIX}")
300 message(STATUS "  LIB_DIR: ${LIB_DIR}")
301 message(STATUS "  PLUGIN_DIR: ${PLUGIN_DIR}")
302 message(STATUS "  BIN_DIR: ${BIN_DIR}")
303 message(STATUS "  DATA_DIR: ${DATA_DIR}")
304 message(STATUS "  MAN_DIR: ${MAN_DIR}")
305 message(STATUS "  SYSCONF_DIR: ${SYSCONF_DIR}")
306 message(STATUS "  DOC_DIR: ${DOC_DIR}")
307 message(STATUS "  ICON_DIR: ${ICON_DIR}")
308 message(STATUS "  MENU_DIR: ${MENU_DIR}")
309 message(STATUS "")
310 message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
311 message(STATUS "")