Add support for compiling C engine with emscripten
authorkoda
Wed, 20 Apr 2016 14:46:04 -0400
changeset 11654 870f22de388b
parent 11653 ef625fe6eaaa
child 11655 e15eb10f1703
Add support for compiling C engine with emscripten
CMakeLists.txt
cmake_modules/FindEmscripten.cmake
project_files/hwc/CMakeLists.txt
--- a/CMakeLists.txt	Tue Apr 19 12:04:21 2016 -0400
+++ b/CMakeLists.txt	Wed Apr 20 14:46:04 2016 -0400
@@ -41,6 +41,7 @@
 option(SKIPBUNDLE "Do not create relocate bundle (off)" OFF)
 
 option(BUILD_ENGINE_C "Compile hwengine as native C (off)" OFF)
+option(BUILD_ENGINE_JS "Compile hwengine as javascript (off)" OFF)
 option(GL2 "Enable OpenGL 2 rendering !!!EXPERIMENTAL - DO NOT USE!!! [default: off)" OFF)
 
 set(GHFLAGS "" CACHE STRING "Additional Haskell flags")
@@ -225,6 +226,9 @@
 else()
     #main pascal engine
     add_subdirectory(hedgewars)
+    if(BUILD_ENGINE_JS)
+        message(FATAL_ERROR "This option needs BUILD_ENGINE_C to be set")
+    endif()
 endif()
 
 #Android related build scripts
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake_modules/FindEmscripten.cmake	Wed Apr 20 14:46:04 2016 -0400
@@ -0,0 +1,36 @@
+# - Try to find the Clang/LLVM executable
+# Once done this will define
+#
+#  EMSCRIPTEN_FOUND       - system has Clang
+#  EMSCRIPTEN_VERSION     - Clang version
+#  EMSCRIPTEN_EXECUTABLE  - Clang executable
+#
+# Copyright (c) 2013, Vittorio Giovara <vittorio.giovara@gmail.com>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+
+find_program(EMSCRIPTEN_EXECUTABLE
+        NAMES emcc
+        PATHS /opt/local/bin /usr/local/bin /usr/bin)
+
+if (EMSCRIPTEN_EXECUTABLE)
+    execute_process(COMMAND ${EMSCRIPTEN_EXECUTABLE} -v
+                    OUTPUT_VARIABLE EMSCRIPTEN_VERSION_OUTPUT
+                    ERROR_VARIABLE EMSCRIPTEN_VERSION_ERROR
+                    RESULT_VARIABLE EMSCRIPTEN_VERSION_RESULT
+                    OUTPUT_STRIP_TRAILING_WHITESPACE
+                    )
+
+    if(${EMSCRIPTEN_VERSION_RESULT} EQUAL 0)
+        string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" EMSCRIPTEN_VERSION "${EMSCRIPTEN_VERSION_OUTPUT}")
+        string(REGEX REPLACE "([0-9]+\\.[0-9]+\\.[0-9]+)" "\\1" EMSCRIPTEN_VERSION "${EMSCRIPTEN_VERSION}")
+    else()
+        message(SEND_ERROR "Command \"${EMSCRIPTEN_EXECUTABLE} --version\" failed with output: ${EMSCRIPTEN_VERSION_ERROR}")
+    endif()
+endif()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Emscripten DEFAULT_MSG EMSCRIPTEN_EXECUTABLE EMSCRIPTEN_VERSION)
+mark_as_advanced(EMSCRIPTEN_VERSION)
+
--- a/project_files/hwc/CMakeLists.txt	Tue Apr 19 12:04:21 2016 -0400
+++ b/project_files/hwc/CMakeLists.txt	Wed Apr 20 14:46:04 2016 -0400
@@ -6,6 +6,9 @@
 find_package(SDL2_net REQUIRED)
 find_package(SDL2_image REQUIRED)
 find_package(SDL2_ttf REQUIRED)
+if(BUILD_ENGINE_JS)
+    find_package(Emscripten REQUIRED)
+endif()
 
 #compile our rtl implementation
 include_directories(${GLEW_INCLUDE_DIR})
@@ -78,6 +81,11 @@
 add_definitions(-DPAS2C)
 add_definitions(-Werror=incompatible-pointer-types)
 
+if(BUILD_ENGINE_JS)
+    #hijack the c compiler with emscripten
+    set(CMAKE_C_COMPILER ${EMSCRIPTEN_EXECUTABLE})
+endif()
+
 add_executable(hwengine WIN32 ${engine_sources})
 
 target_link_libraries(hwengine  fpcrtl