Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: Allow to build with system libraries and other improvements #31

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 184 additions & 114 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ project(
LANGUAGES CXX
)

option(BUILD_EXTERNAL "Build external dependencies" OFF)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(piper_phonemize-config-version.cmake
VERSION ${piper_phonemize_VERSION}
COMPATIBILITY SameMajorVersion)

if(MSVC)
# Force compiler to use UTF-8 for IPA constants
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

elseif(NOT APPLE)
# Linux flags
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wl,-rpath,'$ORIGIN'")
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra")
string(APPEND CMAKE_C_FLAGS " -Wall -Wextra")
endif()

Expand All @@ -37,110 +43,133 @@ set_target_properties(piper_phonemize PROPERTIES
# ---- onnxruntime ---

# Look for onnxruntime files in <root>/lib
if(NOT DEFINED ONNXRUNTIME_DIR)
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")
if (BUILD_EXTERNAL)
if(NOT DEFINED ONNXRUNTIME_DIR)
if(NOT DEFINED ONNXRUNTIME_VERSION)
set(ONNXRUNTIME_VERSION "1.14.1")
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")
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()
message(FATAL_ERROR "Unsupported architecture for onnxruntime")
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()

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()

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}")

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()

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}")
# 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()

# 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()
else()
find_package(onnxruntime REQUIRED CONFIG)
endif()

# ---- espeak-ng ---

if(NOT DEFINED ESPEAK_NG_DIR)
set(ESPEAK_NG_DIR "${CMAKE_CURRENT_BINARY_DIR}/ei")

include(ExternalProject)
ExternalProject_Add(
espeak_ng_external
PREFIX "${CMAKE_CURRENT_BINARY_DIR}/e"
URL "https://github.com/rhasspy/espeak-ng/archive/0f65aa301e0d6bae5e172cc74197d32a6182200f.zip"
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${ESPEAK_NG_DIR}
CMAKE_ARGS -DUSE_ASYNC:BOOL=OFF
CMAKE_ARGS -DBUILD_SHARED_LIBS:BOOL=ON
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="-D_FILE_OFFSET_BITS=64"
)
add_dependencies(piper_phonemize espeak_ng_external)
if (BUILD_EXTERNAL)
if(NOT DEFINED ESPEAK_NG_DIR)
set(ESPEAK_NG_DIR "${CMAKE_CURRENT_BINARY_DIR}/ei")

include(ExternalProject)
ExternalProject_Add(
espeak_ng_external
PREFIX "${CMAKE_CURRENT_BINARY_DIR}/e"
URL "https://github.com/rhasspy/espeak-ng/archive/0f65aa301e0d6bae5e172cc74197d32a6182200f.zip"
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${ESPEAK_NG_DIR}
CMAKE_ARGS -DUSE_ASYNC:BOOL=OFF
CMAKE_ARGS -DBUILD_SHARED_LIBS:BOOL=ON
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="-D_FILE_OFFSET_BITS=64"
)
add_dependencies(piper_phonemize espeak_ng_external)
endif()
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(espeak_ng REQUIRED IMPORTED_TARGET espeak-ng)
endif()


# ---- Declare library ----

add_library(piper::phonemize ALIAS piper_phonemize)

target_include_directories(
piper_phonemize PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
${ESPEAK_NG_DIR}/include
${ONNXRUNTIME_DIR}/include
)

target_link_directories(
piper_phonemize PUBLIC
${ESPEAK_NG_DIR}/lib
${ONNXRUNTIME_DIR}/lib
)
if (BUILD_EXTERNAL)
target_include_directories(
piper_phonemize PUBLIC
${ESPEAK_NG_DIR}/include
${ONNXRUNTIME_DIR}/include
)

target_link_libraries(
piper_phonemize
espeak-ng
onnxruntime
)
target_link_directories(
piper_phonemize PUBLIC
${ESPEAK_NG_DIR}/lib
${ONNXRUNTIME_DIR}/lib
)

target_link_libraries(
piper_phonemize
espeak-ng
onnxruntime
)
else()
target_link_libraries(
piper_phonemize PRIVATE
PkgConfig::espeak_ng
onnxruntime::onnxruntime
)
endif()

target_compile_features(piper_phonemize PUBLIC cxx_std_17)

Expand All @@ -152,23 +181,45 @@ if(NOT WIN32)
set_property(TARGET piper_phonemize_exe PROPERTY OUTPUT_NAME piper_phonemize)
endif()

if (APPLE)
set_target_properties(piper_phonemize_exe PROPERTIES
MACOSX_RPATH TRUE)
elseif(NOT MSVC)
# Linux
set_target_properties(piper_phonemize_exe PROPERTIES
BUILD_RPATH_USE_ORIGIN TRUE)
endif()

target_compile_features(piper_phonemize_exe PUBLIC cxx_std_17)

target_include_directories(
piper_phonemize_exe PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
${ESPEAK_NG_DIR}/include
)

target_link_directories(
piper_phonemize_exe PUBLIC
${ESPEAK_NG_DIR}/lib
)
if (BUILD_EXTERNAL)
target_include_directories(
piper_phonemize_exe PUBLIC
${ESPEAK_NG_DIR}/include
)

target_link_libraries(piper_phonemize_exe PUBLIC
piper_phonemize
espeak-ng
)
target_link_directories(
piper_phonemize_exe PUBLIC
${ESPEAK_NG_DIR}/lib
)

target_link_libraries(piper_phonemize_exe PUBLIC
piper_phonemize
espeak-ng
)
else()
target_link_libraries(
piper_phonemize_exe PRIVATE
piper::phonemize
PkgConfig::espeak_ng
onnxruntime::onnxruntime
)
endif()

# ---- Declare test ----

Expand All @@ -177,37 +228,54 @@ 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"
COMMAND test_piper_phonemize "${ESPEAK_NG_DIR}/share/espeak-ng-data" "${piper_phonemize_SOURCE_DIR}/etc/libtashkeel_model.ort"
)

target_compile_features(test_piper_phonemize PUBLIC cxx_std_17)

target_include_directories(
test_piper_phonemize PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
${ESPEAK_NG_DIR}/include
)

target_link_directories(
test_piper_phonemize PUBLIC
${ESPEAK_NG_DIR}/lib
)
if (BUILD_EXTERNAL)
target_include_directories(
test_piper_phonemize PUBLIC
${ESPEAK_NG_DIR}/include
)

target_link_libraries(test_piper_phonemize PUBLIC
piper_phonemize
espeak-ng
)
target_link_directories(
test_piper_phonemize PUBLIC
${ESPEAK_NG_DIR}/lib
)

target_link_libraries(test_piper_phonemize PUBLIC
piper_phonemize
espeak-ng
)
else()
target_link_libraries(
test_piper_phonemize PRIVATE
piper::phonemize
onnxruntime::onnxruntime
PkgConfig::espeak_ng
)
endif()

# ---- Declare install targets ----

include(GNUInstallDirs)

install(
TARGETS piper_phonemize
EXPORT piper_phonemize-config
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(EXPORT piper_phonemize-config
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/piper_phonemize)

install(
DIRECTORY ${CMAKE_SOURCE_DIR}/src/
DIRECTORY ${piper_phonemize_SOURCE_DIR}/src/
DESTINATION include/piper-phonemize
FILES_MATCHING
PATTERN "*.h"
Expand All @@ -218,18 +286,20 @@ install(
ARCHIVE DESTINATION ${CMAKE_INSTALL_BINDIR})

install(
FILES ${CMAKE_SOURCE_DIR}/etc/libtashkeel_model.ort
TYPE DATA)
FILES ${piper_phonemize_SOURCE_DIR}/etc/libtashkeel_model.ort
DESTINATION ${CMAKE_INSTALL_DATADIR}/piper-phonemize)

# Dependencies
install(
DIRECTORY ${ESPEAK_NG_DIR}/
DESTINATION ${CMAKE_INSTALL_PREFIX})

install(
DIRECTORY ${ONNXRUNTIME_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(
DIRECTORY ${ONNXRUNTIME_DIR}/lib/
DESTINATION ${CMAKE_INSTALL_LIBDIR})
if (BUILD_EXTERNAL)
install(
DIRECTORY ${ESPEAK_NG_DIR}/
DESTINATION ${CMAKE_INSTALL_PREFIX})

install(
DIRECTORY ${ONNXRUNTIME_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(
DIRECTORY ${ONNXRUNTIME_DIR}/lib/
DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: clean

all:
cmake -Bbuild -DCMAKE_INSTALL_PREFIX=install
cmake -Bbuild -DCMAKE_INSTALL_PREFIX="$${PREFIX:-install}" -DBUILD_EXTERNAL="$${BUILD_EXTERNAL:-ON}"
cmake --build build --config Release
cd build && ctest --config Release
cmake --install build
Expand Down