• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
2  message(FATAL_ERROR "Prevented in-tree built. Please create a build directory outside of the SDL source code and call cmake from there")
3endif()
4
5cmake_minimum_required(VERSION 2.8.5)
6project(SDL2 C)
7
8# !!! FIXME: this should probably do "MACOSX_RPATH ON" as a target property
9# !!! FIXME:  for the SDL2 shared library (so you get an
10# !!! FIXME:  install_name ("soname") of "@rpath/libSDL-whatever.dylib"
11# !!! FIXME:  instead of "/usr/local/lib/libSDL-whatever.dylib"), but I'm
12# !!! FIXME:  punting for now and leaving the existing behavior. Until this
13# !!! FIXME:  properly resolved, this line silences a warning in CMake 3.0+.
14# !!! FIXME:  remove it and this comment entirely once the problem is
15# !!! FIXME:  properly resolved.
16#cmake_policy(SET CMP0042 OLD)
17
18include(CheckFunctionExists)
19include(CheckLibraryExists)
20include(CheckIncludeFiles)
21include(CheckIncludeFile)
22include(CheckSymbolExists)
23include(CheckCSourceRuns)
24include(CheckCCompilerFlag)
25include(CheckTypeSize)
26include(CheckStructHasMember)
27include(CMakeDependentOption)
28include(FindPkgConfig)
29include(GNUInstallDirs)
30set(CMAKE_MODULE_PATH "${SDL2_SOURCE_DIR}/cmake")
31include(${SDL2_SOURCE_DIR}/cmake/macros.cmake)
32include(${SDL2_SOURCE_DIR}/cmake/sdlchecks.cmake)
33
34# General settings
35# Edit include/SDL_version.h and change the version, then:
36#   SDL_MICRO_VERSION += 1;
37#   SDL_INTERFACE_AGE += 1;
38#   SDL_BINARY_AGE += 1;
39# if any functions have been added, set SDL_INTERFACE_AGE to 0.
40# if backwards compatibility has been broken,
41# set SDL_BINARY_AGE and SDL_INTERFACE_AGE to 0.
42set(SDL_MAJOR_VERSION 2)
43set(SDL_MINOR_VERSION 0)
44set(SDL_MICRO_VERSION 5)
45set(SDL_INTERFACE_AGE 1)
46set(SDL_BINARY_AGE 5)
47set(SDL_VERSION "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}.${SDL_MICRO_VERSION}")
48
49# Calculate a libtool-like version number
50math(EXPR LT_CURRENT "${SDL_MICRO_VERSION} - ${SDL_INTERFACE_AGE}")
51math(EXPR LT_AGE "${SDL_BINARY_AGE} - ${SDL_INTERFACE_AGE}")
52math(EXPR LT_MAJOR "${LT_CURRENT}- ${LT_AGE}")
53set(LT_REVISION "${SDL_INTERFACE_AGE}")
54set(LT_RELEASE "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}")
55set(LT_VERSION "${LT_MAJOR}.${LT_AGE}.${LT_REVISION}")
56
57message(STATUS "${LT_VERSION} :: ${LT_AGE} :: ${LT_REVISION} :: ${LT_CURRENT} :: ${LT_RELEASE}")
58
59# General settings & flags
60set(LIBRARY_OUTPUT_DIRECTORY "build")
61# Check for 64 or 32 bit
62set(SIZEOF_VOIDP ${CMAKE_SIZEOF_VOID_P})
63if(CMAKE_SIZEOF_VOID_P EQUAL 8)
64  set(ARCH_64 TRUE)
65  set(PROCESSOR_ARCH "x64")
66else()
67  set(ARCH_64 FALSE)
68  set(PROCESSOR_ARCH "x86")
69endif()
70set(LIBNAME SDL2)
71if(NOT LIBTYPE)
72  set(LIBTYPE SHARED)
73endif()
74
75# Get the platform
76if(WIN32)
77  if(NOT WINDOWS)
78    set(WINDOWS TRUE)
79  endif()
80elseif(UNIX AND NOT APPLE)
81  if(CMAKE_SYSTEM_NAME MATCHES ".*Linux")
82    set(LINUX TRUE)
83  elseif(CMAKE_SYSTEM_NAME MATCHES "kFreeBSD.*")
84    set(FREEBSD TRUE)
85  elseif(CMAKE_SYSTEM_NAME MATCHES "kNetBSD.*|NetBSD.*")
86    set(NETBSD TRUE)
87  elseif(CMAKE_SYSTEM_NAME MATCHES "kOpenBSD.*|OpenBSD.*")
88    set(OPENBSD TRUE)
89  elseif(CMAKE_SYSTEM_NAME MATCHES ".*GNU.*")
90    set(GNU TRUE)
91  elseif(CMAKE_SYSTEM_NAME MATCHES ".*BSDI.*")
92    set(BSDI TRUE)
93  elseif(CMAKE_SYSTEM_NAME MATCHES "DragonFly.*|FreeBSD")
94    set(FREEBSD TRUE)
95  elseif(CMAKE_SYSTEM_NAME MATCHES "SYSV5.*")
96    set(SYSV5 TRUE)
97  elseif(CMAKE_SYSTEM_NAME MATCHES "Solaris.*")
98    set(SOLARIS TRUE)
99  elseif(CMAKE_SYSTEM_NAME MATCHES "HP-UX.*")
100    set(HPUX TRUE)
101  elseif(CMAKE_SYSTEM_NAME MATCHES "AIX.*")
102    set(AIX TRUE)
103  elseif(CMAKE_SYSTEM_NAME MATCHES "Minix.*")
104    set(MINIX TRUE)
105  endif()
106elseif(APPLE)
107  if(CMAKE_SYSTEM_NAME MATCHES ".*Darwin.*")
108    set(DARWIN TRUE)
109  elseif(CMAKE_SYSTEM_NAME MATCHES ".*MacOS.*")
110    set(MACOSX TRUE)
111  endif()
112  # TODO: iOS?
113elseif(CMAKE_SYSTEM_NAME MATCHES "BeOS.*")
114  message_error("BeOS support has been removed as of SDL 2.0.2.")
115elseif(CMAKE_SYSTEM_NAME MATCHES "Haiku.*")
116  set(HAIKU TRUE)
117endif()
118
119# Don't mistake osx for unix
120if(UNIX AND NOT APPLE)
121  set(UNIX_SYS ON)
122else()
123  set(UNIX_SYS OFF)
124endif()
125
126if(UNIX OR APPLE)
127  set(UNIX_OR_MAC_SYS ON)
128else()
129  set(UNIX_OR_MAC_SYS OFF)
130endif()
131
132if (UNIX_OR_MAC_SYS AND NOT EMSCRIPTEN) # JavaScript does not yet have threading support, so disable pthreads when building for Emscripten.
133  set(SDL_PTHREADS_ENABLED_BY_DEFAULT ON)
134else()
135  set(SDL_PTHREADS_ENABLED_BY_DEFAULT OFF)
136endif()
137
138# Default option knobs
139if(APPLE OR ARCH_64)
140  set(OPT_DEF_SSEMATH ON)
141endif()
142if(UNIX OR MINGW OR MSYS)
143  set(OPT_DEF_LIBC ON)
144endif()
145
146# Compiler info
147if(CMAKE_COMPILER_IS_GNUCC)
148  set(USE_GCC TRUE)
149  set(OPT_DEF_ASM TRUE)
150elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
151  set(USE_CLANG TRUE)
152  set(OPT_DEF_ASM TRUE)
153elseif(MSVC_VERSION GREATER 1400) # VisualStudio 8.0+
154  set(OPT_DEF_ASM TRUE)
155  #set(CMAKE_C_FLAGS "/ZI /WX- /
156else()
157  set(OPT_DEF_ASM FALSE)
158endif()
159
160# Default flags, if not set otherwise
161if("$ENV{CFLAGS}" STREQUAL "")
162  if(CMAKE_BUILD_TYPE STREQUAL "")
163    if(USE_GCC OR USE_CLANG)
164      set(CMAKE_C_FLAGS "-g -O3")
165    endif()
166  endif()
167else()
168  set(CMAKE_C_FLAGS "$ENV{CFLAGS}")
169  list(APPEND EXTRA_CFLAGS "$ENV{CFLAGS}")
170endif()
171if(NOT ("$ENV{CFLAGS}" STREQUAL "")) # Hackish, but does the trick on Win32
172  list(APPEND EXTRA_LDFLAGS "$ENV{LDFLAGS}")
173endif()
174
175if(MSVC)
176  option(FORCE_STATIC_VCRT "Force /MT for static VC runtimes" OFF)
177  if(FORCE_STATIC_VCRT)
178    foreach(flag_var
179        CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
180        CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
181      if(${flag_var} MATCHES "/MD")
182        string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
183      endif()
184    endforeach()
185  endif()
186
187  # Make sure /RTC1 is disabled, otherwise it will use functions from the CRT
188  foreach(flag_var
189      CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
190      CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
191    string(REGEX REPLACE "/RTC(su|[1su])" "" ${flag_var} "${${flag_var}}")
192  endforeach(flag_var)
193endif()
194
195# Those are used for pkg-config and friends, so that the SDL2.pc, sdl2-config,
196# etc. are created correctly.
197set(SDL_LIBS "-lSDL2")
198set(SDL_CFLAGS "")
199
200# Emscripten toolchain has a nonempty default value for this, and the checks
201# in this file need to change that, so remember the original value, and
202# restore back to that afterwards. For check_function_exists() to work in
203# Emscripten, this value must be at its default value.
204set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
205
206if(CYGWIN)
207  # We build SDL on cygwin without the UNIX emulation layer
208  include_directories("-I/usr/include/mingw")
209  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -mno-cygwin")
210  check_c_source_compiles("int main(int argc, char **argv) {}"
211    HAVE_GCC_NO_CYGWIN)
212  set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
213  if(HAVE_GCC_NO_CYGWIN)
214    list(APPEND EXTRA_LDFLAGS "-mno-cygwin")
215    list(APPEND SDL_LIBS "-mno-cygwin")
216  endif()
217  set(SDL_CFLAGS "${SDL_CFLAGS} -I/usr/include/mingw")
218endif()
219
220add_definitions(-DUSING_GENERATED_CONFIG_H)
221# General includes
222include_directories(${SDL2_BINARY_DIR}/include ${SDL2_SOURCE_DIR}/include)
223
224# All these ENABLED_BY_DEFAULT vars will default to ON if not specified, so
225#  you only need to have a platform override them if they are disabling.
226set(OPT_DEF_ASM TRUE)
227if(EMSCRIPTEN)
228  # Set up default values for the currently supported set of subsystems:
229  # Emscripten/Javascript does not have assembly support, a dynamic library
230  # loading architecture, low-level CPU inspection or multithreading.
231  set(OPT_DEF_ASM FALSE)
232  set(SDL_SHARED_ENABLED_BY_DEFAULT OFF)
233  set(SDL_ATOMIC_ENABLED_BY_DEFAULT OFF)
234  set(SDL_THREADS_ENABLED_BY_DEFAULT OFF)
235  set(SDL_LOADSO_ENABLED_BY_DEFAULT OFF)
236  set(SDL_CPUINFO_ENABLED_BY_DEFAULT OFF)
237  set(SDL_DLOPEN_ENABLED_BY_DEFAULT OFF)
238endif()
239
240if (NOT DEFINED SDL_SHARED_ENABLED_BY_DEFAULT)
241    set(SDL_SHARED_ENABLED_BY_DEFAULT ON)
242endif()
243
244set(SDL_SUBSYSTEMS
245    Atomic Audio Video Render Events Joystick Haptic Power Threads Timers
246    File Loadso CPUinfo Filesystem Dlopen)
247foreach(_SUB ${SDL_SUBSYSTEMS})
248  string(TOUPPER ${_SUB} _OPT)
249  if (NOT DEFINED SDL_${_OPT}_ENABLED_BY_DEFAULT)
250    set(SDL_${_OPT}_ENABLED_BY_DEFAULT ON)
251  endif()
252  option(SDL_${_OPT} "Enable the ${_SUB} subsystem" ${SDL_${_OPT}_ENABLED_BY_DEFAULT})
253endforeach()
254
255option_string(ASSERTIONS "Enable internal sanity checks (auto/disabled/release/enabled/paranoid)" "auto")
256#set_option(DEPENDENCY_TRACKING "Use gcc -MMD -MT dependency tracking" ON)
257set_option(LIBC                "Use the system C library" ${OPT_DEF_LIBC})
258set_option(GCC_ATOMICS         "Use gcc builtin atomics" ${USE_GCC})
259set_option(ASSEMBLY            "Enable assembly routines" ${OPT_DEF_ASM})
260set_option(SSEMATH             "Allow GCC to use SSE floating point math" ${OPT_DEF_SSEMATH})
261set_option(MMX                 "Use MMX assembly routines" ${OPT_DEF_ASM})
262set_option(3DNOW               "Use 3Dnow! MMX assembly routines" ${OPT_DEF_ASM})
263set_option(SSE                 "Use SSE assembly routines" ${OPT_DEF_ASM})
264set_option(SSE2                "Use SSE2 assembly routines" ${OPT_DEF_SSEMATH})
265set_option(ALTIVEC             "Use Altivec assembly routines" ${OPT_DEF_ASM})
266set_option(DISKAUDIO           "Support the disk writer audio driver" ON)
267set_option(DUMMYAUDIO          "Support the dummy audio driver" ON)
268set_option(VIDEO_DIRECTFB      "Use DirectFB video driver" OFF)
269dep_option(DIRECTFB_SHARED     "Dynamically load directfb support" ON "VIDEO_DIRECTFB" OFF)
270set_option(FUSIONSOUND         "Use FusionSound audio driver" OFF)
271dep_option(FUSIONSOUND_SHARED  "Dynamically load fusionsound audio support" ON "FUSIONSOUND" OFF)
272set_option(VIDEO_DUMMY         "Use dummy video driver" ON)
273set_option(VIDEO_OPENGL        "Include OpenGL support" ON)
274set_option(VIDEO_OPENGLES      "Include OpenGL ES support" ON)
275set_option(PTHREADS            "Use POSIX threads for multi-threading" ${SDL_PTHREADS_ENABLED_BY_DEFAULT})
276dep_option(PTHREADS_SEM        "Use pthread semaphores" ON "PTHREADS" OFF)
277set_option(SDL_DLOPEN          "Use dlopen for shared object loading" ${SDL_DLOPEN_ENABLED_BY_DEFAULT})
278set_option(OSS                 "Support the OSS audio API" ${UNIX_SYS})
279set_option(ALSA                "Support the ALSA audio API" ${UNIX_SYS})
280dep_option(ALSA_SHARED         "Dynamically load ALSA audio support" ON "ALSA" OFF)
281set_option(ESD                 "Support the Enlightened Sound Daemon" ${UNIX_SYS})
282dep_option(ESD_SHARED          "Dynamically load ESD audio support" ON "ESD" OFF)
283set_option(PULSEAUDIO          "Use PulseAudio" ${UNIX_SYS})
284dep_option(PULSEAUDIO_SHARED   "Dynamically load PulseAudio support" ON "PULSEAUDIO" OFF)
285set_option(ARTS                "Support the Analog Real Time Synthesizer" ${UNIX_SYS})
286dep_option(ARTS_SHARED         "Dynamically load aRts audio support" ON "ARTS" OFF)
287set_option(NAS                 "Support the NAS audio API" ${UNIX_SYS})
288set_option(NAS_SHARED          "Dynamically load NAS audio API" ${UNIX_SYS})
289set_option(SNDIO               "Support the sndio audio API" ${UNIX_SYS})
290set_option(RPATH               "Use an rpath when linking SDL" ${UNIX_SYS})
291set_option(CLOCK_GETTIME       "Use clock_gettime() instead of gettimeofday()" OFF)
292set_option(INPUT_TSLIB         "Use the Touchscreen library for input" ${UNIX_SYS})
293set_option(VIDEO_X11           "Use X11 video driver" ${UNIX_SYS})
294set_option(VIDEO_WAYLAND       "Use Wayland video driver" ${UNIX_SYS})
295dep_option(WAYLAND_SHARED      "Dynamically load Wayland support" ON "VIDEO_WAYLAND" OFF)
296dep_option(VIDEO_WAYLAND_QT_TOUCH  "QtWayland server support for Wayland video driver" ON "VIDEO_WAYLAND" OFF)
297set_option(VIDEO_MIR           "Use Mir video driver" ${UNIX_SYS})
298dep_option(MIR_SHARED          "Dynamically load Mir support" ON "VIDEO_MIR" OFF)
299set_option(VIDEO_RPI           "Use Raspberry Pi video driver" ${UNIX_SYS})
300dep_option(X11_SHARED          "Dynamically load X11 support" ON "VIDEO_X11" OFF)
301set(SDL_X11_OPTIONS Xcursor Xinerama XInput Xrandr Xscrnsaver XShape Xvm)
302foreach(_SUB ${SDL_X11_OPTIONS})
303  string(TOUPPER "VIDEO_X11_${_SUB}" _OPT)
304  dep_option(${_OPT}           "Enable ${_SUB} support" ON "VIDEO_X11" OFF)
305endforeach()
306set_option(VIDEO_COCOA         "Use Cocoa video driver" ${APPLE})
307set_option(DIRECTX             "Use DirectX for Windows audio/video" ${WINDOWS})
308set_option(RENDER_D3D          "Enable the Direct3D render driver" ${WINDOWS})
309set_option(VIDEO_VIVANTE       "Use Vivante EGL video driver" ${UNIX_SYS})
310
311# TODO: We should (should we?) respect cmake's ${BUILD_SHARED_LIBS} flag here
312# The options below are for compatibility to configure's default behaviour.
313set(SDL_SHARED ${SDL_SHARED_ENABLED_BY_DEFAULT} CACHE BOOL "Build a shared version of the library")
314set(SDL_STATIC ON CACHE BOOL "Build a static version of the library")
315
316dep_option(SDL_STATIC_PIC      "Static version of the library should be built with Position Independent Code" OFF "SDL_STATIC" OFF)
317
318# General source files
319file(GLOB SOURCE_FILES
320  ${SDL2_SOURCE_DIR}/src/*.c
321  ${SDL2_SOURCE_DIR}/src/atomic/*.c
322  ${SDL2_SOURCE_DIR}/src/audio/*.c
323  ${SDL2_SOURCE_DIR}/src/cpuinfo/*.c
324  ${SDL2_SOURCE_DIR}/src/dynapi/*.c
325  ${SDL2_SOURCE_DIR}/src/events/*.c
326  ${SDL2_SOURCE_DIR}/src/file/*.c
327  ${SDL2_SOURCE_DIR}/src/libm/*.c
328  ${SDL2_SOURCE_DIR}/src/render/*.c
329  ${SDL2_SOURCE_DIR}/src/render/*/*.c
330  ${SDL2_SOURCE_DIR}/src/stdlib/*.c
331  ${SDL2_SOURCE_DIR}/src/thread/*.c
332  ${SDL2_SOURCE_DIR}/src/timer/*.c
333  ${SDL2_SOURCE_DIR}/src/video/*.c)
334
335
336if(ASSERTIONS STREQUAL "auto")
337  # Do nada - use optimization settings to determine the assertion level
338elseif(ASSERTIONS STREQUAL "disabled")
339  set(SDL_DEFAULT_ASSERT_LEVEL 0)
340elseif(ASSERTIONS STREQUAL "release")
341  set(SDL_DEFAULT_ASSERT_LEVEL 1)
342elseif(ASSERTIONS STREQUAL "enabled")
343  set(SDL_DEFAULT_ASSERT_LEVEL 2)
344elseif(ASSERTIONS STREQUAL "paranoid")
345  set(SDL_DEFAULT_ASSERT_LEVEL 3)
346else()
347  message_error("unknown assertion level")
348endif()
349set(HAVE_ASSERTIONS ${ASSERTIONS})
350
351# Compiler option evaluation
352if(USE_GCC OR USE_CLANG)
353  # Check for -Wall first, so later things can override pieces of it.
354  check_c_compiler_flag(-Wall HAVE_GCC_WALL)
355  if(HAVE_GCC_WALL)
356    list(APPEND EXTRA_CFLAGS "-Wall")
357    if(HAIKU)
358      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-multichar")
359    endif()
360  endif()
361
362  check_c_compiler_flag(-Wdeclaration-after-statement HAVE_GCC_WDECLARATION_AFTER_STATEMENT)
363  if(HAVE_GCC_WDECLARATION_AFTER_STATEMENT)
364    check_c_compiler_flag(-Werror=declaration-after-statement HAVE_GCC_WERROR_DECLARATION_AFTER_STATEMENT)
365    if(HAVE_GCC_WERROR_DECLARATION_AFTER_STATEMENT)
366      list(APPEND EXTRA_CFLAGS "-Werror=declaration-after-statement")
367    endif()
368    list(APPEND EXTRA_CFLAGS "-Wdeclaration-after-statement")
369  endif()
370
371  if(DEPENDENCY_TRACKING)
372    check_c_source_compiles("
373        #if !defined(__GNUC__) || __GNUC__ < 3
374        #error Dependency tracking requires GCC 3.0 or newer
375        #endif
376        int main(int argc, char **argv) { }" HAVE_DEPENDENCY_TRACKING)
377  endif()
378
379  if(GCC_ATOMICS)
380    check_c_source_compiles("int main(int argc, char **argv) {
381        int a;
382        void *x, *y, *z;
383        __sync_lock_test_and_set(&a, 4);
384        __sync_lock_test_and_set(&x, y);
385        __sync_fetch_and_add(&a, 1);
386        __sync_bool_compare_and_swap(&a, 5, 10);
387        __sync_bool_compare_and_swap(&x, y, z); }" HAVE_GCC_ATOMICS)
388    if(NOT HAVE_GCC_ATOMICS)
389      check_c_source_compiles("int main(int argc, char **argv) {
390          int a;
391          __sync_lock_test_and_set(&a, 1);
392          __sync_lock_release(&a); }" HAVE_GCC_SYNC_LOCK_TEST_AND_SET)
393    endif()
394  endif()
395
396  set(CMAKE_REQUIRED_FLAGS "-mpreferred-stack-boundary=2")
397  check_c_source_compiles("int x = 0; int main(int argc, char **argv) {}"
398    HAVE_GCC_PREFERRED_STACK_BOUNDARY)
399  set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
400
401  set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden -Werror")
402  check_c_source_compiles("
403      #if !defined(__GNUC__) || __GNUC__ < 4
404      #error SDL only uses visibility attributes in GCC 4 or newer
405      #endif
406      int main(int argc, char **argv) {}" HAVE_GCC_FVISIBILITY)
407  if(HAVE_GCC_FVISIBILITY)
408    list(APPEND EXTRA_CFLAGS "-fvisibility=hidden")
409  endif()
410  set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
411
412  check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW)
413  if(HAVE_GCC_WSHADOW)
414    list(APPEND EXTRA_CFLAGS "-Wshadow")
415  endif()
416
417  set(CMAKE_REQUIRED_FLAGS "-Wl,--no-undefined")
418  check_c_compiler_flag("" HAVE_NO_UNDEFINED)
419  set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
420  if(HAVE_NO_UNDEFINED)
421    list(APPEND EXTRA_LDFLAGS "-Wl,--no-undefined")
422  endif()
423endif()
424
425if(ASSEMBLY)
426  if(USE_GCC OR USE_CLANG)
427    set(SDL_ASSEMBLY_ROUTINES 1)
428    # TODO: Those all seem to be quite GCC specific - needs to be
429    # reworked for better compiler support
430    set(HAVE_ASSEMBLY TRUE)
431    if(MMX)
432      set(CMAKE_REQUIRED_FLAGS "-mmmx")
433      check_c_source_compiles("
434          #ifdef __MINGW32__
435          #include <_mingw.h>
436          #ifdef __MINGW64_VERSION_MAJOR
437          #include <intrin.h>
438          #else
439          #include <mmintrin.h>
440          #endif
441          #else
442          #include <mmintrin.h>
443          #endif
444          #ifndef __MMX__
445          #error Assembler CPP flag not enabled
446          #endif
447          int main(int argc, char **argv) { }" HAVE_MMX)
448      if(HAVE_MMX)
449        list(APPEND EXTRA_CFLAGS "-mmmx")
450      endif()
451      set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
452    endif()
453
454    if(3DNOW)
455      set(CMAKE_REQUIRED_FLAGS "-m3dnow")
456      check_c_source_compiles("
457          #include <mm3dnow.h>
458          #ifndef __3dNOW__
459          #error Assembler CPP flag not enabled
460          #endif
461          int main(int argc, char **argv) {
462            void *p = 0;
463            _m_prefetch(p);
464          }" HAVE_3DNOW)
465      if(HAVE_3DNOW)
466        list(APPEND EXTRA_CFLAGS "-m3dnow")
467      endif()
468      set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
469    endif()
470
471    if(SSE)
472      set(CMAKE_REQUIRED_FLAGS "-msse")
473      check_c_source_compiles("
474          #ifdef __MINGW32__
475          #include <_mingw.h>
476          #ifdef __MINGW64_VERSION_MAJOR
477          #include <intrin.h>
478          #else
479          #include <xmmintrin.h>
480          #endif
481          #else
482          #include <xmmintrin.h>
483          #endif
484          #ifndef __SSE__
485          #error Assembler CPP flag not enabled
486          #endif
487          int main(int argc, char **argv) { }" HAVE_SSE)
488      if(HAVE_SSE)
489        list(APPEND EXTRA_CFLAGS "-msse")
490      endif()
491      set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
492    endif()
493
494    if(SSE2)
495      set(CMAKE_REQUIRED_FLAGS "-msse2")
496      check_c_source_compiles("
497          #ifdef __MINGW32__
498          #include <_mingw.h>
499          #ifdef __MINGW64_VERSION_MAJOR
500          #include <intrin.h>
501          #else
502          #include <emmintrin.h>
503          #endif
504          #else
505          #include <emmintrin.h>
506          #endif
507          #ifndef __SSE2__
508          #error Assembler CPP flag not enabled
509          #endif
510          int main(int argc, char **argv) { }" HAVE_SSE2)
511      if(HAVE_SSE2)
512        list(APPEND EXTRA_CFLAGS "-msse2")
513      endif()
514      set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
515    endif()
516
517    if(SSEMATH)
518      if(SSE OR SSE2)
519        if(USE_GCC)
520          list(APPEND EXTRA_CFLAGS "-mfpmath=387")
521        endif()
522        set(HAVE_SSEMATH TRUE)
523      endif()
524    endif()
525
526    if(ALTIVEC)
527      set(CMAKE_REQUIRED_FLAGS "-maltivec")
528      check_c_source_compiles("
529          #include <altivec.h>
530          vector unsigned int vzero() {
531              return vec_splat_u32(0);
532          }
533          int main(int argc, char **argv) { }" HAVE_ALTIVEC_H_HDR)
534      check_c_source_compiles("
535          vector unsigned int vzero() {
536              return vec_splat_u32(0);
537          }
538          int main(int argc, char **argv) { }" HAVE_ALTIVEC)
539      set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
540      if(HAVE_ALTIVEC OR HAVE_ALTIVEC_H_HDR)
541        set(HAVE_ALTIVEC TRUE) # if only HAVE_ALTIVEC_H_HDR is set
542        list(APPEND EXTRA_CFLAGS "-maltivec")
543        set(SDL_ALTIVEC_BLITTERS 1)
544        if(HAVE_ALTIVEC_H_HDR)
545          set(HAVE_ALTIVEC_H 1)
546        endif()
547      endif()
548    endif()
549  elseif(MSVC_VERSION GREATER 1500)
550    # TODO: SDL_cpuinfo.h needs to support the user's configuration wish
551    # for MSVC - right now it is always activated
552    if(NOT ARCH_64)
553      set(HAVE_MMX TRUE)
554      set(HAVE_3DNOW TRUE)
555    endif()
556    set(HAVE_SSE TRUE)
557    set(HAVE_SSE2 TRUE)
558    set(SDL_ASSEMBLY_ROUTINES 1)
559  endif()
560# TODO:
561#else()
562#  if(USE_GCC OR USE_CLANG)
563#    list(APPEND EXTRA_CFLAGS "-mno-sse" "-mno-sse2" "-mno-mmx")
564#  endif()
565endif()
566
567# TODO: Can't deactivate on FreeBSD? w/o LIBC, SDL_stdinc.h can't define
568# anything.
569if(LIBC)
570  if(WINDOWS AND NOT MINGW)
571    set(HAVE_LIBC TRUE)
572    foreach(_HEADER stdio.h string.h ctype.h math.h)
573      string(TOUPPER "HAVE_${_HEADER}" _UPPER)
574      string(REPLACE "." "_" _HAVE_H ${_UPPER})
575      set(${_HAVE_H} 1)
576    endforeach()
577    set(HAVE_SIGNAL_H 1)
578    foreach(_FN
579            malloc calloc realloc free qsort abs memset memcpy memmove memcmp
580            strlen _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa
581            _ultoa strtol strtoul strtoll strtod atoi atof strcmp strncmp
582            _stricmp _strnicmp sscanf atan atan2 acos asin ceil copysign cos
583            cosf fabs floor log pow scalbn sin sinf sqrt sqrtf tan tanf)
584      string(TOUPPER ${_FN} _UPPER)
585      set(HAVE_${_UPPER} 1)
586    endforeach()
587    if(NOT CYGWIN AND NOT MINGW)
588      set(HAVE_ALLOCA 1)
589    endif()
590    set(HAVE_M_PI 1)
591    add_definitions(-D_USE_MATH_DEFINES) # needed for M_PI
592    set(STDC_HEADERS 1)
593  else()
594    set(HAVE_LIBC TRUE)
595    check_include_file(sys/types.h HAVE_SYS_TYPES_H)
596    foreach(_HEADER
597            stdio.h stdlib.h stddef.h stdarg.h malloc.h memory.h string.h
598            strings.h inttypes.h stdint.h ctype.h math.h iconv.h signal.h)
599      string(TOUPPER "HAVE_${_HEADER}" _UPPER)
600      string(REPLACE "." "_" _HAVE_H ${_UPPER})
601      check_include_file("${_HEADER}" ${_HAVE_H})
602    endforeach()
603
604    check_include_files("dlfcn.h;stdint.h;stddef.h;inttypes.h;stdlib.h;strings.h;string.h;float.h" STDC_HEADERS)
605    check_type_size("size_t" SIZEOF_SIZE_T)
606    check_symbol_exists(M_PI math.h HAVE_M_PI)
607    # TODO: refine the mprotect check
608    check_c_source_compiles("#include <sys/types.h>
609                             #include <sys/mman.h>
610                             int main() { }" HAVE_MPROTECT)
611    foreach(_FN
612            strtod malloc calloc realloc free getenv setenv putenv unsetenv
613            qsort abs bcopy memset memcpy memmove memcmp strlen strlcpy strlcat
614            strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa
615            _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull
616            atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp
617            vsscanf vsnprintf fseeko fseeko64 sigaction setjmp
618            nanosleep sysconf sysctlbyname
619            )
620      string(TOUPPER ${_FN} _UPPER)
621      set(_HAVEVAR "HAVE_${_UPPER}")
622      check_function_exists("${_FN}" ${_HAVEVAR})
623    endforeach()
624
625    check_library_exists(m pow "" HAVE_LIBM)
626    if(HAVE_LIBM)
627      set(CMAKE_REQUIRED_LIBRARIES m)
628      foreach(_FN
629              atan atan2 ceil copysign cos cosf fabs floor log pow scalbn sin
630              sinf sqrt sqrtf tan tanf acos asin)
631        string(TOUPPER ${_FN} _UPPER)
632        set(_HAVEVAR "HAVE_${_UPPER}")
633        check_function_exists("${_FN}" ${_HAVEVAR})
634      endforeach()
635      set(CMAKE_REQUIRED_LIBRARIES)
636      list(APPEND EXTRA_LIBS m)
637    endif()
638
639    check_library_exists(iconv iconv_open "" HAVE_LIBICONV)
640    if(HAVE_LIBICONV)
641      list(APPEND EXTRA_LIBS iconv)
642      set(HAVE_ICONV 1)
643    endif()
644
645    if(NOT APPLE)
646      check_include_file(alloca.h HAVE_ALLOCA_H)
647      check_function_exists(alloca HAVE_ALLOCA)
648    else()
649      set(HAVE_ALLOCA_H 1)
650      set(HAVE_ALLOCA 1)
651    endif()
652
653    check_struct_has_member("struct sigaction" "sa_sigaction" "signal.h" HAVE_SA_SIGACTION)
654  endif()
655else()
656  if(WINDOWS)
657    set(HAVE_STDARG_H 1)
658    set(HAVE_STDDEF_H 1)
659  endif()
660endif()
661
662
663# Enable/disable various subsystems of the SDL library
664foreach(_SUB ${SDL_SUBSYSTEMS})
665  string(TOUPPER ${_SUB} _OPT)
666  if(NOT SDL_${_OPT})
667    set(SDL_${_OPT}_DISABLED 1)
668  endif()
669endforeach()
670if(SDL_JOYSTICK)
671  file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/*.c)
672  set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
673endif()
674if(SDL_HAPTIC)
675  if(NOT SDL_JOYSTICK)
676    # Haptic requires some private functions from the joystick subsystem.
677    message_error("SDL_HAPTIC requires SDL_JOYSTICK, which is not enabled")
678  endif()
679  file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/*.c)
680  set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
681endif()
682if(SDL_POWER)
683  file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/*.c)
684  set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES})
685endif()
686# TODO: in configure.in, the test for LOADSO and SDL_DLOPEN is a bit weird:
687# if LOADSO is not wanted, SDL_LOADSO_DISABLED is set
688# If however on Unix or APPLE dlopen() is detected via CheckDLOPEN(),
689# SDL_LOADSO_DISABLED will not be set, regardless of the LOADSO settings
690
691# General SDL subsystem options, valid for all platforms
692if(SDL_AUDIO)
693  # CheckDummyAudio/CheckDiskAudio - valid for all platforms
694  if(DUMMYAUDIO)
695    set(SDL_AUDIO_DRIVER_DUMMY 1)
696    file(GLOB DUMMYAUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/dummy/*.c)
697    set(SOURCE_FILES ${SOURCE_FILES} ${DUMMYAUDIO_SOURCES})
698    set(HAVE_DUMMYAUDIO TRUE)
699  endif()
700  if(DISKAUDIO)
701    set(SDL_AUDIO_DRIVER_DISK 1)
702    file(GLOB DISKAUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/disk/*.c)
703    set(SOURCE_FILES ${SOURCE_FILES} ${DISKAUDIO_SOURCES})
704    set(HAVE_DISKAUDIO TRUE)
705  endif()
706endif()
707
708if(SDL_DLOPEN)
709  # Relevant for Unix/Darwin only
710  if(UNIX OR APPLE)
711    CheckDLOPEN()
712  endif()
713endif()
714
715if(SDL_VIDEO)
716  if(VIDEO_DUMMY)
717    set(SDL_VIDEO_DRIVER_DUMMY 1)
718    file(GLOB VIDEO_DUMMY_SOURCES ${SDL2_SOURCE_DIR}/src/video/dummy/*.c)
719    set(SOURCE_FILES ${SOURCE_FILES} ${VIDEO_DUMMY_SOURCES})
720    set(HAVE_VIDEO_DUMMY TRUE)
721    set(HAVE_SDL_VIDEO TRUE)
722  endif()
723endif()
724
725if(ANDROID)
726  file(GLOB ANDROID_CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/android/*.c)
727  set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_CORE_SOURCES})
728  file(GLOB ANDROID_MAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/android/*.c)
729  set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_MAIN_SOURCES})
730  if(SDL_AUDIO)
731    set(SDL_AUDIO_DRIVER_ANDROID 1)
732    file(GLOB ANDROID_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/android/*.c)
733    set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_AUDIO_SOURCES})
734    set(HAVE_SDL_AUDIO TRUE)
735  endif()
736  if(SDL_FILESYSTEM)
737    set(SDL_FILESYSTEM_ANDROID 1)
738    file(GLOB ANDROID_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/android/*.c)
739    set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_FILESYSTEM_SOURCES})
740    set(HAVE_SDL_FILESYSTEM TRUE)
741  endif()
742  if(SDL_JOYSTICK)
743    set(SDL_JOYSTICK_ANDROID 1)
744    file(GLOB ANDROID_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/android/*.c)
745    set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_JOYSTICK_SOURCES})
746    set(HAVE_SDL_JOYSTICK TRUE)
747  endif()
748  if(SDL_POWER)
749    set(SDL_POWER_ANDROID 1)
750    file(GLOB ANDROID_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/android/*.c)
751    set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_POWER_SOURCES})
752    set(HAVE_SDL_POWER TRUE)
753  endif()
754  if(SDL_VIDEO)
755    set(SDL_VIDEO_DRIVER_ANDROID 1)
756    file(GLOB ANDROID_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/android/*.c)
757    set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_VIDEO_SOURCES})
758    set(HAVE_SDL_VIDEO TRUE)
759
760    #enable gles
761    if(VIDEO_OPENGLES)
762      set(SDL_VIDEO_OPENGL_EGL 1)
763      set(HAVE_VIDEO_OPENGLES TRUE)
764      set(SDL_VIDEO_OPENGL_ES2 1)
765      set(SDL_VIDEO_RENDER_OGL_ES2 1)
766    endif()
767  endif()
768  list(APPEND EXTRA_LDFLAGS "-Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit")
769endif()
770
771# Platform-specific options and settings
772if(EMSCRIPTEN)
773  # Hide noisy warnings that intend to aid mostly during initial stages of porting a new
774  # project. Uncomment at will for verbose cross-compiling -I/../ path info.
775  add_definitions(-Wno-warn-absolute-paths)
776  if(SDL_AUDIO)
777    set(SDL_AUDIO_DRIVER_EMSCRIPTEN 1)
778    file(GLOB EM_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/emscripten/*.c)
779    set(SOURCE_FILES ${SOURCE_FILES} ${EM_AUDIO_SOURCES})
780    set(HAVE_SDL_AUDIO TRUE)
781  endif()
782  if(SDL_FILESYSTEM)
783    set(SDL_FILESYSTEM_EMSCRIPTEN 1)
784    file(GLOB EM_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/emscripten/*.c)
785    set(SOURCE_FILES ${SOURCE_FILES} ${EM_FILESYSTEM_SOURCES})
786    set(HAVE_SDL_FILESYSTEM TRUE)
787  endif()
788  if(SDL_JOYSTICK)
789    set(SDL_JOYSTICK_EMSCRIPTEN 1)
790    file(GLOB EM_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/emscripten/*.c)
791    set(SOURCE_FILES ${SOURCE_FILES} ${EM_JOYSTICK_SOURCES})
792    set(HAVE_SDL_JOYSTICK TRUE)
793  endif()
794  if(SDL_POWER)
795    set(SDL_POWER_EMSCRIPTEN 1)
796    file(GLOB EM_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/emscripten/*.c)
797    set(SOURCE_FILES ${SOURCE_FILES} ${EM_POWER_SOURCES})
798    set(HAVE_SDL_POWER TRUE)
799  endif()
800  if(SDL_TIMERS)
801    set(SDL_TIMER_UNIX 1)
802    file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
803    set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
804    set(HAVE_SDL_TIMERS TRUE)
805
806    if(CLOCK_GETTIME)
807      set(HAVE_CLOCK_GETTIME 1)
808    endif()
809  endif()
810  if(SDL_VIDEO)
811    set(SDL_VIDEO_DRIVER_EMSCRIPTEN 1)
812    file(GLOB EM_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/emscripten/*.c)
813    set(SOURCE_FILES ${SOURCE_FILES} ${EM_VIDEO_SOURCES})
814    set(HAVE_SDL_VIDEO TRUE)
815
816    #enable gles
817    if(VIDEO_OPENGLES)
818      set(SDL_VIDEO_OPENGL_EGL 1)
819      set(HAVE_VIDEO_OPENGLES TRUE)
820      set(SDL_VIDEO_OPENGL_ES2 1)
821      set(SDL_VIDEO_RENDER_OGL_ES2 1)
822    endif()
823  endif()
824elseif(UNIX AND NOT APPLE)
825  if(SDL_AUDIO)
826    if(SYSV5 OR SOLARIS OR HPUX)
827        set(SDL_AUDIO_DRIVER_SUNAUDIO 1)
828        file(GLOB SUN_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/sun/*.c)
829        set(SOURCE_FILES ${SOURCE_FILES} ${SUN_AUDIO_SOURCES})
830        set(HAVE_SDL_AUDIO TRUE)
831    elseif(NETBSD OR OPENBSD)
832        set(SDL_AUDIO_DRIVER_BSD 1)
833        file(GLOB BSD_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/bsd/*.c)
834        set(SOURCE_FILES ${SOURCE_FILES} ${BSD_AUDIO_SOURCES})
835        set(HAVE_SDL_AUDIO TRUE)
836    elseif(AIX)
837        set(SDL_AUDIO_DRIVER_PAUDIO 1)
838        file(GLOB AIX_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/paudio/*.c)
839        set(SOURCE_FILES ${SOURCE_FILES} ${AIX_AUDIO_SOURCES})
840        set(HAVE_SDL_AUDIO TRUE)
841    endif()
842    CheckOSS()
843    CheckALSA()
844    CheckPulseAudio()
845    CheckESD()
846    CheckARTS()
847    CheckNAS()
848    CheckSNDIO()
849    CheckFusionSound()
850  endif()
851
852  if(SDL_VIDEO)
853    # Need to check for Raspberry PI first and add platform specific compiler flags, otherwise the test for GLES fails!
854    CheckRPI()
855    CheckX11()
856    CheckMir()
857    CheckDirectFB()
858    CheckOpenGLX11()
859    CheckOpenGLESX11()
860    CheckWayland()
861    CheckVivante()
862  endif()
863
864  if(LINUX)
865    check_c_source_compiles("
866        #include <linux/input.h>
867        #ifndef EVIOCGNAME
868        #error EVIOCGNAME() ioctl not available
869        #endif
870        int main(int argc, char** argv) {}" HAVE_INPUT_EVENTS)
871
872    check_c_source_compiles("
873        #include <linux/kd.h>
874        #include <linux/keyboard.h>
875
876        int main(int argc, char **argv)
877        {
878            struct kbentry kbe;
879            kbe.kb_table = KG_CTRL;
880            ioctl(0, KDGKBENT, &kbe);
881        }" HAVE_INPUT_KD)
882
883    file(GLOB CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/linux/*.c)
884    set(SOURCE_FILES ${SOURCE_FILES} ${CORE_SOURCES})
885
886    if(HAVE_INPUT_EVENTS)
887      set(SDL_INPUT_LINUXEV 1)
888    endif()
889
890    if(SDL_HAPTIC AND HAVE_INPUT_EVENTS)
891      set(SDL_HAPTIC_LINUX 1)
892      file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/linux/*.c)
893      set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
894      set(HAVE_SDL_HAPTIC TRUE)
895    endif()
896
897    if(HAVE_INPUT_KD)
898      set(SDL_INPUT_LINUXKD 1)
899    endif()
900
901    check_include_file("libudev.h" HAVE_LIBUDEV_H)
902
903    if(PKG_CONFIG_FOUND)
904      pkg_search_module(DBUS dbus-1 dbus)
905      if(DBUS_FOUND)
906        set(HAVE_DBUS_DBUS_H TRUE)
907        include_directories(${DBUS_INCLUDE_DIRS})
908        list(APPEND EXTRA_LIBS ${DBUS_LIBRARIES})
909      endif()
910
911      pkg_search_module(IBUS ibus-1.0 ibus)
912      if(IBUS_FOUND)
913        set(HAVE_IBUS_IBUS_H TRUE)
914        include_directories(${IBUS_INCLUDE_DIRS})
915        list(APPEND EXTRA_LIBS ${IBUS_LIBRARIES})
916      endif()
917    endif()
918
919    check_include_file("fcitx/frontend.h" HAVE_FCITX_FRONTEND_H)
920
921  endif()
922
923  if(INPUT_TSLIB)
924    check_c_source_compiles("
925        #include \"tslib.h\"
926        int main(int argc, char** argv) { }" HAVE_INPUT_TSLIB)
927    if(HAVE_INPUT_TSLIB)
928      set(SDL_INPUT_TSLIB 1)
929      list(APPEND EXTRA_LIBS ts)
930    endif()
931  endif()
932
933  if(SDL_JOYSTICK)
934    CheckUSBHID()   # seems to be BSD specific - limit the test to BSD only?
935    if(LINUX AND NOT ANDROID)
936      set(SDL_JOYSTICK_LINUX 1)
937      file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/linux/*.c)
938      set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
939      set(HAVE_SDL_JOYSTICK TRUE)
940    endif()
941  endif()
942
943  CheckPTHREAD()
944
945  if(CLOCK_GETTIME)
946    check_library_exists(rt clock_gettime "" FOUND_CLOCK_GETTIME)
947    if(FOUND_CLOCK_GETTIME)
948      list(APPEND EXTRA_LIBS rt)
949      set(HAVE_CLOCK_GETTIME 1)
950    else()
951      check_library_exists(c clock_gettime "" FOUND_CLOCK_GETTIME)
952      if(FOUND_CLOCK_GETTIME)
953        set(HAVE_CLOCK_GETTIME 1)
954      endif()
955    endif()
956  endif()
957
958  check_include_file(linux/version.h HAVE_LINUX_VERSION_H)
959  if(HAVE_LINUX_VERSION_H)
960    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_LINUX_VERSION_H")
961  endif()
962
963  if(SDL_POWER)
964    if(LINUX)
965      set(SDL_POWER_LINUX 1)
966      file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/linux/*.c)
967      set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES})
968      set(HAVE_SDL_POWER TRUE)
969    endif()
970  endif()
971
972  if(SDL_FILESYSTEM)
973    set(SDL_FILESYSTEM_UNIX 1)
974    file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/unix/*.c)
975    set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
976    set(HAVE_SDL_FILESYSTEM TRUE)
977  endif()
978
979  if(SDL_TIMERS)
980    set(SDL_TIMER_UNIX 1)
981    file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
982    set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
983    set(HAVE_SDL_TIMERS TRUE)
984  endif()
985
986  if(RPATH)
987    set(SDL_RLD_FLAGS "")
988    if(BSDI OR FREEBSD OR LINUX OR NETBSD)
989      set(CMAKE_REQUIRED_FLAGS "-Wl,--enable-new-dtags")
990      check_c_compiler_flag("" HAVE_ENABLE_NEW_DTAGS)
991      set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
992      if(HAVE_ENABLE_NEW_DTAGS)
993        set(SDL_RLD_FLAGS "-Wl,-rpath,\${libdir} -Wl,--enable-new-dtags")
994      else()
995        set(SDL_RLD_FLAGS "-Wl,-rpath,\${libdir}")
996      endif()
997    elseif(SOLARIS)
998      set(SDL_RLD_FLAGS "-R\${libdir}")
999    endif()
1000    set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
1001    set(HAVE_RPATH TRUE)
1002  endif()
1003
1004elseif(WINDOWS)
1005  find_program(WINDRES windres)
1006
1007  check_c_source_compiles("
1008    #include <windows.h>
1009    int main(int argc, char **argv) { }" HAVE_WIN32_CC)
1010
1011  file(GLOB CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/windows/*.c)
1012  set(SOURCE_FILES ${SOURCE_FILES} ${CORE_SOURCES})
1013
1014  if(MSVC)
1015    # Prevent codegen that would use the VC runtime libraries.
1016    add_definitions(/GS-)
1017    if(NOT ARCH_64)
1018      add_definitions(/arch:SSE)
1019    endif()
1020  endif()
1021
1022  # Check for DirectX
1023  if(DIRECTX)
1024    if(DEFINED MSVC_VERSION AND NOT ${MSVC_VERSION} LESS 1700)
1025        set(USE_WINSDK_DIRECTX TRUE)
1026    endif()
1027    if(NOT CMAKE_COMPILER_IS_MINGW AND NOT USE_WINSDK_DIRECTX)
1028      if("$ENV{DXSDK_DIR}" STREQUAL "")
1029        message_error("DIRECTX requires the \$DXSDK_DIR environment variable to be set")
1030      endif()
1031      set(CMAKE_REQUIRED_FLAGS "/I\"$ENV{DXSDK_DIR}\\Include\"")
1032    endif()
1033
1034    if(HAVE_WIN32_CC)
1035      # xinput.h may need windows.h, but doesn't include it itself.
1036      check_c_source_compiles("
1037        #include <windows.h>
1038        #include <xinput.h>
1039        int main(int argc, char **argv) { }" HAVE_XINPUT_H)
1040    else()
1041      check_include_file(xinput.h HAVE_XINPUT_H)
1042    endif()
1043
1044    check_include_file(d3d9.h HAVE_D3D_H)
1045    check_include_file(d3d11_1.h HAVE_D3D11_H)
1046    check_include_file(ddraw.h HAVE_DDRAW_H)
1047    check_include_file(dsound.h HAVE_DSOUND_H)
1048    check_include_file(dinput.h HAVE_DINPUT_H)
1049    check_include_file(xaudio2.h HAVE_XAUDIO2_H)
1050    check_include_file(dxgi.h HAVE_DXGI_H)
1051    if(HAVE_D3D_H OR HAVE_D3D11_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H OR HAVE_XAUDIO2_H)
1052      set(HAVE_DIRECTX TRUE)
1053      if(NOT CMAKE_COMPILER_IS_MINGW AND NOT USE_WINSDK_DIRECTX)
1054      # TODO: change $ENV{DXSDL_DIR} to get the path from the include checks
1055        link_directories($ENV{DXSDK_DIR}\\lib\\${PROCESSOR_ARCH})
1056        include_directories($ENV{DXSDK_DIR}\\Include)
1057      endif()
1058    endif()
1059    set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
1060  endif()
1061
1062  if(SDL_AUDIO)
1063    set(SDL_AUDIO_DRIVER_WINMM 1)
1064    file(GLOB WINMM_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/winmm/*.c)
1065    set(SOURCE_FILES ${SOURCE_FILES} ${WINMM_AUDIO_SOURCES})
1066    set(HAVE_SDL_AUDIO TRUE)
1067
1068    if(HAVE_DSOUND_H)
1069      set(SDL_AUDIO_DRIVER_DSOUND 1)
1070      file(GLOB DSOUND_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/directsound/*.c)
1071      set(SOURCE_FILES ${SOURCE_FILES} ${DSOUND_AUDIO_SOURCES})
1072    endif()
1073
1074    if(HAVE_XAUDIO2_H)
1075      set(SDL_AUDIO_DRIVER_XAUDIO2 1)
1076      file(GLOB XAUDIO2_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/xaudio2/*.c)
1077      set(SOURCE_FILES ${SOURCE_FILES} ${XAUDIO2_AUDIO_SOURCES})
1078    endif()
1079  endif()
1080
1081  if(SDL_VIDEO)
1082    # requires SDL_LOADSO on Windows (IME, DX, etc.)
1083    if(NOT SDL_LOADSO)
1084      message_error("SDL_VIDEO requires SDL_LOADSO, which is not enabled")
1085    endif()
1086    set(SDL_VIDEO_DRIVER_WINDOWS 1)
1087    file(GLOB WIN_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/windows/*.c)
1088    set(SOURCE_FILES ${SOURCE_FILES} ${WIN_VIDEO_SOURCES})
1089
1090    if(RENDER_D3D AND HAVE_D3D_H)
1091      set(SDL_VIDEO_RENDER_D3D 1)
1092      set(HAVE_RENDER_D3D TRUE)
1093    endif()
1094    if(RENDER_D3D AND HAVE_D3D11_H)
1095      set(SDL_VIDEO_RENDER_D3D11 1)
1096      set(HAVE_RENDER_D3D TRUE)
1097    endif()
1098    set(HAVE_SDL_VIDEO TRUE)
1099  endif()
1100
1101  if(SDL_THREADS)
1102    set(SDL_THREAD_WINDOWS 1)
1103    set(SOURCE_FILES ${SOURCE_FILES}
1104      ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_sysmutex.c
1105      ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_syssem.c
1106      ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_systhread.c
1107      ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_systls.c
1108      ${SDL2_SOURCE_DIR}/src/thread/generic/SDL_syscond.c)
1109    set(HAVE_SDL_THREADS TRUE)
1110  endif()
1111
1112  if(SDL_POWER)
1113    set(SDL_POWER_WINDOWS 1)
1114    set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/power/windows/SDL_syspower.c)
1115    set(HAVE_SDL_POWER TRUE)
1116  endif()
1117
1118  if(SDL_FILESYSTEM)
1119    set(SDL_FILESYSTEM_WINDOWS 1)
1120    file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/windows/*.c)
1121    set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
1122    set(HAVE_SDL_FILESYSTEM TRUE)
1123  endif()
1124
1125  # Libraries for Win32 native and MinGW
1126  list(APPEND EXTRA_LIBS user32 gdi32 winmm imm32 ole32 oleaut32 version uuid)
1127
1128  # TODO: in configure.in the check for timers is set on
1129  # cygwin | mingw32* - does this include mingw32CE?
1130  if(SDL_TIMERS)
1131    set(SDL_TIMER_WINDOWS 1)
1132    file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/windows/*.c)
1133    set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
1134    set(HAVE_SDL_TIMERS TRUE)
1135  endif()
1136
1137  if(SDL_LOADSO)
1138    set(SDL_LOADSO_WINDOWS 1)
1139    file(GLOB LOADSO_SOURCES ${SDL2_SOURCE_DIR}/src/loadso/windows/*.c)
1140    set(SOURCE_FILES ${SOURCE_FILES} ${LOADSO_SOURCES})
1141    set(HAVE_SDL_LOADSO TRUE)
1142  endif()
1143
1144  file(GLOB CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/windows/*.c)
1145  set(SOURCE_FILES ${SOURCE_FILES} ${CORE_SOURCES})
1146
1147  if(SDL_VIDEO)
1148    if(VIDEO_OPENGL)
1149      set(SDL_VIDEO_OPENGL 1)
1150      set(SDL_VIDEO_OPENGL_WGL 1)
1151      set(SDL_VIDEO_RENDER_OGL 1)
1152      set(HAVE_VIDEO_OPENGL TRUE)
1153    endif()
1154
1155    if(VIDEO_OPENGLES)
1156      set(SDL_VIDEO_OPENGL_EGL 1)
1157      set(SDL_VIDEO_OPENGL_ES2 1)
1158      set(SDL_VIDEO_RENDER_OGL_ES2 1)
1159      set(HAVE_VIDEO_OPENGLES TRUE)
1160    endif()
1161  endif()
1162
1163  if(SDL_JOYSTICK)
1164    file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/windows/*.c)
1165    set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
1166    if(HAVE_DINPUT_H)
1167      set(SDL_JOYSTICK_DINPUT 1)
1168      list(APPEND EXTRA_LIBS dinput8)
1169      if(CMAKE_COMPILER_IS_MINGW)
1170        list(APPEND EXTRA_LIBS dxerr8)
1171      elseif (NOT USE_WINSDK_DIRECTX)
1172        list(APPEND EXTRA_LIBS dxerr)
1173      endif()
1174    endif()
1175    if(HAVE_XINPUT_H)
1176      set(SDL_JOYSTICK_XINPUT 1)
1177    endif()
1178    if(NOT HAVE_DINPUT_H AND NOT HAVE_XINPUT_H)
1179      set(SDL_JOYSTICK_WINMM 1)
1180    endif()
1181    set(HAVE_SDL_JOYSTICK TRUE)
1182
1183    if(SDL_HAPTIC)
1184      if(HAVE_DINPUT_H OR HAVE_XINPUT_H)
1185        file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/windows/*.c)
1186        if(HAVE_DINPUT_H)
1187          set(SDL_HAPTIC_DINPUT 1)
1188        endif()
1189        if(HAVE_XINPUT_H)
1190          set(SDL_HAPTIC_XINPUT 1)
1191        endif()
1192      else()
1193        file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c)
1194        set(SDL_HAPTIC_DUMMY 1)
1195      endif()
1196      set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
1197      set(HAVE_SDL_HAPTIC TRUE)
1198    endif()
1199  endif()
1200
1201  file(GLOB VERSION_SOURCES ${SDL2_SOURCE_DIR}/src/main/windows/*.rc)
1202  file(GLOB SDLMAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/windows/*.c)
1203  if(MINGW OR CYGWIN)
1204    list(APPEND EXTRA_LIBS mingw32)
1205    list(APPEND EXTRA_LDFLAGS "-mwindows")
1206    set(SDL_CFLAGS "${SDL_CFLAGS} -Dmain=SDL_main")
1207    list(APPEND SDL_LIBS "-lmingw32" "-lSDL2main" "-mwindows")
1208  endif()
1209elseif(APPLE)
1210  # TODO: rework this for proper MacOS X, iOS and Darwin support
1211
1212  # Requires the darwin file implementation
1213  if(SDL_FILE)
1214    file(GLOB EXTRA_SOURCES ${SDL2_SOURCE_DIR}/src/file/cocoa/*.m)
1215    set(SOURCE_FILES ${EXTRA_SOURCES} ${SOURCE_FILES})
1216    set_source_files_properties(${EXTRA_SOURCES} PROPERTIES LANGUAGE C)
1217    set(HAVE_SDL_FILE TRUE)
1218    set(SDL_FRAMEWORK_COCOA 1)
1219    set(SDL_FRAMEWORK_COREVIDEO 1)
1220  else()
1221    message_error("SDL_FILE must be enabled to build on MacOS X")
1222  endif()
1223
1224  if(SDL_AUDIO)
1225    set(SDL_AUDIO_DRIVER_COREAUDIO 1)
1226    file(GLOB AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/coreaudio/*.m)
1227    set(SOURCE_FILES ${SOURCE_FILES} ${AUDIO_SOURCES})
1228    set(HAVE_SDL_AUDIO TRUE)
1229    set(SDL_FRAMEWORK_COREAUDIO 1)
1230    set(SDL_FRAMEWORK_AUDIOTOOLBOX 1)
1231  endif()
1232
1233  if(SDL_JOYSTICK)
1234    set(SDL_JOYSTICK_IOKIT 1)
1235    if (IOS)
1236      file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/iphoneos/*.m)
1237    else()
1238      file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/darwin/*.c)
1239    endif()
1240    set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
1241    set(HAVE_SDL_JOYSTICK TRUE)
1242    set(SDL_FRAMEWORK_IOKIT 1)
1243    set(SDL_FRAMEWORK_FF 1)
1244  endif()
1245
1246  if(SDL_HAPTIC)
1247    set(SDL_HAPTIC_IOKIT 1)
1248    if (IOS)
1249      file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/dummy/*.c)
1250      set(SDL_HAPTIC_DUMMY 1)
1251    else()
1252      file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/darwin/*.c)
1253    endif()
1254    set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
1255    set(HAVE_SDL_HAPTIC TRUE)
1256    set(SDL_FRAMEWORK_IOKIT 1)
1257    set(SDL_FRAMEWORK_FF 1)
1258    if(NOT SDL_JOYSTICK)
1259      message(FATAL_ERROR "SDL_HAPTIC requires SDL_JOYSTICK to be enabled")
1260    endif()
1261  endif()
1262
1263  if(SDL_POWER)
1264    set(SDL_POWER_MACOSX 1)
1265    if (IOS)
1266      file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/uikit/*.m)
1267    else()
1268      file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/macosx/*.c)
1269    endif()
1270    set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES})
1271    set(HAVE_SDL_POWER TRUE)
1272    set(SDL_FRAMEWORK_CARBON 1)
1273    set(SDL_FRAMEWORK_IOKIT 1)
1274  endif()
1275
1276  if(SDL_TIMERS)
1277    set(SDL_TIMER_UNIX 1)
1278    file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
1279    set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
1280    set(HAVE_SDL_TIMERS TRUE)
1281  endif(SDL_TIMERS)
1282
1283  if(SDL_FILESYSTEM)
1284    set(SDL_FILESYSTEM_COCOA 1)
1285    file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/cocoa/*.m)
1286    set_source_files_properties(${FILESYSTEM_SOURCES} PROPERTIES LANGUAGE C)
1287    set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
1288    set(HAVE_SDL_FILESYSTEM TRUE)
1289  endif()
1290
1291  # Actually load the frameworks at the end so we don't duplicate include.
1292  if(SDL_FRAMEWORK_COREVIDEO)
1293    find_library(COREVIDEO CoreVideo)
1294    list(APPEND EXTRA_LIBS ${COREVIDEO})
1295  endif()
1296  if(SDL_FRAMEWORK_COCOA)
1297    find_library(COCOA_LIBRARY Cocoa)
1298    list(APPEND EXTRA_LIBS ${COCOA_LIBRARY})
1299  endif()
1300  if(SDL_FRAMEWORK_IOKIT)
1301    find_library(IOKIT IOKit)
1302    list(APPEND EXTRA_LIBS ${IOKIT})
1303  endif()
1304  if(SDL_FRAMEWORK_FF)
1305    find_library(FORCEFEEDBACK ForceFeedback)
1306    list(APPEND EXTRA_LIBS ${FORCEFEEDBACK})
1307  endif()
1308  if(SDL_FRAMEWORK_CARBON)
1309    find_library(CARBON_LIBRARY Carbon)
1310    list(APPEND EXTRA_LIBS ${CARBON_LIBRARY})
1311  endif()
1312  if(SDL_FRAMEWORK_COREAUDIO)
1313    find_library(COREAUDIO CoreAudio)
1314    list(APPEND EXTRA_LIBS ${COREAUDIO})
1315  endif()
1316  if(SDL_FRAMEWORK_AUDIOTOOLBOX)
1317    find_library(AUDIOTOOLBOX AudioToolbox)
1318    list(APPEND EXTRA_LIBS ${AUDIOTOOLBOX})
1319  endif()
1320
1321  # iOS hack needed - http://code.google.com/p/ios-cmake/ ?
1322  if(SDL_VIDEO)
1323    if (IOS)
1324      set(SDL_VIDEO_DRIVER_UIKIT 1)
1325      file(GLOB UIKITVIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/uikit/*.m)
1326      set(SOURCE_FILES ${SOURCE_FILES} ${UIKITVIDEO_SOURCES})
1327    else()
1328      CheckCOCOA()
1329      if(VIDEO_OPENGL)
1330        set(SDL_VIDEO_OPENGL 1)
1331        set(SDL_VIDEO_OPENGL_CGL 1)
1332        set(SDL_VIDEO_RENDER_OGL 1)
1333        set(HAVE_VIDEO_OPENGL TRUE)
1334      endif()
1335    endif()
1336  endif()
1337
1338  CheckPTHREAD()
1339elseif(HAIKU)
1340  if(SDL_VIDEO)
1341    set(SDL_VIDEO_DRIVER_HAIKU 1)
1342    file(GLOB HAIKUVIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/haiku/*.c)
1343    set(SOURCE_FILES ${SOURCE_FILES} ${HAIKUVIDEO_SOURCES})
1344    set(HAVE_SDL_VIDEO TRUE)
1345
1346    set(SDL_FILESYSTEM_HAIKU 1)
1347    file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/haiku/*.cc)
1348    set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
1349    set(HAVE_SDL_FILESYSTEM TRUE)
1350
1351    if(SDL_TIMERS)
1352      set(SDL_TIMER_HAIKU 1)
1353      file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/haiku/*.c)
1354      set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
1355      set(HAVE_SDL_TIMERS TRUE)
1356    endif(SDL_TIMERS)
1357
1358    if(VIDEO_OPENGL)
1359      # TODO: Use FIND_PACKAGE(OpenGL) instead
1360      set(SDL_VIDEO_OPENGL 1)
1361      set(SDL_VIDEO_OPENGL_BGL 1)
1362      set(SDL_VIDEO_RENDER_OGL 1)
1363      list(APPEND EXTRA_LIBS GL)
1364      set(HAVE_VIDEO_OPENGL TRUE)
1365    endif()
1366  endif()
1367
1368  CheckPTHREAD()
1369endif()
1370
1371# Dummies
1372# configure.in does it differently:
1373# if not have X
1374#   if enable_X {  SDL_X_DISABLED = 1 }
1375#   [add dummy sources]
1376# so it always adds a dummy, without checking, if it was actually requested.
1377# This leads to missing internal references on building, since the
1378# src/X/*.c does not get included.
1379if(NOT HAVE_SDL_JOYSTICK)
1380  set(SDL_JOYSTICK_DISABLED 1)
1381  if(SDL_JOYSTICK AND NOT APPLE) # results in unresolved symbols on OSX
1382
1383    file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/dummy/*.c)
1384    set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
1385  endif()
1386endif()
1387if(NOT HAVE_SDL_HAPTIC)
1388  set(SDL_HAPTIC_DISABLED 1)
1389  file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c)
1390  set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
1391endif()
1392if(NOT HAVE_SDL_LOADSO)
1393  set(SDL_LOADSO_DISABLED 1)
1394  file(GLOB LOADSO_SOURCES ${SDL2_SOURCE_DIR}/src/loadso/dummy/*.c)
1395  set(SOURCE_FILES ${SOURCE_FILES} ${LOADSO_SOURCES})
1396endif()
1397if(NOT HAVE_SDL_FILESYSTEM)
1398  set(SDL_FILESYSTEM_DISABLED 1)
1399  file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/dummy/*.c)
1400  set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
1401endif()
1402
1403# We always need to have threads and timers around
1404if(NOT HAVE_SDL_THREADS)
1405  set(SDL_THREADS_DISABLED 1)
1406  file(GLOB THREADS_SOURCES ${SDL2_SOURCE_DIR}/src/thread/generic/*.c)
1407  set(SOURCE_FILES ${SOURCE_FILES} ${THREADS_SOURCES})
1408endif()
1409if(NOT HAVE_SDL_TIMERS)
1410  set(SDL_TIMERS_DISABLED 1)
1411  file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/dummy/*.c)
1412  set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
1413endif()
1414
1415if(NOT SDLMAIN_SOURCES)
1416  file(GLOB SDLMAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/dummy/*.c)
1417endif()
1418
1419# Append the -MMD -MT flags
1420# if(DEPENDENCY_TRACKING)
1421#   if(COMPILER_IS_GNUCC)
1422#     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -MMD -MT \$@")
1423#   endif()
1424# endif()
1425
1426configure_file("${SDL2_SOURCE_DIR}/include/SDL_config.h.cmake"
1427  "${SDL2_BINARY_DIR}/include/SDL_config.h")
1428
1429# Prepare the flags and remove duplicates
1430if(EXTRA_LDFLAGS)
1431  list(REMOVE_DUPLICATES EXTRA_LDFLAGS)
1432endif()
1433if(EXTRA_LIBS)
1434  list(REMOVE_DUPLICATES EXTRA_LIBS)
1435endif()
1436if(EXTRA_CFLAGS)
1437  list(REMOVE_DUPLICATES EXTRA_CFLAGS)
1438endif()
1439listtostr(EXTRA_CFLAGS _EXTRA_CFLAGS)
1440set(EXTRA_CFLAGS ${_EXTRA_CFLAGS})
1441
1442# Compat helpers for the configuration files
1443if(NOT WINDOWS OR CYGWIN)
1444  # TODO: we need a Windows script, too
1445  execute_process(COMMAND sh ${SDL2_SOURCE_DIR}/build-scripts/updaterev.sh)
1446
1447  set(prefix ${CMAKE_INSTALL_PREFIX})
1448  set(exec_prefix "\${prefix}")
1449  set(libdir "\${exec_prefix}/lib${LIB_SUFFIX}")
1450  set(bindir "\${exec_prefix}/bin")
1451  set(includedir "\${prefix}/include")
1452  if(SDL_STATIC)
1453    set(ENABLE_STATIC_TRUE "")
1454    set(ENABLE_STATIC_FALSE "#")
1455  else()
1456    set(ENABLE_STATIC_TRUE "#")
1457    set(ENABLE_STATIC_FALSE "")
1458  endif()
1459  if(SDL_SHARED)
1460    set(ENABLE_SHARED_TRUE "")
1461    set(ENABLE_SHARED_FALSE "#")
1462  else()
1463    set(ENABLE_SHARED_TRUE "#")
1464    set(ENABLE_SHARED_FALSE "")
1465  endif()
1466
1467  # Clean up the different lists
1468  listtostr(EXTRA_LIBS _EXTRA_LIBS "-l")
1469  set(SDL_STATIC_LIBS ${SDL_LIBS} ${EXTRA_LDFLAGS} ${_EXTRA_LIBS})
1470  list(REMOVE_DUPLICATES SDL_STATIC_LIBS)
1471  listtostr(SDL_STATIC_LIBS _SDL_STATIC_LIBS)
1472  set(SDL_STATIC_LIBS ${_SDL_STATIC_LIBS})
1473  listtostr(SDL_LIBS _SDL_LIBS)
1474  set(SDL_LIBS ${_SDL_LIBS})
1475
1476  # MESSAGE(STATUS "SDL_LIBS: ${SDL_LIBS}")
1477  # MESSAGE(STATUS "SDL_STATIC_LIBS: ${SDL_STATIC_LIBS}")
1478
1479  configure_file("${SDL2_SOURCE_DIR}/sdl2.pc.in"
1480    "${SDL2_BINARY_DIR}/sdl2.pc" @ONLY)
1481  configure_file("${SDL2_SOURCE_DIR}/sdl2-config.in"
1482    "${SDL2_BINARY_DIR}/sdl2-config")
1483  configure_file("${SDL2_SOURCE_DIR}/sdl2-config.in"
1484    "${SDL2_BINARY_DIR}/sdl2-config" @ONLY)
1485  configure_file("${SDL2_SOURCE_DIR}/SDL2.spec.in"
1486    "${SDL2_BINARY_DIR}/SDL2.spec" @ONLY)
1487endif()
1488
1489##### Info output #####
1490message(STATUS "")
1491message(STATUS "SDL2 was configured with the following options:")
1492message(STATUS "")
1493message(STATUS "Platform: ${CMAKE_SYSTEM}")
1494message(STATUS "64-bit:   ${ARCH_64}")
1495message(STATUS "Compiler: ${CMAKE_C_COMPILER}")
1496message(STATUS "")
1497message(STATUS "Subsystems:")
1498foreach(_SUB ${SDL_SUBSYSTEMS})
1499  string(TOUPPER ${_SUB} _OPT)
1500  message_bool_option(${_SUB} SDL_${_OPT})
1501endforeach()
1502message(STATUS "")
1503message(STATUS "Options:")
1504list(SORT ALLOPTIONS)
1505foreach(_OPT ${ALLOPTIONS})
1506  # Longest option is VIDEO_X11_XSCREENSAVER = 22 characters
1507  # Get the padding
1508  string(LENGTH ${_OPT} _OPTLEN)
1509  math(EXPR _PADLEN "23 - ${_OPTLEN}")
1510  string(RANDOM LENGTH ${_PADLEN} ALPHABET " " _PADDING)
1511  message_tested_option(${_OPT} ${_PADDING})
1512endforeach()
1513message(STATUS "")
1514message(STATUS " CFLAGS:        ${CMAKE_C_FLAGS}")
1515message(STATUS " EXTRA_CFLAGS:  ${EXTRA_CFLAGS}")
1516message(STATUS " EXTRA_LDFLAGS: ${EXTRA_LDFLAGS}")
1517message(STATUS " EXTRA_LIBS:    ${EXTRA_LIBS}")
1518message(STATUS "")
1519message(STATUS " Build Shared Library: ${SDL_SHARED}")
1520message(STATUS " Build Static Library: ${SDL_STATIC}")
1521if(SDL_STATIC)
1522    message(STATUS " Build Static Library with Position Independent Code: ${SDL_STATIC_PIC}")
1523endif()
1524message(STATUS "")
1525if(UNIX)
1526  message(STATUS "If something was not detected, although the libraries")
1527  message(STATUS "were installed, then make sure you have set the")
1528  message(STATUS "CFLAGS and LDFLAGS environment variables correctly.")
1529  message(STATUS "")
1530endif()
1531
1532# Ensure that the extra cflags are used at compile time
1533set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
1534
1535# Always build SDLmain
1536add_library(SDL2main STATIC ${SDLMAIN_SOURCES})
1537set(_INSTALL_LIBS "SDL2main")
1538
1539if(SDL_SHARED)
1540  add_library(SDL2 SHARED ${SOURCE_FILES} ${VERSION_SOURCES})
1541  if(UNIX)
1542    set_target_properties(SDL2 PROPERTIES
1543      VERSION ${LT_VERSION}
1544      SOVERSION ${LT_REVISION}
1545      OUTPUT_NAME "SDL2-${LT_RELEASE}")
1546  else()
1547    set_target_properties(SDL2 PROPERTIES
1548      VERSION ${SDL_VERSION}
1549      SOVERSION ${LT_REVISION}
1550      OUTPUT_NAME "SDL2")
1551  endif()
1552  if(MSVC)
1553    # Don't try to link with the default set of libraries.
1554    set_target_properties(SDL2 PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
1555    set_target_properties(SDL2 PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
1556    set_target_properties(SDL2 PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
1557  endif()
1558  set(_INSTALL_LIBS "SDL2" ${_INSTALL_LIBS})
1559  target_link_libraries(SDL2 ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
1560endif()
1561
1562if(SDL_STATIC)
1563  set (BUILD_SHARED_LIBS FALSE)
1564  add_library(SDL2-static STATIC ${SOURCE_FILES})
1565  set_target_properties(SDL2-static PROPERTIES OUTPUT_NAME "SDL2")
1566  set_target_properties(SDL2-static PROPERTIES POSITION_INDEPENDENT_CODE ${SDL_STATIC_PIC})
1567  if(MSVC)
1568    set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
1569    set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
1570    set_target_properties(SDL2-static PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
1571  endif()
1572  # TODO: Win32 platforms keep the same suffix .lib for import and static
1573  # libraries - do we need to consider this?
1574  set(_INSTALL_LIBS "SDL2-static" ${_INSTALL_LIBS})
1575  target_link_libraries(SDL2-static ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
1576endif()
1577
1578##### Installation targets #####
1579install(TARGETS ${_INSTALL_LIBS}
1580  LIBRARY DESTINATION "lib${LIB_SUFFIX}"
1581  ARCHIVE DESTINATION "lib${LIB_SUFFIX}"
1582  RUNTIME DESTINATION bin)
1583
1584file(GLOB INCLUDE_FILES ${SDL2_SOURCE_DIR}/include/*.h)
1585file(GLOB BIN_INCLUDE_FILES ${SDL2_BINARY_DIR}/include/*.h)
1586foreach(_FNAME ${BIN_INCLUDE_FILES})
1587  get_filename_component(_INCNAME ${_FNAME} NAME)
1588  list(REMOVE_ITEM INCLUDE_FILES ${SDL2_SOURCE_DIR}/include/${_INCNAME})
1589endforeach()
1590list(APPEND INCLUDE_FILES ${BIN_INCLUDE_FILES})
1591install(FILES ${INCLUDE_FILES} DESTINATION include/SDL2)
1592
1593if(NOT (WINDOWS OR CYGWIN))
1594  if(SDL_SHARED)
1595    if (APPLE)
1596        set(SOEXT "dylib")
1597    else()
1598        set(SOEXT "so")
1599    endif()
1600    install(CODE "
1601      execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
1602      \"libSDL2-2.0.${SOEXT}\" \"libSDL2.${SOEXT}\")")
1603    install(FILES ${SDL2_BINARY_DIR}/libSDL2.${SOEXT} DESTINATION "lib${LIB_SUFFIX}")
1604  endif()
1605  if(FREEBSD)
1606    # FreeBSD uses ${PREFIX}/libdata/pkgconfig
1607    install(FILES ${SDL2_BINARY_DIR}/sdl2.pc DESTINATION "libdata/pkgconfig")
1608  else()
1609    install(FILES ${SDL2_BINARY_DIR}/sdl2.pc
1610      DESTINATION "lib${LIB_SUFFIX}/pkgconfig")
1611  endif()
1612  install(PROGRAMS ${SDL2_BINARY_DIR}/sdl2-config DESTINATION bin)
1613  # TODO: what about the .spec file? Is it only needed for RPM creation?
1614  install(FILES "${SDL2_SOURCE_DIR}/sdl2.m4" DESTINATION "${CMAKE_INSTALL_FULL_DATAROOTDIR}/aclocal")
1615endif()
1616
1617##### Uninstall target #####
1618
1619configure_file(
1620    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
1621    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
1622    IMMEDIATE @ONLY)
1623
1624add_custom_target(uninstall
1625    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
1626