From 1915674b4df85bca3ad636d9f60fc95eee864df6 Mon Sep 17 00:00:00 2001 From: Soonho Kong Date: Tue, 14 Oct 2014 18:58:39 -0700 Subject: [PATCH] feat(CMakeLists.txt): support emscripten --- src/CMakeLists.txt | 17 +++++++++++++---- src/shell/CMakeLists.txt | 23 +++++++++++++---------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b1416f3dc..a977ae811 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -84,6 +84,12 @@ if("${STATIC}" MATCHES "ON") set(LEAN_EXTRA_LINKER_FLAGS "${LEAN_EXTRA_LINKER_FLAGS} -static") endif() +if("${CMAKE_C_COMPILER}" MATCHES "emcc") + set(EMSCRIPTEN ON) + set(CROSS_COMPILE ON) + set(IGNORE_SORRY ON) +endif() + # Set Module Path set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") @@ -94,6 +100,7 @@ set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g") set(CMAKE_CXX_FLAGS_GPROF "-O2 -g -pg") +set(CMAKE_CXX_FLAGS_EMSCRIPTEN "-Oz -s DISABLE_EXCEPTION_CATCHING=0 -D LEAN_EMSCRIPTEN -Wno-warn-absolute-paths --bind") include(CheckIncludeFileCXX) check_include_file_cxx("unistd.h" HAVE_UNISTD) @@ -135,7 +142,9 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") set(LEAN_EXTRA_LINKER_FLAGS "-stdlib=libc++") endif () -else () +elseif ("${EMSCRIPTEN}" MATCHES "ON") + message(STATUS "Emscripten is detected: Make sure the wraped compiler supports C++11") +else() message(FATAL_ERROR "Your C++ compiler does not support C++11.") endif () @@ -203,7 +212,7 @@ if("${READLINE}" MATCHES "ON") endif() # Check malloc_usable_size -if(NOT "${TCMALLOC_FOUND}" AND NOT "${JEMALLOC_FOUND}" AND "${TRACK_MEMORY_USAGE}" MATCHES "ON") +if(NOT "${EMSCRIPTEN}" AND NOT "${TCMALLOC_FOUND}" AND NOT "${JEMALLOC_FOUND}" AND "${TRACK_MEMORY_USAGE}" MATCHES "ON") find_package(MallocUsableSize) if("${MUS_FOUND}" MATCHES "TRUE") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${MALLOC_DIR} -D HAS_MALLOC_USABLE_SIZE") @@ -223,7 +232,7 @@ endif() # Lua find_package(Lua REQUIRED) set(EXTRA_LIBS ${EXTRA_LIBS} ${LUA_LIBRARIES}) -if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") +if(NOT "${EMSCRIPTEN}" AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux") # Lua static library for linux depends on dl.so set(EXTRA_LIBS ${EXTRA_LIBS} -ldl) endif() @@ -354,7 +363,7 @@ if(NOT DEFINED PROCESSOR_COUNT) endif() endif() -if("${CROSS_COMPILE}" MATCHES "ON") +if("${CROSS_COMPILE}" MATCHES "ON" OR "${CMAKE_C_COMPILER}" MATCHES "emcc") message(STATUS "Lean standard library will not be compiled when using cross-compilation.") else() add_custom_target( diff --git a/src/shell/CMakeLists.txt b/src/shell/CMakeLists.txt index bd14fe857..d548286e0 100644 --- a/src/shell/CMakeLists.txt +++ b/src/shell/CMakeLists.txt @@ -1,13 +1,16 @@ -add_executable(lean lean.cpp) -target_link_libraries(lean ${ALL_LIBS}) - -ADD_CUSTOM_COMMAND(TARGET lean - POST_BUILD - COMMAND ${CMAKE_COMMAND} -E make_directory ${LEAN_SOURCE_DIR}/../bin - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/lean${CMAKE_EXECUTABLE_SUFFIX} ${LEAN_SOURCE_DIR}/../bin/ -) - -install(TARGETS lean DESTINATION bin) +if(${EMSCRIPTEN}) + add_executable(lean.js lean.cpp) + target_link_libraries(lean.js ${ALL_LIBS} "--preload-file library") +else() + add_executable(lean lean.cpp) + target_link_libraries(lean ${ALL_LIBS}) + ADD_CUSTOM_COMMAND(TARGET lean + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory ${LEAN_SOURCE_DIR}/../bin + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/lean${CMAKE_EXECUTABLE_SUFFIX} ${LEAN_SOURCE_DIR}/../bin/ + ) + install(TARGETS lean DESTINATION bin) +endif() # add_test(example1_stdin1 ${LEAN_SOURCE_DIR}/cmake/redirect.sh ${CMAKE_CURRENT_BINARY_DIR}/lean "${LEAN_SOURCE_DIR}/../tests/lean/single.lean") # add_test(example1_stdin2 ${LEAN_SOURCE_DIR}/cmake/redirect.sh ${CMAKE_CURRENT_BINARY_DIR}/lean "-l" "${LEAN_SOURCE_DIR}/../tests/lean/single.lean")