Emscripten - cmake - pass emscripten options in CMakeList file

后端 未结 2 1585
南笙
南笙 2020-12-15 22:25

How to use emcmake cmake and pass emscripten command-line options?

Pretty new to c++ / CMake, but can\'t find anything helpful on google. So maybe the q

相关标签:
2条回答
  • 2020-12-15 23:12

    Oke I figured out what I was doing wrong..

    As a noob to Emscripten / CMake, I wasn't realising that emcmake does not automatically use the emscripten base cmake file (what I do expect if I run an Special emscripten emcmake command....)

    Anyway, turns out I don't need emcmake and can just call cmake from the ternimal like this ->

    cmake -DCMAKE_BUILD_TYPE=Debug -G \"Unix Makefiles\" -DCMAKE_TOOLCHAIN_FILE=/emscripten/emscripten/incoming/cmake/Modules/Platform/Emscripten.cmake . && make
    

    And here is my final CMakeList.txt

    cmake_minimum_required(VERSION 3.7)
    project(Engine)
    
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist)
    SET(CMAKE_BUILD_TYPE_INIT "Release")
    set(CMAKE_CXX_STANDARD 11)
    
    if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
        set(CMAKE_C_COMPILER "emcc")
    endif ()
    
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
    set(SOURCE_FILES main.cpp src/Background.cpp src/Background.h src/Camera.cpp src/Chart.cpp src/Chart.h src/logger.cpp src/Engine.cpp src/Engine.h src/GL.cpp src/GL.h src/Instrument.cpp src/Instrument.h src/Text.cpp src/Text.h)
    
    find_package(OPENGL REQUIRED)
    
    if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
        find_package(GLEW REQUIRED)
        find_package(glfw3 REQUIRED)
        find_package(assimp REQUIRED)
        find_package(freetype REQUIRED)
    endif ()
    
    include_directories(
            /emscripten/
            /emscripten/emscripten/incoming/system/include
            /opt/X11/include/freetype2
            /usr/local/Cellar/freetype/2.8/include/freetype2
            ${FREETYPE_INCLUDE_DIRS}
            ${OPENGL_INCLUDE_DIR}
    )
    
    if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
        include_directories(${GLEW_INCLUDE_DIR})
    endif ()
    
    add_executable(Engine ${SOURCE_FILES} extern/stb_image.cpp src/Camera.h src/Cubes.cpp src/Chart.cpp)
    
    target_link_libraries(Engine ${OPENGL_gl_LIBRARY})
    
    if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
        set_target_properties(Engine PROPERTIES LINK_FLAGS "-o dist/engine.js -s USE_FREETYPE=1 -s DISABLE_EXCEPTION_CATCHING=0 -s DEMANGLE_SUPPORT=1 -s SAFE_HEAP=1 --bind -s WASM=1 -O2 -s LEGACY_GL_EMULATION=0  -s GL_UNSAFE_OPTS=0 --pre-js pre-module.js --post-js post-module.js -s ASSERTIONS=1 -s GL_ASSERTIONS=1 -s INVOKE_RUN=0  -std=c++11 -s USE_WEBGL2=1 -s FULL_ES3=1 -s USE_GLFW=3 -s OFFSCREENCANVAS_SUPPORT=1 --preload-file textures --preload-file shaders --preload-file fonts")
    
    else ()
        target_link_libraries(Engine glfw glew)
    endif ()
    
    file(INSTALL shaders DESTINATION .)
    file(INSTALL textures DESTINATION .)
    
    0 讨论(0)
  • 2020-12-15 23:14

    I think the issue is that you are overwriting the CMAKE_MODULE_PATH, which emcmake also configure. Try to replace the line by:

    list(APPEND CMAKE_MODULE_PATH <yourstuff>)
    

    Then you just have to do "emcmake cmake" and "emmake make", it should work fine normally.

    In the cmake file, you can use "if (EMSCRIPTEN)" to tweak suff just for the emscripten case, such as the compiler or linker flags

    0 讨论(0)
提交回复
热议问题