Karel Vesely
Committed by GitHub

cmake build, configurable from env (#2115)

- make sure the defaults in `cmake/cmake_extension.py` variable
  `extra_cmake_args` can be overriden by `cmake_args` from
  `SHERPA_ONNX_CMAKE_ARGS` env variable
- fix a bug in `sherpa-onnx/csrc/parse-options.cc` which appears
  when using `-DSHERPA_ONNX_ENABLE_CHECK=ON`
- avoid copying binaries when these are disabled
@@ -153,7 +153,9 @@ class BuildExtension(build_ext): @@ -153,7 +153,9 @@ class BuildExtension(build_ext):
153 print(f"Setting PYTHON_EXECUTABLE to {sys.executable}") 153 print(f"Setting PYTHON_EXECUTABLE to {sys.executable}")
154 cmake_args += f" -DPYTHON_EXECUTABLE={sys.executable}" 154 cmake_args += f" -DPYTHON_EXECUTABLE={sys.executable}"
155 155
156 - cmake_args += extra_cmake_args 156 + # putting `cmake_args` from env variable ${SHERPA_ONNX_CMAKE_ARGS} last,
  157 + # so they can onverride the "defaults" stored in `extra_cmake_args`
  158 + cmake_args = extra_cmake_args + cmake_args
157 159
158 if is_windows(): 160 if is_windows():
159 build_cmd = f""" 161 build_cmd = f"""
@@ -216,11 +218,17 @@ class BuildExtension(build_ext): @@ -216,11 +218,17 @@ class BuildExtension(build_ext):
216 if not src_file.is_file(): 218 if not src_file.is_file():
217 src_file = install_dir / ".." / (f + suffix) 219 src_file = install_dir / ".." / (f + suffix)
218 220
  221 + if not src_file.is_file():
  222 + continue
  223 +
219 print(f"Copying {src_file} to {out_bin_dir}/") 224 print(f"Copying {src_file} to {out_bin_dir}/")
220 shutil.copy(f"{src_file}", f"{out_bin_dir}/") 225 shutil.copy(f"{src_file}", f"{out_bin_dir}/")
221 226
  227 + if Path(f"{install_dir}/bin").is_dir():
222 shutil.rmtree(f"{install_dir}/bin") 228 shutil.rmtree(f"{install_dir}/bin")
  229 + if Path(f"{install_dir}/share").is_dir():
223 shutil.rmtree(f"{install_dir}/share") 230 shutil.rmtree(f"{install_dir}/share")
  231 + if Path(f"{install_dir}/lib/pkgconfig").is_dir():
224 shutil.rmtree(f"{install_dir}/lib/pkgconfig") 232 shutil.rmtree(f"{install_dir}/lib/pkgconfig")
225 233
226 if is_macos(): 234 if is_macos():
@@ -45,6 +45,10 @@ with open("sherpa-onnx/python/sherpa_onnx/__init__.py", "a") as f: @@ -45,6 +45,10 @@ with open("sherpa-onnx/python/sherpa_onnx/__init__.py", "a") as f:
45 45
46 46
47 def get_binaries_to_install(): 47 def get_binaries_to_install():
  48 + cmake_args = os.environ.get("SHERPA_ONNX_CMAKE_ARGS", "")
  49 + if '-DSHERPA_ONNX_ENABLE_BINARY=OFF' in cmake_args:
  50 + return None
  51 +
48 bin_dir = Path("build") / "sherpa_onnx" / "bin" 52 bin_dir = Path("build") / "sherpa_onnx" / "bin"
49 bin_dir.mkdir(parents=True, exist_ok=True) 53 bin_dir.mkdir(parents=True, exist_ok=True)
50 suffix = ".exe" if is_windows() else "" 54 suffix = ".exe" if is_windows() else ""
@@ -69,7 +73,7 @@ setuptools.setup( @@ -69,7 +73,7 @@ setuptools.setup(
69 "sherpa_onnx": "sherpa-onnx/python/sherpa_onnx", 73 "sherpa_onnx": "sherpa-onnx/python/sherpa_onnx",
70 }, 74 },
71 packages=["sherpa_onnx"], 75 packages=["sherpa_onnx"],
72 - data_files=[("bin", get_binaries_to_install())], 76 + data_files=[("bin", get_binaries_to_install())] if get_binaries_to_install() else None,
73 url="https://github.com/k2-fsa/sherpa-onnx", 77 url="https://github.com/k2-fsa/sherpa-onnx",
74 long_description=read_long_description(), 78 long_description=read_long_description(),
75 long_description_content_type="text/markdown", 79 long_description_content_type="text/markdown",
@@ -261,9 +261,6 @@ static bool MustBeQuoted(const std::string &str, ShellType st) { @@ -261,9 +261,6 @@ static bool MustBeQuoted(const std::string &str, ShellType st) {
261 // pasted into a shell of ShellType "st" (only bash for now), it 261 // pasted into a shell of ShellType "st" (only bash for now), it
262 // will get passed to the program in the same way. 262 // will get passed to the program in the same way.
263 static std::string QuoteAndEscape(const std::string &str, ShellType /*st*/) { 263 static std::string QuoteAndEscape(const std::string &str, ShellType /*st*/) {
264 - // Only Bash is supported (for the moment).  
265 - SHERPA_ONNX_CHECK_EQ(st, kBash) << "Invalid shell type.";  
266 -  
267 // For now we use the following rules: 264 // For now we use the following rules:
268 // In the normal case, we quote with single-quote "'", and to escape 265 // In the normal case, we quote with single-quote "'", and to escape
269 // a single-quote we use the string: '\'' (interpreted as closing the 266 // a single-quote we use the string: '\'' (interpreted as closing the