I am using CMake with a custom toolchain that I built using yocto. I have a problem though, the toolchain has sysroot for the target machine and one for the build machine.
CMake keeps finding the libraries in the build system sysroot only.
For example I am using:
find_package(libxml2)
But it always keeps finding libxml2 in the build system sysroot instead of the target sysroot. How can I tell it to only look in the target sysroot?
Cross-compiling is fully supported by CMake, ranging from cross-compiling from Linux to Windows; cross-compiling for supercomputers, through to cross-compiling for small embedded devices without an operating system (OS). Cross-compiling has several consequences for CMake: CMake cannot automatically detect the target platform.
When the basic signature is used, the command searches in Module mode first. If the package is not found, the search falls back to Config mode. A user may set the CMAKE_FIND_PACKAGE_PREFER_CONFIG variable to true to reverse the priority and direct CMake to search using Config mode first before falling back to Module mode.
How to use host tools using find_package when cross-compiling (eg. Qt5LinguistTools) Cross compile a Qt application: building on Linux and target Windows using MinGW Use Qt Linguists tools from the host using find_package (Qt5LinguistTools) to build translations files
CMake is a great tool for cross platform software development. It uses a set of utilities called a toolchain to drive the build. There are two main scenarios of using CMake for builds: Normal builds where CMake is responsible for choosing the toolchain
How can I tell it to look in the target sysroot only?
There is a family of CMake variables CMAKE_FIND_ROOT_PATH_MODE_*
, which adjusts search strategy for different CMake commands:
BOTH value means that both target and host (build) paths are searched. This is also a default behavior, when a variable is not set.
ONLY value means that only target is searched.
NEVER value means, that only host is searched.
List of variables:
CMAKE_FIND_ROOT_PATH_MODE_LIBRARY affects on find_library()
calls
CMAKE_FIND_ROOT_PATH_MODE_INCLUDE affects on find_path()
and find_file()
calls
CMAKE_FIND_ROOT_PATH_MODE_PACKAGE affects on find_package()
in CONFIG mode (when *Config.cmake
file is searched).
CMAKE_FIND_ROOT_PATH_MODE_PROGRAM affects on find_program()
call.
Generally, concrete find_package()
call may be affected by all of these variables. In case of searching libraries, it is usually suffificient to set only 3 of them:
# Search libraries only under *target* paths.
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
Variables CMAKE_FIND_ROOT_PATH_MODE_*
are normally set in toolchain files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With