cmake_extension.py (3964B)
1 # Copyright (c) 2021 Xiaomi Corporation (author: Fangjun Kuang) 2 3 import glob 4 import os 5 import platform 6 import shutil 7 import sys 8 from pathlib import Path 9 10 import setuptools 11 from setuptools.command.build_ext import build_ext 12 13 14 def is_for_pypi(): 15 ans = os.environ.get("KALDI_NATIVE_FBANK_IS_FOR_PYPI", None) 16 return ans is not None 17 18 19 def is_macos(): 20 return platform.system() == "Darwin" 21 22 23 def is_windows(): 24 return platform.system() == "Windows" 25 26 27 try: 28 from wheel.bdist_wheel import bdist_wheel as _bdist_wheel 29 30 class bdist_wheel(_bdist_wheel): 31 def finalize_options(self): 32 _bdist_wheel.finalize_options(self) 33 # In this case, the generated wheel has a name in the form 34 # kaldifeat-xxx-pyxx-none-any.whl 35 if is_for_pypi() and not is_macos(): 36 self.root_is_pure = True 37 else: 38 # The generated wheel has a name ending with 39 # -linux_x86_64.whl 40 self.root_is_pure = False 41 42 43 except ImportError: 44 bdist_wheel = None 45 46 47 def cmake_extension(name, *args, **kwargs) -> setuptools.Extension: 48 kwargs["language"] = "c++" 49 sources = [] 50 return setuptools.Extension(name, sources, *args, **kwargs) 51 52 53 class BuildExtension(build_ext): 54 def build_extension(self, ext: setuptools.extension.Extension): 55 # build/temp.linux-x86_64-3.8 56 os.makedirs(self.build_temp, exist_ok=True) 57 58 # build/lib.linux-x86_64-3.8 59 os.makedirs(self.build_lib, exist_ok=True) 60 61 install_dir = Path(self.build_lib).resolve() / "kaldi_native_fbank" 62 63 kaldi_native_fbank_dir = Path(__file__).parent.parent.resolve() 64 65 cmake_args = os.environ.get("KALDI_NATIVE_FBANK_CMAKE_ARGS", "") 66 make_args = os.environ.get("KALDI_NATIVE_FBANK_MAKE_ARGS", "") 67 system_make_args = os.environ.get("MAKEFLAGS", "") 68 69 if cmake_args == "": 70 cmake_args = "-DCMAKE_BUILD_TYPE=Release" 71 72 extra_cmake_args = f" -DCMAKE_INSTALL_PREFIX={install_dir} " 73 extra_cmake_args += " -DKALDI_NATIVE_FBANK_BUILD_TESTS=OFF " 74 75 if "PYTHON_EXECUTABLE" not in cmake_args: 76 print(f"Setting PYTHON_EXECUTABLE to {sys.executable}") 77 cmake_args += f" -DPYTHON_EXECUTABLE={sys.executable}" 78 79 cmake_args += extra_cmake_args 80 81 if is_windows(): 82 build_cmd = f""" 83 cmake {cmake_args} -B {self.build_temp} -S {kaldi_native_fbank_dir} 84 cmake --build {self.build_temp} --target install --config Release -- -m 85 """ 86 print(f"build command is:\n{build_cmd}") 87 ret = os.system( 88 f"cmake {cmake_args} -B {self.build_temp} -S {kaldi_native_fbank_dir}" 89 ) 90 if ret != 0: 91 raise Exception("Failed to configure kaldi_native_fbank") 92 93 ret = os.system( 94 f"cmake --build {self.build_temp} --target install --config Release -- -m" 95 ) 96 if ret != 0: 97 raise Exception("Failed to install kaldi_native_fbank") 98 else: 99 if make_args == "" and system_make_args == "": 100 print("For fast compilation, run:") 101 print( 102 'export KALDI_NATIVE_FBANK_MAKE_ARGS="-j"; python setup.py install' 103 ) 104 105 build_cmd = f""" 106 cd {self.build_temp} 107 108 cmake {cmake_args} {kaldi_native_fbank_dir} 109 110 make {make_args} install 111 """ 112 print(f"build command is:\n{build_cmd}") 113 114 ret = os.system(build_cmd) 115 if ret != 0: 116 raise Exception( 117 "\nBuild kaldi-native-fbank failed. Please check the error message.\n" 118 "You can ask for help by creating an issue on GitHub.\n" 119 "\nClick:\n\thttps://github.com/csukuangfj/kaldi-native-fbank/issues/new\n" # noqa 120 )