1+ if (NOT $ENV{SHERPA_ONNX_IS_USING_BUILD_WASM_SH} )
2+ message (FATAL_ERROR "Please use ./build-wasm-combined.sh to build" )
3+ endif ()
4+
5+ # Collect all exported functions from all modules
6+ set (exported_functions
7+ # Core utilities
8+ CopyHeap
9+ MyPrintOnlineASR
10+ MyPrintVAD
11+ MyPrintTTS
12+ MyPrintSpeakerDiarization
13+ MyPrintSpeechEnhancement
14+ MyPrintKeywordSpotting
15+ SherpaOnnxFileExists
16+
17+ # Online ASR
18+ SherpaOnnxCreateOnlineRecognizer
19+ SherpaOnnxCreateOnlineStream
20+ SherpaOnnxDecodeOnlineStream
21+ SherpaOnnxDestroyOfflineStreamResultJson
22+ SherpaOnnxDestroyOnlineRecognizer
23+ SherpaOnnxDestroyOnlineRecognizerResult
24+ SherpaOnnxDestroyOnlineStream
25+ SherpaOnnxDestroyOnlineStreamResultJson
26+ SherpaOnnxGetOfflineStreamResultAsJson
27+ SherpaOnnxGetOnlineStreamResult
28+ SherpaOnnxGetOnlineStreamResultAsJson
29+ SherpaOnnxIsOnlineStreamReady
30+ SherpaOnnxOnlineStreamAcceptWaveform
31+ SherpaOnnxOnlineStreamInputFinished
32+ SherpaOnnxOnlineStreamIsEndpoint
33+ SherpaOnnxOnlineStreamReset
34+
35+ # Offline ASR
36+ SherpaOnnxCreateOfflineRecognizer
37+ SherpaOnnxCreateOfflineStream
38+ SherpaOnnxDecodeOfflineStream
39+ SherpaOnnxDecodeMultipleOfflineStreams
40+ SherpaOnnxDestroyOfflineRecognizer
41+ SherpaOnnxDestroyOfflineRecognizerResult
42+ SherpaOnnxDestroyOfflineStream
43+ SherpaOnnxAcceptWaveformOffline
44+ SherpaOnnxGetOfflineStreamResult
45+
46+ # TTS
47+ SherpaOnnxCreateOfflineTts
48+ SherpaOnnxDestroyOfflineTts
49+ SherpaOnnxDestroyOfflineTtsGeneratedAudio
50+ SherpaOnnxOfflineTtsGenerate
51+ SherpaOnnxOfflineTtsGenerateWithCallback
52+ SherpaOnnxOfflineTtsSampleRate
53+ SherpaOnnxOfflineTtsNumSpeakers
54+ SherpaOnnxWriteWave
55+
56+ # VAD
57+ SherpaOnnxCreateCircularBuffer
58+ SherpaOnnxDestroyCircularBuffer
59+ SherpaOnnxCircularBufferPush
60+ SherpaOnnxCircularBufferGet
61+ SherpaOnnxCircularBufferFree
62+ SherpaOnnxCircularBufferPop
63+ SherpaOnnxCircularBufferSize
64+ SherpaOnnxCircularBufferHead
65+ SherpaOnnxCircularBufferReset
66+ SherpaOnnxCreateVoiceActivityDetector
67+ SherpaOnnxDestroyVoiceActivityDetector
68+ SherpaOnnxVoiceActivityDetectorAcceptWaveform
69+ SherpaOnnxVoiceActivityDetectorEmpty
70+ SherpaOnnxVoiceActivityDetectorDetected
71+ SherpaOnnxVoiceActivityDetectorPop
72+ SherpaOnnxVoiceActivityDetectorClear
73+ SherpaOnnxVoiceActivityDetectorFront
74+ SherpaOnnxDestroySpeechSegment
75+ SherpaOnnxVoiceActivityDetectorReset
76+ SherpaOnnxVoiceActivityDetectorFlush
77+
78+ # KWS
79+ SherpaOnnxCreateKeywordSpotter
80+ SherpaOnnxDestroyKeywordSpotter
81+ SherpaOnnxCreateKeywordStream
82+ SherpaOnnxIsKeywordStreamReady
83+ SherpaOnnxDecodeKeywordStream
84+ SherpaOnnxResetKeywordStream
85+ SherpaOnnxGetKeywordResult
86+ SherpaOnnxDestroyKeywordResult
87+ )
88+
89+ set (mangled_exported_functions)
90+ foreach (x IN LISTS exported_functions)
91+ list (APPEND mangled_exported_functions "_${x} " )
92+ endforeach ()
93+ list (JOIN mangled_exported_functions "," all_exported_functions)
94+
95+ include_directories (${CMAKE_SOURCE_DIR} )
96+ set (MY_FLAGS " -s FORCE_FILESYSTEM=1 -s INITIAL_MEMORY=512MB -s ALLOW_MEMORY_GROWTH=1" )
97+ string (APPEND MY_FLAGS " -sSTACK_SIZE=10485760 " ) # 10MB
98+ string (APPEND MY_FLAGS " -sASYNCIFY=1 -sFETCH=1 " ) # For async loading
99+ string (APPEND MY_FLAGS " -sEXPORTED_FUNCTIONS=[_malloc,_free,${all_exported_functions} ] " )
100+ # No preloaded assets - all models will be loaded dynamically
101+ string (APPEND MY_FLAGS " -sEXPORTED_RUNTIME_METHODS=['ccall','stringToUTF8','setValue','getValue','lengthBytesUTF8','UTF8ToString','FS'] " )
102+
103+ message (STATUS "MY_FLAGS: ${MY_FLAGS} " )
104+
105+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_FLAGS} " )
106+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_FLAGS} " )
107+ set (CMAKE_EXECUTBLE_LINKER_FLAGS "${CMAKE_EXECUTBLE_LINKER_FLAGS} ${MY_FLAGS} " )
108+
109+ add_executable (sherpa-onnx-wasm-combined sherpa-onnx-wasm-combined.cc)
110+ target_link_libraries (sherpa-onnx-wasm-combined sherpa-onnx-c-api)
111+ install (TARGETS sherpa-onnx-wasm-combined DESTINATION bin/wasm/combined)
112+
113+ install (
114+ FILES
115+ "$<TARGET_FILE_DIR:sherpa-onnx-wasm-combined>/sherpa-onnx-wasm-combined.js"
116+ "index.html"
117+ "sherpa-onnx-combined.js"
118+ "$<TARGET_FILE_DIR:sherpa-onnx-wasm-combined>/sherpa-onnx-wasm-combined.wasm"
119+ DESTINATION
120+ bin/wasm/combined
121+ )
122+
123+ # Add option to install to original repo
124+ option (SHERPA_ONNX_INSTALL_TO_REPO "Install compiled WASM files to original repo directory" OFF )
125+ set (SHERPA_ONNX_REPO_PATH "${CMAKE_SOURCE_DIR} /wasm/combined" CACHE PATH "Path to original repo wasm directory" )
126+
127+ if (SHERPA_ONNX_INSTALL_TO_REPO)
128+ # Add a custom target that will run after the installation
129+ add_custom_target (install_to_repo ALL
130+ COMMAND ${CMAKE_COMMAND} -E echo "Installing to original repo at ${SHERPA_ONNX_REPO_PATH} ..."
131+ COMMAND ${CMAKE_COMMAND} -E make_directory ${SHERPA_ONNX_REPO_PATH}
132+
133+ # Copy the JS file
134+ COMMAND ${CMAKE_COMMAND}
135+ -DSRC_DIR=${CMAKE_BINARY_DIR} /bin
136+ -DDEST_DIR=${SHERPA_ONNX_REPO_PATH}
137+ -DCOPY_FILES="sherpa-onnx-wasm-combined.js"
138+ -P ${CMAKE_CURRENT_SOURCE_DIR} /copy_with_confirm.cmake
139+
140+ # Copy the WASM file
141+ COMMAND ${CMAKE_COMMAND}
142+ -DSRC_DIR=${CMAKE_BINARY_DIR} /bin
143+ -DDEST_DIR=${SHERPA_ONNX_REPO_PATH}
144+ -DCOPY_FILES="sherpa-onnx-wasm-combined.wasm"
145+ -P ${CMAKE_CURRENT_SOURCE_DIR} /copy_with_confirm.cmake
146+
147+ # Copy the index.html file
148+ COMMAND ${CMAKE_COMMAND}
149+ -DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
150+ -DDEST_DIR=${SHERPA_ONNX_REPO_PATH}
151+ -DCOPY_FILES="index.html"
152+ -P ${CMAKE_CURRENT_SOURCE_DIR} /copy_with_confirm.cmake
153+
154+ # Copy the JS library file
155+ COMMAND ${CMAKE_COMMAND}
156+ -DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
157+ -DDEST_DIR=${SHERPA_ONNX_REPO_PATH}
158+ -DCOPY_FILES="sherpa-onnx-combined.js"
159+ -P ${CMAKE_CURRENT_SOURCE_DIR} /copy_with_confirm.cmake
160+
161+ DEPENDS sherpa-onnx-wasm-combined
162+ COMMENT "Checking and installing WASM files to original repo"
163+ )
164+ endif ()
0 commit comments