Skip to content

Commit

Permalink
Support "make install" (#47)
Browse files Browse the repository at this point in the history
* Support "make install"

* fix a typo
  • Loading branch information
csukuangfj authored Aug 1, 2022
1 parent e78f081 commit 01f30d2
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 34 deletions.
52 changes: 45 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 ./
)
40 changes: 14 additions & 26 deletions cmake/cmake_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
Expand All @@ -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}")

Expand All @@ -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",
)
65 changes: 65 additions & 0 deletions cmake/kaldifeatConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -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 <install-prefix>/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
)
12 changes: 12 additions & 0 deletions cmake/kaldifeatConfigVersion.cmake.in
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 21 additions & 1 deletion kaldifeat/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 8 additions & 0 deletions kaldifeat/python/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ../
)
5 changes: 5 additions & 0 deletions kaldifeat/python/kaldifeat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 01f30d2

Please sign in to comment.