cmake_minimum_required(VERSION 3.14) set(CMAKE_VERBOSE_MAKEFILE off) set(CMAKE_DEBUG_POSTFIX d) project( piper_phonemize VERSION 1.2.0 DESCRIPTION "Phonemization library for Piper text to speech system" HOMEPAGE_URL "https://github.com/rhasspy/piper-phonemize" LANGUAGES CXX ) option(PHONEMIZE_BUILD_STATIC "Build as static libs" TRUE) option(PHONEMIZE_BUILD_LIB_ONLY "Build only libs, no exe" TRUE) if (PHONEMIZE_BUILD_STATIC) set(BUILD_PHONEMIZE_LIB_TYPE STATIC) else() set(BUILD_PHONEMIZE_LIB_TYPE SHARED) endif() if(MSVC) # Force compiler to use UTF-8 for IPA constants add_compile_options("$<$:/utf-8>") add_compile_options("$<$:/utf-8>") elseif(NOT APPLE) # Linux flags string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wl,-rpath,'$ORIGIN'") string(APPEND CMAKE_C_FLAGS " -Wall -Wextra") endif() add_library( piper_phonemize ${BUILD_PHONEMIZE_LIB_TYPE} src/phonemize.cpp src/phoneme_ids.cpp src/tashkeel.cpp src/shared.cpp ) set_target_properties(piper_phonemize PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) include(ExternalProject) # ---- onnxruntime --- # Look for onnxruntime files in /lib if(NOT DEFINED ONNXRUNTIME_DIR) set(ONNXRUNTIME_DIR ${CMAKE_INSTALL_PREFIX}) #use short path to prevent too long path on windows set(ONNXRUNTIME_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx") if(NOT DEFINED ONNXRUNTIME_VERSION) set(ONNXRUNTIME_VERSION "1.14.1") endif() if(RPI) set(BUILD_ONNX_RPI ON) else() set(BUILD_ONNX_RPI OFF) endif() # CMAKE_ARGS -Donnxruntime_BUILD_FOR_NATIVE_MACHINE:BOOL=OFF # CMAKE_ARGS -Donnxruntime_USE_ACL:BOOL=${BUILD_ONNX_RPI} # CMAKE_ARGS -Donnxruntime_USE_ARMNN:BOOL=${BUILD_ONNX_RPI} #CMAKE_ARGS -Donnxruntime_USE_XNNPACK:BOOL=${BUILD_ONNX_RPI} #CMAKE_ARGS -Donnxruntime_ENABLE_CPU_FP16_OPS:BOOL=${BUILD_ONNX_RPI} ExternalProject_Add( onnxruntime_external PREFIX "${ONNXRUNTIME_BUILD_DIR}" SOURCE_DIR "${ONNXRUNTIME_BUILD_DIR}/../../autobuild/onnxruntime-1.14.1/" SOURCE_SUBDIR "cmake" CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} CMAKE_ARGS -DCMAKE_DEBUG_POSTFIX=d CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${ONNXRUNTIME_DIR} CMAKE_ARGS -DBUILD_TESTING:BOOL=OFF CMAKE_ARGS -Donnxruntime_ENABLE_LTO:BOOL=${BUILD_ONNX_RPI} CMAKE_ARGS -Donnxruntime_USE_OPENMP:BOOL=${BUILD_ONNX_RPI} CMAKE_ARGS -Donnxruntime_BUILD_UNIT_TESTS:BOOL=OFF CMAKE_ARGS -Donnxruntime_BUILD_SHARED_LIB:BOOL=OFF CMAKE_ARGS -Donnxruntime_ENABLE_TRAINING:BOOL=OFF CMAKE_ARGS -Donnxruntime_DISABLE_RTTI:BOOL=ON CMAKE_ARGS -Donnxruntime_ENABLE_PYTHON:BOOL=OFF ) #URL "https://github.com/microsoft/onnxruntime/archive/refs/tags/v${ONNXRUNTIME_VERSION}.zip" #PATCH_COMMAND ${CMAKE_COMMAND} -E copy "$ENV{SCOL_DEPENDENCIES_PATH}/piper/patch/onnxruntime-CMakeLists.txt" /cmake/CMakeLists.txt add_dependencies(piper_phonemize onnxruntime_external) # if(NOT DEFINED ONNXRUNTIME_VERSION) # set(ONNXRUNTIME_VERSION "1.14.1") # endif() # # if(WIN32) # # Windows x86-64 # set(ONNXRUNTIME_PREFIX "onnxruntime-win-x64-${ONNXRUNTIME_VERSION}") # set(ONNXRUNTIME_EXT "zip") # elseif (APPLE) # if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) # # MacOS x86-64 # set(ONNXRUNTIME_PREFIX "onnxruntime-osx-x86_64-${ONNXRUNTIME_VERSION}") # elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm64) # # MacOS Apple Silicon # set(ONNXRUNTIME_PREFIX "onnxruntime-osx-arm64-${ONNXRUNTIME_VERSION}") # else() # message(FATAL_ERROR "Unsupported architecture for onnxruntime") # endif() # # set(ONNXRUNTIME_EXT "tgz") # else() # if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) # # Linux x86-64 # set(ONNXRUNTIME_PREFIX "onnxruntime-linux-x64-${ONNXRUNTIME_VERSION}") # elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) # # Linux ARM 64-bit # set(ONNXRUNTIME_PREFIX "onnxruntime-linux-aarch64-${ONNXRUNTIME_VERSION}") # elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l) # # Linux ARM 32-bit # set(ONNXRUNTIME_PREFIX "onnxruntime-linux-arm32-${ONNXRUNTIME_VERSION}") # set(ONNXRUNTIME_URL "https://github.com/synesthesiam/prebuilt-apps/releases/download/v1.0/onnxruntime-linux-arm32-${ONNXRUNTIME_VERSION}.tgz") # else() # message(FATAL_ERROR "Unsupported architecture for onnxruntime") # endif() # # set(ONNXRUNTIME_EXT "tgz") # endif() # # if(NOT DEFINED ONNXRUNTIME_URL) # set(ONNXRUNTIME_URL "https://github.com/microsoft/onnxruntime/releases/download/v${ONNXRUNTIME_VERSION}/${ONNXRUNTIME_PREFIX}.${ONNXRUNTIME_EXT}") # endif() # # set(ONNXRUNTIME_FILENAME "${ONNXRUNTIME_PREFIX}.${ONNXRUNTIME_EXT}") # set(ONNXRUNTIME_DIR "${CMAKE_CURRENT_LIST_DIR}/lib/${ONNXRUNTIME_PREFIX}") # # if(NOT EXISTS "${ONNXRUNTIME_DIR}") # if(NOT EXISTS "download/${ONNXRUNTIME_FILENAME}") # # Download onnxruntime release # message("Downloading ${ONNXRUNTIME_URL}") # file(DOWNLOAD "${ONNXRUNTIME_URL}" "download/${ONNXRUNTIME_FILENAME}") # endif() # # # Extract .zip or .tgz to a directory like lib/onnxruntime-linux-x64-1.14.1/ # file(ARCHIVE_EXTRACT INPUT "download/${ONNXRUNTIME_FILENAME}" DESTINATION "${CMAKE_CURRENT_LIST_DIR}/lib") # endif() endif() # ---- espeak-ng --- if(NOT DEFINED ESPEAK_NG_DIR) set(ESPEAK_NG_DIR ${CMAKE_INSTALL_PREFIX}) if(WIN32 AND NOT ANDROID) set(BUILD_ESPEAK_INSTALL_DATA ON) set(BUILD_ESPEAK_BIN ON) else() set(BUILD_ESPEAK_INSTALL_DATA OFF) set(BUILD_ESPEAK_BIN OFF) endif() ExternalProject_Add( espeak_ng_external PREFIX "${CMAKE_CURRENT_BINARY_DIR}/../../../e" URL "https://github.com/rhasspy/espeak-ng/archive/0f65aa301e0d6bae5e172cc74197d32a6182200f.zip" CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} CMAKE_ARGS -DCMAKE_DEBUG_POSTFIX=d CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${ESPEAK_NG_DIR} CMAKE_ARGS -DUSE_ASYNC:BOOL=OFF CMAKE_ARGS -DBUILD_TESTING:BOOL=OFF CMAKE_ARGS -DBUILD_ESPEAK_DATA:BOOL=${BUILD_ESPEAK_INSTALL_DATA} CMAKE_ARGS -DBUILD_ESPEAK_NG_EXE:BOOL=${BUILD_ESPEAK_BIN} CMAKE_ARGS -DBUILD_ESPEAK_NG_TESTS:BOOL=OFF CMAKE_ARGS -DBUILD_SHARED_LIBS:BOOL=OFF CMAKE_ARGS -DUSE_MBROLA:BOOL=OFF CMAKE_ARGS -DUSE_LIBSONIC:BOOL=OFF CMAKE_ARGS -DUSE_LIBPCAUDIO:BOOL=OFF CMAKE_ARGS -DUSE_KLATT:BOOL=OFF CMAKE_ARGS -DUSE_SPEECHPLAYER:BOOL=OFF CMAKE_ARGS -DEXTRA_cmn:BOOL=ON CMAKE_ARGS -DEXTRA_ru:BOOL=ON CMAKE_ARGS -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} -D_FILE_OFFSET_BITS=64 PATCH_COMMAND ${CMAKE_COMMAND} -E copy "$ENV{SCOL_DEPENDENCIES_PATH}/piper/patch/espeak-ng_main_CMakeLists.txt" /CMakeLists.txt && ${CMAKE_COMMAND} -E copy "$ENV{SCOL_DEPENDENCIES_PATH}/piper/patch/espeak-ng_CMakeLists.txt" /src/libespeak-ng/CMakeLists.txt && ${CMAKE_COMMAND} -E copy "$ENV{SCOL_DEPENDENCIES_PATH}/piper/patch/espeak-ng_src_CMakeLists.txt" /src/CMakeLists.txt && ${CMAKE_COMMAND} -E copy "$ENV{SCOL_DEPENDENCIES_PATH}/piper/patch/espeak-ng_deps.cmake" /cmake/deps.cmake && ${CMAKE_COMMAND} -E copy "$ENV{SCOL_DEPENDENCIES_PATH}/piper/patch/speak_lib.h" /src/include/espeak-ng/speak_lib.h ) add_dependencies(piper_phonemize espeak_ng_external) endif() target_compile_definitions(piper_phonemize PUBLIC LIBESPEAK_NG_STATIC) # ---- Declare library ---- set(DEPS_Release_DIR "") set(DEPS_Debug_DIR "") if(WIN32) set(DEPS_Release_DIR "/Release") set(DEPS_Debug_DIR "/Debug") endif() function(addImportedLibrary name dir) message("Add external lib: ${name} from ${dir}${DEPS_Release_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX} to ${CMAKE_INSTALL_PREFIX}/lib${DEPS_Debug_DIR}") if(WIN32) install(FILES ${dir}${DEPS_Debug_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_DEBUG_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX} CONFIGURATIONS Debug DESTINATION ${CMAKE_INSTALL_PREFIX}/lib${DEPS_Debug_DIR}) install(FILES ${dir}${DEPS_Release_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX} CONFIGURATIONS Release DESTINATION ${CMAKE_INSTALL_PREFIX}/lib${DEPS_Release_DIR}) else() install(FILES ${dir}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) endif() #if (EXISTS "${dir}${DEPS_Release_DIR}/${name}${CMAKE_DEBUG_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}") # #else() # message("Dependencie on import ${name} not found in ${dir}${DEPS_Release_DIR}/${name}${CMAKE_DEBUG_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}") #endif() endfunction() addImportedLibrary(onnx "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/onnx-build") addImportedLibrary(onnx_proto "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/onnx-build") #addImportedLibrary(libprotobuf-lite "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/protobuf-build") addImportedLibrary(absl_base "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/abseil_cpp-build/absl/base") addImportedLibrary(absl_throw_delegate "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/abseil_cpp-build/absl/base") addImportedLibrary(absl_raw_hash_set "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/abseil_cpp-build/absl/container") addImportedLibrary(absl_hash "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/abseil_cpp-build/absl/hash") addImportedLibrary(absl_city "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/abseil_cpp-build/absl/hash") addImportedLibrary(absl_low_level_hash "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/abseil_cpp-build/absl/hash") addImportedLibrary(ucd "${CMAKE_CURRENT_BINARY_DIR}/../../../e/src/espeak_ng_external-build/src/ucd-tools") #addImportedLibrary(re2 "${CMAKE_CURRENT_BINARY_DIR}/../../../onnx/src/onnxruntime_external-build/_deps/re2-build") set(PHONEMIZE_DEPS_LIBS espeak-ng onnxruntime_session onnxruntime_optimizer onnxruntime_providers onnxruntime_util onnxruntime_framework onnxruntime_graph onnxruntime_common onnxruntime_mlas onnxruntime_flatbuffers onnx onnx_proto absl_base absl_throw_delegate absl_raw_hash_set absl_hash absl_city absl_low_level_hash re2 ucd fmt spdlog protobuf-lite) #if (RPI) #set(PHONEMIZE_DEPS_LIBS ${PHONEMIZE_DEPS_LIBS} onnxruntime_providers_xnnpack XNNPACK pthreadpool) #endif() foreach(DEP IN LISTS PHONEMIZE_DEPS_LIBS) if (NOT EXISTS ${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${DEP}${CMAKE_STATIC_LIBRARY_SUFFIX}) set(PHONEMIZE_IMPORTED_LIBS ${PHONEMIZE_IMPORTED_LIBS} optimized ${CMAKE_INSTALL_PREFIX}/lib${DEPS_Release_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${DEP}${CMAKE_STATIC_LIBRARY_SUFFIX} debug ${CMAKE_INSTALL_PREFIX}/lib${DEPS_Debug_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${DEP}${CMAKE_DEBUG_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}) else() set(PHONEMIZE_IMPORTED_LIBS ${PHONEMIZE_IMPORTED_LIBS} optimized ${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${DEP}${CMAKE_STATIC_LIBRARY_SUFFIX} debug ${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${DEP}${CMAKE_DEBUG_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}) endif() endforeach() if (CMAKE_SYSTEM_NAME STREQUAL "Android") set(PHONEMIZE_IMPORTED_LIBS ${PHONEMIZE_IMPORTED_LIBS} nsync_cpp cpuinfo clog log) elseif (NOT WIN32) set(PHONEMIZE_IMPORTED_LIBS ${PHONEMIZE_IMPORTED_LIBS} nsync_cpp cpuinfo) endif() if(NOT WIN32) find_package(Threads) set(PHONEMIZE_IMPORTED_LIBS ${PHONEMIZE_IMPORTED_LIBS} Threads::Threads) endif() target_include_directories( piper_phonemize PUBLIC "$" ${ESPEAK_NG_DIR}/include ${ONNXRUNTIME_DIR}/include/onnxruntime/core/session ${ONNXRUNTIME_DIR}/include ) target_link_directories( piper_phonemize PUBLIC ${ESPEAK_NG_DIR}/lib ${ONNXRUNTIME_DIR}/lib ) message("onnxruntime_EXTERNAL_LIBRARIES: ${onnxruntime_EXTERNAL_LIBRARIES}") target_link_libraries(piper_phonemize PUBLIC ${PHONEMIZE_IMPORTED_LIBS} ) target_compile_features(piper_phonemize PUBLIC cxx_std_17) # ---- Declare executable ---- if (NOT PHONEMIZE_BUILD_LIB_ONLY) add_executable(piper_phonemize_exe src/main.cpp src/phoneme_ids.cpp) if(NOT WIN32) set_property(TARGET piper_phonemize_exe PROPERTY OUTPUT_NAME piper_phonemize) endif() target_compile_features(piper_phonemize_exe PUBLIC cxx_std_17) target_include_directories( piper_phonemize_exe PUBLIC "$" ${ESPEAK_NG_DIR}/include ${ONNXRUNTIME_DIR}/include/onnxruntime/core/session ${ONNXRUNTIME_DIR}/include ) target_link_directories( piper_phonemize_exe PUBLIC ${ESPEAK_NG_DIR}/lib ${ONNXRUNTIME_DIR}/lib ) target_link_libraries(piper_phonemize_exe PUBLIC piper_phonemize espeak-ng onnxruntime ) endif() # ---- Declare test ---- if(BUILD_TESTING) include(CTest) enable_testing() add_executable(test_piper_phonemize src/test.cpp src/phoneme_ids.cpp) add_test( NAME test_piper_phonemize COMMAND test_piper_phonemize "${ESPEAK_NG_DIR}/share/espeak-ng-data" "${CMAKE_SOURCE_DIR}/etc/libtashkeel_model.ort" ) target_compile_features(test_piper_phonemize PUBLIC cxx_std_17) target_include_directories( test_piper_phonemize PUBLIC "$" ${ESPEAK_NG_DIR}/include ) target_link_directories( test_piper_phonemize PUBLIC ${ESPEAK_NG_DIR}/lib ) target_link_libraries(test_piper_phonemize PUBLIC piper_phonemize espeak-ng ) endif() # ---- Declare install targets ---- include(GNUInstallDirs) #install deps install( TARGETS piper_phonemize LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) install( DIRECTORY ${CMAKE_SOURCE_DIR}/src/ DESTINATION ${CMAKE_INSTALL_PREFIX}/include/piper-phonemize FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp") if (NOT PHONEMIZE_BUILD_LIB_ONLY) install( TARGETS piper_phonemize_exe ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) endif() install( FILES ${CMAKE_SOURCE_DIR}/etc/libtashkeel_model.ort TYPE DATA)