From 959da88b6ed3d88af97d829a1fe90c58b1988333 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 3 Apr 2022 14:26:25 +0800 Subject: [PATCH] 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}/")