Allow for overriding the default lib destination with LIB_INSTALL_DIR
[openal-soft/openal-hmr.git] / CMakeLists.txt
blob810f61e7f809a8ed122898584e3f70fd7e1de8d2
1 # CMake build file list for OpenAL
3 INCLUDE(CheckFunctionExists)
4 INCLUDE(CheckLibraryExists)
5 INCLUDE(CheckIncludeFile)
6 INCLUDE(CheckIncludeFiles)
7 INCLUDE(CheckCCompilerFlag)
8 INCLUDE(CheckCSourceCompiles)
9 INCLUDE(CheckTypeSize)
12 PROJECT(OpenAL C)
15 SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
18 OPTION(ALSA    "Check for ALSA backend"                ON)
19 OPTION(OSS     "Check for OSS backend"                 ON)
20 OPTION(DSOUND  "Check for DirectSound backend"         ON)
21 OPTION(WINMM   "Check for Windows Multimedia backend"  ON)
23 OPTION(DLOPEN  "Check for the dlopen API for loading optional libs"  ON)
25 OPTION(WERROR  "Treat compile warnings as errors"      OFF)
27 OPTION(EXAMPLES  "Build example programs"              ON)
30 SET(LIB_MAJOR_VERSION "1")
31 SET(LIB_MINOR_VERSION "4")
32 SET(LIB_BUILD_VERSION "272")
33 SET(LIB_VERSION "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_BUILD_VERSION}")
34 IF(NOT DEFINED LIB_INSTALL_DIR)
35   SET(LIB_INSTALL_DIR "lib")
36 ENDIF(NOT DEFINED LIB_INSTALL_DIR)
39 CHECK_TYPE_SIZE("long" SIZEOF_LONG)
40 CHECK_TYPE_SIZE("long long" SIZEOF_LONG_LONG)
41 CHECK_TYPE_SIZE("unsigned int" SIZEOF_UINT)
42 CHECK_TYPE_SIZE("void*" SIZEOF_VOIDP)
45 # Add definitions, compiler switches, etc.
46 INCLUDE_DIRECTORIES(OpenAL32/Include include "${OpenAL_BINARY_DIR}")
48 IF(NOT CMAKE_BUILD_TYPE)
49     SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
50         "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
51         FORCE)
52 ENDIF()
54 IF(MSVC)
55     # ???
56     SET(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -D_DEBUG")
57     SET(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -DNDEBUG")
58     SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DNDEBUG")
59     SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
60 ELSE()
61     ADD_DEFINITIONS(-Wall)
62     CHECK_C_COMPILER_FLAG(-Wextra HAVE_W_EXTRA)
63     IF("${HAVE_W_EXTRA}")
64         ADD_DEFINITIONS(-Wextra)
65     ENDIF()
67     IF(WERROR)
68         ADD_DEFINITIONS(-Werror)
69     ENDIF()
71     SET(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O2 -funroll-loops -D_DEBUG" CACHE STRING
72         "Flags used by the compiler during Release with Debug Info builds."
73         FORCE)
74     SET(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG" CACHE STRING
75         "Flags used by the compiler during release minsize builds."
76         FORCE)
77     SET(CMAKE_C_FLAGS_RELEASE "-O2 -funroll-loops -fomit-frame-pointer -DNDEBUG" CACHE STRING
78         "Flags used by the compiler during release builds"
79         FORCE)
80     SET(CMAKE_C_FLAGS_DEBUG "-g3 -D_DEBUG" CACHE STRING
81         "Flags used by the compiler during debug builds."
82         FORCE)
84     # Set visibility options if available
85     IF(NOT WIN32)
86         CHECK_C_SOURCE_COMPILES("int foo() __attribute__((destructor));
87                                  int main() {return 0;}" HAVE_GCC_DESTRUCTOR)
89         CHECK_C_COMPILER_FLAG(-fvisibility=hidden HAVE_VISIBILITY_SWITCH)
90         IF("${HAVE_VISIBILITY_SWITCH}")
91             CHECK_C_SOURCE_COMPILES("int foo() __attribute__((visibility(\"default\")));
92                                      int main() {return 0;}" HAVE_GCC_VISIBILITY)
93             IF("${HAVE_GCC_VISIBILITY}")
94                 ADD_DEFINITIONS(-fvisibility=hidden -DHAVE_GCC_VISIBILITY)
95             ENDIF()
96         ENDIF()
97     ENDIF()
98 ENDIF()
101 CHECK_LIBRARY_EXISTS(m  sqrtf  "" HAVE_SQRTF)
102 CHECK_LIBRARY_EXISTS(m  acosf  "" HAVE_ACOSF)
103 IF(HAVE_SQRTF OR HAVE_ACOSF)
104     SET(EXTRA_LIBS m ${EXTRA_LIBS})
105 ENDIF()
106 CHECK_FUNCTION_EXISTS(strtof HAVE_STRTOF)
108 CHECK_FUNCTION_EXISTS(strcasecmp HAVE_STRCASECMP)
109 IF(NOT HAVE_STRCASECMP)
110     CHECK_FUNCTION_EXISTS(_stricmp HAVE__STRICMP)
111     IF(NOT HAVE__STRICMP)
112         MESSAGE(FATAL_ERROR "No case-insensitive compare function found, please report!")
113     ENDIF()
115     ADD_DEFINITIONS(-Dstrcasecmp=_stricmp)
116 ENDIF()
118 CHECK_FUNCTION_EXISTS(strncasecmp HAVE_STRNCASECMP)
119 IF(NOT HAVE_STRNCASECMP)
120     CHECK_FUNCTION_EXISTS(_strnicmp HAVE__STRNICMP)
121     IF(NOT HAVE__STRNICMP)
122         MESSAGE(FATAL_ERROR "No case-insensitive size-limitted compare function found, please report!")
123     ENDIF()
125     ADD_DEFINITIONS(-Dstrncasecmp=_strnicmp)
126 ENDIF()
128 CHECK_FUNCTION_EXISTS(snprintf HAVE_SNPRINTF)
129 IF(NOT HAVE_SNPRINTF)
130     CHECK_FUNCTION_EXISTS(_snprintf HAVE__SNPRINTF)
131     IF(NOT HAVE__SNPRINTF)
132         MESSAGE(FATAL_ERROR "No snprintf function found, please report!")
133     ENDIF()
135     ADD_DEFINITIONS(-Dsnprintf=_snprintf)
136 ENDIF()
138 # Check for the dlopen API (for dynamicly loading backend libs)
139 IF(DLOPEN)
140     CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
141     IF(HAVE_DLFCN_H)
142         CHECK_LIBRARY_EXISTS(dl dlopen "" HAVE_LIBDL)
143         IF(HAVE_LIBDL)
144             SET(EXTRA_LIBS dl ${EXTRA_LIBS})
145         ENDIF()
146     ENDIF()
147 ENDIF()
149 # Check if we have Windows headers
150 CHECK_INCLUDE_FILE(windows.h HAVE_WINDOWS_H -D_WIN32_WINNT=0x0500)
151 IF(NOT HAVE_WINDOWS_H)
152     CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
153     IF(NOT HAVE_GETTIMEOFDAY)
154         MESSAGE(FATAL_ERROR "No timing function found!")
155     ENDIF()
157     CHECK_FUNCTION_EXISTS(nanosleep HAVE_NANOSLEEP)
158     IF(NOT HAVE_NANOSLEEP)
159         MESSAGE(FATAL_ERROR "No sleep function found!")
160     ENDIF()
162     # We need pthreads outside of Windows
163     CHECK_INCLUDE_FILE(pthread.h HAVE_PTHREAD_H)
164     IF(NOT HAVE_PTHREAD_H)
165         MESSAGE(FATAL_ERROR "PThreads is required for non-Windows builds!")
166     ENDIF()
168     # Some systems need pthread_np.h to get recursive mutexes
169     CHECK_INCLUDE_FILES("pthread.h;pthread_np.h" HAVE_PTHREAD_NP_H)
171     # _GNU_SOURCE is needed on some systems for extra attributes, and
172     # _REENTRANT is needed for libc thread-safety
173     ADD_DEFINITIONS(-D_GNU_SOURCE=1 -D_REENTRANT)
174     CHECK_LIBRARY_EXISTS(pthread pthread_create "" HAVE_LIBPTHREAD)
175     IF(HAVE_LIBPTHREAD)
176         SET(EXTRA_LIBS pthread ${EXTRA_LIBS})
177     ENDIF()
178 ENDIF()
180 # Check for a 64-bit type
181 CHECK_INCLUDE_FILE(stdint.h HAVE_STDINT_H)
182 IF(NOT HAVE_STDINT_H)
183     IF(HAVE_WINDOWS_H)
184         CHECK_C_SOURCE_COMPILES("\#define _WIN32_WINNT 0x0500
185                                  \#include <windows.h>
186                                  __int64 foo;
187                                  int main() {return 0;}" HAVE___INT64)
188     ENDIF()
189     IF(NOT HAVE___INT64)
190         IF(NOT SIZEOF_LONG MATCHES "8")
191             IF(NOT SIZEOF_LONG_LONG MATCHES "8")
192                 MESSAGE(FATAL_ERROR "No 64-bit types found, please report!")
193             ENDIF()
194         ENDIF()
195     ENDIF()
196 ENDIF()
199 SET(OPENAL_OBJS  OpenAL32/alAuxEffectSlot.c
200                  OpenAL32/alBuffer.c
201                  OpenAL32/alEffect.c
202                  OpenAL32/alError.c
203                  OpenAL32/alExtension.c
204                  OpenAL32/alFilter.c
205                  OpenAL32/alListener.c
206                  OpenAL32/alSource.c
207                  OpenAL32/alState.c
208                  OpenAL32/alThunk.c
209                  OpenAL32/OpenAL32.c
211 SET(ALC_OBJS  Alc/ALc.c
212               Alc/ALu.c
213               Alc/alcConfig.c
214               Alc/alcRing.c
215               Alc/alcThread.c
216               Alc/bs2b.c
217               Alc/wave.c
220 SET(BACKENDS "")
222 # Check ALSA backend
223 IF(ALSA)
224     CHECK_INCLUDE_FILE(alsa/asoundlib.h HAVE_ALSA_ASOUNDLIB_H)
225     IF(HAVE_ALSA_ASOUNDLIB_H)
226         CHECK_LIBRARY_EXISTS(asound snd_pcm_open "" HAVE_LIBASOUND)
227         IF(HAVE_DLFCN_H OR HAVE_LIBASOUND)
228             SET(HAVE_ALSA 1)
229             SET(ALC_OBJS  ${ALC_OBJS} Alc/alsa.c)
230             IF(HAVE_DLFCN_H)
231                 SET(BACKENDS  "${BACKENDS} ALSA,")
232             ELSE()
233                 SET(BACKENDS  "${BACKENDS} ALSA \(linked\),")
234                 SET(EXTRA_LIBS asound ${EXTRA_LIBS})
235             ENDIF()
236         ENDIF()
237     ENDIF()
238 ENDIF()
240 # Check OSS backend
241 IF(OSS)
242     CHECK_INCLUDE_FILE(sys/soundcard.h HAVE_SYS_SOUNDCARD_H)
243     IF(HAVE_SYS_SOUNDCARD_H)
244         SET(HAVE_OSS 1)
245         SET(ALC_OBJS  ${ALC_OBJS} Alc/oss.c)
246         SET(BACKENDS  "${BACKENDS} OSS,")
247     ENDIF()
248 ENDIF()
250 # Check DSound/MMSystem backend
251 IF(HAVE_WINDOWS_H)
252     IF(DSOUND)
253         CHECK_INCLUDE_FILE(dsound.h HAVE_DSOUND_H)
254         IF(HAVE_DSOUND_H)
255             SET(HAVE_DSOUND 1)
256             SET(ALC_OBJS  ${ALC_OBJS} Alc/dsound.c)
257             SET(BACKENDS  "${BACKENDS} DirectSound,")
259             SET(CMAKE_REQUIRED_LIBRARIES dsound)
260             CHECK_C_SOURCE_COMPILES("int main() {return 0;}" HAVE_LIBDSOUND)
261             SET(CMAKE_REQUIRED_LIBRARIES "")
262 #            CHECK_LIBRARY_EXISTS(dsound DirectSoundCreate "" HAVE_LIBDSOUND)
263             IF(HAVE_LIBDSOUND)
264                 SET(EXTRA_LIBS dsound ${EXTRA_LIBS})
265             ENDIF()
266         ENDIF()
267     ENDIF()
268     IF(WINMM)
269         CHECK_INCLUDE_FILES("windows.h;mmsystem.h" HAVE_MMSYSTEM_H -D_WIN32_WINNT=0x0500)
270         IF(HAVE_MMSYSTEM_H)
271             SET(HAVE_WINMM 1)
272             SET(ALC_OBJS  ${ALC_OBJS} Alc/winmm.c)
273             SET(BACKENDS  "${BACKENDS} WinMM,")
275             SET(CMAKE_REQUIRED_LIBRARIES winmm)
276             CHECK_C_SOURCE_COMPILES("int main() {return 0;}" HAVE_LIBWINMM)
277             SET(CMAKE_REQUIRED_LIBRARIES "")
278 #            CHECK_LIBRARY_EXISTS(winmm waveInOpen "" HAVE_LIBWINMM)
279             IF(HAVE_LIBWINMM)
280                 SET(EXTRA_LIBS winmm ${EXTRA_LIBS})
281             ENDIF()
282         ENDIF()
283     ENDIF()
284 ENDIF()
286 # This is always available
287 SET(BACKENDS  "${BACKENDS} WaveFile")
289 # End configuration
290 CONFIGURE_FILE(
291     "${OpenAL_SOURCE_DIR}/config.h.in"
292     "${OpenAL_BINARY_DIR}/config.h")
294 ADD_DEFINITIONS(-DAL_BUILD_LIBRARY)
296 # Build a shared library
297 IF(WIN32)
298     SET(LIBNAME openal32)
299 ELSE()
300     SET(LIBNAME openal)
301 ENDIF()
303 ADD_LIBRARY(${LIBNAME} SHARED ${OPENAL_OBJS} ${ALC_OBJS})
304 SET_TARGET_PROPERTIES(${LIBNAME} PROPERTIES VERSION ${LIB_VERSION}
305                                             SOVERSION ${LIB_MAJOR_VERSION})
306 IF(WIN32)
307     SET_TARGET_PROPERTIES(${LIBNAME} PROPERTIES PREFIX "")
308 ENDIF()
310 TARGET_LINK_LIBRARIES(${LIBNAME} ${EXTRA_LIBS})
312 # Add an install target here
313 INSTALL(TARGETS ${LIBNAME}
314         RUNTIME DESTINATION bin
315         LIBRARY DESTINATION ${LIB_INSTALL_DIR}
316         ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
318 INSTALL(FILES include/AL/al.h
319               include/AL/alc.h
320               include/AL/alext.h
321         DESTINATION include/AL
324 IF(EXAMPLES)
325     ADD_EXECUTABLE(openal-info examples/openal-info.c)
326     TARGET_LINK_LIBRARIES(openal-info ${LIBNAME})
327     INSTALL(TARGETS openal-info
328             RUNTIME DESTINATION bin
329             LIBRARY DESTINATION ${LIB_INSTALL_DIR}
330             ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
331     )
332 ENDIF()
334 MESSAGE(STATUS "")
335 MESSAGE(STATUS "Building OpenAL with support for the following backends:")
336 MESSAGE(STATUS "    ${BACKENDS}")
337 MESSAGE(STATUS "")