-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
335 lines (283 loc) · 13.2 KB
/
Copy pathCMakeLists.txt
File metadata and controls
335 lines (283 loc) · 13.2 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
# Copyright (C) 2024-2025 Tong Qiu (tong.qiu@intel.com)
# SPDX-License-Identifier: Apache-2.0
#
cmake_minimum_required(VERSION 3.25)
project(MeloTTS)
# Set C++ Standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# for env witout setting utf-8 for Chinese
if(WIN32)
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>") #https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=msvc-170
#add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/bigobj>")
endif()
if(WIN32) #default compiler on win is msvc. I've not verifed it with clang-cl yet.
add_definitions(-DNOMINMAX) # Otherwise, std::max() and std::min() won't work
add_compile_options(/Zc:__cplusplus) # Add /Zc:__cplusplus flag for Visual Studio to properly set the __cplusplus macro
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Choose the configuration types" FORCE)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL") # refer to https://developercommunity.visualstudio.com/t/debug-build-works-but-release-build-failsas-well-a/428160
endif()
# Workaround for an MSVC compiler issue in some versions of Visual Studio 2022.
# The issue involves a null dereference to a mutex. For details, refer to link https://github.com/microsoft/STL/wiki/Changelog#vs-2022-1710
if(MSVC AND MSVC_VERSION GREATER_EQUAL 1930 AND MSVC_VERSION LESS 1941)
add_compile_definitions(_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)
endif()
find_package(OpenVINOGenAI REQUIRED
PATHS
${INTEL_OPENVINO_DIR} # get the environment variable
NO_CMAKE_FIND_ROOT_PATH
)
# Include directories
include_directories(${CMAKE_SOURCE_DIR}/src)
# Source files
set(SOURCE_FILES
melo.cpp
src/openvino_model_base.cpp
src/openvino_tokenizer.cpp
src/utils.cpp
src/bert.cpp
src/openvoice_tts.cpp
src/tts.cpp
src/language_modules/cmudict.cpp
src/language_modules/chinese_mix.cpp
src/language_modules/english.cpp
src/language_modules/tone_sandhi.cpp
src/language_modules/language_module_base.cpp
src/text_normalization/text_normalization.cpp
src/text_normalization/char_convert.cpp
src/text_normalization/chronology.cpp
src/text_normalization/constants.cpp
src/text_normalization/num.cpp
src/text_normalization/phonecode.cpp
src/text_normalization/quantifier.cpp
${CMAKE_SOURCE_DIR}/thirdParty/cppinyin/csrc/cppinyin.cc
${CMAKE_SOURCE_DIR}/thirdParty/cppinyin/csrc/cppinyin_csrc_utils.cc
src/text_normalization/text_normalization.cpp
src/text_normalization/char_convert.cpp
src/text_normalization/chronology.cpp
src/text_normalization/constants.cpp
src/text_normalization/num.cpp
src/text_normalization/phonecode.cpp
src/text_normalization/quantifier.cpp
src/deepfilternet/noisefilter.cpp
src/deepfilternet/deepfilter.cpp
src/deepfilternet/dfnet_model.cpp
src/deepfilternet/multiframe.cpp
src/mini-bart-g2p/mini-bart-g2p.cpp
src/text_normalization/text_normalization_eng.cpp
${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/cppinyin/csrc/cppinyin.cc
${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/cppinyin/csrc/cppinyin_csrc_utils.cc
)
# Header files
set(HEADER_FILES
src/parse_args.h
src/openvino_model_base.h
src/info_data.h
src/openvino_tokenizer.h
src/utils.h
src/bert.h
src/openvoice_tts.h
src/tts.h
src/language_modules/cmudict.h
src/language_modules/chinese_mix.h
src/language_modules/english.h
src/language_modules/tone_sandhi.h
src/language_modules/language_module_base.h
src/text_normalization/text_normalization.h
src/text_normalization/char_convert.h
src/text_normalization/chronology.h
src/text_normalization/constant.h
src/text_normalization/number.h
src/text_normalization/phonecode.h
src/text_normalization/quantifier.h
src/text_normalization/text_normalization_eng.h
${CMAKE_SOURCE_DIR}/thirdParty/cppinyin/csrc/cppinyin.h
${CMAKE_SOURCE_DIR}/thirdParty/cppinyin/csrc/cppinyin_csrc_utils.h
src/deepfilternet/noisefilter.h
src/deepfilternet/deepfilter.h
src/deepfilternet/multiframe.h
src/deepfilternet/dfnet_model.h
src/mini-bart-g2p/mini-bart-g2p.h
${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/cppinyin/csrc/cppinyin.h
${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/cppinyin/csrc/cppinyin_csrc_utils.h
)
# Define the executable
add_executable(meloTTS_ov ${SOURCE_FILES} ${HEADER_FILES})
# Whether use deep filter net; We do not support this feature on Linux now.
option(USE_DEEPFILTERNET "Enable DeepFilterNet support" ON)
# Disable DeepFilterNet on MacOS for now due to linking issues
if(APPLE)
set(USE_DEEPFILTERNET OFF)
message(STATUS "DeepFilterNet is disabled on macOS")
endif()
if(USE_DEEPFILTERNET)
add_compile_definitions(USE_DEEPFILTERNET)
message(STATUS "DeepFilterNet is enabled")
else()
message(STATUS "DeepFilterNet is disabled")
endif()
if(USE_DEEPFILTERNET AND WIN32)
add_compile_definitions(USE_DEEPFILTERNET)
# unzip libtorch
set(LIBTORCH_DIR ${CMAKE_SOURCE_DIR}/thirdParty/libtorch)
if(NOT EXISTS ${LIBTORCH_DIR})
message(STATUS "libtorch directory does not exist, will perform unzip.")
add_custom_target(unZip ALL)
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar xzf libtorch.tar.gz
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/thirdParty
)
else()
message(STATUS "libtorch folder already exists, skipping unzip.")
endif()
# COPY torch runtime to exe folder
set(RELEASE_DIR ${CMAKE_BINARY_DIR}/Release)
#set(DEBUG_DIR ${CMAKE_BINARY_DIR}/Debug)
file(MAKE_DIRECTORY ${RELEASE_DIR})
set(DLLS torch.dll torch_cpu.dll asmjit.dll fbgemm.dll c10.dll uv.dll libiomp5md.dll)
foreach(dll ${DLLS})
message("Copying ${dll} from ${LIBTORCH_DIR}/lib to ${RELEASE_DIR}")
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${LIBTORCH_DIR}/lib/${dll} ${RELEASE_DIR}
#COMMAND ${CMAKE_COMMAND} -E copy_if_different ${LIBTORCH_DIR}/lib/${dll} ${DEBUG_DIR}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endforeach()
# Find libraries
find_library(TORCH_LIB torch PATHS ${LIBTORCH_DIR}/lib)
if (NOT TORCH_LIB)
message(FATAL_ERROR "torch.lib not found")
endif()
find_library(C10_LIB c10 PATHS ${LIBTORCH_DIR}/lib)
if (NOT C10_LIB)
message(FATAL_ERROR "c10.lib not found")
endif()
find_library(TORCH_CPU_LIB torch_cpu PATHS ${LIBTORCH_DIR}/lib)
if (NOT TORCH_CPU_LIB)
message(FATAL_ERROR "torch_cpu.lib not found")
endif()
target_link_directories(meloTTS_ov PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libtorch/lib")
target_include_directories(meloTTS_ov PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libtorch/include)
target_include_directories(meloTTS_ov PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libtorch/include/torch/csrc/api/include)
if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
set(ADDITIONAL_LIBRARY_DEPENDENCIES "torch.lib" "c10.lib" "torch_cpu.lib")
target_link_libraries(meloTTS_ov PRIVATE ${ADDITIONAL_LIBRARY_DEPENDENCIES})
endif()
endif() # end USE_DEEPFILTERNET AND WIN32
if(USE_DEEPFILTERNET AND UNIX)
message(STATUS "Checking if libtorch is already downloaded and extracted")
set(LIBTORCH_ZIP ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libtorch-cxx11-abi-shared-with-deps-2.6.0*cpu.zip)
set(LIBTORCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libtorch)
if(NOT EXISTS ${LIBTORCH_ZIP} AND NOT EXISTS ${LIBTORCH_DIR})
message(STATUS "Downloading and extracting libtorch for Linux")
file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty)
execute_process(
COMMAND wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.6.0%2Bcpu.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty
RESULT_VARIABLE download_result
)
if(NOT download_result EQUAL 0)
message(FATAL_ERROR "Failed to download libtorch.")
endif()
execute_process(
COMMAND unzip libtorch-cxx11-abi-shared-with-deps-2.6.0*cpu.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty
RESULT_VARIABLE unzip_result
)
if(NOT unzip_result EQUAL 0)
message(FATAL_ERROR "Failed to unzip libtorch.")
endif()
message(STATUS "libtorch downloaded and extracted successfully")
else()
message(STATUS "libtorch already downloaded and extracted, skipping download")
endif()
set(CMAKE_PREFIX_PATH ${LIBTORCH_DIR})
find_package(Torch REQUIRED)
target_link_libraries(meloTTS_ov PRIVATE "${TORCH_LIBRARIES}")
# Copy torch runtime to executable folder
file(GLOB_RECURSE LIBSO_FILES ${LIBTORCH_DIR}/lib/*.so)
set(RELEASE_DIR ${CMAKE_BINARY_DIR})
foreach(SO_FILE ${LIBSO_FILES})
message("Copying ${SO_FILE} from ${LIBTORCH_DIR}/lib to ${RELEASE_DIR}")
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${LIBTORCH_DIR}/lib/${dll} ${RELEASE_DIR}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endforeach()
endif() # end USE_DEEPFILTERNET AND UNIX
# Special handling for macOS platform
if(APPLE)
message(STATUS "Configuring build for macOS")
# Reset all linker flags on macOS to avoid Linux-specific flags
set(CMAKE_EXE_LINKER_FLAGS "")
set(CMAKE_SHARED_LINKER_FLAGS "")
set(CMAKE_MODULE_LINKER_FLAGS "")
# Enable verbose output from the linker to help diagnose issues
set(CMAKE_VERBOSE_MAKEFILE ON)
# Override the link rule to not use unsupported flags
set(CMAKE_CXX_LINK_EXECUTABLE
"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
# Handle libtorch location for macOS if using DeepFilterNet
if(USE_DEEPFILTERNET)
set(LIBTORCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libtorch)
# Directly include and link libtorch
target_include_directories(meloTTS_ov PRIVATE ${LIBTORCH_DIR}/include)
target_include_directories(meloTTS_ov PRIVATE ${LIBTORCH_DIR}/include/torch/csrc/api/include)
# Find the direct library paths
find_library(TORCH_LIB torch PATHS ${LIBTORCH_DIR}/lib)
find_library(C10_LIB c10 PATHS ${LIBTORCH_DIR}/lib)
find_library(TORCH_CPU_LIB torch_cpu PATHS ${LIBTORCH_DIR}/lib)
if(TORCH_LIB AND C10_LIB AND TORCH_CPU_LIB)
message(STATUS "Found torch libraries directly:")
message(STATUS " TORCH_LIB: ${TORCH_LIB}")
message(STATUS " C10_LIB: ${C10_LIB}")
message(STATUS " TORCH_CPU_LIB: ${TORCH_CPU_LIB}")
# Link directly with the found libraries
target_link_libraries(meloTTS_ov PRIVATE ${TORCH_LIB} ${C10_LIB} ${TORCH_CPU_LIB})
else()
# Fallback to standard approach
set(CMAKE_PREFIX_PATH ${LIBTORCH_DIR})
find_package(Torch REQUIRED)
# Override any problematic flags from Torch config
set(TORCH_LIBRARIES_CLEAN "")
foreach(lib ${TORCH_LIBRARIES})
string(REGEX REPLACE "--no-as-needed|--as-needed" "" lib_clean "${lib}")
list(APPEND TORCH_LIBRARIES_CLEAN ${lib_clean})
endforeach()
# Debug Torch flags
message(STATUS "Original TORCH_LIBRARIES: ${TORCH_LIBRARIES}")
message(STATUS "Cleaned TORCH_LIBRARIES: ${TORCH_LIBRARIES_CLEAN}")
target_link_libraries(meloTTS_ov PRIVATE ${TORCH_LIBRARIES_CLEAN})
endif()
endif()
endif()
# Define DEBUG macro for Debug configuration
target_compile_definitions(meloTTS_ov PRIVATE "$<$<CONFIG:DEBUG>:DEBUG>")
target_include_directories(meloTTS_ov PRIVATE ${CMAKE_SOURCE_DIR}/thirdParty/cppjieba)
target_include_directories(meloTTS_ov PRIVATE ${CMAKE_SOURCE_DIR}/thirdParty/cppjieba/include)
target_include_directories(meloTTS_ov PRIVATE ${CMAKE_SOURCE_DIR}/thirdParty/cppinyin/csrc)
target_include_directories(meloTTS_ov PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/cppjieba)
target_include_directories(meloTTS_ov PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/cppjieba/include)
target_include_directories(meloTTS_ov PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/cppinyin/csrc)
target_link_libraries(meloTTS_ov
PRIVATE openvino::genai # Link OpenVINO.GenAI Runtime privately
)
if (UNIX)
target_link_libraries(meloTTS_ov PRIVATE pthread)
endif()
option(USE_BERT_NPU "Set Bert on NPU" OFF)
if(USE_BERT_NPU)
message(STATUS "USE BERT on NPU: We need a static shape of the model.")
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/ov_models/bert_ZH_int8.bin
${CMAKE_SOURCE_DIR}/ov_models/bert_ZH_static_int8.bin
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
message("Copying bert_ZH_int8.bin to bert_ZH_int8.bin if bert_ZH_static_int8.bin does not exist")
endif()
option(ENABLE_TEST "Enable tests" OFF) # Disable the tests by default
if (${ENABLE_TEST})
enable_testing()
add_subdirectory(tests)
endif()