Skip to content

cmake build, configurable from env #2115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged

Conversation

KarelVesely84
Copy link
Contributor

@KarelVesely84 KarelVesely84 commented Apr 10, 2025

  • 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

@KarelVesely84
Copy link
Contributor Author

KarelVesely84 commented Apr 10, 2025

Next, could someone look into the setuptools and cmake code ?

I tried to build with custom config:

SHERPA_ONNX_CMAKE_ARGS=(
-DCMAKE_BUILD_TYPE=Debug
-DBUILD_SHARED_LIBS=ON
-DSHERPA_ONNX_ENABLE_GPU=OFF
-DSHERPA_ONNX_ENABLE_CHECK=ON
-DSHERPA_ONNX_ENABLE_PYTHON=ON
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF
-DSHERPA_ONNX_ENABLE_TTS=OFF
-DSHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=OFF
)

I am building with:

SHERPA_ONNX_CMAKE_ARGS="${SHERPA_ONNX_CMAKE_ARGS[@]}" python3 setup.py bdist_wheel

so cmake build logs are visible on the screen.

The build fails as the cmake/cmake_extension.py code assumes the binaries do exist.
But here they are disabled by : -DSHERPA_ONNX_ENABLE_BINARY=OFF.

Similar problems arise also from other non-default options:

SHERPA_ONNX_ENABLE_PORTAUDIO=OFF
DSHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=OFF
...

@csukuangfj
Copy link
Collaborator

please add after

if not src_file.is_file():
src_file = install_dir / ".." / (f + suffix)

if not src_file.is_file():
  continue

@KarelVesely84
Copy link
Contributor Author

Okay, added those two lines, now getting:

[100%] Built target _sherpa_onnx
Installing the project stripped...
-- Install configuration: "Debug"
-- Installing: /mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/lib/libonnxruntime.so
-- Installing: /mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/./sherpa-onnx.pc
-- Installing: /mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/../_sherpa_onnx.cpython-39-x86_64-linux-gnu.so
-- Set non-toolchain portion of runtime path of "/mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/../_sherpa_onnx.cpython-39-x86_64-linux-gnu.so" to 
"$ORIGIN"
-- Installing: /mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/lib/libsherpa-onnx-c-api.so
-- Set non-toolchain portion of runtime path of "/mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/lib/libsherpa-onnx-c-api.so" to "$ORIGIN"
-- Installing: /mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/lib/libsherpa-onnx-cxx-api.so
-- Set non-toolchain portion of runtime path of "/mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/lib/libsherpa-onnx-cxx-api.so" to "$ORIGIN"
-- Installing: /mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/include/sherpa-onnx/c-api/c-api.h
-- Installing: /mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/include/sherpa-onnx/c-api/cxx-api.h
**error: [Errno 2] No such file or directory: '/mnt/matylda5/iveselyk/EU-ASR_TENDER/SHERPA_ONNX/src/sherpa-onnx/build/lib.linux-x86_64-cpython-39/sherpa_onnx/bin'**

@KarelVesely84
Copy link
Contributor Author

KarelVesely84 commented Apr 11, 2025

it seems like something in sherpa-onnx/csrc/CMakeLists.txt, but all the install() commands with
bin are "guarded" by the SHERPA_ONNX_ENABLE_BINARY variable... so, on the 1st read it seems okay

but it can be also somewhere else, there is also one install() in the main CMakelists.txt
and also some in cmake/*.cmake files

@csukuangfj
Copy link
Collaborator

I see the problem.

You need to change two places.

  1. Please change
    def get_binaries_to_install():

You can add something like:

def get_binaries_to_install():
  cmake_args = os.environ.get("SHERPA_ONNX_CMAKE_ARGS", "")
  if '-DSHERPA_ONNX_ENABLE_BINARY=OFF' in cmake_args:
    return None
  1. Change
    data_files=[("bin", get_binaries_to_install())],

    to
 data_files=[("bin", get_binaries_to_install())] if get_binaries_to_install() else [], 

or

 data_files=[("bin", get_binaries_to_install())] if get_binaries_to_install() else None, 

@KarelVesely84
Copy link
Contributor Author

Thank you for the suggestions!
Now, the build with -DSHERPA_ONNX_ENABLE_BINARY=OFF works for me locally.
(some extra checks for existing folders were also added into cmake/cmake_extension.py:227-232)

PR is updated.

@csukuangfj
Copy link
Collaborator

Is it ready to merge?

@@ -262,7 +262,7 @@ static bool MustBeQuoted(const std::string &str, ShellType st) {
// will get passed to the program in the same way.
static std::string QuoteAndEscape(const std::string &str, ShellType /*st*/) {
// Only Bash is supported (for the moment).
SHERPA_ONNX_CHECK_EQ(st, kBash) << "Invalid shell type.";
// SHERPA_ONNX_CHECK_EQ(st, kBash) << "Invalid shell type.";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you call exit() after printing a log?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returned to it. i don't understand the context now... there seems to be no logprint...

here the compilation was crashing when -DSHERPA_ONNX_ENABLE_CHECK=ON was set.
the variable st was already commented in the header of the function QuoteAndEscape,
and it was used in the macro SHERPA_ONNX_CHECK_EQ, being an undefined variable.

so I commented out the line 265 fix it... there is no other use of variable st in that function...
should there be done something more ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then please just remove it instead of commenting it out. Removing it leads to less code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, good, done !

@KarelVesely84
Copy link
Contributor Author

Is it ready to merge?

Left a comment above on the call exit() remark.
Otherwise, to me it seems ready for merging.

- 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
@KarelVesely84
Copy link
Contributor Author

Ok, the commented line is removed. The PR was squashed to be a single commit.
It is ready for mering.

Copy link
Collaborator

@csukuangfj csukuangfj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

@csukuangfj csukuangfj merged commit f3d23aa into k2-fsa:master Apr 16, 2025
180 of 222 checks passed
@KarelVesely84 KarelVesely84 deleted the cmake_build_env branch May 22, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants