Update to C++14
[alure.git] / CMakeLists.txt
blob4c1c16e55bcc4eb793402cd927eb83f13931705a
1 cmake_minimum_required(VERSION 3.0)
2 project(alure)
4 IF(COMMAND CMAKE_POLICY)
5     CMAKE_POLICY(SET CMP0003 NEW)
6     CMAKE_POLICY(SET CMP0005 NEW)
7     IF(POLICY CMP0042)
8         CMAKE_POLICY(SET CMP0042 NEW)
9     ENDIF(POLICY CMP0042)
10 ENDIF(COMMAND CMAKE_POLICY)
12 set(CMAKE_MODULE_PATH "${alure_SOURCE_DIR}/cmake")
14 include(CheckCXXCompilerFlag)
15 include(CheckCXXSourceCompiles)
16 include(GNUInstallDirs)
18 find_package(OpenAL REQUIRED)
19 find_package(Threads)
21 # Require C++14
22 set(CMAKE_CXX_STANDARD 14)
23 set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
25 set(CXX_FLAGS )
27 option(ALURE_DISABLE_RTTI "Disable run-time type information" OFF)
28 if(MSVC)
29     if(ALURE_DISABLE_RTTI)
30         check_cxx_compiler_flag(/GR- HAVE_GRX_SWITCH)
31         if(HAVE_GRX_SWITCH)
32             set(CXX_FLAGS ${CXX_FLAGS} /GR-)
33         endif()
34     endif()
35 else()
36     check_cxx_compiler_flag(-Wall HAVE_WALL_SWITCH)
37     if(HAVE_WALL_SWITCH)
38         set(CXX_FLAGS ${CXX_FLAGS} -Wall)
39     endif()
40     check_cxx_compiler_flag(-Wextra HAVE_WEXTRA_SWITCH)
41     if(HAVE_WEXTRA_SWITCH)
42         set(CXX_FLAGS ${CXX_FLAGS} -Wextra)
43     endif()
45     if(ALURE_DISABLE_RTTI)
46         check_cxx_compiler_flag(-fno-rtti HAVE_NO_RTTI_SWITCH)
47         if(HAVE_NO_RTTI_SWITCH)
48             set(CXX_FLAGS ${CXX_FLAGS} -fno-rtti)
49         endif()
50     endif()
51 endif()
53 unset(EXPORT_DECL)
54 set(VISIBILITY_FLAGS )
55 if(WIN32)
56     set(EXPORT_DECL "__declspec(dllexport)")
57 else()
58     set(OLD_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
59     # Yes GCC, really don't accept visibility modes you don't support
60     set(CMAKE_REQUIRED_FLAGS "${OLD_REQUIRED_FLAGS} -Wattributes -Werror")
62     check_cxx_source_compiles("int foo() __attribute__((visibility(\"default\")));
63                                 int main() {return 0;}" HAVE_GCC_DEFAULT_VISIBILITY)
64     if(HAVE_GCC_DEFAULT_VISIBILITY)
65         set(EXPORT_DECL "__attribute__((visibility(\"default\")))")
66     endif()
68     if(HAVE_GCC_DEFAULT_VISIBILITY)
69         check_cxx_compiler_flag(-fvisibility=hidden HAVE_VISIBILITY_HIDDEN_SWITCH)
70         if(HAVE_VISIBILITY_HIDDEN_SWITCH)
71             set(VISIBILITY_FLAGS ${VISIBILITY_FLAGS} -fvisibility=hidden)
72         endif()
73     endif()
75     set(CMAKE_REQUIRED_FLAGS "${OLD_REQUIRED_FLAGS}")
76 endif()
79 set(LINKER_OPTS )
80 if(MSVC)
81     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
82     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO")
83     set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO")
84 else()
85     option(ALURE_STATIC_GCCRT "Static-link libgcc and libstdc++ runtimes" OFF)
86     if(ALURE_STATIC_GCCRT)
87         set(OLD_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
88         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} -static-libgcc -static-libstdc++)
89         check_cxx_source_compiles(
90 "#include <vector>
91 int main()
93     std::vector<int> test;
94     return test.size();
96             HAVE_STATIC_GCCRT_SWITCHES
97         )
98         if(HAVE_STATIC_GCCRT_SWITCHES)
99             set(LINKER_OPTS ${LINKER_OPTS} -static-libgcc -static-libstdc++)
100         endif()
101         set(CMAKE_REQUIRED_LIBRARIES ${OLD_REQUIRED_LIBRARIES})
102         unset(OLD_REQUIRED_LIBRARIES)
103     endif()
104 endif()
107 set(alure_srcs src/devicemanager.cpp
108                src/device.cpp
109                src/context.cpp
110                src/buffer.cpp
111                src/source.cpp
112                src/sourcegroup.cpp
113                src/auxeffectslot.cpp
114                src/effect.cpp
116 set(alure_libs ${OPENAL_LIBRARY})
117 set(decoder_incls )
119 unset(HAVE_WAVE)
120 unset(HAVE_VORBISFILE)
121 unset(HAVE_FLAC)
122 unset(HAVE_OPUSFILE)
123 unset(HAVE_LIBSNDFILE)
124 unset(HAVE_MINIMP3)
126 option(ALURE_INSTALL "Install library and import module" ON)
127 option(ALURE_BUILD_SHARED "Build shared library" ON)
128 option(ALURE_BUILD_STATIC "Build static library" ON)
130 if(NOT ALURE_BUILD_SHARED AND NOT ALURE_BUILD_STATIC)
131     message(FATAL_ERROR "Neither shared or static libraries are enabled!")
132 endif()
134 option(ALURE_ENABLE_WAVE    "Enables the built-in wave file decoder"     ON)
135 option(ALURE_ENABLE_VORBIS  "Enables the built-in libvorbisfile decoder" ON)
136 option(ALURE_ENABLE_FLAC    "Enables the built-in FLAC decoder"          ON)
137 option(ALURE_ENABLE_OPUS    "Enables the built-in libopusfile decoder"   ON)
138 option(ALURE_ENABLE_SNDFILE "Enables the built-in libsndfile decoder"    ON)
139 option(ALURE_ENABLE_MINIMP3 "Enables the built-in MINIMP3 decoder"       ON)
141 if(ALURE_ENABLE_WAVE)
142     set(alure_srcs ${alure_srcs} src/decoders/wave.cpp)
143     set(HAVE_WAVE 1)
144 endif()
146 find_package(Ogg)
147 if(OGG_FOUND)
148     set(decoder_incls ${decoder_incls} ${OGG_INCLUDE_DIRS})
149     find_package(Vorbis)
150     if(VORBIS_FOUND AND ALURE_ENABLE_VORBIS)
151         set(decoder_incls ${decoder_incls} ${VORBIS_INCLUDE_DIRS})
152         set(alure_libs ${alure_libs} ${VORBIS_LIBRARIES})
153         set(alure_srcs ${alure_srcs} src/decoders/vorbisfile.cpp)
154         set(HAVE_VORBISFILE 1)
155     endif()
157     find_package(Opus)
158     if(OPUS_FOUND AND ALURE_ENABLE_OPUS)
159         set(decoder_incls ${decoder_incls} ${OPUS_INCLUDE_DIRS})
160         set(alure_libs ${alure_libs} ${OPUS_LIBRARIES})
161         set(alure_srcs ${alure_srcs} src/decoders/opusfile.cpp)
162         set(HAVE_OPUSFILE 1)
163     endif()
164     set(alure_libs ${alure_libs} ${OGG_LIBRARIES})
165 endif()
167 if(ALURE_ENABLE_FLAC)
168     set(alure_srcs ${alure_srcs} src/decoders/dr_flac.h src/decoders/flac.cpp)
169     set(HAVE_FLAC 1)
170 endif()
172 find_package(SndFile)
173 if(SNDFILE_FOUND AND ALURE_ENABLE_SNDFILE)
174     set(decoder_incls ${decoder_incls} ${SNDFILE_INCLUDE_DIRS})
175     set(alure_libs ${alure_libs} ${SNDFILE_LIBRARIES})
176     set(alure_srcs ${alure_srcs} src/decoders/sndfile.cpp)
177     set(HAVE_LIBSNDFILE 1)
178 endif()
180 if(ALURE_ENABLE_MINIMP3)
181     set(alure_srcs ${alure_srcs} src/decoders/minimp3.h src/decoders/mp3.hpp src/decoders/mp3.cpp)
182     set(HAVE_MINIMP3 1)
183 endif()
186 CONFIGURE_FILE(
187     "${alure_SOURCE_DIR}/config.h.in"
188     "${alure_BINARY_DIR}/config.h")
191 unset(TARGET_NAMES)
192 unset(MAIN_TARGET)
194 if(ALURE_BUILD_SHARED)
195     add_library(alure2 SHARED ${alure_srcs})
196     if(EXPORT_DECL)
197         target_compile_definitions(alure2 PRIVATE ALURE_API=${EXPORT_DECL} ALURE_TEMPLATE=template
198                                                   NOMINMAX)
199     endif()
200     target_include_directories(alure2
201         PUBLIC $<BUILD_INTERFACE:${alure_SOURCE_DIR}/include/AL> ${OPENAL_INCLUDE_DIR}
202         PRIVATE ${alure_SOURCE_DIR}/include ${alure_SOURCE_DIR}/src ${alure_BINARY_DIR}
203                 ${decoder_incls}
204     )
205     target_compile_options(alure2 PRIVATE ${CXX_FLAGS} ${VISIBILITY_FLAGS})
206     target_link_libraries(alure2 PUBLIC ${alure_libs} PRIVATE Threads::Threads ${LINKER_OPTS})
208     set(TARGET_NAMES ${TARGET_NAMES} alure2)
209     if(NOT MAIN_TARGET)
210         set(MAIN_TARGET alure2)
211     endif()
212 endif()
214 if(ALURE_BUILD_STATIC)
215     add_library(alure2_s STATIC ${alure_srcs})
216     target_compile_definitions(alure2_s PUBLIC ALURE_STATIC_LIB PRIVATE NOMINMAX)
217     target_include_directories(alure2_s
218         PUBLIC $<BUILD_INTERFACE:${alure_SOURCE_DIR}/include/AL> ${OPENAL_INCLUDE_DIR}
219         PRIVATE ${alure_SOURCE_DIR}/include ${alure_SOURCE_DIR}/src ${alure_BINARY_DIR}
220                 ${decoder_incls}
221     )
222     target_compile_options(alure2_s PRIVATE ${CXX_FLAGS} ${VISIBILITY_FLAGS})
223     target_link_libraries(alure2_s PUBLIC ${alure_libs} PRIVATE Threads::Threads)
225     set(TARGET_NAMES ${TARGET_NAMES} alure2_s)
226     if(NOT MAIN_TARGET)
227         set(MAIN_TARGET alure2_s)
228     endif()
229 endif()
231 export(
232     TARGETS ${TARGET_NAMES}
233     NAMESPACE Alure2::
234     FILE Alure2Config.cmake
236 if(ALURE_INSTALL)
237     install(TARGETS ${TARGET_NAMES} EXPORT alure2
238         RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
239         LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
240         ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
241         INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ${CMAKE_INSTALL_INCLUDEDIR}/AL
242     )
243     install(
244         EXPORT alure2
245         DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Alure2
246         NAMESPACE Alure2::
247         FILE Alure2Config.cmake
248     )
249     install(FILES
250         include/AL/alure2.h
251         include/AL/alure2-aliases.h
252         include/AL/alure2-typeviews.h
253         include/AL/alure2-alext.h
254         include/AL/efx.h
255         include/AL/efx-presets.h
256         DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/AL
257     )
258 endif()
261 option(ALURE_BUILD_EXAMPLES "Build example programs" ON)
262 if(ALURE_BUILD_EXAMPLES)
263     add_executable(alure-enum examples/alure-enum.cpp)
264     target_compile_options(alure-enum PRIVATE ${CXX_FLAGS})
265     target_link_libraries(alure-enum PRIVATE ${MAIN_TARGET} ${LINKER_OPTS})
267     add_executable(alure-play examples/alure-play.cpp)
268     target_compile_options(alure-play PRIVATE ${CXX_FLAGS})
269     target_link_libraries(alure-play PRIVATE ${MAIN_TARGET} ${LINKER_OPTS})
271     add_executable(alure-play3d examples/alure-play3d.cpp)
272     target_compile_options(alure-play3d PRIVATE ${CXX_FLAGS})
273     target_link_libraries(alure-play3d PRIVATE ${MAIN_TARGET} ${LINKER_OPTS})
275     add_executable(alure-stream examples/alure-stream.cpp)
276     target_compile_options(alure-stream PRIVATE ${CXX_FLAGS})
277     target_link_libraries(alure-stream PRIVATE ${MAIN_TARGET} ${LINKER_OPTS})
279     add_executable(alure-reverb examples/alure-reverb.cpp)
280     target_compile_options(alure-reverb PRIVATE ${CXX_FLAGS})
281     target_link_libraries(alure-reverb PRIVATE ${MAIN_TARGET} ${LINKER_OPTS})
283     add_executable(alure-hrtf examples/alure-hrtf.cpp)
284     target_compile_options(alure-hrtf PRIVATE ${CXX_FLAGS})
285     target_link_libraries(alure-hrtf PRIVATE ${MAIN_TARGET} ${LINKER_OPTS})
287     find_package(PhysFS)
288     if(PHYSFS_FOUND)
289         add_executable(alure-physfs examples/alure-physfs.cpp)
290         target_include_directories(alure-physfs PRIVATE ${PHYSFS_INCLUDE_DIR})
291         target_compile_options(alure-physfs PRIVATE ${CXX_FLAGS})
292         target_link_libraries(alure-physfs PRIVATE ${MAIN_TARGET} ${PHYSFS_LIBRARY} ${LINKER_OPTS})
293     endif()
295     find_package(DUMB)
296     if(DUMB_FOUND)
297         add_executable(alure-dumb examples/alure-dumb.cpp)
298         target_include_directories(alure-dumb PRIVATE ${DUMB_INCLUDE_DIRS})
299         target_compile_options(alure-dumb PRIVATE ${CXX_FLAGS})
300         target_link_libraries(alure-dumb PRIVATE ${MAIN_TARGET} ${DUMB_LIBRARIES} ${LINKER_OPTS})
301     endif()
302 endif()