Add Python API (#31)
This commit is contained in:
0
cmake/__init__.py
Normal file
0
cmake/__init__.py
Normal file
124
cmake/cmake_extension.py
Normal file
124
cmake/cmake_extension.py
Normal file
@@ -0,0 +1,124 @@
|
||||
# cmake/cmake_extension.py
|
||||
# Copyright (c) 2023 Xiaomi Corporation
|
||||
#
|
||||
# flake8: noqa
|
||||
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import setuptools
|
||||
from setuptools.command.build_ext import build_ext
|
||||
|
||||
|
||||
def is_for_pypi():
|
||||
ans = os.environ.get("SHERPA_ONNX_IS_FOR_PYPI", None)
|
||||
return ans is not None
|
||||
|
||||
|
||||
def is_macos():
|
||||
return platform.system() == "Darwin"
|
||||
|
||||
|
||||
def is_windows():
|
||||
return platform.system() == "Windows"
|
||||
|
||||
|
||||
try:
|
||||
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
|
||||
|
||||
class bdist_wheel(_bdist_wheel):
|
||||
def finalize_options(self):
|
||||
_bdist_wheel.finalize_options(self)
|
||||
# In this case, the generated wheel has a name in the form
|
||||
# sherpa-xxx-pyxx-none-any.whl
|
||||
if is_for_pypi() and not is_macos():
|
||||
self.root_is_pure = True
|
||||
else:
|
||||
# The generated wheel has a name ending with
|
||||
# -linux_x86_64.whl
|
||||
self.root_is_pure = False
|
||||
|
||||
except ImportError:
|
||||
bdist_wheel = None
|
||||
|
||||
|
||||
def cmake_extension(name, *args, **kwargs) -> setuptools.Extension:
|
||||
kwargs["language"] = "c++"
|
||||
sources = []
|
||||
return setuptools.Extension(name, sources, *args, **kwargs)
|
||||
|
||||
|
||||
class BuildExtension(build_ext):
|
||||
def build_extension(self, ext: setuptools.extension.Extension):
|
||||
# build/temp.linux-x86_64-3.8
|
||||
os.makedirs(self.build_temp, exist_ok=True)
|
||||
|
||||
# build/lib.linux-x86_64-3.8
|
||||
os.makedirs(self.build_lib, exist_ok=True)
|
||||
|
||||
install_dir = Path(self.build_lib).resolve() / "sherpa_onnx"
|
||||
|
||||
sherpa_onnx_dir = Path(__file__).parent.parent.resolve()
|
||||
|
||||
cmake_args = os.environ.get("SHERPA_ONNX_CMAKE_ARGS", "")
|
||||
make_args = os.environ.get("SHERPA_ONNX_MAKE_ARGS", "")
|
||||
system_make_args = os.environ.get("MAKEFLAGS", "")
|
||||
|
||||
if cmake_args == "":
|
||||
cmake_args = "-DCMAKE_BUILD_TYPE=Release"
|
||||
|
||||
extra_cmake_args = f" -DCMAKE_INSTALL_PREFIX={install_dir} "
|
||||
if not is_windows():
|
||||
extra_cmake_args += " -DBUILD_SHARED_LIBS=ON "
|
||||
else:
|
||||
extra_cmake_args += " -DBUILD_SHARED_LIBS=OFF "
|
||||
extra_cmake_args += " -DSHERPA_ONNX_ENABLE_PYTHON=ON "
|
||||
|
||||
if "PYTHON_EXECUTABLE" not in cmake_args:
|
||||
print(f"Setting PYTHON_EXECUTABLE to {sys.executable}")
|
||||
cmake_args += f" -DPYTHON_EXECUTABLE={sys.executable}"
|
||||
|
||||
cmake_args += extra_cmake_args
|
||||
|
||||
if is_windows():
|
||||
build_cmd = f"""
|
||||
cmake {cmake_args} -B {self.build_temp} -S {sherpa_onnx_dir}
|
||||
cmake --build {self.build_temp} --target install --config Release -- -m
|
||||
"""
|
||||
print(f"build command is:\n{build_cmd}")
|
||||
ret = os.system(
|
||||
f"cmake {cmake_args} -B {self.build_temp} -S {sherpa_onnx_dir}"
|
||||
)
|
||||
if ret != 0:
|
||||
raise Exception("Failed to configure sherpa")
|
||||
|
||||
ret = os.system(
|
||||
f"cmake --build {self.build_temp} --target install --config Release -- -m" # noqa
|
||||
)
|
||||
if ret != 0:
|
||||
raise Exception("Failed to build and install sherpa")
|
||||
else:
|
||||
if make_args == "" and system_make_args == "":
|
||||
print("for fast compilation, run:")
|
||||
print('export SHERPA_ONNX_MAKE_ARGS="-j"; python setup.py install')
|
||||
print('Setting make_args to "-j4"')
|
||||
make_args = "-j4"
|
||||
|
||||
build_cmd = f"""
|
||||
cd {self.build_temp}
|
||||
|
||||
cmake {cmake_args} {sherpa_onnx_dir}
|
||||
|
||||
make {make_args} install/strip
|
||||
"""
|
||||
print(f"build command is:\n{build_cmd}")
|
||||
|
||||
ret = os.system(build_cmd)
|
||||
if ret != 0:
|
||||
raise Exception(
|
||||
"\nBuild sherpa-onnx failed. Please check the error message.\n"
|
||||
"You can ask for help by creating an issue on GitHub.\n"
|
||||
"\nClick:\n\thttps://github.com/k2-fsa/sherpa-onnx/issues/new\n" # noqa
|
||||
)
|
||||
@@ -1,8 +1,8 @@
|
||||
function(download_kaldi_native_fbank)
|
||||
include(FetchContent)
|
||||
|
||||
set(kaldi_native_fbank_URL "https://github.com/csukuangfj/kaldi-native-fbank/archive/refs/tags/v1.12.tar.gz")
|
||||
set(kaldi_native_fbank_HASH "SHA256=8f4dfc3f6ddb1adcd9ac0ae87743ebc6cbcae147aacf9d46e76fa54134e12b44")
|
||||
set(kaldi_native_fbank_URL "https://github.com/csukuangfj/kaldi-native-fbank/archive/refs/tags/v1.13.tar.gz")
|
||||
set(kaldi_native_fbank_HASH "SHA256=1f4d228f9fe3e3e9f92a74a7eecd2489071a03982e4ba6d7c70fc5fa7444df57")
|
||||
|
||||
set(KALDI_NATIVE_FBANK_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
||||
set(KALDI_NATIVE_FBANK_BUILD_PYTHON OFF CACHE BOOL "" FORCE)
|
||||
@@ -11,11 +11,11 @@ function(download_kaldi_native_fbank)
|
||||
# If you don't have access to the Internet,
|
||||
# please pre-download kaldi-native-fbank
|
||||
set(possible_file_locations
|
||||
$ENV{HOME}/Downloads/kaldi-native-fbank-1.12.tar.gz
|
||||
${PROJECT_SOURCE_DIR}/kaldi-native-fbank-1.12.tar.gz
|
||||
${PROJECT_BINARY_DIR}/kaldi-native-fbank-1.12.tar.gz
|
||||
/tmp/kaldi-native-fbank-1.12.tar.gz
|
||||
/star-fj/fangjun/download/github/kaldi-native-fbank-1.12.tar.gz
|
||||
$ENV{HOME}/Downloads/kaldi-native-fbank-1.13.tar.gz
|
||||
${PROJECT_SOURCE_DIR}/kaldi-native-fbank-1.13.tar.gz
|
||||
${PROJECT_BINARY_DIR}/kaldi-native-fbank-1.13.tar.gz
|
||||
/tmp/kaldi-native-fbank-1.13.tar.gz
|
||||
/star-fj/fangjun/download/github/kaldi-native-fbank-1.13.tar.gz
|
||||
)
|
||||
|
||||
foreach(f IN LISTS possible_file_locations)
|
||||
@@ -44,6 +44,7 @@ function(download_kaldi_native_fbank)
|
||||
INTERFACE
|
||||
${kaldi_native_fbank_SOURCE_DIR}/
|
||||
)
|
||||
install(TARGETS kaldi-native-fbank-core DESTINATION lib)
|
||||
endfunction()
|
||||
|
||||
download_kaldi_native_fbank()
|
||||
|
||||
@@ -85,6 +85,7 @@ function(download_onnxruntime)
|
||||
message(STATUS "location_onnxruntime: ${location_onnxruntime}")
|
||||
|
||||
add_library(onnxruntime SHARED IMPORTED)
|
||||
|
||||
set_target_properties(onnxruntime PROPERTIES
|
||||
IMPORTED_LOCATION ${location_onnxruntime}
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include"
|
||||
@@ -100,6 +101,18 @@ function(download_onnxruntime)
|
||||
${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
|
||||
)
|
||||
endif()
|
||||
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*")
|
||||
elseif(APPLE)
|
||||
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*dylib")
|
||||
elseif(WIN32)
|
||||
file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/*.dll")
|
||||
endif()
|
||||
|
||||
message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
|
||||
install(FILES ${onnxruntime_lib_files} DESTINATION lib)
|
||||
endfunction()
|
||||
|
||||
download_onnxruntime()
|
||||
|
||||
38
cmake/pybind11.cmake
Normal file
38
cmake/pybind11.cmake
Normal file
@@ -0,0 +1,38 @@
|
||||
function(download_pybind11)
|
||||
include(FetchContent)
|
||||
|
||||
set(pybind11_URL "https://github.com/pybind/pybind11/archive/refs/tags/v2.10.2.tar.gz")
|
||||
set(pybind11_HASH "SHA256=93bd1e625e43e03028a3ea7389bba5d3f9f2596abc074b068e70f4ef9b1314ae")
|
||||
|
||||
# If you don't have access to the Internet,
|
||||
# please pre-download pybind11
|
||||
set(possible_file_locations
|
||||
$ENV{HOME}/Downloads/pybind11-2.10.2.tar.gz
|
||||
${PROJECT_SOURCE_DIR}/pybind11-2.10.2.tar.gz
|
||||
${PROJECT_BINARY_DIR}/pybind11-2.10.2.tar.gz
|
||||
/tmp/pybind11-2.10.2.tar.gz
|
||||
/star-fj/fangjun/download/github/pybind11-2.10.2.tar.gz
|
||||
)
|
||||
|
||||
foreach(f IN LISTS possible_file_locations)
|
||||
if(EXISTS ${f})
|
||||
set(pybind11_URL "file://${f}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
FetchContent_Declare(pybind11
|
||||
URL ${pybind11_URL}
|
||||
URL_HASH ${pybind11_HASH}
|
||||
)
|
||||
|
||||
FetchContent_GetProperties(pybind11)
|
||||
if(NOT pybind11_POPULATED)
|
||||
message(STATUS "Downloading pybind11 from ${pybind11_URL}")
|
||||
FetchContent_Populate(pybind11)
|
||||
endif()
|
||||
message(STATUS "pybind11 is downloaded to ${pybind11_SOURCE_DIR}")
|
||||
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR} EXCLUDE_FROM_ALL)
|
||||
endfunction()
|
||||
|
||||
download_pybind11()
|
||||
Reference in New Issue
Block a user