Skip to content

Doubts regarding compiling CppInterOp using emscripten #1402

@anutosh491

Description

@anutosh491

Context for anyone interested.

@argentite was able to run clang-repl in the browser and this is how we was able to accomplish this

  1. He built llvm (clang, lld) for emscripten-wasm32
  2. He then framed a CompilerModule.cpp (some simple code calling Clang-Repl's Parse & Execute )
  3. A CMakeLists.txt file with all configurations
cmake_minimum_required(VERSION 3.20.0)
project(ClangWASMREPL)

find_package(LLVM REQUIRED CONFIG)
find_package(Clang REQUIRED CONFIG)
find_package(LLD REQUIRED CONFIG)

message(STATUS "Using ClangConfig.cmake in: ${Clang_DIR}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")

set (CMAKE_CXX_STANDARD 17)
add_compile_options(-Wall -pedantic -fPIC)

include_directories(include)

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${CLANG_INCLUDE_DIRS})
include_directories(${LLD_INCLUDE_DIRS})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})

add_executable(Compiler CompilerModule.cpp)

llvm_map_components_to_libnames(llvm_libs Core MC Support TargetParser WebAssembly)

# Link against LLVM libraries
target_link_libraries(Compiler embind)
target_link_libraries(Compiler lldWasm)
target_link_libraries(Compiler clangInterpreter)

target_link_options(Compiler PRIVATE
	-O1
	-sMODULARIZE
	-sEXPORT_ES6=1
	-sASSERTIONS
	-sALLOW_MEMORY_GROWTH=1
	-sINITIAL_MEMORY=128MB
	-sTOTAL_STACK=32MB
	-sMAIN_MODULE=1
	-sUSE_SDL=2
	-sEXPORTED_RUNTIME_METHODS=ccall,cwrap,stringToNewUTF8,getValue,setValue
	-sEXPORTED_FUNCTIONS=_malloc,_free,__ZTIN10emscripten3valE
	--preload-file ${EMSCRIPTEN_SYSROOT}/@/
	#--preload-file /media/hdstorage/builds/llvm-wasm-ems/lib/clang/19/include@/lib/clang/19/include
	#--preload-file /opt/emscripten-llvm/lib@/lib
)

For my use case, we just have 1 more layer of abstractions as compared to the 2 above
Xeus-Cpp, CppInterOp and llvm. In our case

  1. We have a very clean/efficient recipe on emscripten-forge (leaking no symbols, generating all required libraries like libclanginterpreter.a and liblldwasm.a) so this part is done.
  2. In our case the abstract "Compiler" is CppInterOp's API provides Parse and Execute so this part is done.
  3. We now have to correctly compile CppInterOp to wasm (and host on emscripten-forge) and that shall be used by xeus-cpp and we should be done. Hence only this part is remaining.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions