From 959da88b6ed3d88af97d829a1fe90c58b1988333 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 3 Apr 2022 14:26:25 +0800 Subject: [PATCH 1/8] Fix building on Windows. --- cmake/cmake_extension.py | 74 +++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/cmake/cmake_extension.py b/cmake/cmake_extension.py index ae27abf..205ca71 100644 --- a/cmake/cmake_extension.py +++ b/cmake/cmake_extension.py @@ -20,6 +20,10 @@ 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 @@ -63,34 +67,64 @@ class BuildExtension(build_ext): if cmake_args == "": cmake_args = "-DCMAKE_BUILD_TYPE=Release" - if make_args == "" and system_make_args == "": - print("For fast compilation, run:") - print('export KALDIFEAT_MAKE_ARGS="-j"; python setup.py install') - if "PYTHON_EXECUTABLE" not in cmake_args: print(f"Setting PYTHON_EXECUTABLE to {sys.executable}") cmake_args += f" -DPYTHON_EXECUTABLE={sys.executable}" - build_cmd = f""" - cd {self.build_temp} - - cmake {cmake_args} {kaldifeat_dir} - - - make {make_args} _kaldifeat - """ - print(f"build command is:\n{build_cmd}") - - ret = os.system(build_cmd) - if ret != 0: - raise Exception( - "\nBuild kaldifeat failed. Please check the error message.\n" - "You can ask for help by creating an issue on GitHub.\n" - "\nClick:\n\thttps://github.com/csukuangfj/kaldifeat/issues/new\n" # noqa + 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 + """ + 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") + + ret = os.system( + f"cmake --build {self.build_temp} --target _kaldifeat --config Release -- -m" + ) + if ret != 0: + raise Exception("Failed to build kaldifeat") + else: + if make_args == "" and system_make_args == "": + print("For fast compilation, run:") + print( + 'export KALDIFEAT_MAKE_ARGS="-j"; python setup.py install' + ) + + build_cmd = f""" + cd {self.build_temp} + + cmake {cmake_args} {kaldifeat_dir} + + + make {make_args} _kaldifeat + """ + print(f"build command is:\n{build_cmd}") + + ret = os.system(build_cmd) + if ret != 0: + raise Exception( + "\nBuild kaldifeat failed. Please check the error message.\n" + "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/_k2.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}/") From 00e550c11b10b8abf095a2be313a1d469e964230 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 3 Apr 2022 14:36:24 +0800 Subject: [PATCH 2/8] Disable build shared libs on Windows. --- CMakeLists.txt | 6 ++++++ kaldifeat/csrc/CMakeLists.txt | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2457641..ce7b7e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,12 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) set(CMAKE_INSTALL_RPATH "$ORIGIN") set(CMAKE_BUILD_RPATH "$ORIGIN") +set(BUILD_SHARED_LIBS ON) +if(WIN32) + message(STATUS "Set BUILD_SHARED_LIBS to OFF for Windows") + set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) +endif() + if(NOT CMAKE_BUILD_TYPE) message(STATUS "No CMAKE_BUILD_TYPE given, default to Release") set(CMAKE_BUILD_TYPE Release) diff --git a/kaldifeat/csrc/CMakeLists.txt b/kaldifeat/csrc/CMakeLists.txt index 7e6f943..39f2c1c 100644 --- a/kaldifeat/csrc/CMakeLists.txt +++ b/kaldifeat/csrc/CMakeLists.txt @@ -12,7 +12,7 @@ set(kaldifeat_srcs online-feature.cc ) -add_library(kaldifeat_core SHARED ${kaldifeat_srcs}) +add_library(kaldifeat_core ${kaldifeat_srcs}) target_link_libraries(kaldifeat_core PUBLIC ${TORCH_LIBRARIES}) target_compile_definitions(kaldifeat_core PUBLIC KALDIFEAT_TORCH_VERSION_MAJOR=${KALDIFEAT_TORCH_VERSION_MAJOR}) From 07d11476c144def36f19e5d21740bb4260dda8d4 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 3 Apr 2022 14:50:24 +0800 Subject: [PATCH 3/8] Fix building on windows --- kaldifeat/python/csrc/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kaldifeat/python/csrc/CMakeLists.txt b/kaldifeat/python/csrc/CMakeLists.txt index 956263f..c80637c 100644 --- a/kaldifeat/python/csrc/CMakeLists.txt +++ b/kaldifeat/python/csrc/CMakeLists.txt @@ -14,4 +14,7 @@ target_link_libraries(_kaldifeat PRIVATE kaldifeat_core) if(UNIX AND NOT APPLE) target_link_libraries(_kaldifeat PUBLIC ${TORCH_DIR}/lib/libtorch_python.so) target_link_libraries(_kaldifeat PUBLIC ${PYTHON_LIBRARY}) +elseif(WIN32) + target_link_libraries(_kaldifeat PUBLIC ${TORCH_DIR}/lib/torch_python.lib) + target_link_libraries(_kaldifeat PUBLIC ${PYTHON_LIBRARIES}) endif() From 2f9fc99e6b84e3397349a95d57fe79f343c6540a Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 3 Apr 2022 14:56:42 +0800 Subject: [PATCH 4/8] Fix building on windows --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 57250c4..3436265 100644 --- a/setup.py +++ b/setup.py @@ -41,6 +41,7 @@ setuptools.setup( long_description_content_type="text/markdown", ext_modules=[cmake_extension("_kaldifeat")], cmdclass={"build_ext": BuildExtension, "bdist_wheel": bdist_wheel}, + zip_safe=False, classifiers=[ "Programming Language :: C++", "Programming Language :: Python", From 08dd6c52de7053e6046f29faa48f00b3c563ee0c Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 3 Apr 2022 15:29:22 +0800 Subject: [PATCH 5/8] Minor fixes. --- cmake/cmake_extension.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/cmake_extension.py b/cmake/cmake_extension.py index 205ca71..8bd21ca 100644 --- a/cmake/cmake_extension.py +++ b/cmake/cmake_extension.py @@ -116,13 +116,13 @@ class BuildExtension(build_ext): 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/_k2.cp38-win_amd64.pyd - lib_so = glob.glob( + # 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( + lib_so += glob.glob( f"{self.build_temp}/**/*kaldifeat*.lib", recursive=True ) # windows for so in lib_so: From 49039c074a28287b1117b6ca756f7e9b76a81ba8 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 3 Apr 2022 15:38:09 +0800 Subject: [PATCH 6/8] Add CI for windows. --- .github/workflows/build_conda.yml | 4 +- .github/workflows/build_windows.yml | 87 +++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/build_windows.yml diff --git a/.github/workflows/build_conda.yml b/.github/workflows/build_conda.yml index 66322cf..124e2db 100644 --- a/.github/workflows/build_conda.yml +++ b/.github/workflows/build_conda.yml @@ -20,8 +20,8 @@ name: build_conda_cuda on: push: - branches: - - conda-cuda + tags: + - '*' env: KALDIFEAT_BUILD_TYPE: Release diff --git a/.github/workflows/build_windows.yml b/.github/workflows/build_windows.yml new file mode 100644 index 0000000..c1dd2ff --- /dev/null +++ b/.github/workflows/build_windows.yml @@ -0,0 +1,87 @@ +name: build + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build-windows: + # see https://github.com/actions/virtual-environments/blob/win19/20210525.0/images/win/Windows2019-Readme.md + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [windows-2019] + python-version: [3.6, 3.7, 3.8, 3.9] + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + + # see https://github.com/microsoft/setup-msbuild + - name: Add msbuild to PATH + uses: microsoft/setup-msbuild@v1.0.2 + + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + + - name: Display Python version + run: python -c "import sys; print(sys.version)" + + - name: Install PyTorch ${{ matrix.torch }} + run: | + pip3 install -qq torch==${{ matrix.torch }}+cpu -f https://download.pytorch.org/whl/torch_stable.html + pip3 install -qq wheel twine dataclasses numpy typing_extensions + + - name: Display CMake version + run: | + cmake --version + cmake --help + + - name: Configure CMake + shell: bash + run: | + mkdir build_release + cd build_release + cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE .. + ls -lh + + - name: Build kaldifeat + run: | + cd build_release + cmake --build . --target _kaldifeat --config Release + + - name: Display generated files + shell: bash + run: | + cd build_release + ls -lh lib/*/* + + - name: Build wheel + shell: bash + run: | + python3 setup.py bdist_wheel + ls -lh dist/ + pip install ./dist/*.whl + python3 -c "import kaldifeat; print(kaldifeat.__version__)" + + - name: Upload Wheel + uses: actions/upload-artifact@v2 + with: + name: python-${{ matrix.python-version }}-${{ matrix.os }}-cpu + path: dist/*.whl + + - name: Build tests + shell: bash + run: | + cd build_release + cmake --build . --target ALL_BUILD --config Release + ls -lh bin/*/* + ctest -C Release --verbose --output-on-failure From d4f2a89b50d7e1c49d70248872c07c7e55771db4 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 3 Apr 2022 15:56:59 +0800 Subject: [PATCH 7/8] Minor fixes. --- .github/workflows/build_windows.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_windows.yml b/.github/workflows/build_windows.yml index c1dd2ff..93d3bb4 100644 --- a/.github/workflows/build_windows.yml +++ b/.github/workflows/build_windows.yml @@ -16,6 +16,7 @@ jobs: fail-fast: false matrix: os: [windows-2019] + torch: ["1.6.0", "1.7.0", "1.7.1", "1.8.0", "1.8.1", "1.9.0", "1.10.0", "1.11.0"] python-version: [3.6, 3.7, 3.8, 3.9] steps: From 2ea9ff75b4c1267735fe34414b52194a58d98f96 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 3 Apr 2022 17:09:14 +0800 Subject: [PATCH 8/8] minor fixes. --- .github/workflows/build_windows.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_windows.yml b/.github/workflows/build_windows.yml index 93d3bb4..ea8ae90 100644 --- a/.github/workflows/build_windows.yml +++ b/.github/workflows/build_windows.yml @@ -39,7 +39,7 @@ jobs: - name: Install PyTorch ${{ matrix.torch }} run: | pip3 install -qq torch==${{ matrix.torch }}+cpu -f https://download.pytorch.org/whl/torch_stable.html - pip3 install -qq wheel twine dataclasses numpy typing_extensions + pip3 install -qq wheel twine dataclasses numpy typing_extensions soundfile - name: Display CMake version run: |