diff --git a/CMakeLists.txt b/CMakeLists.txt index f96e5b7..dc1441f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,8 +16,15 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") set(CMAKE_SKIP_BUILD_RPATH FALSE) set(BUILD_RPATH_USE_ORIGIN TRUE) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) -set(CMAKE_INSTALL_RPATH "$ORIGIN") -set(CMAKE_BUILD_RPATH "$ORIGIN") + +if(NOT APPLE) + set(kaldifeat_rpath_origin "$ORIGIN") +else() + set(kaldifeat_rpath_origin "@loader_path") +endif() + +set(CMAKE_INSTALL_RPATH ${kaldifeat_rpath_origin}) +set(CMAKE_BUILD_RPATH ${kaldifeat_rpath_origin}) set(BUILD_SHARED_LIBS ON) if(WIN32) @@ -52,11 +59,6 @@ if(kaldifeat_BUILD_TESTS) enable_testing() endif() -# TORCH_VERSION is defined in cmake/torch.cmake -configure_file( - ${CMAKE_SOURCE_DIR}/kaldifeat/python/kaldifeat/torch_version.py.in - ${CMAKE_SOURCE_DIR}/kaldifeat/python/kaldifeat/torch_version.py @ONLY -) if(WIN32) # disable various warnings for MSVC @@ -71,4 +73,40 @@ if(WIN32) endif() include_directories(${CMAKE_SOURCE_DIR}) + +message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") + +include(GNUInstallDirs) + +message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}") + add_subdirectory(kaldifeat) + +# TORCH_VERSION is defined in cmake/torch.cmake +configure_file( + ${PROJECT_SOURCE_DIR}/kaldifeat/python/kaldifeat/torch_version.py.in + ${PROJECT_SOURCE_DIR}/kaldifeat/python/kaldifeat/torch_version.py @ONLY +) + +configure_file( + ${PROJECT_SOURCE_DIR}/cmake/kaldifeatConfigVersion.cmake.in + ${PROJECT_BINARY_DIR}/kaldifeatConfigVersion.cmake + @ONLY +) + +configure_file( + ${PROJECT_SOURCE_DIR}/cmake/kaldifeatConfig.cmake.in + ${PROJECT_BINARY_DIR}/kaldifeatConfig.cmake + @ONLY +) + +install(FILES + ${PROJECT_BINARY_DIR}/kaldifeatConfigVersion.cmake + ${PROJECT_BINARY_DIR}/kaldifeatConfig.cmake + DESTINATION share/cmake/kaldifeat +) + +install(FILES + ${PROJECT_SOURCE_DIR}/kaldifeat/python/kaldifeat/torch_version.py + DESTINATION ./ +) diff --git a/cmake/cmake_extension.py b/cmake/cmake_extension.py index 0d14815..79dfbed 100644 --- a/cmake/cmake_extension.py +++ b/cmake/cmake_extension.py @@ -67,27 +67,39 @@ def build_extension(self, ext: setuptools.extension.Extension): if cmake_args == "": cmake_args = "-DCMAKE_BUILD_TYPE=Release" + extra_cmake_args = " -Dkaldifeat_BUILD_TESTS=OFF " + extra_cmake_args += f" -DCMAKE_INSTALL_PREFIX={Path(self.build_lib).resolve()}/kaldifeat " # noqa + 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 {kaldifeat_dir} cmake --build {self.build_temp} --target _kaldifeat --config Release -- -m + 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 {kaldifeat_dir}" ) if ret != 0: - raise Exception("Failed to build kaldifeat") + raise Exception("Failed to configure kaldifeat") ret = os.system( f"cmake --build {self.build_temp} --target _kaldifeat --config Release -- -m" ) if ret != 0: raise Exception("Failed to build kaldifeat") + + ret = os.system( + f"cmake --build {self.build_temp} --target install --config Release -- -m" + ) + if ret != 0: + raise Exception("Failed to install kaldifeat") else: if make_args == "" and system_make_args == "": print("For fast compilation, run:") @@ -101,7 +113,7 @@ def build_extension(self, ext: setuptools.extension.Extension): cmake {cmake_args} {kaldifeat_dir} - make {make_args} _kaldifeat + make {make_args} _kaldifeat install """ print(f"build command is:\n{build_cmd}") @@ -112,27 +124,3 @@ def build_extension(self, ext: setuptools.extension.Extension): "You can ask for help by creating an issue on GitHub.\n" "\nClick:\n\thttps://github.com/csukuangfj/kaldifeat/issues/new\n" # noqa ) - - lib_so = glob.glob(f"{self.build_temp}/lib/*kaldifeat*.so") - lib_so += glob.glob(f"{self.build_temp}/lib/*kaldifeat*.dylib") # macOS - - # bin/Release/_kaldifeat.cp38-win_amd64.pyd - lib_so += glob.glob( - f"{self.build_temp}/**/*kaldifeat*.pyd", recursive=True - ) # windows - - # lib/Release/*.lib - lib_so += glob.glob( - f"{self.build_temp}/**/*kaldifeat*.lib", recursive=True - ) # windows - for so in lib_so: - print(f"Copying {so} to {self.build_lib}/") - shutil.copy(f"{so}", f"{self.build_lib}/") - - print( - f"Copying {kaldifeat_dir}/kaldifeat/python/kaldifeat/torch_version.py to {self.build_lib}/kaldifeat" # noqa - ) - shutil.copy( - f"{kaldifeat_dir}/kaldifeat/python/kaldifeat/torch_version.py", - f"{self.build_lib}/kaldifeat", - ) diff --git a/cmake/kaldifeatConfig.cmake.in b/cmake/kaldifeatConfig.cmake.in new file mode 100644 index 0000000..6e8afac --- /dev/null +++ b/cmake/kaldifeatConfig.cmake.in @@ -0,0 +1,65 @@ +# Findkaldifeat +# ------------- +# +# Finds the kaldifeat library +# +# This will define the following variables: +# +# KALDIFEAT_FOUND -- True if the system has the kaldifeat library +# KALDIFEAT_INCLUDE_DIRS -- The include directories for kaldifeat +# KALDIFEAT_LIBRARIES -- Libraries to link against +# KALDIFEAT_CXX_FLAGS -- Additional (required) compiler flags +# KALDIFEAT_TORCH_VERSION_MAJOR -- The major version of PyTorch used to compile kaldifeat +# KALDIFEAT_TORCH_VERSION_MINOR -- The minor version of PyTorch used to compile kaldifeat +# KALDIFEAT_VERSION -- The version of kaldifeat +# +# and the following imported targets: +# +# kaldifeat_core + +# This file is modified from pytorch/cmake/TorchConfig.cmake.in + +set(KALDIFEAT_CXX_FLAGS "@CMAKE_CXX_FLAGS@") +set(KALDIFEAT_TORCH_VERSION_MAJOR @KALDIFEAT_TORCH_VERSION_MAJOR@) +set(KALDIFEAT_TORCH_VERSION_MINOR @KALDIFEAT_TORCH_VERSION_MINOR@) +set(KALDIFEAT_VERSION @KALDIFEAT_VERSION@) + +if(DEFINED ENV{KALDIFEAT_INSTALL_PREFIX}) + set(KALDIFEAT_INSTALL_PREFIX $ENV{KALDIFEAT_INSTALL_PREFIX}) +else() + # Assume we are in /share/cmake/kaldifeat/kaldifeatConfig.cmake + get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) + get_filename_component(KALDIFEAT_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE) +endif() + +set(KALDIFEAT_INCLUDE_DIRS ${KALDIFEAT_INSTALL_PREFIX}/include) + +set(KALDIFEAT_LIBRARIES kaldifeat_core) + +foreach(lib IN LISTS KALDIFEAT_LIBRARIES) + find_library(location_${lib} ${lib} + PATHS + "${KALDIFEAT_INSTALL_PREFIX}/lib" + "${KALDIFEAT_INSTALL_PREFIX}/lib64" + ) + + if(NOT MSVC) + add_library(${lib} SHARED IMPORTED) + else() + add_library(${lib} STATIC IMPORTED) + endif() + + set_target_properties(${lib} PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${KALDIFEAT_INCLUDE_DIRS}" + IMPORTED_LOCATION "${location_${lib}}" + CXX_STANDARD 14 + ) + + set_property(TARGET ${lib} PROPERTY INTERFACE_COMPILE_OPTIONS @CMAKE_CXX_FLAGS@) +endforeach() + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args(kaldifeat DEFAULT_MSG + location_kaldifeat_core +) diff --git a/cmake/kaldifeatConfigVersion.cmake.in b/cmake/kaldifeatConfigVersion.cmake.in new file mode 100644 index 0000000..9f60c82 --- /dev/null +++ b/cmake/kaldifeatConfigVersion.cmake.in @@ -0,0 +1,12 @@ +# This file is modified from pytorch/cmake/TorchConfigVersion.cmake.in +set(PACKAGE_VERSION "@kaldifeat_VERSION@") + +# Check whether the requested PACKAGE_FIND_VERSION is compatible +if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") + set(PACKAGE_VERSION_COMPATIBLE FALSE) +else() + set(PACKAGE_VERSION_COMPATIBLE TRUE) + if("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") + set(PACKAGE_VERSION_EXACT TRUE) + endif() +endif() diff --git a/kaldifeat/csrc/CMakeLists.txt b/kaldifeat/csrc/CMakeLists.txt index 9900b96..7a52a7d 100644 --- a/kaldifeat/csrc/CMakeLists.txt +++ b/kaldifeat/csrc/CMakeLists.txt @@ -53,5 +53,25 @@ if(kaldifeat_BUILD_TESTS) foreach(source IN LISTS test_srcs) kaldifeat_add_test(${source}) endforeach() - endif() + +file(MAKE_DIRECTORY + DESTINATION + ${PROJECT_BINARY_DIR}/include/kaldifeat/csrc +) + +file(GLOB_RECURSE all_headers *.h) +message(STATUS "All headers: ${all_headers}") + +file(COPY + ${all_headers} + DESTINATION + ${PROJECT_BINARY_DIR}/include/kaldifeat/csrc +) + +install(TARGETS kaldifeat_core + DESTINATION ${CMAKE_INSTALL_LIBDIR} +) +install(FILES ${all_headers} + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/kaldifeat/csrc +) diff --git a/kaldifeat/python/csrc/CMakeLists.txt b/kaldifeat/python/csrc/CMakeLists.txt index 1403e6d..9487bee 100644 --- a/kaldifeat/python/csrc/CMakeLists.txt +++ b/kaldifeat/python/csrc/CMakeLists.txt @@ -21,6 +21,10 @@ if(APPLE) target_link_libraries(_kaldifeat PRIVATE "-Wl,-rpath,${PYTHON_SITE_PACKAGE_DIR}") endif() +if(NOT WIN32) + target_link_libraries(_kaldifeat PRIVATE "-Wl,-rpath,${kaldifeat_rpath_origin}/kaldifeat/${CMAKE_INSTALL_LIBDIR}") +endif() + target_link_libraries(_kaldifeat PRIVATE kaldifeat_core) if(UNIX AND NOT APPLE) target_link_libraries(_kaldifeat PUBLIC ${TORCH_DIR}/lib/libtorch_python.so) @@ -29,3 +33,7 @@ elseif(WIN32) target_link_libraries(_kaldifeat PUBLIC ${TORCH_DIR}/lib/torch_python.lib) target_link_libraries(_kaldifeat PUBLIC ${PYTHON_LIBRARIES}) endif() + +install(TARGETS _kaldifeat + DESTINATION ../ +) diff --git a/kaldifeat/python/kaldifeat/__init__.py b/kaldifeat/python/kaldifeat/__init__.py index adf7d79..d32c46c 100644 --- a/kaldifeat/python/kaldifeat/__init__.py +++ b/kaldifeat/python/kaldifeat/__init__.py @@ -23,3 +23,8 @@ from .online_feature import OnlineFeature from .plp import OnlinePlp, Plp from .spectrogram import Spectrogram + +from pathlib import Path as _Path + +cmake_prefix_path = _Path(__file__).parent / "share" / "cmake" +del _Path