Skip to content

Commit

Permalink
Support building a single DLL (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Aug 30, 2024
1 parent 503eacc commit 04e6cd4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
python3 -m pip install -U pip wheel numpy setuptools
python3 setup.py bdist_wheel
ls -lh dist
unzip -l ./dist/*.whl
python3 -m pip install ./dist/kaldi*.whl
cd ../..
python3 -c "import kaldi_decoder; print(kaldi_decoder.__version__)"
Expand Down
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

set(KALDI_DECODER_VERSION "0.2.6")
set(KALDI_DECODER_VERSION "0.2.7")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
Expand Down Expand Up @@ -48,6 +48,10 @@ if(BUILD_SHARED_LIBS AND MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()

if(BUILD_SHARED_LIBS)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

if(NOT BUILD_SHARED_LIBS AND MSVC)
# see https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html
# https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake
Expand Down
5 changes: 1 addition & 4 deletions cmake/cmake_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ def build_extension(self, ext: setuptools.extension.Extension):
cmake_args = "-DCMAKE_BUILD_TYPE=Release"

extra_cmake_args = f" -DCMAKE_INSTALL_PREFIX={install_dir} "
if is_windows():
extra_cmake_args += f" -DBUILD_SHARED_LIBS=OFF "
else:
extra_cmake_args += f" -DBUILD_SHARED_LIBS=ON "
extra_cmake_args += f" -DBUILD_SHARED_LIBS=ON "

extra_cmake_args += f" -DKALDI_DECODER_BUILD_PYTHON=ON "
extra_cmake_args += f" -DKALDI_DECODER_ENABLE_TESTS=OFF "
Expand Down
27 changes: 20 additions & 7 deletions cmake/kaldifst.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function(download_kaldifst)
if(EXISTS ${f})
set(kaldifst_URL "${f}")
file(TO_CMAKE_PATH "${kaldifst_URL}" kaldifst_URL)
message(STATUS "Found local downloaded kaldifst: ${kaldifst_URL}")
set(kaldifst_URL2)
break()
endif()
Expand All @@ -34,27 +35,39 @@ function(download_kaldifst)

FetchContent_GetProperties(kaldifst)
if(NOT kaldifst_POPULATED)
message(STATUS "Downloading kaldifst ${kaldifst_URL}")
message(STATUS "Downloading kaldifst from ${kaldifst_URL}")
FetchContent_Populate(kaldifst)
endif()
message(STATUS "kaldifst is downloaded to ${kaldifst_SOURCE_DIR}")
message(STATUS "kaldifst's binary dir is ${kaldifst_BINARY_DIR}")

list(APPEND CMAKE_MODULE_PATH ${kaldifst_SOURCE_DIR}/cmake)

include_directories(${kaldifst_SOURCE_DIR})
add_subdirectory(${kaldifst_SOURCE_DIR} ${kaldifst_BINARY_DIR})
if(BUILD_SHARED_LIBS)
set(_build_shared_libs_bak ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
endif()

add_subdirectory(${kaldifst_SOURCE_DIR} ${kaldifst_BINARY_DIR} EXCLUDE_FROM_ALL)

if(_build_shared_libs_bak)
set_target_properties(kaldifst_core
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
set(BUILD_SHARED_LIBS ON)
endif()

target_include_directories(kaldifst_core
PUBLIC
${kaldifst_SOURCE_DIR}
${kaldifst_SOURCE_DIR}/
)

set_target_properties(kaldifst_core PROPERTIES OUTPUT_NAME "kaldi-decoder-kaldi-fst-core")

if(KALDI_DECODER_BUILD_PYTHON AND WIN32)
install(TARGETS kaldifst_core DESTINATION ..)
else()
if(NOT BUILD_SHARED_LIBS)
install(TARGETS kaldifst_core DESTINATION lib)
endif()

Expand Down
45 changes: 30 additions & 15 deletions cmake/openfst.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
function(download_openfst)
include(FetchContent)

set(openfst_URL "https://github.com/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-13.tar.gz")
set(openfst_URL2 "https://hub.nuaa.cf/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-13.tar.gz")
set(openfst_HASH "SHA256=f10a71c6b64d89eabdc316d372b956c30c825c7c298e2f20c780320e8181ffb6")
set(openfst_URL "https://github.com/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-19.tar.gz")
set(openfst_URL2 "https://hub.nuaa.cf/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-19.tar.gz")
set(openfst_HASH "SHA256=5c98e82cc509c5618502dde4860b8ea04d843850ed57e6d6b590b644b268853d")

# If you don't have access to the Internet,
# please pre-download it
set(possible_file_locations
$ENV{HOME}/Downloads/openfst-sherpa-onnx-2024-06-13.tar.gz
${CMAKE_SOURCE_DIR}/openfst-sherpa-onnx-2024-06-13.tar.gz
${CMAKE_BINARY_DIR}/openfst-sherpa-onnx-2024-06-13.tar.gz
/tmp/openfst-sherpa-onnx-2024-06-13.tar.gz
/star-fj/fangjun/download/github/openfst-sherpa-onnx-2024-06-13.tar.gz
$ENV{HOME}/Downloads/openfst-sherpa-onnx-2024-06-19.tar.gz
${CMAKE_SOURCE_DIR}/openfst-sherpa-onnx-2024-06-19.tar.gz
${CMAKE_BINARY_DIR}/openfst-sherpa-onnx-2024-06-19.tar.gz
/tmp/openfst-sherpa-onnx-2024-06-19.tar.gz
/star-fj/fangjun/download/github/openfst-sherpa-onnx-2024-06-19.tar.gz
)

foreach(f IN LISTS possible_file_locations)
Expand Down Expand Up @@ -67,25 +67,40 @@ function(download_openfst)
FetchContent_Populate(openfst)
endif()
message(STATUS "openfst is downloaded to ${openfst_SOURCE_DIR}")

if(_build_shared_libs_bak)
set(_build_shared_libs_bak ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
endif()

add_subdirectory(${openfst_SOURCE_DIR} ${openfst_BINARY_DIR} EXCLUDE_FROM_ALL)

if(_build_shared_libs_bak)
set_target_properties(fst fstfar
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
set(BUILD_SHARED_LIBS ON)
endif()

set(openfst_SOURCE_DIR ${openfst_SOURCE_DIR} PARENT_SCOPE)

set_target_properties(fst PROPERTIES OUTPUT_NAME "kaldi-decoder-fst")

if(LINUX)
target_compile_options(fst PUBLIC -Wno-missing-template-keyword)
endif()

target_include_directories(fst
PUBLIC
${openfst_SOURCE_DIR}/src/include
)

if(KALDI_DECODER_BUILD_PYTHON AND WIN32)
install(TARGETS fst DESTINATION ..)
else()
if(NOT BUILD_SHARED_LIBS)
install(TARGETS fst DESTINATION lib)
endif()

if(WIN32 AND BUILD_SHARED_LIBS)
install(TARGETS fst DESTINATION bin)
endif()
endfunction()

download_openfst()
7 changes: 3 additions & 4 deletions kaldi-decoder/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ set(srcs
simple-decoder.cc
)

add_library(kaldi-decoder-core ${srcs})
# Always static build
add_library(kaldi-decoder-core STATIC ${srcs})

target_link_libraries(kaldi-decoder-core PUBLIC kaldifst_core)
target_link_libraries(kaldi-decoder-core PUBLIC Eigen3::Eigen)
Expand Down Expand Up @@ -42,8 +43,6 @@ if(KALDI_DECODER_ENABLE_TESTS)
endforeach()
endif()

if(KALDI_DECODER_BUILD_PYTHON AND WIN32)
install(TARGETS kaldi-decoder-core DESTINATION ..)
else()
if(NOT BUILD_SHARED_LIBS)
install(TARGETS kaldi-decoder-core DESTINATION lib)
endif()

0 comments on commit 04e6cd4

Please sign in to comment.