Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
924 lines (835 loc) · 34.9 KB
/
Copy pathCMakeLists.txt
File metadata and controls
924 lines (835 loc) · 34.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
cmake_minimum_required(VERSION 3.14)
project(RapidSpeech.cpp VERSION 1.1.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
# --- Compilation Options ---
option(RS_BUILD_TESTS "Build test executables" ON)
option(RS_CUDA "Enable CUDA acceleration" OFF)
option(RS_VULKAN "Enable Vulkan acceleration" OFF)
option(RS_CANN "Enable CANN acceleration (Huawei)" OFF)
option(RS_OPENCL "Enable OpenCL acceleration" OFF)
option(RS_WEBGPU "Enable WebGPU acceleration (Dawn native / emdawnwebgpu for WASM)" OFF)
option(RS_ENABLE_PYTHON "Enable Python bindings via pybind11" OFF)
option(RS_BUILD_CLI "Build command-line example executables" ON)
option(RS_STATIC_EXE "Build fully self-contained executables (no DLL dependencies)" OFF)
option(RS_ENABLE_XQUIC "Enable the optional XQUIC transport adapter" OFF)
option(RS_ENABLE_LIBDATACHANNEL
"Enable the optional libdatachannel WebRTC adapter" OFF)
# RS_STATIC_EXE implies static linkage everywhere: static ggml + static
# rapidspeech-core so the CLIs carry no runtime DLL/.so dependency.
if (RS_STATIC_EXE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
endif()
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
if (RS_ENABLE_PYTHON)
include(pybind11)
endif()
# Auto-detect Apple platform to set default value for Metal
if (APPLE)
set(RS_METAL_DEFAULT ON)
else()
set(RS_METAL_DEFAULT OFF)
endif()
option(RS_METAL "Enable Metal acceleration (Apple only)" ${RS_METAL_DEFAULT})
if (APPLE)
set(RS_LIBRARY_INSTALL_RPATH "@loader_path")
set(RS_EXECUTABLE_INSTALL_RPATH "@loader_path/../lib")
else()
set(RS_LIBRARY_INSTALL_RPATH "\$ORIGIN")
set(RS_EXECUTABLE_INSTALL_RPATH "\$ORIGIN/../lib")
endif()
# --- Dependencies Configuration ---
set(GGML_PATH "${CMAKE_CURRENT_SOURCE_DIR}/ggml")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
if (NOT EXISTS "${GGML_PATH}/CMakeLists.txt")
message(FATAL_ERROR "ggml not found at ${GGML_PATH}. Please init submodules.")
endif()
# Note: Pass options to ggml before adding the directory
if (RS_CUDA)
set(GGML_CUDA ON CACHE BOOL "ggml: enable CUDA" FORCE)
# The Windows GitHub runners can ship a newer MSVC toolset than the
# CUDA toolkit bundled by cibuildwheel supports. CUDA rejects that host
# compiler during CMake's compiler-identification step unless this flag
# is enabled. Keep the workaround scoped to Windows CUDA builds.
if (WIN32)
string(APPEND CMAKE_CUDA_FLAGS " --allow-unsupported-compiler")
endif()
endif()
if (RS_METAL AND APPLE)
set(GGML_METAL ON CACHE BOOL "ggml: enable Metal" FORCE)
endif()
if (RS_VULKAN)
set(GGML_VULKAN ON CACHE BOOL "ggml: enable Vulkan" FORCE)
endif()
if (RS_CANN)
set(GGML_CANN ON CACHE BOOL "ggml: enable CANN" FORCE)
endif()
if (RS_OPENCL)
set(GGML_OPENCL ON CACHE BOOL "ggml: enable OpenCL" FORCE)
endif()
if (RS_WEBGPU)
set(GGML_WEBGPU ON CACHE BOOL "ggml: enable WebGPU" FORCE)
endif()
# Override ggml's install directories so all shared libraries end up
# in the same directory as the pybind module (required for wheel packaging).
# Without this, GNUInstallDirs puts backends in lib/ and the pybind in .
if (RS_ENABLE_PYTHON)
set(CMAKE_INSTALL_LIBDIR "." CACHE PATH "lib install dir" FORCE)
set(CMAKE_INSTALL_BINDIR "." CACHE PATH "bin install dir" FORCE)
set(CMAKE_INSTALL_INCLUDEDIR "include" CACHE PATH "header install dir" FORCE)
# Disable library SONAME versioning for Python wheel packaging.
# Versioned sonames (e.g., libfoo.so.1) confuse auditwheel/delocate
# because the symlink .so.1 -> .so.1.0.0 is not preserved in the wheel.
set(GGML_VERSION_MAJOR 0 CACHE STRING "" FORCE)
set(GGML_VERSION_MINOR 10 CACHE STRING "" FORCE)
set(GGML_VERSION_PATCH 0 CACHE STRING "" FORCE)
set(GGML_VERSION "0.10.0" CACHE STRING "" FORCE)
set(CMAKE_PLATFORM_NO_VERSIONED_SONAME ON)
# Python wheels keep all shared libraries next to the extension module.
set(RS_RPATH "${RS_LIBRARY_INSTALL_RPATH}")
# Python wheels install CLI executables below rapidspeech/bin while shared
# libraries live in rapidspeech/. Keep those executables self-contained.
set(RS_EXECUTABLE_INSTALL_RPATH "${RS_LIBRARY_INSTALL_RPATH}/..")
endif()
add_compile_definitions(GGML_MAX_NAME=128)
add_subdirectory(${GGML_PATH} ggml)
# Suppress implicit integer-to-enum and pointer-to-id conversion warnings in
# third-party ggml Metal code (MTLGPUFamilyMetal*_GGML are defined as integers).
if (TARGET ggml-metal)
target_compile_options(ggml-metal PRIVATE -Wno-int-conversion)
endif()
# Remove SONAME versioning from ggml targets for Python wheel packaging.
# ggml explicitly sets SOVERSION on its targets, which creates versioned symlinks
# (libggml.so.0 -> libggml.so.0.10.0) that aren't preserved in wheels, causing
# auditwheel/delocate to fail.
if (RS_ENABLE_PYTHON)
set(_ggml_targets ggml ggml-base ggml-cpu ggml-metal ggml-blas ggml-vulkan)
foreach(_t IN LISTS _ggml_targets)
if (TARGET ${_t})
if (APPLE)
# On macOS, NO_SONAME ON causes install_name to be the build-tree
# path, which delocate-wheel cannot resolve. Use INSTALL_NAME_DIR
# instead so install_name becomes @rpath/libname.dylib.
set_target_properties(${_t} PROPERTIES
SOVERSION ""
VERSION ""
INSTALL_NAME_DIR "@rpath"
INSTALL_RPATH "${RS_RPATH}"
BUILD_WITH_INSTALL_RPATH ON
)
else()
set_target_properties(${_t} PROPERTIES
SOVERSION ""
VERSION ""
INSTALL_RPATH "${RS_RPATH}"
BUILD_WITH_INSTALL_RPATH ON
)
endif()
endif()
endforeach()
endif()
# --- Core Source Files ---
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/c_api
)
# Note: LLM-related files are now included for Qwen3 integration
set(RS_SOURCES
rapidspeech/src/core/rs_context.cpp
rapidspeech/src/core/rs_processor.cpp
rapidspeech/src/core/rs_kws.cpp
rapidspeech/src/realtime/rs_realtime_protocol.cpp
rapidspeech/src/realtime/rs_webrtc_transport.cpp
rapidspeech/src/frontend/audio_processor.cpp
rapidspeech/src/frontend/fftsg.c
rapidspeech/src/utils/rs_log.cpp
rapidspeech/src/arch/sensevoice.cpp
rapidspeech/src/arch/sensevoice_encoder.cpp
rapidspeech/src/arch/context_graph.cpp
rapidspeech/src/arch/keyword_loader.cpp
rapidspeech/src/arch/hotword_retriever.cpp
rapidspeech/src/arch/kws_decoder.cpp
rapidspeech/src/arch/funasr-nano.cpp
rapidspeech/src/arch/silero_vad.cpp
rapidspeech/src/arch/fireredvad.cpp
rapidspeech/src/arch/tenvad.cpp
third_party/ten-vad/fftw.c
third_party/ten-vad/stft.cc
third_party/ten-vad/biquad.cc
third_party/ten-vad/fscvrt.cc
third_party/ten-vad/pitch_est.cc
rapidspeech/src/arch/xasr.h
rapidspeech/src/arch/xasr.cpp
rapidspeech/src/arch/xasr_zipformer.cpp
rapidspeech/src/arch/llm_model.h
rapidspeech/src/arch/llm_model.cpp
rapidspeech/src/arch/llm_graph.h
rapidspeech/src/arch/llm_graph.cpp
rapidspeech/src/arch/llm_kv_cache.h
rapidspeech/src/arch/llm_kv_cache.cpp
rapidspeech/src/arch/qwen3.h
rapidspeech/src/arch/qwen3.cpp
rapidspeech/src/arch/qwen2.h
rapidspeech/src/arch/qwen2.cpp
rapidspeech/src/arch/cosyvoice3.h
rapidspeech/src/arch/cosyvoice3.cpp
rapidspeech/src/arch/cosyvoice3_decode.cpp
rapidspeech/src/arch/cosyvoice3_batch.h
rapidspeech/src/arch/cosyvoice3_batch.cpp
rapidspeech/src/arch/cosyvoice3_ras.h
rapidspeech/src/arch/cosyvoice3_ras.cpp
rapidspeech/src/arch/cosyvoice3_flow.h
rapidspeech/src/arch/cosyvoice3_flow.cpp
rapidspeech/src/arch/cosyvoice3_hift.h
rapidspeech/src/arch/cosyvoice3_hift.cpp
rapidspeech/src/arch/cosyvoice3_speech_tokenizer.h
rapidspeech/src/arch/cosyvoice3_speech_tokenizer.cpp
rapidspeech/src/frontend/cosyvoice3_mel.h
rapidspeech/src/frontend/cosyvoice3_mel.cpp
rapidspeech/src/arch/openvoice2.cpp
rapidspeech/src/arch/kokoro.cpp
rapidspeech/src/frontend/kokoro_pinyin.cpp
rapidspeech/src/frontend/kokoro_zh_tone_sandhi.cpp
rapidspeech/src/frontend/kokoro_g2p_zh.cpp
rapidspeech/src/frontend/kokoro_g2p_en.cpp
rapidspeech/src/frontend/wetext_normalizer.cpp
rapidspeech/src/arch/omnivoice.cpp
rapidspeech/src/arch/voxcpm2.h
rapidspeech/src/arch/voxcpm2.cpp
rapidspeech/src/arch/stable_audio3.h
rapidspeech/src/arch/stable_audio3.cpp
rapidspeech/src/arch/indextts2.h
rapidspeech/src/arch/indextts2.cpp
rapidspeech/src/arch/indextts2_gpt.cpp
rapidspeech/src/arch/indextts2_s2mel.cpp
rapidspeech/src/arch/indextts2_bigvgan.cpp
rapidspeech/src/arch/indextts2_tokenizer.cpp
rapidspeech/src/arch/indextts2_qwen_emo.cpp
rapidspeech/src/arch/campplus.cpp
rapidspeech/src/arch/w2v_bert.h
rapidspeech/src/arch/w2v_bert.cpp
rapidspeech/src/arch/semantic_codec.h
rapidspeech/src/arch/semantic_codec.cpp
rapidspeech/src/arch/bert.cpp
rapidspeech/src/arch/bert.h
rapidspeech/src/frontend/text_frontend.cpp
rapidspeech/src/frontend/whisper_mel.h
rapidspeech/src/frontend/whisper_mel.cpp
rapidspeech/src/frontend/seamless_mel.h
rapidspeech/src/frontend/seamless_mel.cpp
rapidspeech/src/arch/qwen3_asr.h
rapidspeech/src/arch/qwen3_asr.cpp
rapidspeech/src/arch/moss_transcribe.h
rapidspeech/src/arch/moss_transcribe.cpp
rapidspeech/src/utils/rs_wav.cpp
rapidspeech/src/utils/rs_data_paths.cpp
# rapidspeech/src/arch/mimo_tokenizer.cpp
# rapidspeech/src/arch/mimo_tokenizer.h
# rapidspeech/src/arch/mimo_asr.cpp
# rapidspeech/src/arch/mimo_asr.h
)
# --- Library Compilation ---
# STATIC for self-contained executables (RS_STATIC_EXE), otherwise SHARED.
if (RS_STATIC_EXE)
set(RS_CORE_LIB_TYPE STATIC)
else()
set(RS_CORE_LIB_TYPE SHARED)
endif()
add_library(rapidspeech-core ${RS_CORE_LIB_TYPE}
${RS_SOURCES}
rapidspeech/src/c_api/rapidspeech_c.cpp
rapidspeech/src/c_api/rs_vad.cpp
rapidspeech/src/c_api/rs_speaker.cpp
)
target_include_directories(rapidspeech-core PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/c_api
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rapidspeech-core PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/third_party/cppjieba/include
${CMAKE_CURRENT_SOURCE_DIR}/third_party/ten-vad
)
# Symbol-export macro. For a static build (RS_STATIC_EXE) define RS_STATIC so
# rapidspeech.h expands RS_API to nothing (no __declspec(dllimport/dllexport));
# for a shared build define RAPIDSPEECH_BUILD to export. RS_STATIC is PUBLIC so
# the CLIs that link the static core see the same (empty) RS_API.
if (RS_STATIC_EXE)
target_compile_definitions(rapidspeech-core PUBLIC RS_STATIC)
else()
target_compile_definitions(rapidspeech-core PRIVATE RAPIDSPEECH_BUILD)
endif()
# MSVC portability: /utf-8 forces UTF-8 for both source and execution
# charsets (otherwise the system code page garbles Chinese comments/strings),
# _USE_MATH_DEFINES enables M_PI from <cmath>, _CRT_SECURE_NO_WARNINGS
# suppresses getenv deprecation, /wd4244 suppresses int64→int truncation
# warnings from the ggml API.
if (MSVC)
target_compile_definitions(rapidspeech-core PRIVATE _USE_MATH_DEFINES _CRT_SECURE_NO_WARNINGS)
target_compile_options(rapidspeech-core PRIVATE /utf-8 /wd4244)
endif()
# Shared-library-only properties: SONAME/VERSION and hidden default visibility
# only make sense for a .so/.dylib. A static build (RS_STATIC_EXE) skips them —
# VERSION/SOVERSION are ignored for STATIC targets and hiding symbols would
# strip the API the CLIs link against.
if (NOT RS_STATIC_EXE)
# Set library version (skip SOVERSION for Python wheels to avoid auditwheel issues)
if (NOT RS_ENABLE_PYTHON)
if (APPLE)
set_target_properties(rapidspeech-core PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION 1
INSTALL_NAME_DIR "@rpath"
INSTALL_RPATH "${RS_LIBRARY_INSTALL_RPATH}"
)
else()
set_target_properties(rapidspeech-core PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION 1
INSTALL_RPATH "${RS_LIBRARY_INSTALL_RPATH}"
)
endif()
else()
if (APPLE)
set_target_properties(rapidspeech-core PROPERTIES
INSTALL_NAME_DIR "@rpath"
INSTALL_RPATH "${RS_RPATH}"
BUILD_WITH_INSTALL_RPATH ON
)
else()
set_target_properties(rapidspeech-core PROPERTIES
INSTALL_RPATH "${RS_RPATH}"
BUILD_WITH_INSTALL_RPATH ON
)
endif()
endif()
set_target_properties(rapidspeech-core PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
)
endif()
target_link_libraries(rapidspeech-core PRIVATE ggml Threads::Threads)
if (RS_ENABLE_XQUIC)
find_path(RS_XQUIC_INCLUDE_DIR
NAMES xquic/xquic.h
DOC "XQUIC include directory"
)
find_library(RS_XQUIC_LIBRARY
NAMES xquic
DOC "XQUIC library"
)
if (NOT RS_XQUIC_INCLUDE_DIR OR NOT RS_XQUIC_LIBRARY)
message(FATAL_ERROR
"RS_ENABLE_XQUIC requires XQUIC. Set RS_XQUIC_INCLUDE_DIR and "
"RS_XQUIC_LIBRARY, or install XQUIC in a standard prefix."
)
endif()
target_sources(rapidspeech-core PRIVATE
rapidspeech/src/realtime/rs_xquic_transport.cpp
)
target_include_directories(rapidspeech-core PUBLIC ${RS_XQUIC_INCLUDE_DIR})
target_link_libraries(rapidspeech-core PRIVATE ${RS_XQUIC_LIBRARY})
target_compile_definitions(rapidspeech-core PUBLIC RS_ENABLE_XQUIC)
endif()
if (RS_ENABLE_LIBDATACHANNEL)
find_path(RS_LIBDATACHANNEL_INCLUDE_DIR
NAMES rtc/rtc.h
DOC "libdatachannel include directory"
)
find_library(RS_LIBDATACHANNEL_LIBRARY
NAMES datachannel
DOC "libdatachannel library"
)
if (NOT RS_LIBDATACHANNEL_INCLUDE_DIR OR NOT RS_LIBDATACHANNEL_LIBRARY)
message(FATAL_ERROR
"RS_ENABLE_LIBDATACHANNEL requires libdatachannel. Set "
"RS_LIBDATACHANNEL_INCLUDE_DIR and RS_LIBDATACHANNEL_LIBRARY, "
"or install libdatachannel in a standard prefix."
)
endif()
target_sources(rapidspeech-core PRIVATE
rapidspeech/src/realtime/rs_libdatachannel_peer.cpp
)
target_include_directories(rapidspeech-core PUBLIC
${RS_LIBDATACHANNEL_INCLUDE_DIR}
)
target_link_libraries(rapidspeech-core PRIVATE ${RS_LIBDATACHANNEL_LIBRARY})
target_compile_definitions(rapidspeech-core PUBLIC RS_ENABLE_LIBDATACHANNEL)
endif()
# --- WeTextProcessing (text normalization for ZH TTS) ---
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/openfst.cmake)
add_library(wetext_processor STATIC
third_party/wetext/utils/wetext_string.cc
third_party/wetext/processor/wetext_token_parser.cc
third_party/wetext/processor/wetext_processor.cc)
target_include_directories(wetext_processor PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/third_party/wetext)
target_link_libraries(wetext_processor PUBLIC fst)
set_target_properties(wetext_processor PROPERTIES
POSITION_INDEPENDENT_CODE ON
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON)
if(MSVC)
# openfst/randgen.h uses std::numeric_limits<...>::max(); without
# NOMINMAX the Windows SDK max() macro turns that into a syntax error.
target_compile_definitions(wetext_processor PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
endif()
target_link_libraries(rapidspeech-core PRIVATE wetext_processor)
# --- Hardware Acceleration Logic ---
# 1. CUDA Configuration
if (RS_CUDA)
message(STATUS "RapidSpeech: CUDA acceleration enabled.")
target_compile_definitions(rapidspeech-core PRIVATE RS_USE_CUDA)
if (TARGET ggml-cuda)
target_link_libraries(rapidspeech-core PRIVATE ggml-cuda)
endif()
endif()
# 2. Metal Configuration (Apple Platforms)
if (APPLE)
# Always link Metal + Foundation — used by our custom DAC vocoder shader.
# ggml-metal is not required for our Metal path (we use MTL API directly).
target_link_libraries(rapidspeech-core PRIVATE "-framework Foundation" "-framework Metal" "-framework MetalPerformanceShaders" "-framework Accelerate")
# Custom Metal DAC vocoder (fused compute kernels, bypasses ggml per-op overhead)
target_sources(rapidspeech-core PRIVATE rapidspeech/src/arch/dac_metal.mm)
set_source_files_properties(rapidspeech/src/arch/dac_metal.mm PROPERTIES
COMPILE_FLAGS "-x objective-c++ -fobjc-arc")
target_compile_definitions(rapidspeech-core PRIVATE RS_USE_METAL_DAC)
# Custom Metal kernel for Kokoro iSTFTNet ConvTranspose1d (simdgroup-over-IC).
target_sources(rapidspeech-core PRIVATE rapidspeech/src/arch/kokoro_metal.mm)
set_source_files_properties(rapidspeech/src/arch/kokoro_metal.mm PROPERTIES
COMPILE_FLAGS "-x objective-c++ -fobjc-arc")
target_compile_definitions(rapidspeech-core PRIVATE RS_USE_METAL_KOKORO)
# Custom Metal BigVGAN-v2 vocoder for IndexTTS-2 (single command buffer,
# bypasses the ~467 CPU/Metal graph splits of the anti-aliased Snake path).
target_sources(rapidspeech-core PRIVATE rapidspeech/src/arch/indextts2_bigvgan_metal.mm)
set_source_files_properties(rapidspeech/src/arch/indextts2_bigvgan_metal.mm PROPERTIES
COMPILE_FLAGS "-x objective-c++ -fobjc-arc")
target_compile_definitions(rapidspeech-core PRIVATE RS_USE_METAL_BIGVGAN)
endif()
if (RS_METAL AND APPLE)
message(STATUS "RapidSpeech: Metal acceleration enabled.")
target_compile_definitions(rapidspeech-core PRIVATE RS_USE_METAL)
if (TARGET ggml-metal)
target_link_libraries(rapidspeech-core PRIVATE ggml-metal)
endif()
target_link_libraries(rapidspeech-core PRIVATE "-framework MetalKit")
endif()
# 3. Vulkan Configuration
if (RS_VULKAN)
message(STATUS "RapidSpeech: Vulkan acceleration enabled.")
target_compile_definitions(rapidspeech-core PRIVATE RS_USE_VULKAN)
if (TARGET ggml-vulkan)
target_link_libraries(rapidspeech-core PRIVATE ggml-vulkan)
endif()
endif()
# 4. CANN Configuration (Huawei Ascend)
if (RS_CANN)
message(STATUS "RapidSpeech: CANN acceleration enabled.")
target_compile_definitions(rapidspeech-core PRIVATE RS_USE_CANN)
if (TARGET ggml-cann)
target_link_libraries(rapidspeech-core PRIVATE ggml-cann)
endif()
endif()
# 5. OpenCL Configuration
if (RS_OPENCL)
message(STATUS "RapidSpeech: OpenCL acceleration enabled.")
target_compile_definitions(rapidspeech-core PRIVATE RS_USE_OPENCL)
if (TARGET ggml-opencl)
target_link_libraries(rapidspeech-core PRIVATE ggml-opencl)
endif()
endif()
# 6. WebGPU Configuration (browser/Dawn-native — works as a portable GPU backend)
if (RS_WEBGPU)
message(STATUS "RapidSpeech: WebGPU acceleration enabled.")
target_compile_definitions(rapidspeech-core PRIVATE RS_USE_WEBGPU)
if (TARGET ggml-webgpu)
target_link_libraries(rapidspeech-core PRIVATE ggml-webgpu)
endif()
endif()
if (RS_ENABLE_PYTHON)
pybind11_add_module(rapidspeech
rapidspeech/python/pybind_rapidspeech.cpp
)
# Install the pybind11 module directly into the package root
# so that `import rapidspeech` finds it without path tricks.
install(TARGETS rapidspeech
LIBRARY DESTINATION . # Linux/macOS: rapidspeech/*.so
RUNTIME DESTINATION . # Windows: rapidspeech/*.pyd
)
# Install rapidspeech-core shared library next to the pybind module
# so that the OS loader can find it at runtime.
install(TARGETS rapidspeech-core
LIBRARY DESTINATION . # Linux/macOS
RUNTIME DESTINATION . # Windows (.dll)
)
# ggml shared libraries are installed by ggml's own install rules.
# We overrode CMAKE_INSTALL_LIBDIR/BINDIR to "." above so they end up
# in the same directory as the pybind module.
target_link_libraries(rapidspeech
PRIVATE rapidspeech-core
)
target_include_directories(rapidspeech
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)
# Embed $ORIGIN (Linux) / @loader_path (macOS) rpath so the pybind module
# finds librapidspeech-core.so / .dylib in the same directory at runtime.
# Setting INSTALL_RPATH directly on the target (rather than via the
# CMAKE_INSTALL_RPATH initializer) avoids the ordering trap where the
# variable is set after pybind11_add_module() has already captured it.
set_target_properties(rapidspeech PROPERTIES
INSTALL_RPATH "${RS_RPATH}"
BUILD_WITH_INSTALL_RPATH ON
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
)
endif()
if (NOT RS_ENABLE_PYTHON)
install(TARGETS rapidspeech-core
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
endif()
# --- Example Executables ---
if (RS_BUILD_CLI)
# Download miniaudio.h if not present (3rd-party single-header library)
set(MINIAUDIO_PATH "${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils/miniaudio.h")
if (NOT EXISTS "${MINIAUDIO_PATH}")
message(STATUS "Downloading miniaudio.h ...")
file(DOWNLOAD
"https://raw.githubusercontent.com/mackron/miniaudio/refs/tags/0.11.21/miniaudio.h"
"${MINIAUDIO_PATH}"
STATUS _ma_status
TIMEOUT 60
)
list(GET _ma_status 0 _ma_code)
if (NOT _ma_code EQUAL 0)
message(FATAL_ERROR "Failed to download miniaudio.h. "
"Place it at third_party/miniaudio/miniaudio.h manually.")
endif()
endif()
add_executable(rs-asr-offline
examples/asr/rs-asr-offline.cpp
)
add_executable(rs-asr-vad-online
examples/asr/rs-asr-vad-online.cpp
)
add_executable(rs-asr-online
examples/asr/rs-asr-online.cpp
)
add_executable(rs-tts-offline
examples/tts/rs-tts-offline.cpp
)
add_executable(rs-tts-online
examples/tts/rs-tts-online.cpp
)
add_executable(rs-quantize
examples/quantize/quantize.cpp
examples/quantize/common_quantize.cpp
)
add_executable(rs-imatrix
examples/imatrix/rs-imatrix.cpp
)
add_executable(rs-tts-split
examples/tts/rs-tts-split.cpp
)
add_executable(rs-speaker-id
examples/asr/rs-speaker-id.cpp
)
add_executable(rs-kws
examples/kws/rs-kws.cpp
)
add_executable(rs-server
examples/server/rs-server.cpp
)
add_executable(rs-aoq-sim
examples/aoq/rs-aoq-sim.cpp
)
set(RS_CLI_TARGETS
rs-asr-offline
rs-asr-vad-online
rs-asr-online
rs-tts-offline
rs-tts-online
rs-tts-split
rs-quantize
rs-imatrix
rs-speaker-id
rs-kws
rs-server
rs-aoq-sim
)
if (NOT WIN32)
foreach(_rs_cli_target IN LISTS RS_CLI_TARGETS)
set_target_properties(${_rs_cli_target} PROPERTIES
INSTALL_RPATH "${RS_EXECUTABLE_INSTALL_RPATH}"
)
endforeach()
endif()
# /utf-8 so the CLIs' Chinese usage/help string literals are emitted as
# UTF-8 instead of the MSVC system code page (matches rapidspeech-core).
if (MSVC)
foreach(_rs_cli_target IN LISTS RS_CLI_TARGETS)
target_compile_options(${_rs_cli_target} PRIVATE /utf-8)
endforeach()
endif()
target_link_libraries(rs-asr-offline PRIVATE rapidspeech-core ggml)
target_link_libraries(rs-asr-vad-online PRIVATE rapidspeech-core ggml Threads::Threads)
target_link_libraries(rs-asr-online PRIVATE rapidspeech-core ggml Threads::Threads)
target_link_libraries(rs-tts-offline PRIVATE rapidspeech-core ggml)
target_link_libraries(rs-tts-online PRIVATE rapidspeech-core ggml Threads::Threads)
target_link_libraries(rs-tts-split PRIVATE rapidspeech-core ggml)
target_link_libraries(rs-quantize PRIVATE ggml)
target_link_libraries(rs-imatrix PRIVATE rapidspeech-core ggml)
target_link_libraries(rs-speaker-id PRIVATE rapidspeech-core ggml)
target_link_libraries(rs-kws PRIVATE rapidspeech-core ggml Threads::Threads)
target_link_libraries(rs-aoq-sim PRIVATE Threads::Threads)
# rs-server: cpp-httplib needs threads; Windows needs Winsock.
target_link_libraries(rs-server PRIVATE rapidspeech-core ggml Threads::Threads)
if (WIN32)
target_link_libraries(rs-server PRIVATE ws2_32)
endif()
target_include_directories(rs-server PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples/server
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
if (NOT MSVC)
target_link_libraries(rs-quantize PRIVATE Threads::Threads)
endif()
target_include_directories(rs-quantize PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples/quantize
${CMAKE_CURRENT_SOURCE_DIR}/examples/imatrix
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rs-imatrix PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples/imatrix
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rs-asr-offline PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rs-asr-vad-online PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rs-asr-online PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rs-tts-offline PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rs-tts-online PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rs-tts-split PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/examples/tts
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rs-speaker-id PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
target_include_directories(rs-kws PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
${GGML_PATH}/include
${GGML_PATH}/src
)
# miniaudio uses dlopen on Linux for ALSA/PulseAudio/JACK backend discovery
if(UNIX AND NOT APPLE)
target_link_libraries(rs-asr-vad-online PRIVATE ${CMAKE_DL_LIBS})
target_link_libraries(rs-asr-online PRIVATE ${CMAKE_DL_LIBS})
target_link_libraries(rs-tts-online PRIVATE ${CMAKE_DL_LIBS})
target_link_libraries(rs-kws PRIVATE ${CMAKE_DL_LIBS})
endif()
install(TARGETS rs-asr-offline
RUNTIME DESTINATION bin
)
install(TARGETS rs-asr-vad-online
RUNTIME DESTINATION bin
)
install(TARGETS rs-tts-offline
RUNTIME DESTINATION bin
)
install(TARGETS rs-tts-online
RUNTIME DESTINATION bin
)
install(TARGETS rs-speaker-id
RUNTIME DESTINATION bin
)
install(TARGETS rs-kws
RUNTIME DESTINATION bin
)
install(TARGETS rs-tts-split
RUNTIME DESTINATION bin
)
install(TARGETS rs-quantize
RUNTIME DESTINATION bin
)
install(TARGETS rs-imatrix
RUNTIME DESTINATION bin
)
install(TARGETS rs-server
RUNTIME DESTINATION bin
)
install(TARGETS rs-aoq-sim
RUNTIME DESTINATION bin
)
# Runtime data (G2P dictionaries, WeText FSTs, cppjieba dict).
# Installed into share/rapidspeech/ to match the FHS layout the runtime
# discovery (rs::find_data_dir) checks first relative to <exe>/.
install(DIRECTORY rapidspeech/data/
DESTINATION share/rapidspeech
FILES_MATCHING
PATTERN "*"
PATTERN ".*" EXCLUDE
)
if (RS_ENABLE_PYTHON)
install(FILES examples/server/webui.html
DESTINATION server
)
endif()
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/cppjieba/dict)
install(DIRECTORY third_party/cppjieba/dict/
DESTINATION share/rapidspeech/cppjieba/dict
FILES_MATCHING
PATTERN "*"
PATTERN ".*" EXCLUDE
)
endif()
endif()
# --- Tests ---
if (RS_BUILD_TESTS)
message(STATUS "Tests are enabled.")
enable_testing()
set(TEST_MODULES
audio_frontend
cosyvoice3_e2e
kokoro_metal_convt
kokoro_en_g2p
voxcpm2_deconv
wetext_normalizer
realtime_protocol
)
foreach(module ${TEST_MODULES})
set(test_target "test_${module}")
set(test_source "tests/${test_target}.cpp")
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${test_source}")
add_executable(${test_target} ${test_source})
target_include_directories(${test_target} PRIVATE rapidspeech/src)
target_link_libraries(${test_target} PRIVATE rapidspeech-core ggml)
# test_kokoro_metal_convt directly exercises the Metal kernel class
# whose symbols are hidden inside librapidspeech-core. Compile the
# .mm in directly so the linker can see them.
if (module STREQUAL "kokoro_metal_convt" AND APPLE)
target_sources(${test_target} PRIVATE rapidspeech/src/arch/kokoro_metal.mm)
set_source_files_properties(rapidspeech/src/arch/kokoro_metal.mm
TARGET_DIRECTORY ${test_target}
PROPERTIES COMPILE_FLAGS "-x objective-c++ -fobjc-arc")
target_link_libraries(${test_target} PRIVATE "-framework Foundation" "-framework Metal")
endif()
# WeTextNormalizer symbols are hidden in librapidspeech-core
# (CXX_VISIBILITY_PRESET hidden). Compile the wrapper TU
# directly into the test binary and link the OpenFST-backed
# static lib so the wrapper resolves.
if (module STREQUAL "wetext_normalizer")
target_sources(${test_target} PRIVATE
rapidspeech/src/frontend/wetext_normalizer.cpp)
target_link_libraries(${test_target} PRIVATE wetext_processor)
endif()
# EnG2P symbols are likewise hidden inside librapidspeech-core;
# compile the TU directly into the test so the linker sees them.
if (module STREQUAL "kokoro_en_g2p")
target_sources(${test_target} PRIVATE
rapidspeech/src/frontend/kokoro_g2p_en.cpp)
endif()
# AudioProcessor methods are hidden inside librapidspeech-core on
# Unix-like shared builds (CXX_VISIBILITY_PRESET hidden). Compile
# the TU directly into those test binaries. On Windows the class is
# exported from the DLL, so compiling the TU again causes LNK2005.
if (module STREQUAL "audio_frontend")
if (WIN32)
target_sources(${test_target} PRIVATE
rapidspeech/src/frontend/fftsg.c)
else()
target_sources(${test_target} PRIVATE
rapidspeech/src/frontend/audio_processor.cpp
rapidspeech/src/frontend/fftsg.c)
endif()
endif()
if (module STREQUAL "realtime_protocol")
target_include_directories(${test_target} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/rapidspeech/src
)
target_sources(${test_target} PRIVATE
rapidspeech/src/realtime/rs_realtime_protocol.cpp
rapidspeech/src/realtime/rs_webrtc_transport.cpp
)
endif()
add_test(NAME ${test_target} COMMAND ${test_target})
# On Windows the loader resolves DLLs from the exe's own
# directory or PATH — there's no rpath. CMake's multi-config
# generator puts test exes in build/Release/ but shared
# dependencies (ggml*.dll, etc.) land in build/bin/Release/,
# so launching the test fails with STATUS_DLL_NOT_FOUND
# (0xC0000135). Copy all transitive DLL deps next to the exe.
# $<TARGET_RUNTIME_DLLS:> requires CMake 3.21+.
if (WIN32 AND CMAKE_VERSION VERSION_GREATER_EQUAL 3.21)
add_custom_command(TARGET ${test_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_RUNTIME_DLLS:${test_target}>
$<TARGET_FILE_DIR:${test_target}>
COMMAND_EXPAND_LISTS)
endif()
# test_cosyvoice3_e2e exits 77 (GNU automake skip convention) when
# RS_CV3_E2E_MODEL is unset. Teach ctest to treat that as Skipped
# instead of Failed.
if (module STREQUAL "cosyvoice3_e2e")
set_tests_properties(${test_target} PROPERTIES
SKIP_RETURN_CODE 77)
endif()
endif()
endforeach()
endif()