-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[bug] Conan setting CMAKE_FIND_ROOT_PATH_MODE_*=BOTH
fundamentally breaks cross-compilation
#16324
Comments
Hi @tttapa Thanks for your report. Can you please clarify what Conan version are you using? Conan 2 sets only: {% if build_paths %}
# The explicitly defined "builddirs" of "host" context dependencies must be in PREFIX_PATH
list(PREPEND CMAKE_PREFIX_PATH {{ build_paths }})
{% endif %}
{% if cmake_program_path %}
list(PREPEND CMAKE_PROGRAM_PATH {{ cmake_program_path }})
{% endif %} from the build context, should this 2 variables CMAKE_PREFIX_PATH or CMAKE_PROGRAM_PATH make the |
Hi, I'm using version 2.3.1. At first sight, the code you posted looks fine, the problematic lines are further down: conan/conan/tools/cmake/toolchain/blocks.py Lines 484 to 502 in 95da083
Looking at the git blame, the last change to this bit of code was two years ago in 861170c. |
If I remember correctly, if
I could agree that if the variable is already defined with an The only case where I could see this working properly is when the only package provided by conan is the sysroot itself, and the sysroot contains everything and no individual libraries are provided as conan packages. The provided example is fine and valid, but in the situation described, both are an error for
Obviously I can see how erroring out early is much preferable, as it would point you to arrive at the solution sooner - but once the underlying problem is addressed ( |
This is true, but only because Conan does not currently set
No, this is not a given. For example, I'm in a situation where I'm cross-compiling with build=x86_64-linux-gnu and host=x86_64-linux-gnu, but they're different systems with different Linux distributions, different toolchains, and different versions of glibc etc. Even though it may happen to link without errors, linking to binaries from the build system will still result in binaries that do not run once deployed on the host system. Or even worse: it may appear to run correctly in most cases, but ABI incompatibilities or ODR violations cause it to crash sporadically or make it exploitable to bad actors. Second, when cross-compiling, one often prepares a sysroot with specific versions of packages. If for some reason, CMake is unable to find the intended package in the sysroot (e.g. because of a typo in the paths provided by the user), or it is unable to find the Conan-provided package, CMake should should fail, not silently link to a random version of the package it picked up in a |
The new CMakeDeps generator in #16964 will propose a new ay of locating config files. |
Thanks! I plan to look into this some more and propose some changes, but I haven't found the time yet, unfortunately. |
We are releasing in Conan 2.9 a completely new
Current known pending functionality (to be added soon):
The new Your feedback is very importantAs this is a major change, we will only remove the conf gate when we get confirmation from users that it works and solve the issues. Please try the new generator for your project, and let us know if it works. If it doesn't, please re-open this ticket and let us know what failed. Thanks very much! |
Hi @memsharded, I've tried the new CMakeDeps generator, and ran into some problems:
If useful, I could open new issues with more information. |
Describe the bug
When cross-compiling, it is essential that the build system and the host system are strictly separated. In CMake, this can be ensured by setting
CMAKE_FIND_ROOT_PATH_MODE_{LIBRARY,INCLUDE,PACKAGE}=ONLY
, which tells the correspondingfind_*
command to only look for libraries, headers and packages in the host system's sysroot or in theCMAKE_FIND_ROOT_PATH
.However, when
tools.build.cross_building:cross_build=true
, Conan silently (!) sets these variables toBOTH
, and in doing so it allows CMake to look for libraries, headers and packages in the build system's root filesystem as well. As a result, packages cross-compiled using Conan can silently link to wrong versions of libraries it found in/usr/lib
, headers of different versions than intended, etc.In the best case, this results in linker errors because of incompatible architectures, in the worst case, the resulting binaries are subtly broken because they depend on libraries (or versioned symbols in those libraries) that are unavailable on the host system.
If a user explicitly sets
CMAKE_FIND_ROOT_PATH_MODE_{LIBRARY,INCLUDE,PACKAGE}=ONLY
in their toolchain file, it is often for good reasons, and IMHO silently changing those values inside of Conan's toolchain file is not an acceptable solution.I can help with a PR, but wanted to discuss here first. In the meantime, I'd suggest adding a very clear warning to Conan's toolchain file if it detects that the user set these variables to
ONLY
. (It will still be broken, but at least then the people know that it's broken.)Here are some previous discussions on the same topic:
Addressing some of the comments there:
I don't believe that this is a problem. Re-rooting all paths is exactly what you want when cross-compiling. If a user wants to search the build system's paths as well, they should not set
CMAKE_FIND_ROOT_PATH_MODE_*=ONLY
in the first place. But that's for the user to decide, Conan should not silently change this.Exactly, that's what you want when cross-compiling.
Not if you set
CMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER
, which is the common use case when cross-compiling.No, this is not the correct conclusion. They should stay separate, and
CMAKE_FIND_ROOT_PATH
andCMAKE_FIND_ROOT_PATH_MODE_*
are exactly the tools that CMake provides to allow you to keep them separate.Which is why you usually don't see people setting
CMAKE_FIND_ROOT_PATH_MODE_PROGRAM=ONLY
when cross-compiling :)How to reproduce it
Tested on Linux, you can find a reproducible version with Docker here:
Script: https://github.com/tttapa/conan-find-mode-bug/blob/main/.github/workflows/test.yml
Output: https://github.com/tttapa/conan-find-mode-bug/actions/runs/9197591352/job/25298402017
conanfile.txt
toolchain.cmake
CMakeLists.txt
Let's first build this project using only CMake:
cmake -B build-cmake -S . --toolchain toolchain.cmake
This works as expected,
libreadline.so
is not found, because it does not exist in the (empty) sysroot.In the output, you'll see that CMake only searched the allowed locations: the host system's sysroot, and the directories explicitly allowed by
CMAKE_FIND_ROOT_PATH
.Now using Conan:
This fails. In the output, you can see that CMake is looking in folders like
/usr/lib
on the build system. Eventually, it finds the file in/usr/lib/x86_64-linux-gnu/libreadline.so
. If you're cross-compiling, this file is not likely to be compatible with your host system (e.g. different architecture, different glibc version, different ABI, different compilation flags, etc.), and the resulting binaries are now broken.The text was updated successfully, but these errors were encountered: