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
[feature] Set CMAKE_FIND_PACKAGE_PREFER_CONFIG
in conan_toolchain.cmake
#8582
Comments
I think it makes sense, could you please have a look and propose a PR if looks good to you @SSE4 ? |
do we just need this one variable? sounds narrow, maybe we should allow arbitrary variables to be defined in toolchain instead? |
I read this one as special, it is a paradigm, that goes together with the xxx-config.cmake files that |
I am not sure to be honest. there are so many CMake variables which alter CMake behavior in one way or another. I don't see why just and okay, we may set, but how would users specify its value? is the following enough?
|
I understand this should be a default, if our toolchain is going to use Config files. Every recipe should define it (it is a boolean, IIUC, so it would be setting it to set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON)
# To support the cmake_find_package generators
{% if cmake_module_path -%}
set(CMAKE_MODULE_PATH {{ cmake_module_path }} ${CMAKE_MODULE_PATH})
{%- endif %}
{% if cmake_prefix_path -%}
set(CMAKE_PREFIX_PATH {{ cmake_prefix_path }} ${CMAKE_PREFIX_PATH}) We could do the counter argument and say that all recipes should do: def generate(self):
tc = CMakeToolchain(self)
tc.definitions["CMAKE_FIND_PACKAGE_PREFER_CONFIG"] = some_value
tc.definitions["CMAKE_MODULE_PATH "] = self.install_folder
tc.definitions["CMAKE_PREFIX_PATH "] = self.install_folder
tc.generate() But that seems too verbose, and it would make impossible to use the simple |
so, if we decide to always define it, how do we pass the value to the
or should it be just hard-coded to ON/OFF/whatever in toolchain? I'd say it's similar to CMAKE_FIND_ROOT_PATH_MODE_INCLUDE (and friends), which we may set, but how to override it? |
Again, the first fundamental question is, is this a flag that should be defined by "conan", the "recipe author" or by the "caller of the CLI", or all three with precedence:
I definitely agree that this is "part of a paradigm" where Conan should take a strong position on the default. It seems like this could REALLY help us provide better handling of this problem (upstream findxxx.cmake and conan's generated files) which has been discussed at-length for years. I am actually shocked that this didn't come up until now. |
Ok, i see it's new as of Cmake 3.15 |
@SSE4 no need to add anything to the class, just hardcode the https://cmake.org/cmake/help/git-stage/variable/CMAKE_FIND_PACKAGE_PREFER_CONFIG.html Given that the whole proposed CMake integration If some user wants to disable that to prefer old modules, they can just change that value in their CMakeLists.txt, before calling the |
@memsharded okay, I've added it, as seems like you already have a confidence this is a good thing, so I trust your opinion. just wanted to make sure my voice is heard, as I have some concerns:
|
Thanks @SSE4 I think those are valid concerns. Please @mpusz if you have more compelling evidence why this should be added, please tell (like cases in which the current solution failed). Team, please vote:
|
My two cents. I agree with @SSE4 that this variable modifies CMake behavior substantially (like others we might consider) and I would prefer to have the full picture instead of adding them one by one. And, by default, I prefer to have the same defaults as CMake does unless it is needed for Conan to work properly. IMHO, this class Recipe:
generators = 'CMakeDeps'
def generate(self):
# Because I know I'm using CMakeDeps I set it to TRUE
tc = CMakeToolchain(self)
tc.definitions["CMAKE_FIND_PACKAGE_PREFER_CONFIG"] = ON
tc.generate() ... and/or a |
I agree with @SSE4 concerns as well. However, I believe that this variable was added to CMake in order to fix a long-standing issue with finding dependencies. If a user installs a custom package and provides a path to its configuration files (i.e. via CMAKE_INSTALL_PREFIX) this package should be found by default rather than the obsoleted system one. CMake cannot change the default behavior because they strive for long-time backward compatibility. However, I hope that at some point they will decide to do so with their policy mechanism. With |
So the opt-in is already there (note that it is called def generate(self):
# Because I know I'm using CMakeDeps I set it to TRUE
tc = CMakeToolchain(self)
tc.variables["CMAKE_FIND_PACKAGE_PREFER_CONFIG"] = ON
tc.generate() But still, we need to educate users to (almost) always use this form, or change their CMakeLists to |
BTW, do we have any important use cases to not use them together? I understand that separation of concerns is important and having the functionality divided into two different classes is great (for Conan developers and maintenance) but I not sure if it helps users. I am teaching C++ for living and I am already providing slides saying to always write def generate(self):
tc = CMakeToolchain(self)
tc.variables["SOMETHING"] = ...
tc.generate()
deps = CMakeDeps(self)
deps.configurations ....
deps.generate() and that forgetting about one of the above will end up with problems. From a teachability point of view, I do not think it is the right approach. The better would be something like: def generate(self):
cmake = CMake(self)
cmake.toolchain.variables["SOMETHING"] = ...
cmake.deps.configurations ....
cmake.generate() Well, but this is probably a discussion for a totally different thread... |
@mpusz I would also suggest in your teachings that you be sure to emphasize which things (like these generators) are still experimental. It's great to teach the "cutting edge", especially when it's clear that it's going to be the "new way" soon enough. But, I suggest highlighting that Conan is a project that evolves, and set the expectations properly. |
Sure, this is not only for teaching, but mostly to inform customers what to do now to ease the process of migration to Conan 2.0 when it comes. |
One additional point for the discussion here. Of course, we can do The people that actually need it are "common" customers (mostly C++ developers) that are not familiar with Conan details. They expect that if they provide Any solution discussed above as an alternative to my proposal does not address the "common" user case as it is specific to So as I stated before, please consider making this behavior a default one with an option to disable it by advanced users if needed. |
Please consider setting
CMAKE_FIND_PACKAGE_PREFER_CONFIG
in theconan_toolchain.cmake
. This will allow CMake to prefer configuration files generated byCMakeDeps
over the ones provided with CMake release (i.e.FindGTest.cmake
).The text was updated successfully, but these errors were encountered: