Skip to content

Commit

Permalink
Add BUILD_EXTERNAL CMake option
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunren committed Oct 1, 2023
1 parent 4d03e2b commit f99d41d
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 125 deletions.
287 changes: 163 additions & 124 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ project(
LANGUAGES CXX
)

option(BUILD_EXTERNAL "Build external dependencies" OFF)

if(MSVC)
# Force compiler to use UTF-8 for IPA constants
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
Expand All @@ -34,112 +36,133 @@ set_target_properties(piper_phonemize PROPERTIES
SOVERSION ${PROJECT_VERSION_MAJOR}
)

# ---- 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(BUILD_EXTERNAL)
# ---- onnxruntime ---

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")
# Look for onnxruntime files in <root>/lib
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()
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")
# ---- 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/refs/heads/master.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
)
add_dependencies(piper_phonemize espeak_ng_external)
endif()
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/refs/heads/master.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
)
add_dependencies(piper_phonemize espeak_ng_external)
endif()
else()

find_package(PkgConfig REQUIRED)
pkg_check_modules(espeak_ng REQUIRED IMPORTED_TARGET espeak-ng)
find_package(onnxruntime REQUIRED)

endif()

# ---- Declare library ----

target_include_directories(
piper_phonemize PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
${ESPEAK_NG_DIR}/include
${ONNXRUNTIME_DIR}/include
)
if(BUILD_EXTERNAL)
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
)
target_link_directories(
piper_phonemize PUBLIC
${ESPEAK_NG_DIR}/lib
${ONNXRUNTIME_DIR}/lib
)

target_link_libraries(
piper_phonemize
espeak-ng
onnxruntime
)
target_link_libraries(
piper_phonemize
espeak-ng
onnxruntime
)
else()
target_include_directories(
piper_phonemize PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
)

target_link_libraries(
piper_phonemize
PkgConfig::espeak_ng
onnxruntime::onnxruntime
)
endif()

target_compile_features(piper_phonemize PUBLIC cxx_std_17)

Expand All @@ -153,21 +176,28 @@ 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
)
if(BUILD_EXTERNAL)
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
)
target_link_directories(
piper_phonemize_exe PUBLIC
${ESPEAK_NG_DIR}/lib
)

target_link_libraries(piper_phonemize_exe PUBLIC
piper_phonemize
espeak-ng
)
target_link_libraries(piper_phonemize_exe PUBLIC
piper_phonemize
espeak-ng
)
else()
target_link_libraries(piper_phonemize_exe PUBLIC
piper_phonemize
PkgConfig::espeak_ng
)
endif()

# ---- Declare test ----

Expand All @@ -181,21 +211,28 @@ add_test(

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
)
if(BUILD_EXTERNAL)
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
)
target_link_directories(
test_piper_phonemize PUBLIC
${ESPEAK_NG_DIR}/lib
)

target_link_libraries(test_piper_phonemize PUBLIC
piper_phonemize
espeak-ng
)
target_link_libraries(test_piper_phonemize PUBLIC
piper_phonemize
espeak-ng
)
else()
target_link_libraries(test_piper_phonemize PUBLIC
piper_phonemize
PkgConfig::espeak_ng
)
endif()

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

Expand All @@ -221,14 +258,16 @@ install(
TYPE DATA)

# 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

0 comments on commit f99d41d

Please sign in to comment.