CMake: How can I add a search path for include() from the environment? - cmake

I try to create a collection of CMake scripts that are supposed to provide some useful macros and functions for our group of developers and all the projects we're working on. These scripts should be rolled out to all development machines (Win10 & Centos Linux) and the top-level CMakeLists.txt of the projects can include this collection.
From what I read from the CMake docs, include() is what should be used to import these scripts. But, of course, CMake doesn't know about the location on the file system. The documentation points to CMAKE_MODULE_PATH, but I couldn't figure out a way to set it from "outside" on a global scope, e.g. an environment variable or a CMake configuration value. Setting it from within a project's CMake file would drop portability, as especially on Windows users potentially could choose the install directory of the scripts.
I also don't want to deploy the script collection into CMake's installation directory. Although it would work, it feels dirty to mix up my own scripts with the ones from the CMake distribution.
I could use find_package() instead and also provide a package config file and use the CMAKE_PREFIX_PATH environment variable. But from my understanding of the docu this is meant to be for build dependencies, e.g. libraries.
I also found questions similar to mine here and in other places, but they usually were about importing external projects for building the own one ( -> find_package()). And, of course, about include directories for the compilation process. If I just didn't found the proper question & answer, please point me there.
So, what is the best/proper way to make CMake aware of my script collection? Preferably in a way that just a call to include() is required in a project's CMakeLists.txt.

but I couldn't figure out a way to set it from "outside" on a global scope, e.g. an environment variable or a CMake configuration value.
Just cmake -D CMAKE_MODULE_PATH=/some/path. Works for me:
$ cd /tmp; echo "include(ulumulu)" > CMakeLists.txt ; strace -e trace=file cmake -D CMAKE_MODULE_PATH=/some/path . |& grep access | grep ulumulu
access("/some/path/ulumulu.cmake", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/share/cmake-3.19/Modules/ulumulu.cmake", R_OK) = -1 ENOENT (No such file or directory)
access("/tmp/ulumulu", R_OK) = -1 ENOENT (No such file or directory)
How can I add a search path for include() from the environment?
I would setup my own logic. As simplest as include($ENV{SEARCH_IN_THIS_PATH}), but way better would be with cmake -D PATH_TO_MY_LIBRARY=some_path and then include(${PATH_TO_MY_LIBRARY}).
I could use find_package() instead and also provide a package config file and use the CMAKE_PREFIX_PATH environment variable. But from my understanding of the docu this is meant to be for build dependencies, e.g. libraries.
And module mode find_package( ... MODULE) is for finding modules. I would use it. And I would use it also not for libraries.
what is the best/proper way to make CMake aware of my script collection?
Install your script at /usr/share/cmake/ path on linux and I think c:/Program Files/CMake/share/cmake on windows (I have no experience in windows) and use find_package(the_library MODULE).
If not, I recommend just use find_package anyway and install your Find*.cmake file to <prefix>/<name>/cmake/.

Related

Qt example project fails to build (No CMake configuration found)

I am trying to simply open Qt6.4.1 sensors example project, but it says that no CMake configuration found. I already made some simple Qt6 applications for Windows, and i have entire Qt6.4.1 package installed, so cant blame on bad installation. There is an error on line find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick Sensors Svg) in CMakeLists.txt. Full error message:
C:\Qt\Examples\Qt-6.4.1\sensors\sensorsshowcase\CMakeLists.txt:12: error: By not providing "FindQt6.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Qt6", but CMake did not find one. Could not find a package configuration file provided by "Qt6" with any of the following names: Qt6Config.cmake qt6-config.cmake Add the installation prefix of "Qt6" to CMAKE_PREFIX_PATH or set "Qt6_DIR" to a directory containing one of the above files. If "Qt6" provides a separate development package or SDK, be sure it has been installed.
Pass the path to the directory containing the Qt6 files via CMAKE_PREFIX_PATH cache variable during configuration.
Note: Make sure to specify the type of the variable as PATH or use forward slashes as path separator for this to work properly.
E.g. for me I'd need to pass
-D CMAKE_PREFIX_PATH=D:/Qt/6.4.1/msvc2019_64
to tell CMake to look into the directory containing the Qt6.4.1 files compiled with MSVC.
Make sure that the directory you pass contains the file lib/cmake/Qt6/Qt6Config.cmake; this is the file find_package(Qt6 REQUIRED COMPONENTS ...) is looking for.
You can also fix your project setup that previously failed by adding the cache variable.
cmake -D CMAKE_PREFIX_PATH=D:/Qt/6.4.1/msvc2019_64 path/to/build_dir
Note: Don't forget to add the -A ... option when configuring a VS project; CMake defaults to Win32 as architecture, at least on my system.
You could add this info to a CMAKE_PREFIX_PATH environment variable, if you don't want to specify the info cache variable for every single project using qt 6 that you want to set up on your machine.

cmake script mode what is supported and what not

I'm using cmake to build one of my projects, I see the way it installs files is calling a cmake script by cmake -P cmake_install.cmake, but the functions used in this cmake file looks different than what is documented, e.g. for shared library install target it has:
file(RPATH_CHECK FILE ... RPATH ...)
But I cannot find this file sub-command in the cmake documentation, so is there a place that have the available functions to use in script mode?
This looks like an internal command for cmake internal use.
so is there a place that have the available functions to use in script mode?
The source code is the ultimate documentation https://gitlab.kitware.com/cmake/cmake/-/blob/master/Source/cmFileCommand.cxx#L3757 .
There is no difference in available functions between -P and cmake . invocations. You can use file(RPATH_CHECK in any cmake.

How to deploy a Find*.cmake file for an Autotools library in the correct place for Yocto?

I created a new layer over an existing Yocto git for my company project.
In this layer I added a few external autotools based libraries.
A few applications need to link against this libraries and the application projects are all cmake based.
Taking one of this libraries (e.g. libcoap) I could easily find some FindCoAP.cmake to add to my library recipe.
Now if I was running on PC, it would simply be a matter of placing this FindCoAP.cmake file in cmake's ${CMAKE_ROOT}/Modules dir, but how should I, from inside a bitbake recipe (do_install hook), proceed to make my Find*.cmake modules available to anyone's dependent projects?
Should I try to get Yocto's cmake CMAKE_ROOT variable from system-information like this or is it a safer and more reliable way?
do_install_append() {
cmake --system-information | grep CMAKE_ROOT | cut -d \" -f2
install -d ${D}/$CMAKE_ROOT}/Modules
install ${S}/FindCoAP.cmake ${D}/$CMAKE_ROOT}/Modules
}
Thanks in advance.
To ship FindFoo.cmake with non-yet-cmake project
The ideal way is to update upstream project itself. So you will update your recipe and package FindFoo.cmake appropriately.
If you want to do it right now:
Add FindFoo.cmake to your layer (into the files directory next to your recipe).
Add that cmake file to SRC_URI (i.e. SRC_URI += "file://FindFoo.cmake").
Install it in do_install into the directory ${D}${datadir}/cmake/Modules/ for example.
Package it to the dev package by FILES_${PN}-dev variable (see example recipes below).
To use that cmake by other recipe
The usual way is to package .cmake files into the ${PN}-dev package. In your case, your application (which depends on the libcoap) will just set DEPENDS = "libcoap" and all the needed files (like headers, libraries and cmake file) will be copied (well, hardlinked) to the sysroot of your application.
CMake modules are packaged in various recipes for example:
libeigen
opencv
json-spirit
Your application is cmake based, so you will use inherit cmake in the recipe. Native module search path is set in cmake.bbclass.
(BTW, I do a build test of libcoap recipe from homeassistant layer and it worked, but obviously there is no cmake shipped.)

Best practices with CMake with non-standard include and library directories

I have been trying to build Mozilla RR on a Linux box at work using CMake. We have a slightly eccentric arrangement where shared libraries are stored on network drives in locations like /sw/external/product-name/linux64_g63.dll/. Further, I have built some dependencies for the project in $HOME/sw/. (I am not a sudoer on this box.)
I am rather baffled as how I am supposed to communicate to CMake to look in non-standard directories. So far I have fudged:
PKG_CONFIG_PATH=$HOME/sw/capnproto-0.6.1/lib/pkconfig \
CC=gcc-6.3 CXX=g++-6.3 \
cmake \
-DCMAKE_INSTALL_PREFIX=$HOME/sw/rr-5.1.0 \
-DPYTHON_EXECUTABLE=$HOME/bin/python2 \
-DCMAKE_FIND_ROOT_PATH=$HOME/sw/libseccomp-2.2.3/ \
../src/
Which is obviously not a scalable solution, but it does at least complete the configuration successfully and emit some Makefiles.
If I omit -DCMAKE_FIND_ROOT_PATH=$HOME/sw/libseccomp-2.2.3/, CMake fails, complaining about a missing libseccomp-2.2.3 dependency. But it works if I do have that definition, telling me that CMake understands where the libseccomp-2.2.3 files are and so will properly add the paths to the necessary compiler invocations.
However, make does not succeed, because gcc fails to find a required header file from the libseccomp probject. Examining make VERBOSE=1, I find that CMake hasn't added -I$HOME/sw/libseccomp-2.2.3/include to the gcc invocation.
I feel like this is not the right approach. The other answers I have looked at tell me to modify the CMakeLists.txt file, but surely
that is not going to be scalable across multiple CMake projects, and
for each project, that will need me to maintain a separate CMakeLists.txt file for every platform (Solaris/Linux/Darwin/Cygwin) I build the software on.
Is there a canonical solution to solving this problem? Perhaps a per-site configuration file that will tell CMake how to find libraries and headers, for all projects I build on that site?
Your approach is correct, but cmake is never told to include SECCOMP - see end of this post.
The way cmake can be informed about custom dependency directory depends on how the dependency is searched (i.e. on what is written in CMakeLists.txt).
find_package/find_library/find_path/find_program
If dependency is found with one of above-mentioned commands, custom search directories can be easily added with CMAKE_PREFIX_PATH. There is no need to add full path to include, lib or bin - when package root is added find_-command will check appropriate sub-directories. CMAKE_PREFIX_PATH can be also set with environment variable.
Second option is CMAKE_FIND_ROOT_PATH. Every path added to CMAKE_FIND_ROOT_PATH list treated as separate root directory and is searched before system root directory.
Note that CMAKE_FIND_ROOT_PATH will be ignored by find_-commands with NO_CMAKE_FIND_ROOT_PATH argument.
Following four variables may be used to tune the usage of CMAKE_FIND_ROOT_PATH:
CMAKE_FIND_ROOT_PATH_MODE_PACKAGE
CMAKE_FIND_ROOT_PATH_MODE_INCLUDE
CMAKE_FIND_ROOT_PATH_MODE_LIBRARY
CMAKE_FIND_ROOT_PATH_MODE_PROGRAM
When use of host system default libraries is undesired setting CMAKE_FIND_ROOT_PATH_MODE_INCLUDE and CMAKE_FIND_ROOT_PATH_MODE_LIBRARY to ONLY is a good practice. If dependency library or header is not found in CMAKE_FIND_ROOT_PATH the configuration will fail. If cmake is allowed search system paths too, it is most likely that errors will occur during linking step or even runtime.
See find_package docs for more details.
find_package only
All above applies to find_package command too.
find_package can operate in two modes MODULE and CONFIG.
In MODULE mode cmake uses Find[PackageName].cmake script (module) to find dependent package. CMake comes with large number of modules and custom modules can be added with CMAKE_MODULE_PATH variable. Often find-modules can be informed about custom search paths via environment or cmake variables.
E.g. FindGTest.cmake searches path stored in GTEST_ROOT variable.
If no find module is available, find_package enters CONFIG mode. If a dependency package provides [PackageName]Config.cmake or [LowercasePackageName]-config.cmake cmake can be easily informed about that package with [PackageName]_DIR variable.
Example:
CMakeLists.txt contains:
find_package(Qt5)
FindQt5.cmake is not available, but ~/Qt5/Qt5.8/lib/cmake/Qt5Config.cmake file exists, so add
-DQt5_DIR="${HOME}/Qt5/Qt5.8/lib/cmake"
to cmake call.
pkg-config
CMake can use information provided by external pkg-config tool. It is usually done with pkg_check_modules command. Directory used by pkg-config can be customized with PKG_CONFIG_PATH environment variable. According to cmake documentation instead of setting PKG_CONFIG_PATH, custom .pc-files directories can be added via CMAKE_PREFIX_PATH. If CMake version is pre-3.1, PKG_CONFIG_USE_CMAKE_PREFIX_PATH have to be set to TRUE(ON) to enable this feature.
Methods of customizing dependencies search path is defined by CMakeLists.txt content. There is no universal solution here.
And now back to missing SECCOMP headers...
In CMakeLists.txt SECCOMP header is found with
find_path(SECCOMP NAMES "linux/seccomp.h")
but I cannot find any command telling CMake to use the found header. For example:
target_include_directories(<target_name> ${SECCOMP})
or globally:
include_directories(${SECCOMP})
I belive that CMakeLists.txt should be fixed. It is not a platform dependent solution.

Is it possible to alter CMAKE_MODULE_PATH from CMake commandline?

Edit: The accepted answer actually shows that it is pretty normally possible to set CMAKE_MODULE_PATH as any other CMake variable e.g. via the -DCMAKE_MODULE_PATH path CLI parameter. It seems that in my case there is some included CMake script that calls set(CMAKE_MODULE_PATH /library_path), which erases all previous paths set to the variable. That's why I couldn't get the variable to do what I wanted it to do. I'll leave the question here in case anybody else faces this kind of situation.
I'm building a (3rd party) project that uses the Protobuf library (but this question is general). My system has a system-wide install of a newer version of Protobuf than the project is compatible with. So I've downloaded and compiled from source an older version of Protobuf.
The project uses CMake, and in its CMakeLists.txt, there is:
find_package(Protobuf REQUIRED)
Which, however, finds the (incompatible) system install. Of course, CMake doesn't know about my custom build of Protobuf. But how do I tell it?
I've created a FindProtobuf.cmake file in, say, ~/usr/share/cmake-3.0/Modules/ and want the build process to use this one for finding Protobuf. But I haven't succeeded forcing CMake to pick up this one and not the system one. I think the reason is quite obvious from the CMake docs of find_package:
The command has two modes by which it searches for packages: “Module” mode and “Config” mode. Module mode is available when the command is invoked with the above reduced signature. CMake searches for a file called Find<package>.cmake in the CMAKE_MODULE_PATH followed by the CMake installation. If the file is found, it is read and processed by CMake. ... If no module is found and the MODULE option is not given the command proceeds to Config mode.
So until I succeed to change CMAKE_MODULE_PATH, CMake will just pick up the FindProtobuf.cmake installed to the default system path and won't ever proceed to the "Config" mode where I could probably make use of CMAKE_PREFIX_PATH.
It's important for me to not edit the CMakeLists.txt since it belongs to a 3rd party project I don't maintain.
What I've tried (all without success):
calling CMAKE_MODULE_PATH=~/usr/share/cmake-3.0/Modules cmake ... (the env. variable is not "transferred" to the CMake variable with the same name)
calling cmake -DCMAKE_MODULE_PATH=~/usr/share/cmake-3.0/Modules ... (doesn't work, probably by design?)
calling Protobuf_DIR=path/to/my/protobuf cmake ... (the project doesn't support this kind of override for Protobuf)
It seems to me that, unfortunately, the only way to alter the CMAKE_MODULE_PATH used by find_package is to alter it from within CMakeLists.txt, which is exactly what I want to avoid.
Do you have any ideas/workarounds on how not to touch the CMakeLists.txt and still convince find_package to find my custom Protobuf?
For reference, the CMake part of this project is on github .
As a direct answer to your question, yes, you can set CMAKE_MODULE_PATH at the command line by running cmake -DCMAKE_MODULE_PATH=/some/path -S /path/to/src -B /path/to/build.
But that probably doesn't do what you want it to do; see below.
The Bitbucket link you supplied is dead, but here are a few suggestions that might help.
Avoid writing your own find modules, especially when the upstream supplies CMake config modules.
You can direct CMake to your custom Protobuf installation by setting one of CMAKE_PREFIX_PATH or Protobuf_ROOT (v3.12+) to the Protobuf install root.
You can tell find_package to try CONFIG mode first by setting CMAKE_FIND_PACKAGE_PREFER_CONFIG to true (v3.15+). Then set Protobuf_DIR to the directory containing ProtobufConfig.cmake.
Failing all else, you can manually set the variables documented in CMake's own FindProtobuf module, here: https://cmake.org/cmake/help/latest/module/FindProtobuf.html
All these variables can be set at the configure command line with the -D flag.
There are very few environment variables that populate CMake variables to start and I would avoid relying on them. There is an exhaustive list here: https://cmake.org/cmake/help/latest/manual/cmake-env-variables.7.html. CMAKE_MODULE_PATH is not among them.