diff --git a/.github/workflows/build-pip.yml b/.github/workflows/build-pip.yml index 14801c7..a07a51e 100644 --- a/.github/workflows/build-pip.yml +++ b/.github/workflows/build-pip.yml @@ -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__)" diff --git a/CMakeLists.txt b/CMakeLists.txt index 0198359..743ed0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") @@ -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 diff --git a/cmake/cmake_extension.py b/cmake/cmake_extension.py index 649369d..5cfdb91 100644 --- a/cmake/cmake_extension.py +++ b/cmake/cmake_extension.py @@ -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 " diff --git a/cmake/kaldifst.cmake b/cmake/kaldifst.cmake index e7b493a..be7e4a9 100644 --- a/cmake/kaldifst.cmake +++ b/cmake/kaldifst.cmake @@ -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() @@ -34,7 +35,7 @@ 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}") @@ -42,19 +43,31 @@ function(download_kaldifst) 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() diff --git a/cmake/openfst.cmake b/cmake/openfst.cmake index 535ea66..0bbd933 100644 --- a/cmake/openfst.cmake +++ b/cmake/openfst.cmake @@ -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) @@ -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() diff --git a/kaldi-decoder/csrc/CMakeLists.txt b/kaldi-decoder/csrc/CMakeLists.txt index 9e74d23..b78c8e1 100644 --- a/kaldi-decoder/csrc/CMakeLists.txt +++ b/kaldi-decoder/csrc/CMakeLists.txt @@ -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) @@ -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()