view OrthancStone/SharedLibrary/WebAssembly/CMakeLists.txt @ 1550:012ab2c1f23b

notes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 13 Aug 2020 21:00:55 +0200
parents a48ae10857b1
children c54bc5bffd01
line wrap: on
line source

cmake_minimum_required(VERSION 2.8.3)

project(OrthancStoneModule)


# Warning message related to WebAssembly modules: We know that 1.38.41
# DOES NOT work, but that 1.39.17 works.
message("")
message("=== IMPORTANT: Make sure to use a recent version of Emscripten (preferably >= 2.0.0) ===")
message("")


set(ORTHANC_STONE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/../../../wasm-binaries/OrthancStoneModule" CACHE PATH "Where to put the WebAssembly binaries")


# Ask for the generation of a side module
set(WASM_FLAGS "-s SIDE_MODULE=1 -s EXPORT_ALL=1")  # Must be before "Compiler.cmake"


# Configuration of the Emscripten compiler for WebAssembly target
# ---------------------------------------------------------------

set(USE_WASM ON CACHE BOOL "")

set(EMSCRIPTEN_SET_LLVM_WASM_BACKEND ON CACHE BOOL "")

set(WASM_FLAGS "${WASM_FLAGS} -s WASM=1 -s FETCH=1")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  set(WASM_FLAGS "${WASM_FLAGS} -s SAFE_HEAP=1")
endif()

set(WASM_LINKER_FLAGS "${WASM_LINKER_FLAGS} -s ASSERTIONS=1 -s DISABLE_EXCEPTION_CATCHING=0")
set(WASM_LINKER_FLAGS "${WASM_LINKER_FLAGS} -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1")
add_definitions(
  -DDISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1
)


# Stone of Orthanc configuration
# ---------------------------------------------------------------

include(${CMAKE_SOURCE_DIR}/../../Resources/CMake/OrthancStoneParameters.cmake)

SET(ENABLE_DCMTK ON)
SET(ENABLE_DCMTK_NETWORKING OFF)
SET(ENABLE_DCMTK_TRANSCODING OFF)
SET(ENABLE_GOOGLE_TEST OFF)
SET(ENABLE_LOCALE ON)  # Necessary for text rendering
SET(ENABLE_WASM ON)
SET(ORTHANC_SANDBOXED ON)

include(${CMAKE_SOURCE_DIR}/../../Resources/CMake/OrthancStoneConfiguration.cmake)




################################################################################

# The source files that register a callback cannot be part of a side
# module, and must be compiled in the main module. The following
# command can be used to identify such files:
#  $ grep -lrE 'emscripten_' ../../Sources/

set(SOURCES_WITH_EMSCRIPTEN_CALLBACKS
  ${ORTHANC_STONE_ROOT}/Sources/Oracle/WebAssemblyOracle.cpp
  ${ORTHANC_STONE_ROOT}/Sources/Viewport/WebAssemblyViewport.cpp
  )

list(REMOVE_ITEM ORTHANC_STONE_SOURCES
  ${SOURCES_WITH_EMSCRIPTEN_CALLBACKS}
  )

configure_file(
  ${CMAKE_SOURCE_DIR}/../OrthancStone.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/Include/orthanc-stone/OrthancStone.h
  )

configure_file(
  ${ORTHANC_FRAMEWORK_ROOT}/../SharedLibrary/OrthancFramework.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/Include/orthanc-framework/OrthancFramework.h
  )

file(
  COPY ${CMAKE_SOURCE_DIR}/../../Sources/
  DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Include/orthanc-stone
  NO_SOURCE_PERMISSIONS
  FILES_MATCHING
  PATTERN "*.h"
  PATTERN OrthancStone.h EXCLUDE
  PATTERN "Deprecated*" EXCLUDE
  )

file(
  COPY ${ORTHANC_FRAMEWORK_ROOT}/
  DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Include/orthanc-framework
  NO_SOURCE_PERMISSIONS
  FILES_MATCHING
  PATTERN "*.h"
  PATTERN OrthancFramework.h EXCLUDE
  )

add_executable(OrthancStoneModule
  ${ORTHANC_STONE_SOURCES}
  ${AUTOGENERATED_SOURCES}  
  ${CAIRO_SOURCES}
  ${PIXMAN_SOURCES}
  ${FREETYPE_SOURCES}
  )

set_target_properties(OrthancStoneModule
  PROPERTIES
  COMPILE_FLAGS "${WASM_FLAGS}"
  LINK_FLAGS "${WASM_LINKER_FLAGS}"
  )

# CMake does not natively handle SIDE_MODULE, and believes that
# Emscripten produces a ".js" file (whereas it creates only the
# ".wasm"). Create a dummy ".js" for target to work.
add_custom_command(
  TARGET OrthancStoneModule POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/OrthancStoneModule.js
  )

file(
  COPY ${BOOST_SOURCES_DIR}/boost/
  DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Include/boost/
  NO_SOURCE_PERMISSIONS
  FILES_MATCHING
  PATTERN "*.h"
  PATTERN "*.hpp"
  PATTERN "*.ipp"
  )

file(
  COPY ${JSONCPP_SOURCES_DIR}/include/json/
  DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Include/json/
  NO_SOURCE_PERMISSIONS
  FILES_MATCHING
  PATTERN "*.h"
  )

set(DCMTK_MODULES
  dcmdata
  config
  ofstd
  oflog
  )

foreach (module IN LISTS DCMTK_MODULES)
  file(
    COPY ${DCMTK_SOURCES_DIR}/ofstd/include/dcmtk/${module}/
    DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Include/dcmtk/${module}/
    NO_SOURCE_PERMISSIONS
    FILES_MATCHING
    PATTERN "*.h"
    )
endforeach()

    
install(
  TARGETS OrthancStoneModule
  DESTINATION ${ORTHANC_STONE_INSTALL_PREFIX}/lib
  )

install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/OrthancStoneModule.wasm
  DESTINATION ${ORTHANC_STONE_INSTALL_PREFIX}/lib
  )

install(
  DIRECTORY
  ${CMAKE_CURRENT_BINARY_DIR}/Include/boost
  ${CMAKE_CURRENT_BINARY_DIR}/Include/dcmtk
  ${CMAKE_CURRENT_BINARY_DIR}/Include/json
  ${CMAKE_CURRENT_BINARY_DIR}/Include/orthanc-framework
  ${CMAKE_CURRENT_BINARY_DIR}/Include/orthanc-stone
  DESTINATION ${ORTHANC_STONE_INSTALL_PREFIX}/include/
  )

install(FILES
  ${SOURCES_WITH_EMSCRIPTEN_CALLBACKS}
  DESTINATION ${ORTHANC_STONE_INSTALL_PREFIX}/src/orthanc-stone
  )