Skip to content

Commit fe40a17

Browse files
committed
Initial merge of Emscripten port!
With this commit, you can compile SDL2 with Emscripten ( http://emscripten.org/ ), and make your SDL-based C/C++ program into a web app. This port was due to the efforts of several people, including: Charlie Birks, Sathyanarayanan Gunasekaran, Jukka Jyl?nki, Alon Zakai, Edward Rudd, Bruce Mitchener, and Martin Gerhardy. (Thanks, everyone!)
1 parent a228b67 commit fe40a17

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+4044
-597
lines changed

CMakeLists.txt

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ else()
117117
set(UNIX_OR_MAC_SYS OFF)
118118
endif()
119119

120+
if (UNIX_OR_MAC_SYS AND NOT EMSCRIPTEN) # JavaScript does not yet have threading support, so disable pthreads when building for Emscripten.
121+
set(PTHREADS_ENABLED_BY_DEFAULT ON)
122+
else()
123+
set(PTHREADS_ENABLED_BY_DEFAULT OFF)
124+
endif()
125+
120126
# Default option knobs
121127
if(APPLE OR ARCH_64)
122128
set(OPT_DEF_SSEMATH ON)
@@ -170,13 +176,19 @@ endif()
170176
set(SDL_LIBS "-lSDL2")
171177
set(SDL_CFLAGS "")
172178

179+
# Emscripten toolchain has a nonempty default value for this, and the checks
180+
# in this file need to change that, so remember the original value, and
181+
# restore back to that afterwards. For check_function_exists() to work in
182+
# Emscripten, this value must be at its default value.
183+
set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
184+
173185
if(CYGWIN)
174186
# We build SDL on cygwin without the UNIX emulation layer
175187
include_directories("-I/usr/include/mingw")
176188
set(CMAKE_REQUIRED_FLAGS "-mno-cygwin")
177189
check_c_source_compiles("int main(int argc, char **argv) {}"
178190
HAVE_GCC_NO_CYGWIN)
179-
set(CMAKE_REQUIRED_FLAGS)
191+
set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
180192
if(HAVE_GCC_NO_CYGWIN)
181193
list(APPEND EXTRA_LDFLAGS "-mno-cygwin")
182194
list(APPEND SDL_LIBS "-mno-cygwin")
@@ -188,12 +200,35 @@ add_definitions(-DUSING_GENERATED_CONFIG_H)
188200
# General includes
189201
include_directories(${SDL2_BINARY_DIR}/include ${SDL2_SOURCE_DIR}/include)
190202

203+
if(EMSCRIPTEN)
204+
# Set up default values for the currently supported set of subsystems:
205+
# Emscripten/Javascript does not have assembly support, a dynamic library
206+
# loading architecture, low-level CPU inspection or multithreading.
207+
set(OPT_DEF_ASM FALSE)
208+
set(SDL_SHARED_ENABLED_BY_DEFAULT OFF)
209+
set(SDL_ATOMIC_ENABLED_BY_DEFAULT OFF)
210+
set(SDL_THREADS_ENABLED_BY_DEFAULT OFF)
211+
set(SDL_LOADSO_ENABLED_BY_DEFAULT OFF)
212+
set(SDL_CPUINFO_ENABLED_BY_DEFAULT OFF)
213+
set(DLOPEN_ENABLED_BY_DEFAULT OFF)
214+
else()
215+
set(SDL_SHARED_ENABLED_BY_DEFAULT ON)
216+
set(SDL_ATOMIC_ENABLED_BY_DEFAULT ON)
217+
set(SDL_THREADS_ENABLED_BY_DEFAULT ON)
218+
set(SDL_LOADSO_ENABLED_BY_DEFAULT ON)
219+
set(SDL_CPUINFO_ENABLED_BY_DEFAULT ON)
220+
set(DLOPEN_ENABLED_BY_DEFAULT ON)
221+
endif()
222+
191223
set(SDL_SUBSYSTEMS
192224
Atomic Audio Video Render Events Joystick Haptic Power Threads Timers
193225
File Loadso CPUinfo Filesystem)
194226
foreach(_SUB ${SDL_SUBSYSTEMS})
195227
string(TOUPPER ${_SUB} _OPT)
196-
option(SDL_${_OPT} "Enable the ${_SUB} subsystem" ON)
228+
if (NOT DEFINED SDL_${_OPT}_ENABLED_BY_DEFAULT)
229+
set(SDL_${_OPT}_ENABLED_BY_DEFAULT ON)
230+
endif()
231+
option(SDL_${_OPT} "Enable the ${_SUB} subsystem" ${SDL_${_OPT}_ENABLED_BY_DEFAULT})
197232
endforeach()
198233

199234
option_string(ASSERTIONS "Enable internal sanity checks (auto/disabled/release/enabled/paranoid)" "auto")
@@ -216,9 +251,9 @@ dep_option(FUSIONSOUND_SHARED "Dynamically load fusionsound audio support" ON "
216251
set_option(VIDEO_DUMMY "Use dummy video driver" ON)
217252
set_option(VIDEO_OPENGL "Include OpenGL support" ON)
218253
set_option(VIDEO_OPENGLES "Include OpenGL ES support" ON)
219-
set_option(PTHREADS "Use POSIX threads for multi-threading" ${UNIX_OR_MAC_SYS})
254+
set_option(PTHREADS "Use POSIX threads for multi-threading" ${PTHREADS_ENABLED_BY_DEFAULT})
220255
dep_option(PTHREADS_SEM "Use pthread semaphores" ON "PTHREADS" OFF)
221-
set_option(SDL_DLOPEN "Use dlopen for shared object loading" ON)
256+
set_option(SDL_DLOPEN "Use dlopen for shared object loading" ${DLOPEN_ENABLED_BY_DEFAULT})
222257
set_option(OSS "Support the OSS audio API" ${UNIX_SYS})
223258
set_option(ALSA "Support the ALSA audio API" ${UNIX_SYS})
224259
dep_option(ALSA_SHARED "Dynamically load ALSA audio support" ON "ALSA" OFF)
@@ -251,7 +286,7 @@ set_option(VIDEO_VIVANTE "Use Vivante EGL video driver" ${UNIX_SYS})
251286

252287
# TODO: We should (should we?) respect cmake's ${BUILD_SHARED_LIBS} flag here
253288
# The options below are for compatibility to configure's default behaviour.
254-
set(SDL_SHARED ON CACHE BOOL "Build a shared version of the library")
289+
set(SDL_SHARED ${SDL_SHARED_ENABLED_BY_DEFAULT} CACHE BOOL "Build a shared version of the library")
255290
set(SDL_STATIC ON CACHE BOOL "Build a static version of the library")
256291

257292
# General source files
@@ -317,7 +352,7 @@ if(USE_GCC OR USE_CLANG)
317352
set(CMAKE_REQUIRED_FLAGS "-mpreferred-stack-boundary=2")
318353
check_c_source_compiles("int x = 0; int main(int argc, char **argv) {}"
319354
HAVE_GCC_PREFERRED_STACK_BOUNDARY)
320-
set(CMAKE_REQUIRED_FLAGS)
355+
set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
321356

322357
set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden -Werror")
323358
check_c_source_compiles("
@@ -328,7 +363,7 @@ if(USE_GCC OR USE_CLANG)
328363
if(HAVE_GCC_FVISIBILITY)
329364
list(APPEND EXTRA_CFLAGS "-fvisibility=hidden")
330365
endif()
331-
set(CMAKE_REQUIRED_FLAGS)
366+
set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
332367

333368
check_c_compiler_flag(-Wall HAVE_GCC_WALL)
334369
if(HAVE_GCC_WALL)
@@ -376,7 +411,7 @@ if(ASSEMBLY)
376411
if(HAVE_MMX)
377412
list(APPEND EXTRA_CFLAGS "-mmmx")
378413
endif()
379-
set(CMAKE_REQUIRED_FLAGS)
414+
set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
380415
endif()
381416

382417
if(3DNOW)
@@ -393,7 +428,7 @@ if(ASSEMBLY)
393428
if(HAVE_3DNOW)
394429
list(APPEND EXTRA_CFLAGS "-m3dnow")
395430
endif()
396-
set(CMAKE_REQUIRED_FLAGS)
431+
set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
397432
endif()
398433

399434
if(SSE)
@@ -416,7 +451,7 @@ if(ASSEMBLY)
416451
if(HAVE_SSE)
417452
list(APPEND EXTRA_CFLAGS "-msse")
418453
endif()
419-
set(CMAKE_REQUIRED_FLAGS)
454+
set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
420455
endif()
421456

422457
if(SSE2)
@@ -439,7 +474,7 @@ if(ASSEMBLY)
439474
if(HAVE_SSE2)
440475
list(APPEND EXTRA_CFLAGS "-msse2")
441476
endif()
442-
set(CMAKE_REQUIRED_FLAGS)
477+
set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
443478
endif()
444479

445480
if(SSEMATH)
@@ -464,7 +499,7 @@ if(ASSEMBLY)
464499
return vec_splat_u32(0);
465500
}
466501
int main(int argc, char **argv) { }" HAVE_ALTIVEC)
467-
set(CMAKE_REQUIRED_FLAGS)
502+
set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
468503
if(HAVE_ALTIVEC OR HAVE_ALTIVEC_H_HDR)
469504
set(HAVE_ALTIVEC TRUE) # if only HAVE_ALTIVEC_H_HDR is set
470505
list(APPEND EXTRA_CFLAGS "-maltivec")
@@ -642,7 +677,49 @@ if(SDL_VIDEO)
642677
endif()
643678

644679
# Platform-specific options and settings
645-
if(UNIX AND NOT APPLE)
680+
if(EMSCRIPTEN)
681+
# Hide noisy warnings that intend to aid mostly during initial stages of porting a new
682+
# project. Uncomment at will for verbose cross-compiling -I/../ path info.
683+
add_definitions(-Wno-warn-absolute-paths)
684+
if(SDL_AUDIO)
685+
set(SDL_AUDIO_DRIVER_EMSCRIPTEN 1)
686+
file(GLOB EM_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/emscripten/*.c)
687+
set(SOURCE_FILES ${SOURCE_FILES} ${EM_AUDIO_SOURCES})
688+
set(HAVE_SDL_AUDIO TRUE)
689+
endif()
690+
if(SDL_FILESYSTEM)
691+
set(SDL_FILESYSTEM_EMSCRIPTEN 1)
692+
file(GLOB EM_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/emscripten/*.c)
693+
set(SOURCE_FILES ${SOURCE_FILES} ${EM_FILESYSTEM_SOURCES})
694+
set(HAVE_SDL_FILESYSTEM TRUE)
695+
endif()
696+
if(SDL_JOYSTICK)
697+
set(SDL_JOYSTICK_EMSCRIPTEN 1)
698+
file(GLOB EM_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/emscripten/*.c)
699+
set(SOURCE_FILES ${SOURCE_FILES} ${EM_JOYSTICK_SOURCES})
700+
set(HAVE_SDL_JOYSTICK TRUE)
701+
endif()
702+
if(SDL_POWER)
703+
set(SDL_POWER_EMSCRIPTEN 1)
704+
file(GLOB EM_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/emscripten/*.c)
705+
set(SOURCE_FILES ${SOURCE_FILES} ${EM_POWER_SOURCES})
706+
set(HAVE_SDL_POWER TRUE)
707+
endif()
708+
if(SDL_VIDEO)
709+
set(SDL_VIDEO_DRIVER_EMSCRIPTEN 1)
710+
file(GLOB EM_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/emscripten/*.c)
711+
set(SOURCE_FILES ${SOURCE_FILES} ${EM_VIDEO_SOURCES})
712+
set(HAVE_SDL_VIDEO TRUE)
713+
714+
#enable gles
715+
if(VIDEO_OPENGLES)
716+
set(SDL_VIDEO_OPENGL_EGL 1)
717+
set(HAVE_VIDEO_OPENGLES TRUE)
718+
set(SDL_VIDEO_OPENGL_ES2 1)
719+
set(SDL_VIDEO_RENDER_OGL_ES2 1)
720+
endif()
721+
endif()
722+
elseif(UNIX AND NOT APPLE)
646723
if(SDL_AUDIO)
647724
if(SYSV5 OR SOLARIS OR HPUX)
648725
set(SDL_AUDIO_DRIVER_SUNAUDIO 1)
@@ -829,7 +906,7 @@ elseif(WINDOWS)
829906
link_directories($ENV{DXSDK_DIR}\\lib\\${PROCESSOR_ARCH})
830907
include_directories($ENV{DXSDK_DIR}\\Include)
831908
endif()
832-
set(CMAKE_REQUIRED_FLAGS)
909+
set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
833910
endif()
834911

835912
if(SDL_AUDIO)

build-scripts/config.sub

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,8 @@ case $os in
15341534
-pnacl*)
15351535
os=-pnacl
15361536
;;
1537+
-emscripten*)
1538+
;;
15371539
-none)
15381540
;;
15391541
*)

configure

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21385,6 +21385,78 @@ $as_echo "#define SDL_VIDEO_RENDER_OGL 1" >>confdefs.h
2138521385
fi
2138621386
}
2138721387

21388+
CheckEmscriptenGLES()
21389+
{
21390+
if test x$enable_video = xyes -a x$enable_video_opengles = xyes; then
21391+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for EGL support" >&5
21392+
$as_echo_n "checking for EGL support... " >&6; }
21393+
video_opengl_egl=no
21394+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
21395+
/* end confdefs.h. */
21396+
21397+
#include <EGL/egl.h>
21398+
21399+
int
21400+
main ()
21401+
{
21402+
21403+
21404+
;
21405+
return 0;
21406+
}
21407+
_ACEOF
21408+
if ac_fn_c_try_compile "$LINENO"; then :
21409+
21410+
video_opengl_egl=yes
21411+
21412+
fi
21413+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
21414+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengl_egl" >&5
21415+
$as_echo "$video_opengl_egl" >&6; }
21416+
if test x$video_opengl_egl = xyes; then
21417+
21418+
$as_echo "#define SDL_VIDEO_OPENGL_EGL 1" >>confdefs.h
21419+
21420+
fi
21421+
21422+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES v2 headers" >&5
21423+
$as_echo_n "checking for OpenGL ES v2 headers... " >&6; }
21424+
video_opengles_v2=no
21425+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
21426+
/* end confdefs.h. */
21427+
21428+
#include <GLES2/gl2.h>
21429+
#include <GLES2/gl2ext.h>
21430+
21431+
int
21432+
main ()
21433+
{
21434+
21435+
21436+
;
21437+
return 0;
21438+
}
21439+
_ACEOF
21440+
if ac_fn_c_try_compile "$LINENO"; then :
21441+
21442+
video_opengles_v2=yes
21443+
21444+
fi
21445+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
21446+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles_v2" >&5
21447+
$as_echo "$video_opengles_v2" >&6; }
21448+
if test x$video_opengles_v2 = xyes; then
21449+
21450+
$as_echo "#define SDL_VIDEO_OPENGL_ES2 1" >>confdefs.h
21451+
21452+
21453+
$as_echo "#define SDL_VIDEO_RENDER_OGL_ES2 1" >>confdefs.h
21454+
21455+
SUMMARY_video="${SUMMARY_video} opengl_es2"
21456+
fi
21457+
fi
21458+
}
21459+
2138821460
CheckInputEvents()
2138921461
{
2139021462
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for Linux 2.4 unified input interface" >&5
@@ -23483,7 +23555,68 @@ $as_echo "#define SDL_FILESYSTEM_NACL 1" >>confdefs.h
2348323555
SOURCES="$SOURCES $srcdir/src/filesystem/nacl/*.c"
2348423556
have_filesystem=yes
2348523557
fi
23558+
;;
23559+
*-*-emscripten* )
23560+
if test x$enable_video = xyes; then
23561+
23562+
$as_echo "#define SDL_VIDEO_DRIVER_EMSCRIPTEN 1" >>confdefs.h
23563+
23564+
SOURCES="$SOURCES $srcdir/src/video/emscripten/*.c"
23565+
have_video=yes
23566+
SUMMARY_video="${SUMMARY_video} emscripten"
23567+
fi
23568+
23569+
if test x$enable_audio = xyes; then
23570+
23571+
$as_echo "#define SDL_AUDIO_DRIVER_EMSCRIPTEN 1" >>confdefs.h
23572+
23573+
SOURCES="$SOURCES $srcdir/src/audio/emscripten/*.c"
23574+
have_audio=yes
23575+
SUMMARY_audio="${SUMMARY_audio} emscripten"
23576+
fi
23577+
23578+
CheckVisibilityHidden
23579+
CheckDummyVideo
23580+
CheckDiskAudio
23581+
CheckDummyAudio
23582+
CheckDLOPEN
23583+
CheckClockGettime
23584+
CheckEmscriptenGLES
23585+
23586+
# Set up files for the power library
23587+
if test x$enable_power = xyes; then
23588+
23589+
$as_echo "#define SDL_POWER_EMSCRIPTEN 1" >>confdefs.h
23590+
23591+
SOURCES="$SOURCES $srcdir/src/power/emscripten/*.c"
23592+
have_power=yes
23593+
fi
23594+
23595+
# Set up files for the power library
23596+
if test x$enable_joystick = xyes; then
23597+
23598+
$as_echo "#define SDL_JOYSTICK_EMSCRIPTEN 1" >>confdefs.h
23599+
23600+
SOURCES="$SOURCES $srcdir/src/joystick/emscripten/*.c"
23601+
have_joystick=yes
23602+
fi
23603+
23604+
# Set up files for the filesystem library
23605+
if test x$enable_filesystem = xyes; then
23606+
23607+
$as_echo "#define SDL_FILESYSTEM_EMSCRIPTEN 1" >>confdefs.h
23608+
23609+
SOURCES="$SOURCES $srcdir/src/filesystem/emscripten/*.c"
23610+
have_filesystem=yes
23611+
fi
23612+
# Set up files for the timer library
23613+
if test x$enable_timers = xyes; then
23614+
23615+
$as_echo "#define SDL_TIMER_UNIX 1" >>confdefs.h
2348623616

23617+
SOURCES="$SOURCES $srcdir/src/timer/unix/*.c"
23618+
have_timers=yes
23619+
fi
2348723620
;;
2348823621
*)
2348923622
as_fn_error $? "

0 commit comments

Comments
 (0)