find_package() ignores <PackageName>_ROOT - c++

In my CMake script I append the path to a folder containing <PackageName>Config.cmake to <PackageName>_ROOT and then call find_package(<PackageName> REQUIRED) but it can't find my package.
When I use CMAKE_FIND_DEBUG_MODE I see my folder listed in <PackageName>_ROOT CMake variable [CMAKE_FIND_USE_PACKAGE_ROOT_PATH] (it is actually the only folder there), but it is not listed after find_package considered the following locations for the Config module:.
Why am I getting such behaviour? I use CMake 3.18.1 from Android Studio. The package I'm trying to find is OpenCV.
Relatively to my CMakeLists.txt file OpenCV is installed in ../../build/opencv-build<custom suffix>, so my code for finding it is:
get_filename_component(OPENCV_BUILD_DIRS_ROOT ../../build REALPATH)
file(GLOB OPENCV_BUILD_DIRS ${OPENCV_BUILD_DIRS_ROOT}/opencv-build*)
list(APPEND OpenCV_ROOT ${OPENCV_BUILD_DIRS})
find_package(OpenCV ${OPENCV_VERSION} REQUIRED ${OPENCV_PUBLIC_LIBRARIES} ${OPENCV_PRIVATE_LIBRARIES})
When I run it on Linux (WSL actually), everything works fine and CMake finds OpenCV succesfully in ../../build/opencv-build (or <project path>/build/opencv-build in the form of absolute path).
But when I try to build the project from Android Studio it doesn't, and I get this output from CMAKE_FIND_DEBUG_MODE:
CMake Debug Log at external/opencv/CMakeLists.txt:12 (find_package):
find_package considered the following paths for OpenCV.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/cmake/3.18.1/share/cmake-3.18/Modules/FindOpenCV.cmake
The file was not found.
<PackageName>_ROOT CMake variable [CMAKE_FIND_USE_PACKAGE_ROOT_PATH].
<project path>/build/opencv-build
CMAKE_PREFIX_PATH variable [CMAKE_FIND_USE_CMAKE_PATH].
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64
CMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH variables
[CMAKE_FIND_USE_CMAKE_PATH].
Env variable OpenCV_DIR [CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].
none
CMAKE_PREFIX_PATH env variable [CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].
none
CMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH env variables
[CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].
none
Paths specified by the find_package HINTS option.
none
Standard system environment variables
[CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH].
<a lot of unrelated directories>
CMake User Package Registry [CMAKE_FIND_USE_PACKAGE_REGISTRY].
none
CMake variables defined in the Platform file
[CMAKE_FIND_USE_CMAKE_SYSTEM_PATH].
C:/Users/<username>/AppData/Local/Android/Sdk/cmake/3.18.1
<other unrelated directories>
CMake System Package Registry
[CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY].
none
Paths specified by the find_package PATHS option.
none
find_package considered the following locations for the Config module:
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/i686-linux-android/21/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/i686-linux-android/21/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/local/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/local/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/opencv-config.cmake
The file was not found.

So, the problem actually was that for Android (and, probably, iOS) CMake sets CMAKE_FIND_ROOT_PATH_MODE_PACKAGE variable (and other CMAKE_FIND_ROOT_PATH_MODE_* variables) to ONLY which makes find_package (and other find_* functions) prefix paths with what is in CMAKE_FIND_ROOT_PATH list.
To override this, one can do some of the following:
Set CMAKE_FIND_ROOT_PATH_MODE_* to BOTH or NEVER
Use CMAKE_FIND_ROOT_PATH_BOTH or NO_CMAKE_FIND_ROOT_PATH for each find_* call
It should also fix the issues when CMake ignores paths from HINTS, PATHS and other prefix construction steps.

Related

CMake cannot find OpenCVConfig.cmake even after specifying OpenCV_DIR

I'm running a C++ program using OpenCV. I've installed all necessary libraries but it seems cmake cannot find OpenCV libraries.
The command port content opencv3 shows my opencv libraries are under these folders:
/opt/local/libexec/
/opt/local/bin/
/opt/local/lib/
/opt/local/include/
but adding set(OpenCV_DIR /opt/local) before find_package(OpenCV REQUIRED) didn't work and the same error just pop up every time.
What should I do?
CMake commands do not separate their arguments via ,. Thus when you wrote:
set(OpenCV_DIR, /opt/local/share/OpenCV)
You in fact set a variable named OpenCV_DIR, to /opt/local/share/OpenCV. You can "fix" it by removing the comma:
set(OpenCV_DIR /opt/local/share/OpenCV)
I say "fix" because this is not a good way of doing things. Your CMakeLists.txt should be free of absolute paths. Instead, set -DCMAKE_PREFIX_PATH=/opt/local on the command line or in a preset.

grpc can't find protobuf library

I went over the grpc installation and finished building and installation.
Now when I try to:
find_package(gRPC CONFIG REQUIRED)
I get
CMake Error at CMakeLists.txt:15 (find_package):
Found package configuration file:
/usr/lib64/cmake/grpc/gRPCConfig.cmake
but it set gRPC_FOUND to FALSE so package "gRPC" is considered to be NOT
FOUND. Reason given by package:
The following imported targets are referenced, but are missing:
protobuf::libprotobuf protobuf::libprotoc
Event though
find_package(Protobuf REQUIRED)
Works just fine.
I read I'm supposed to run cmake ../.. -DBUILD_DEPS=ON -DBUILD_SHARED_LIBS=ON to solve this. However that results in:
CMake Error at cmake/abseil-cpp.cmake:38 (find_package):
Could not find a package configuration file provided by "absl" with any of
the following names:
abslConfig.cmake
absl-config.cmake
Add the installation prefix of "absl" to CMAKE_PREFIX_PATH or set
"absl_DIR" to a directory containing one of the above files. If "absl"
provides a separate development package or SDK, be sure it has been
installed.
Call Stack (most recent call first):
CMakeLists.txt:191 (include)
-- Configuring incomplete, errors occurred!
See also "/home/ray/CLionProjects/grpc/grpc/CMakeFiles/CMakeOutput.log".
See also "/home/ray/CLionProjects/grpc/grpc/CMakeFiles/CMakeError.log".
Content of /usr/lib64/cmake/grpc/gRPCConfig.cmake
# Module path
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/modules)
# Depend packages
# Targets
include(${CMAKE_CURRENT_LIST_DIR}/gRPCTargets.cmake)
The problem is actually with the project you use via find_package() and with its package file (/usr/lib64/cmake/grpc/gRPCConfig.cmake in your case). The error message
The following imported targets are referenced, but are missing:
means, that the package file references IMPORTED targets but they are never defined.
The usual reason for that problem is following:
During its own build, the project uses find_package() for some other packages. This find_package() call defines IMPORTED targets, which are used in the project for link.
Its package file includes the script(s), which is created by install(EXPORT) command and filled according to install(TARGETS ... EXPORT ...) command. This included script uses IMPORTED targets, but doesn't define them.
The project's package file forgets to use find_package, or, better, find_dependency for define the IMPORTED target for the included scripts.
If you don't want to fix the (other) project's package file, then the most direct solution is to add missed find_package into your own project:
# Hack: This will define IMPORTED targets, needed for gRPC project, but not defined by it.
find_package(Protobuf REQUIRED)
# Now perform the original 'find_package' call.
find_package(gRPC CONFIG REQUIRED)
Actually, gRPCConfig.cmake package file was intended to contain the call find_package(Protobuf). Its template cmake/gRPCConfig.cmake.in is following:
# Module path
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/modules)
# Depend packages
#_gRPC_FIND_ZLIB#
#_gRPC_FIND_PROTOBUF#
#_gRPC_FIND_SSL#
#_gRPC_FIND_CARES#
#_gRPC_FIND_ABSL#
# Targets
include(${CMAKE_CURRENT_LIST_DIR}/gRPCTargets.cmake)
and on substitution the variable _gRPC_FIND_PROTOBUF should emit the following code:
if(NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND)
find_package(Protobuf ${gRPC_PROTOBUF_PACKAGE_TYPE})
endif()
(the variable should be set in cmake/protobuf.cmake).
But something goes wrong, and the resulted /usr/lib64/cmake/grpc/gRPCConfig.cmake contains the empty variable's substitution:
# Module path
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/modules)
# Depend packages
# Targets
include(${CMAKE_CURRENT_LIST_DIR}/gRPCTargets.cmake)

CMake and external dependency

I'd like to add an external dependency to my project. The one I'm trying to add is the Leptonica library as a submodule.
My project has the following directory structure:
|root
CMakeLists.txt
|-bin
|-build
|-buildsystem
|-executable
|-leptonica
|--CMakeLists.txt
|--cmake
|---Configure.cmake
|-production
In my root CMakeLists.txt file I added ADD_SUBDIRECTORY(${ROOT_DIR}/leptonica)
Unfortunately, CMake is not searching for Configure.cmake in the proper directory:
CMake Error at leptonica/CMakeLists.txt:107 (include):
include could not find load file:
Configure
CMake Error: File
<root>/cmake/templates/LeptonicaConfig-version.cmake.in does not exist.
CMake Error at leptonica/CMakeLists.txt:113 (configure_file):
configure_file Problem configuring file
When I build the project by myself, everything goes fine. In my opinion, the problem is with CMAKE_SOURCE_DIR. When using add_subdirectory it has the value of ROOT CMake instead ROOT/leptonica, so it's searching the wrong paths - as you can see in Leptonica CMake, it's used to determinate paths of its files.
What should be the proper way to fix this - should I set CMAKE_SOURCE_DIR to ROOT/leptonica just before calling add_subdirectory and set it back when it's finished, or does some other, more elegant solutions exist?
Not every CMake project is suitable for inclusion via add_subdirectory.
Among those are projects which uses CMAKE_SOURCE_DIR or CMAKE_BINARY_DIR variables.
However, inclusion via ExternalProject_Add (optionally wrapped with execute_process) always works.
Modifying variable CMAKE_SOURCE_DIR (and CMAKE_BINARY_DIR too) is a bad idea: this variable should be changed only by CMake itself. Otherwise you may get weird errors.
Instead, you may replace (automatically, with some script) all references to the variable with another variable, which is not used in the project. This new variable you may safely set before stepping into the subproject.
${CMAKE_SOURCE_DIR} and ${CMAKE_BINARY_DIR} are set relative to the top-level CMakeLists.txt. If you need something relative to your current CMakeLists.txt (leptonica), use ${CMAKE_CURRENT_SOURCE_DIR} and ${CMAKE_CURRENT_BINARY_DIR}.
If you're having trouble finding a cmake file like LeptonicaConfig-version.cmake.in, try appending the appropriate directory to ${CMAKE_MODULE_DIR}.
list(APPEND ${CMAKE_MODULE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/templates)
I prefer to use ${CMAKE_CURRENT_SOURCE_DIR} over ${CMAKE_SOURCE_DIR} any day because using the latter will break your build if you try to integrate it into a super-build later. If I need to pass my current top-level directory to subdirectories, then I do the following and use that later down the chain.
set( LEPTONICA_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )

CMake find_package() cannot find include dirs

I am trying to install two projects: Foo and NeedsFoo. I have successfully compiled and locally installed Foo using cmake. However, I'm on a server and cmake doesn't appear to "remember" where Foo is.
In the cmake to configure NeedsFoo I have
list(APPEND CMAKE_MODULE_PATH "<prefix>/foo-install/CMake/FOO") # add path to FOOConfig.cmake
find_package(FOO REQUIRED)
if( FOO_FOUND )
MESSAGE(STATUS "Found FOO!")
endif( FOO_FOUND )
MESSAGE(STATUS ${FOO_INCLUDE_DIRS})
"Found Foo!" is printed --- so cmake finds FOO --- but the variable ${FOO_INCLUDE_DIRS} is empty and, therefore, the package does not compile. Any thoughts?
EDIT: There seems to be another copy of Foo installed on the server. Unfortunately, I can't use it (it is the 'master' branch of our project and I need to use my own branch). I tried changing the find_package call to
find_package(FOO REQUIRED PATHS "<prefix>/foo-install/CMake/Foo" NO_DEFAULT_PATH)
but that did not solve the problem.
Make sure the headers are installed, often you'll need to install a -dev or -devel package to get the headers. If they are installed and you can see them in your system, they might be on a path that cmake isn't expecting. Open the findFoo.cmake file (on Linux this will generally be in /usr/share/c make-x.y/modules) and check the list of expected locations, see if you're have installed in a less conventional one.

cmake - find_library - custom library location

I'm currently trying to get CMake running for my project (on windows). I want to use a custom location where all libraries are installed. To inform CMake about that path I tried to do that:
set(CMAKE_PREFIX_PATH D:/develop/cmake/libs)
But when I try to find the library with
find_library(CURL_LIBRARY NAMES curl curllib libcurl_imp curllib_static)
CMake can't find it.
When I set my prefix path to
set(CMAKE_PREFIX_PATH D:/develop/cmake/libs/curl)
... the library is located.
So my question is:
How can I configure CMake properly to work with a directory structore at a custom location which looks like that:
D:/develop/cmake/libs/
-> libA
-> include
-> lib
-> libB
-> include
-> lib
-> ...
-> include
-> lib
In "include" lie the public headers and in "lib" are the compiled libraries.
edit:
The current workaround for me is, to do this before i search for libraries:
set(CUSTOM_LIBRARY_PATH D:/develop/cmake/libs)
file(GLOB sub-dir ${CUSTOM_LIBRARY_PATH}/*)
foreach(dir ${sub-dir})
if(IS_DIRECTORY ${dir})
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH};${dir})
endif()
endforeach()
But that way the default module for boost wont find it until it because the directory structore of boost is a bit different.
boost -> include -> boost-1_50 -> *.hpp
When I move the content if "boost-1_50" to "include" the library can be found but that way it's not possible to handle multiple versions right?
The simplest solution may be to add HINTS to each find_* request.
For example:
find_library(CURL_LIBRARY
NAMES curl curllib libcurl_imp curllib_static
HINTS "${CMAKE_PREFIX_PATH}/curl/lib"
)
For Boost I would strongly recommend using the FindBoost standard module and setting the BOOST_DIR variable to point to your Boost libraries.
I saw that two people put that question to their favorites so I will try to answer the solution which works for me:
Instead of using find modules I'm writing configuration files for all libraries which are installed. Those files are extremly simple and can also be used to set non-standard variables. CMake will (at least on windows) search for those configuration files in
CMAKE_PREFIX_PATH/<<package_name>>-<<version>>/<<package_name>>-config.cmake
(which can be set through an environment variable).
So for example the boost configuration is in the path
CMAKE_PREFIX_PATH/boost-1_50/boost-config.cmake
In that configuration you can set variables. My config file for boost looks like that:
set(boost_INCLUDE_DIRS ${boost_DIR}/include)
set(boost_LIBRARY_DIR ${boost_DIR}/lib)
foreach(component ${boost_FIND_COMPONENTS})
set(boost_LIBRARIES ${boost_LIBRARIES} debug ${boost_LIBRARY_DIR}/libboost_${component}-vc110-mt-gd-1_50.lib)
set(boost_LIBRARIES ${boost_LIBRARIES} optimized ${boost_LIBRARY_DIR}/libboost_${component}-vc110-mt-1_50.lib)
endforeach()
add_definitions( -D_WIN32_WINNT=0x0501 )
Pretty straight forward + it's possible to shrink the size of the config files even more when you write some helper functions. The only issue I have with this setup is that I havn't found a way to give config files a priority over find modules - so you need to remove the find modules.
Hope this this is helpful for other people.
Use CMAKE_PREFIX_PATH by adding multiple paths (separated by semicolons and no white spaces). You can set it as an environmental variable to avoid having absolute paths in your cmake configuration files
Notice that cmake will look for config file in any of the following folders
where is any of the path in CMAKE_PREFIX_PATH and name is the name of the library you are looking for
<prefix>/ (W)
<prefix>/(cmake|CMake)/ (W)
<prefix>/<name>*/ (W)
<prefix>/<name>*/(cmake|CMake)/ (W)
<prefix>/(lib/<arch>|lib|share)/cmake/<name>*/ (U)
<prefix>/(lib/<arch>|lib|share)/<name>*/ (U)
<prefix>/(lib/<arch>|lib|share)/<name>*/(cmake|CMake)/ (U)
In your case you need to add to CMAKE_PREFIX_PATH the following two paths:
D:/develop/cmake/libs/libA;D:/develop/cmake/libB
There is no way to automatically set CMAKE_PREFIX_PATH in a way you want. I see following ways to solve this problem:
Put all libraries files in the same dir. That is, include/ would contain headers for all libs, lib/ - binaries, etc. FYI, this is common layout for most UNIX-like systems.
Set global environment variable CMAKE_PREFIX_PATH to D:/develop/cmake/libs/libA;D:/develop/cmake/libs/libB;.... When you run CMake, it would aautomatically pick up this env var and populate it's own CMAKE_PREFIX_PATH.
Write a wrapper .bat script, which would call cmake command with -D CMAKE_PREFIX_PATH=... argument.
You have one extra level of nesting.
CMAKE will search under $CMAKE_PREFIX_PATH/include for headers and $CMAKE_PREFIX_PATH/libs for libraries.
From CMAKE documentation:
For each path in the CMAKE_PREFIX_PATH list, CMake will check
"PATH/include" and "PATH" when FIND_PATH() is called, "PATH/bin" and
"PATH" when FIND_PROGRAM() is called, and "PATH/lib and "PATH" when
FIND_LIBRARY() is called.
I've encountered a similar scenario. I solved it by adding in this following code just before find_library():
set(CMAKE_PREFIX_PATH /the/custom/path/to/your/lib/)
then it can find the library location.