1include (CheckFunctionExists)
2include (CheckIncludeFile)
3include (CheckLibraryExists)
4include (CheckSymbolExists)
5include (CheckTypeSize)
6include (TestBigEndian)
7
8include (TestInline)
9include (ClipMode)
10include (TestLargeFiles)
11include (CheckCPUArch)
12
13test_large_files (_LARGEFILES)
14
15if (LARGE_FILES_DEFINITIONS)
16	add_definitions(${LARGE_FILES_DEFINITIONS})
17endif ()
18
19if (WIN32)
20    set(TYPEOF_SF_COUNT_T __int64)
21else ()
22    set(TYPEOF_SF_COUNT_T int64_t)
23endif ()
24set (SF_COUNT_MAX 0x7fffffffffffffffll)
25
26if (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
27	find_package (Sndio)
28elseif (NOT WIN32)
29	find_package (ALSA)
30endif ()
31
32if (VCPKG_TOOLCHAIN AND (NOT CMAKE_VERSION VERSION_LESS 3.15))
33	set (CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
34endif ()
35
36if (CMAKE_FIND_PACKAGE_PREFER_CONFIG)
37	find_package (Ogg 1.3 CONFIG)
38	find_package (Vorbis CONFIG COMPONENTS Enc)
39	find_package (FLAC CONFIG)
40	find_package (Opus CONFIG)
41
42	include (FindPackageHandleStandardArgs)
43	find_package_handle_standard_args (Ogg CONFIG_MODE)
44	find_package_handle_standard_args (Vorbis CONFIG_MODE)
45	find_package_handle_standard_args (FLAC CONFIG_MODE)
46	find_package_handle_standard_args (Opus CONFIG_MODE)
47else ()
48	find_package (Ogg 1.3)
49	find_package (Vorbis COMPONENTS Enc)
50	find_package (FLAC)
51	find_package (Opus)
52endif ()
53if (Vorbis_FOUND AND FLAC_FOUND AND Opus_FOUND)
54	set (HAVE_EXTERNAL_XIPH_LIBS 1)
55else ()
56	set (HAVE_EXTERNAL_XIPH_LIBS 0)
57endif ()
58
59find_package (Speex)
60find_package (SQLite3)
61
62check_include_file (byteswap.h		HAVE_BYTESWAP_H)
63check_include_file (dlfcn.h			HAVE_DLFCN_H)
64check_include_file (direct.h		HAVE_DIRECT_H)
65check_include_file (endian.h		HAVE_ENDIAN_H)
66check_include_file (inttypes.h		HAVE_INTTYPES_H)
67check_include_file (io.h			HAVE_IO_H)
68check_include_file (stdint.h		HAVE_STDINT_H)
69check_include_file (sys/time.h		HAVE_SYS_TIME_H)
70check_include_file (sys/types.h		HAVE_SYS_TYPES_H)
71check_include_file (unistd.h		HAVE_UNISTD_H)
72check_include_file (immintrin.h		HAVE_IMMINTRIN_H)
73
74check_cpu_arch_x86 (CPU_IS_X86)
75check_cpu_arch_x64 (CPU_IS_X64)
76if ((CPU_IS_X86 OR CPU_IS_X64) AND HAVE_IMMINTRIN_H)
77	set (HAVE_SSE2 1)
78endif ()
79
80# Never checked
81# check_include_file (stdlib.h		HAVE_STDLIB_H)
82# check_include_file (string.h		HAVE_STRING_H)
83# check_include_file (strings.h		HAVE_STRINGS_H)
84# check_include_file (sys/stat.h	HAVE_SYS_STAT_H)
85# check_include_file (memory.h		HAVE_MEMORY_H)
86
87if (BUILD_TESTING)
88	check_include_file (locale.h	HAVE_LOCALE_H)
89	check_include_file (sys/wait.h	HAVE_SYS_WAIT_H)
90endif ()
91
92check_type_size (int64_t			SIZEOF_INT64_T)
93check_type_size (long				SIZEOF_LONG)
94check_type_size (long\ long			SIZEOF_LONG_LONG)
95check_type_size (ssize_t			SIZEOF_SSIZE_T)
96check_type_size (wchar_t			SIZEOF_WCHAR_T)
97
98# Never used
99# check_type_size (loff_t			SIZEOF_LOFF_T)
100# check_type_size (offt64_t			SIZEOF_OFF64_T)
101
102# Never checked
103# check_type_size (size_t			SIZEOF_SIZE_T)
104
105# Used in configre.ac
106# check_type_size (double			SIZEOF_DOUBLE)
107# check_type_size (float			SIZEOF_FLOAT)
108# check_type_size (int				SIZEOF_INT)
109# check_type_size (short			SIZEOF_SHORT)
110
111if (ENABLE_TESTING)
112	check_type_size (void*			SIZEOF_VOIDP)
113endif()
114
115if ((SIZEOF_OFF_T EQUAL 8) OR (SIZEOF_LOFF_T EQUAL 8) OR (SIZEOF_OFF64_T EQUAL 8))
116	set (TYPEOF_SF_COUNT_T "int64_t")
117	set (SF_COUNT_MAX "0x7FFFFFFFFFFFFFFFLL")
118	set (SIZEOF_SF_COUNT_T 8)
119else ()
120	if (WIN32)
121		set (TYPEOF_SF_COUNT_T "__int64")
122		set (SF_COUNT_MAX "0x7FFFFFFFFFFFFFFFLL")
123		set (SIZEOF_SF_COUNT_T 8)
124	else ()
125		message ("")
126		message ("*** The configure process has determined that this system is capable")
127		message ("*** of Large File Support but has not been able to find a type which")
128		message ("*** is an unambiguous 64 bit file offset.")
129		message ("*** Please contact the author to help resolve this problem.")
130		message ("")
131		message (FATAL_ERROR "Bad file offset type.")
132	endif ()
133endif ()
134
135check_type_size (${TYPEOF_SF_COUNT_T} SIZEOF_SF_COUNT_T)
136
137if (NOT WIN32)
138	check_library_exists (m floor "" LIBM_REQUIRED)
139	if (LIBM_REQUIRED)
140		list (APPEND CMAKE_REQUIRED_LIBRARIES m)
141	endif ()
142endif ()
143
144check_library_exists (sqlite3 sqlite3_close "" HAVE_SQLITE3)
145
146check_function_exists (fstat     		HAVE_FSTAT)
147check_function_exists (fstat64			HAVE_FSTAT64)
148check_function_exists (gettimeofday		HAVE_GETTIMEOFDAY)
149check_function_exists (gmtime			HAVE_GMTIME)
150check_function_exists (gmtime_r			HAVE_GMTIME_R)
151check_function_exists (localtime		HAVE_LOCALTIME)
152check_function_exists (localtime_r		HAVE_LOCALTIME_R)
153check_function_exists (lseek      		HAVE_LSEEK)
154check_function_exists (open				HAVE_OPEN)
155check_function_exists (read				HAVE_READ)
156check_function_exists (write			HAVE_WRITE)
157check_function_exists (lrint			HAVE_LRINT)
158check_function_exists (lrintf			HAVE_LRINTF)
159
160if (NOT WIN32)
161	check_function_exists (ftruncate	HAVE_FTRUNCATE)
162	check_function_exists (fsync    	HAVE_FSYNC)
163endif ()
164
165if (BUILD_TESTING)
166	check_function_exists (pipe			HAVE_PIPE)
167	check_function_exists (setlocale	HAVE_SETLOCALE)
168	check_function_exists (waitpid		HAVE_WAITPID)
169endif ()
170
171# Never checked
172# check_function_exists (calloc			HAVE_CALLOC)
173# check_function_exists (free			HAVE_FREE)
174# check_function_exists (getpagesize	HAVE_GETPAGESIZE)
175# check_function_exists (malloc			HAVE_MALLOC)
176# check_function_exists (realloc		HAVE_REALLOC)
177# check_function_exists (snprintf		HAVE_SNPRINTF)
178# check_function_exists (vsnprintf		HAVE_VSNPRINTF)
179# check_function_exists (floor			HAVE_FLOOR)
180# check_function_exists (fmod			HAVE_FMOD)
181
182# Never used
183# check_function_exists (mmap			HAVE_MMAP)
184# check_function_exists (ceil			HAVE_CEIL)
185# check_function_exists (lround			HAVE_LROUND)
186# check_function_exists (lseek64		HAVE_LSEEK64)
187
188
189check_symbol_exists (S_IRGRP sys/stat.h HAVE_DECL_S_IRGRP)
190
191test_big_endian (WORDS_BIGENDIAN)
192if (WORDS_BIGENDIAN)
193	set (CPU_IS_BIG_ENDIAN 1)
194else ()
195	set (CPU_IS_LITTLE_ENDIAN 1)
196endif ()
197
198if (WIN32)
199	set (OS_IS_WIN32 1)
200	set (USE_WINDOWS_API 1)
201	if (BUILD_SHARED_LIBS)
202		set (WIN32_TARGET_DLL 1)
203	endif ()
204	if (MINGW)
205		add_definitions (-D__USE_MINGW_ANSI_STDIO=1)
206	endif ()
207endif ()
208
209if (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
210	set (OS_IS_OPENBSD 1)
211endif ()
212
213
214if (CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
215	set (COMPILER_IS_GCC 1)
216endif ()
217
218test_inline ()
219clip_mode ()
220
221if (MSVC)
222	add_definitions (-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE)
223endif (MSVC)
224
225if (DEFINED ENABLE_STATIC_RUNTIME)
226	if (MSVC)
227		if (ENABLE_STATIC_RUNTIME)
228			foreach (flag_var
229				CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
230				CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
231				CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
232				CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
233				)
234				if (${flag_var} MATCHES "/MD")
235					string (REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
236				endif ()
237			endforeach (flag_var)
238		else ()
239			foreach (flag_var
240				CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
241				CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
242				CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
243				CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
244				)
245				if (${flag_var} MATCHES "/MT")
246					string (REGEX REPLACE "/MT" "/MD" ${flag_var} "${${flag_var}}")
247				endif (${flag_var} MATCHES "/MT")
248			endforeach (flag_var)
249		endif ( )
250	elseif (MINGW)
251		if (ENABLE_STATIC_RUNTIME)
252			if (CMAKE_C_COMPILER_ID STREQUAL GNU)
253				set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc")
254				set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc -s")
255			endif ()
256			if (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
257				set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
258				set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++ -s")
259			endif ()
260			if (CMAKE_C_COMPILER_ID STREQUAL Clang)
261				set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static")
262				set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static")
263			endif ()
264			if (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
265				set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
266				set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static")
267			endif ()
268		endif ()
269	else ()
270		message (AUTHOR_WARNING "ENABLE_STATIC_RUNTIME option is for MSVC or MinGW only.")
271	endif ()
272endif ()
273
274if (BUILD_SHARED_LIBS)
275	find_package (PythonInterp REQUIRED)
276endif()
277