Skip to content

Commit

Permalink
re-enable static library mode for WASM (#873)
Browse files Browse the repository at this point in the history
* re-enable static library mode for wasm

* refine the code

* more refinement
  • Loading branch information
wenbingl authored Jan 10, 2025
1 parent 169d682 commit f3f6caa
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 32 deletions.
21 changes: 16 additions & 5 deletions .pipelines/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -748,13 +748,24 @@ stages:
- script: |
bash ./build.sh \
-DCMAKE_TOOLCHAIN_FILE=$(Build.BinariesDirectory)/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake \
-DOCOS_ENABLE_STATIC_LIB=OFF \
-DOCOS_ENABLE_SPM_TOKENIZER=ON \
-DOCOS_BUILD_PYTHON=OFF \
-DOCOS_ENABLE_CV2=OFF \
-DOCOS_BUILD_PYTHON=OFF \
-DOCOS_ENABLE_VISION=OFF \
-DOCOS_ENABLE_CTEST=OFF
displayName: build the customop library with onnxruntime
# TODO add unittest for webassembly
-DOCOS_ENABLE_CTEST=OFF \
-DOCOS_ENABLE_C_API=ON
displayName: build ort-extensions as an executable
- script: |
bash ./build.sh \
-DCMAKE_TOOLCHAIN_FILE=$(Build.BinariesDirectory)/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake \
-DOCOS_ENABLE_STATIC_LIB=ON \
-DOCOS_ENABLE_SPM_TOKENIZER=ON \
-DOCOS_BUILD_PYTHON=OFF \
-DOCOS_ENABLE_VISION=OFF \
-DOCOS_ENABLE_CTEST=OFF \
-DOCOS_ENABLE_C_API=ON
displayName: build ort-extensions as a static library
- stage: AndroidBuilds
dependsOn: []
Expand Down
36 changes: 9 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ include(CMakeDependentOption)

set(_ORTX_STANDALONE_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(_ORTX_STANDALONE_PROJECT ON)
set(_ORTX_STANDALONE_PROJECT ON)
endif()

set(_ORTX_SHARED_BUILD_SUPPORTED ON)
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set(_ORTX_SHARED_BUILD_SUPPORTED OFF)
endif()

option(CC_OPTIMIZE "Allow compiler optimizations, Set to OFF to disable" ON)
Expand All @@ -78,11 +83,11 @@ option(OCOS_ENABLE_VISION "Enable the operators in `operators/vision`" ON)
option(OCOS_ENABLE_AUDIO "Enable the operators for audio processing" ON)
option(OCOS_ENABLE_AZURE "Enable the operators for azure execution provider" OFF)

option(OCOS_ENABLE_STATIC_LIB "Enable generating static library" OFF)
option(OCOS_ENABLE_STATIC_LIB "Enable generating static library" OFF) # only for WebAssembly
option(OCOS_ENABLE_SELECTED_OPLIST "Enable including the selected_ops tool file" OFF)
option(OCOS_ENABLE_C_API "Enable building the C API" OFF)

option(OCOS_BUILD_SHARED_LIB "Enable building the dynamic library" ON)
option(OCOS_BUILD_SHARED_LIB "Enable building the dynamic library" ${_ORTX_SHARED_BUILD_SUPPORTED})
option(OCOS_BUILD_PYTHON "Enable building the Python package" OFF)
option(OCOS_BUILD_JAVA "Enable building the Java package" OFF)
option(OCOS_BUILD_ANDROID "Enable building the Android package" OFF)
Expand Down Expand Up @@ -773,30 +778,7 @@ if(OCOS_ENABLE_C_API)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
if(OCOS_ENABLE_STATIC_LIB)
message(FATAL_ERROR "Emscripten build does not support building a static library.")
endif()
# Emscripten does not support building a shared library with custom ops.
# and backward compatible with the previous version, we silently turn off the shared library build.
set(OCOS_BUILD_SHARED_LIB OFF CACHE INTERNAL "" FORCE)
add_executable(ortcustomops ${_TARGET_LIB_SRC})
set_target_properties(ortcustomops PROPERTIES LINK_FLAGS " \
-s WASM=1 \
-s NO_EXIT_RUNTIME=0 \
-s ALLOW_MEMORY_GROWTH=1 \
-s SAFE_HEAP=0 \
-s MODULARIZE=1 \
-s SAFE_HEAP_LOG=0 \
-s STACK_OVERFLOW_CHECK=0 \
-s EXPORT_ALL=0 \
-s VERBOSE=0 \
--no-entry")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set_property(TARGET ortcustomops APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1")
else()
set_property(TARGET ortcustomops APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=0 -s DEMANGLE_SUPPORT=0")
endif()
include(ext_wasm)
else()
add_library(ortcustomops STATIC ${_TARGET_LIB_SRC})
if (HAS_SDL)
Expand Down
3 changes: 3 additions & 0 deletions cmake/ext_apple_framework.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Darwin|iOS")
message(FATAL_ERROR "Building an Apple framework can only be enabled for MacOS or iOS.")
endif()
Expand Down
3 changes: 3 additions & 0 deletions cmake/ext_java.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

include(FindJava)
find_package(Java REQUIRED)
include(UseJava)
Expand Down
3 changes: 3 additions & 0 deletions cmake/ext_ortlib.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

if(_ONNXRUNTIME_EMBEDDED)
set(ONNXRUNTIME_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/../include/onnxruntime/core/session)
set(ONNXRUNTIME_LIB_DIR "")
Expand Down
3 changes: 3 additions & 0 deletions cmake/ext_python.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

block(PROPAGATE Python3_FOUND)
set(Python3_FIND_REGISTRY NEVER)
# if we don't set this to NEVER (or possibly LAST) the builds of the wheel for different python versions will fail
Expand Down
3 changes: 3 additions & 0 deletions cmake/ext_tests.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

if (OCOS_ENABLE_SELECTED_OPLIST)
# currently the tests don't handle operator exclusion cleanly.
message(FATAL_ERROR "Due to usage of OCOS_ENABLE_SELECTED_OPLIST excluding operators the tests are unable to be built and run")
Expand Down
30 changes: 30 additions & 0 deletions cmake/ext_wasm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

if(OCOS_ENABLE_STATIC_LIB)
add_library(ortcustomops STATIC ${_TARGET_LIB_SRC})
if (HAS_SDL)
target_compile_options(ortcustomops PRIVATE "/sdl")
endif()
add_library(onnxruntime_extensions ALIAS ortcustomops)
standardize_output_folder(ortcustomops)
else()
add_executable(ortcustomops ${_TARGET_LIB_SRC})
set_target_properties(ortcustomops PROPERTIES LINK_FLAGS " \
-s WASM=1 \
-s NO_EXIT_RUNTIME=0 \
-s ALLOW_MEMORY_GROWTH=1 \
-s SAFE_HEAP=0 \
-s MODULARIZE=1 \
-s SAFE_HEAP_LOG=0 \
-s STACK_OVERFLOW_CHECK=0 \
-s EXPORT_ALL=0 \
-s VERBOSE=0 \
--no-entry")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set_property(TARGET ortcustomops APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1")
else()
set_property(TARGET ortcustomops APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=0 -s DEMANGLE_SUPPORT=0")
endif()
endif()

0 comments on commit f3f6caa

Please sign in to comment.