feat(CMakeLists.txt): support emscripten

This commit is contained in:
Soonho Kong 2014-10-14 18:58:39 -07:00
parent e75c9fe9fc
commit 1915674b4d
2 changed files with 26 additions and 14 deletions

View file

@ -84,6 +84,12 @@ if("${STATIC}" MATCHES "ON")
set(LEAN_EXTRA_LINKER_FLAGS "${LEAN_EXTRA_LINKER_FLAGS} -static") set(LEAN_EXTRA_LINKER_FLAGS "${LEAN_EXTRA_LINKER_FLAGS} -static")
endif() endif()
if("${CMAKE_C_COMPILER}" MATCHES "emcc")
set(EMSCRIPTEN ON)
set(CROSS_COMPILE ON)
set(IGNORE_SORRY ON)
endif()
# Set Module Path # Set Module Path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") 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_RELEASE "-O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g") set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
set(CMAKE_CXX_FLAGS_GPROF "-O2 -g -pg") 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) include(CheckIncludeFileCXX)
check_include_file_cxx("unistd.h" HAVE_UNISTD) check_include_file_cxx("unistd.h" HAVE_UNISTD)
@ -135,6 +142,8 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(LEAN_EXTRA_LINKER_FLAGS "-stdlib=libc++") set(LEAN_EXTRA_LINKER_FLAGS "-stdlib=libc++")
endif () endif ()
elseif ("${EMSCRIPTEN}" MATCHES "ON")
message(STATUS "Emscripten is detected: Make sure the wraped compiler supports C++11")
else() else()
message(FATAL_ERROR "Your C++ compiler does not support C++11.") message(FATAL_ERROR "Your C++ compiler does not support C++11.")
endif () endif ()
@ -203,7 +212,7 @@ if("${READLINE}" MATCHES "ON")
endif() endif()
# Check malloc_usable_size # 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) find_package(MallocUsableSize)
if("${MUS_FOUND}" MATCHES "TRUE") if("${MUS_FOUND}" MATCHES "TRUE")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${MALLOC_DIR} -D HAS_MALLOC_USABLE_SIZE") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${MALLOC_DIR} -D HAS_MALLOC_USABLE_SIZE")
@ -223,7 +232,7 @@ endif()
# Lua # Lua
find_package(Lua REQUIRED) find_package(Lua REQUIRED)
set(EXTRA_LIBS ${EXTRA_LIBS} ${LUA_LIBRARIES}) 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 # Lua static library for linux depends on dl.so
set(EXTRA_LIBS ${EXTRA_LIBS} -ldl) set(EXTRA_LIBS ${EXTRA_LIBS} -ldl)
endif() endif()
@ -354,7 +363,7 @@ if(NOT DEFINED PROCESSOR_COUNT)
endif() endif()
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.") message(STATUS "Lean standard library will not be compiled when using cross-compilation.")
else() else()
add_custom_target( add_custom_target(

View file

@ -1,13 +1,16 @@
if(${EMSCRIPTEN})
add_executable(lean.js lean.cpp)
target_link_libraries(lean.js ${ALL_LIBS} "--preload-file library")
else()
add_executable(lean lean.cpp) add_executable(lean lean.cpp)
target_link_libraries(lean ${ALL_LIBS}) target_link_libraries(lean ${ALL_LIBS})
ADD_CUSTOM_COMMAND(TARGET lean ADD_CUSTOM_COMMAND(TARGET lean
POST_BUILD POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${LEAN_SOURCE_DIR}/../bin 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/ COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/lean${CMAKE_EXECUTABLE_SUFFIX} ${LEAN_SOURCE_DIR}/../bin/
) )
install(TARGETS lean DESTINATION 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_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") # add_test(example1_stdin2 ${LEAN_SOURCE_DIR}/cmake/redirect.sh ${CMAKE_CURRENT_BINARY_DIR}/lean "-l" "${LEAN_SOURCE_DIR}/../tests/lean/single.lean")