CMake get list of tried package when find_package fails - cmake

I have a cmake project and I want to obtain information when a find_package fails.
My setup is this: I have a project that compiles a few libraries and export the targets. The package is exported to the CMake package registry. Then, I have another project that depends on it.
The thing is, the library project has dependencies too. To make the importing package aware, we are using find_dependency in the config file.
When find_package(libs REQUIRED) fails because of missing dependencies, I would like to extract the path of the package it tried. I would use that information to maybe update the list of prefix path in order to find the missing dependencies next time, but I only want to do that if the reason of the failed import is missing dependencies.
Is there any way to obtain these information in a failed find package?

From the CMake documentation for find_package:
All configuration files which have been considered by CMake while searching for an installation of the package with an appropriate version are stored in the cmake variable <PackageName>_CONSIDERED_CONFIGS, the associated versions in <PackageName>_CONSIDERED_VERSIONS.
So using <PackageName>_CONSIDERED_CONFIGS and <PackageName>_CONSIDERED_VERSIONS can tell what packages CMake has found.

Related

How to perform the "make install" step after building aws-sdk-cpp using Qt Creator and CMake

I am trying to add aws-sdk-cpp as a submodule in my Qt application using Qt Creator and CMake. I want it to build for any platform without doing the building and installing on the command line as described here.
My project structure and CMakeLists.txt files looks like this:
I have successfully built the entire aws-sdk-cpp using MSVC2019 in debug mode using Qt Creator. My projects build folder is now 15 GB containing all the built libraries. The current issue I'm now facing is this error:
CMake Error at app/CMakeLists.txt:23 (find_package):
By not providing "FindAWSSDK.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "AWSSDK", but
CMake did not find one.
Could not find a package configuration file provided by "AWSSDK" with any
of the following names:
AWSSDKConfig.cmake
awssdk-config.cmake
Add the installation prefix of "AWSSDK" to CMAKE_PREFIX_PATH or set
"AWSSDK_DIR" to a directory containing one of the above files. If "AWSSDK"
provides a separate development package or SDK, be sure it has been
installed.
I think what is missing is the make install step described here and to set the path to AWSSDK_DIR.
I'm very new to CMake and I have not found any way to perform the make install step in the CMakeLists.txt file and then be able to set the AWSSDK_DIR which points to the AWSSDKConfig.cmake or awssdk-config.cmake file missing.
I'm also not sure which CMakeLists.txt file this should be written or if there is an entire other way to do this? Currently I'm stuck getting nowhere..

cmake find_package unable to find Eigen3Config.cmake spectra Windows

I am using Eigen3 with spectra (https://spectralib.org/), a library built on top of Eigen. Spectra uses find_package to find Eigen, and comes up with the error:
Could not find a package configuration file provided by "Eigen3" with any
of the following names:
Eigen3Config.cmake
eigen3-config.cmake
Looking through the directory of Eigen, I found that there were no files by those names, but rather one called Eigen3Config.cmake.in. I tried copying the file and renaming it Eigen3Config.cmake, but that gave me a different error of
find_package Error reading CMake code from "C:/Program Files
(x86)/Eigen3/cmake/Eigen3Config.cmake".
which was somewhat expected, but it does tell me that it can at least find the directory where Eigen3Config.cmake.in is. I'm assuming that either find_package is supposed to use Eigen3Config.cmake.in, or Eigen3Config.cmake.in is supposed to generate Eigen3Config.cmake, but i'm very new to cmake, so i'm not sure. How do I fix this?
There is no need to build/install Eigen to use it if you include it manually, as done in the getting started example (https://eigen.tuxfamily.org/dox/GettingStarted.html#title0)
But in order to be found by CMake, you will need to build / install it, as explained in the INSTALL file. https://gitlab.com/libeigen/eigen/-/blob/master/INSTALL
Usually, your error is followed by an hint asking you to set the variable Eigen3_DIR (or something similar) to point the build/install dir of the target project (Eigen3 here). It appears typically when you have built but not installed the project.
So:
Build Eigen
Install it (optional)
For spectra set the cmake var Eigen3_DIR to /path/to/Eigen/build . (if eigen not install or still not found)
These steps worked for me:
Install Eigen
Create a build directory for Eigen
cd into the build directory created
run "cmake ../"Your Eigen Source Directory""
After this is done, in your CMakeLists.txt of your project, you'll want to add "find_package( Eigen3 REQUIRED)" and "include_directories( ${EIGEN3_INCLUDE_DIR})".

How to instruct "find_package" to find a package installed in a user-specified location?

I installed a software package using CMake, on a customized location, with the command line below
cmake .. -DCMAKE_INSTALL_PREFIX=../install
Now CMake can no more find the package with the find_package. The error message suggests me to specify either CMAKE_MODULE_PATH or CMAKE_PREFIX_PATH.
I tried to specify the installation path using
cmake .. -DCMAKE_MODULE_PATH=../install
It did not work. But the following worked:
cmake .. -DCMAKE_PREFIX_PATH=../install
Question: In general, what would be the sound and reliable way to specify the path for "find_package" to work correctly for finding a package installed to a user-specified location?

How to get package name for use in CMake?

I have a couple of packages that I require as dependencies for a CMake build of my code. I got them through apt-get and they work.
The specific packages are;
mingw-w64
mingw-w64-i686-dev
I do realize that some people may want to build these dependencies from source, and I'm afraid the name for the apt-get package might not be the same name for the installed source package. I was just wondering, what package name do I put in my CMakeLists.txt file in the find_package(XXX REQUIRED) directive for either or both of these scenarios?
find_package(<package>) has two modes, Module and Config.
In Config mode (which will be used first) looks for a file called <package>-config.cmake / <package>Config.cmake in CMAKE_PREFIX_PATH.
In Module mode, CMake looks for a file calle Find<package>.cmake.
So, if your package is called mingw-w64, than there has to be one of the files mentioned above, e.g. mingw-w64Config.cmake.
The special architecture package mingw-w64-i686-dev is provided for 32bit machines. You will install that package on a 32bit machine and the package would be still called mingw-w64.
PS: Keep in mind that not all packages provide cmake files. If that's the case, you have to write your own Findmingw-w64.cmake file. Have a look here for inspiration.

CMake does not compĂ­le Qt5 generate from buildroot

I generate a tarball with Qt5 using Buildroot 2014.05.
When I uncompressed this files to compile my project using CMake I receive the following error message:
CMake Error at <project> /sysroot/usr/lib/cmake/Qt5Core/Qt5CoreConfig.cmake:27 (message):
The imported target "Qt5::Core" references the file
"<project>/host/usr/bin/qmake"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
"/<project>/sysroot/usr/lib/cmake/Qt5Core/Qt5CoreConfigExtras.cmake"
but not all the files it references.
I'm using the cmake configs under
<project>/sysroot/usr/lib/cmake/
and my CMakeLists.txt
set(CMAKE_PREFIX_PATH <project>/sysroot/usr/lib/cmake/QtWidgets/ )
find_package(Qt5Widgets REQUIRED)
Any suggestion?
Buildroot 2014.05 does not have qmake to install
This part: I generate a tarball with Qt5 using Buildroot does make much sense. What do you mean exactly by this ?
If by this you mean that you tarball'ed the contents of output/host/ (which is the toolchain and all the libraries that you need to build applications for your target), then beware that it is not relocatable. So it has to be installed in the same location as the one it was generated in. So for now, if you want to do this, you should set the BR2_HOST_DIR configuration option to something like /opt/<something>/, and then tarball that up and uncompress it on the other machines where you want to use the toolchain.
It's in our TODO-list to make this relocatable.