Skip to content

Commit

Permalink
Fix windows wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfrank63 committed Feb 4, 2024
1 parent 350f649 commit 09ddcde
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ if(NOT DEFINED ONNXRUNTIME_DIR)
endif()

# ---- espeak-ng ---
set (ESPEAK_NG_REPO $ENV{ESPEAK_NG_REPO})
if(NOT DEFINED ESPEAK_NG_REPO)
set(ESPEAK_NG_REPO "https://github.com/rhasspy/espeak-ng")
endif()

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

Expand Down
4 changes: 4 additions & 0 deletions piper_phonemize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import sys
from collections import Counter
from enum import Enum
from pathlib import Path
Expand All @@ -17,6 +19,8 @@
_DIR = Path(__file__).parent
_TASHKEEL_MODEL = _DIR / "libtashkeel_model.ort"

espeak_data_path = sys.prefix / "share" / "espeak-ng-data"
os.environ["ESPEAK_DATA_PATH"] = str(espeak_data_path)

class TextCasing(str, Enum):
"""Casing applied to text for phonemize_codepoints"""
Expand Down
110 changes: 80 additions & 30 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,97 @@
import platform
import shutil
import sys

from pathlib import Path

# Available at setup time due to pyproject.toml
from pybind11.setup_helpers import Pybind11Extension, build_ext
from setuptools.command.install import install
from setuptools import setup

_DIR = Path(__file__).parent
_ESPEAK_DIR = _DIR / "install"

__version__ = "1.2.0"

_DIR = Path(__file__).parent

# Platform-specific settings
if platform.system() == "Windows":
_LIB_DIR = _DIR / "lib" / "Windows-AMD64"
_ONNXRUNTIME_DIR = _LIB_DIR / "onnxruntime"
libraries = ["espeak-ng", "onnxruntime"]
extra_compile_args = ["/MT"] # Example for static linking the MSVC runtime
library_dirs = [str(_ESPEAK_DIR / "lib"), str(_ONNXRUNTIME_DIR / "lib")]
include_dirs = [str(_ESPEAK_DIR / "include"), str(_ONNXRUNTIME_DIR / "include")]
_ESPEAK_DIR = _DIR / "build" / "ei"
_LIB_DIR = _DIR / "lib"
_ONNXRUNTIME_DIR = _LIB_DIR / "onnxruntime-win-x64-1.14.1"
else:
_ESPEAK_DIR = _DIR / "install"
_LIB_DIR = _DIR / "lib" / f"Linux-{platform.machine()}"
_ONNXRUNTIME_DIR = _LIB_DIR / "onnxruntime"
libraries = ["espeak-ng", "onnxruntime"]
extra_compile_args = [] # Linux/macOS might not need specific flags here
library_dirs = [str(_ESPEAK_DIR / "lib"), str(_ONNXRUNTIME_DIR / "lib")]
include_dirs = [str(_ESPEAK_DIR / "include"), str(_ONNXRUNTIME_DIR / "include")]

ext_modules = [
Pybind11Extension(
"piper_phonemize_cpp",
[
"src/python.cpp",
"src/phonemize.cpp",
"src/phoneme_ids.cpp",
"src/tashkeel.cpp",
],
define_macros=[("VERSION_INFO", __version__)],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
extra_compile_args=extra_compile_args,
),
]
if platform.system() == "Windows":
ext_modules = [
Pybind11Extension(
"piper_phonemize_cpp",
[
"src/python.cpp",
"src/phonemize.cpp",
"src/phoneme_ids.cpp",
"src/tashkeel.cpp",
],
define_macros=[("VERSION_INFO", __version__)],
include_dirs=[
str(_ESPEAK_DIR / "include/espeak-ng"),
str(_ONNXRUNTIME_DIR / "include"),
],
library_dirs=[str(_ESPEAK_DIR / "bin"), str(_ONNXRUNTIME_DIR / "lib")],
libraries=["espeak-ng", "onnxruntime"],
),
]
else:
ext_modules = [
Pybind11Extension(
"piper_phonemize_cpp",
[
"src/python.cpp",
"src/phonemize.cpp",
"src/phoneme_ids.cpp",
"src/tashkeel.cpp",
],
define_macros=[("VERSION_INFO", __version__)],
include_dirs=[str(_ESPEAK_DIR / "include"), str(_ONNXRUNTIME_DIR / "include")],
library_dirs=[str(_ESPEAK_DIR / "lib"), str(_ONNXRUNTIME_DIR / "lib")],
libraries=["espeak-ng", "onnxruntime"],
),
]


class CustomInstallCommand(install):
"""Customized setuptools install command to copy espeak-ng-data."""

def run(self):
# Call superclass install command
super().run()

# Check if the operating system is Windows
if platform.system() != "Windows":
print("Skipping custom installation steps: not running on Windows")
return

# Define the source directory for espeak-ng-data
source_dir = Path(__file__).parent / "build" / "ei" / "share" / "espeak-ng-data"

# Define the target directory within the Python environment's site-packages
target_dir = Path(sys.prefix) / "share" / "espeak-ng-data"

# Check if source_dir exists to prevent copying errors
if source_dir.exists():
# Use shutil.copytree to copy the entire directory
if target_dir.exists():
shutil.rmtree(
target_dir
) # Remove target if it already exists to avoid conflicts
shutil.copytree(source_dir, target_dir)
print(f"Copied espeak-ng-data to {target_dir}")
else:
print(
f"Source directory {source_dir} does not exist. espeak-ng-data not copied."
)


setup(
name="piper_phonemize",
Expand All @@ -60,7 +110,7 @@
},
include_package_data=True,
ext_modules=ext_modules,
cmdclass={"build_ext": build_ext},
cmdclass={"build_ext": build_ext, "install": CustomInstallCommand},
zip_safe=False,
python_requires=">=3.7",
)

0 comments on commit 09ddcde

Please sign in to comment.