jeremy-rifkin / cpptrace

Simple, portable, and self-contained stacktrace library for C++11 and newer
MIT License
621 stars 64 forks source link

Could not find zstdConfig.cmake error when using cpptrace #112

Closed stkw0 closed 4 months ago

stkw0 commented 4 months ago

I compiled cpptrace with -DCPPTRACE_USE_EXTERNAL_LIBDWARF=On as I want to use the system libraries for zstd and libdwarf. cpptrace compiles and installs correctly.

Then, on my main project I added find_package(cpptrace REQUIRED) to the CMakeLists.txt file. When I try to configure it the next error is shown:

CMake Error at /usr/lib64/cmake/cpptrace/cpptrace-config.cmake:7 (find_package): By not providing "Findzstd.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "zstd", but CMake did not find one.

Could not find a package configuration file provided by "zstd" with any of the following names:

zstdConfig.cmake
zstd-config.cmake

Add the installation prefix of "zstd" to CMAKE_PREFIX_PATH or set "zstd_DIR" to a directory containing one of the above files. If "zstd" provides a separate development package or SDK, be sure it has been installed.

If I remove the two find_dependency lines from cmake/in/cpptrace-config-cmake.in my project compiles, links and runs correctly. I think that probably the if(@CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF@) should be changed so it also checks for CPPTRACE_USE_EXTERNAL_LIBDWARF in such a way that if it's enabled it should not try to find those dependencies as they are already linked to cpptrace.

jeremy-rifkin commented 4 months ago

Thanks for opening this, good catch

jeremy-rifkin commented 4 months ago

I think the original reason for adding the find_dependency was for package manger stuff. I will figure out how best to deal with this tomorrow.

stkw0 commented 4 months ago

I see. In my use case I created a cpptrace pacakge for Gentoo (that's why I turned on the settings to use system libraries) and then when using the installed package it failed bcause of that.

jeremy-rifkin commented 4 months ago

Oh awesome thanks so much for doing that! I have been looking at a similar issue today with the find_dependency(ZLIB) not working for someone else and I think I now better understand what’s going on with that issue and this one.

On your gentoo setup, is zstd installed but not zstd-dev (or similar) which would have the config files for the library or Findzstd.cmake?

stkw0 commented 4 months ago

I don't know the exact reason, but the zstd package only installs /usr/lib64/pkgconfig/libzstd.pc. It's libdwarf package the one which installs Findzstd.cmake (at /usr/lib64/cmake/libdwarf/Findzstd.cmake). I believe that cpptrace links against libdwarf and libdwarf links against zstd, so everything works fine. But when cpptrace tries to link explicetely with zstd it fails, as it can not find Findzstd.cmake (because it's installed in libdwarf path). Luckily, cpptrace doesn't actually needs to link against zstd, so when removing that part everything works fine.

jeremy-rifkin commented 4 months ago

Studying git blames a bit and playing around with removing the find_dependency(zstd) line I realized the reason it was needed is libdwarf's Findzstd.cmake script doesn't define zstd::libzstd_static / zstd::libzstd_shared targets. I think I've settled on a reasonable solution which is to provide a Findzstd.cmake script as part of cpptrace's installation and resolve the dependency that way. (reasonable in that I actually understand what's going on :P)

Please let me know if the setup in https://github.com/jeremy-rifkin/cpptrace/pull/115 works for your use case! I'll look to do a patch release soon.

stkw0 commented 4 months ago

Yep. Applying the patch seems to fix the issue. Given that cpptrace depends in libdwarf and this was a workaround for a libdwarf issue, I think it can make sense to submit your Findzstd.cmake to libdwarf?

jeremy-rifkin commented 4 months ago

Awesome thanks for confirming! I agree it would be good to apply upstream too.

thesamesam commented 4 months ago

On your gentoo setup, is zstd installed but not zstd-dev (or similar) which would have the config files for the library or Findzstd.cmake?

https://bugs.gentoo.org/872254

TL;DR: It depends on whether you build upstream zstd with CMake or not :(