conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
8.31k stars 986 forks source link

Could not find toolchain file when calling CMake after installing the GDAL package #17209

Open amine-aboufirass opened 1 month ago

amine-aboufirass commented 1 month ago

I would like to write some code which has GDAL as a dependency. Here's what I came up with so far:

profile

[settings]
arch=x86_64
build_type=Debug
compiler=msvc
compiler.cppstd=17
compiler.runtime=dynamic
compiler.version=194
os=Windows

conanfile.txt

[requires]
gdal/3.8.3
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(TestGdal)

find_package(GDAL REQUIRED)

add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} GDAL::GDAL)

CMakeUserPresets.json

{
    "version": 4,
    "vendor": {
        "conan": {}
    },
    "include": [
        "build\\build\\generators\\CMakePresets.json"
    ]
}

I followed the Getting Started portion of the Conan docs as closely as was necessary. It does so happen that I would like to debug the code, so I am focusing on creating a debug configuration. The following install command took a WHILE, but appears to have run successfully:

conan install . --output-folder=build --build=missing --profile=profile

I then cd to my build directory and run the following:

cmake .. -DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake"

This unfortunately fails with an error:

CMake Error at C:/Program Files/CMake/share/cmake-3.31/Modules/CMakeDetermineSystem.cmake:152 (message): Could not find toolchain file: conan_toolchain.cmake Call Stack (most recent call first): CMakeLists.txt:2 (project)

-- Configuring incomplete, errors occurred! MSBuild version 17.11.9+a69bbaaf5 for .NET Framework MSBUILD : error MSB1009: Project file does not exist. Switch: ALL_BUILD.vcxproj

What could I possibly be doing wrong here?

amine-aboufirass commented 1 month ago

I also notice that a directory called p has been created in $HOME/.conan2 which weighs over 10GB. Not sure what that's doing there....

memsharded commented 1 month ago

Hi @amine-aboufirass

Thanks for your question

conan install . --output-folder=build

It seems this is an issue with the paths. If your conanfile has cmake_layout, this is typically no longer necessary. Then please check the output, when you run conan install in the output messages you will see the real location of the different generated files. The conan toolchain will be somewhere more like:

$ cmake . -DCMAKE_TOOLCHAIN_FILE=build/generators/conan_toolchain.cmake

Also, in many cases and if using CMake>=3.23, it is more convenient to just use the generated presets, so

$ cmake --preset conan-default
$ cmake --build --preset conan-debug

I also notice that a directory called p has been created in $HOME/.conan2 which weighs over 10GB. Not sure what that's doing there....

Yes, that is the Conan cache, where the actual packages are installed, and yes, the C++ packages can be large, 10Gb sounds a bit too much unless you are installing a ton of dependencies. It also depends on different factors, for example Debug packages use way more space than Release packages

memsharded commented 3 weeks ago

Hi @amine-aboufirass

Any further question or issue here? If not maybe we can close the ticket? Thanks for your feedback.

ibis-hdl commented 2 weeks ago

Hello James,

I like to jump in here since I suffer from the same conceptional problem.

I have a conanfile.py

from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake, cmake_layout

class CompressorRecipe(ConanFile):
    settings = "os", "compiler", "build_type", "arch"

    def requirements(self):
        self.requires("catch2/3.7.1")
    def layout(self):
        cmake_layout(self)
    def generate(self):
        deps = CMakeDeps(self)
        deps.generate()
        tc = CMakeToolchain(self)
        tc.user_presets_path = "ConanCMakePresets.json"
        tc.generate()

with common.json (included by appropriate compiler json files)

    "configurePresets": [
        ...
        {
            "name": "default-ninja-settings",
            "hidden": true,
            "binaryDir": "${sourceDir}/build/${presetName}",
            "cacheVariables": {
                "CMAKE_EXPORT_COMPILE_COMMANDS": true,
                "CMAKE_CONFIGURATION_TYPES": "Debug;Release"
            }
        },
        {
            "name": "ninja-config",
            "hidden": true,
            "generator": "Ninja",
            "inherits": [
                "default-ninja-settings"
            ]
        },
        ...

and my CMakePresets.json

{
    "version": 6,
    "include": [
        "cmake/presets/gnuc.json",
        "cmake/presets/clang.json",
        "cmake/presets/clang-libcpp.json",
        "cmake/presets/msvc.json",
        "cmake/presets/msvc-cl.json",
        "cmake/presets/os.json",
        "ConanCMakePresets.json"
    ],
    "configurePresets": [
        {
            "name": "gcc-release",
            "displayName": "Linux GnuC: Release",
            "inherits": [
                "condition-linux",
                "ninja-config",
                "gcc-build-type-release",
                "conan-release",
                "default-cmake-configure-settings",
                "default-install-settings",
                "default-environment"
            ]
        },
    ....

After running e.g. conan install . --settings build_type=Release --build=missing --profile:all=gcc I have the default build/{Debug,Release}/generators structure from conan, where "toolchainFile": "generators/conan_toolchain.cmake". The point is here "binaryDir": "${sourceDir}/build/${presetName}" from CMakePresets.json. This is my preferred build tree for Linux, Windows and Darwin. Hence, this error rises:

$ cmake --preset gcc-release
...
CMake Error at /opt/cmake/share/cmake-3.30/Modules/CMakeDetermineSystem.cmake:152 (message):
  Could not find toolchain file: generators/conan_toolchain.cmake
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)

I would assume, set I can the build_dir at conanfile.py (how to do in detail? cmake_layout?), but this would pass detailed information from CMake presets into conanfile, which in turn has an influence on the use of a specific CMake preset (e.g. by use of clang-release, msvc-release). To make things more complicated, Windows and XCode? uses Ninja's multi-config feature where conan's directory hierarchy is different. How to cope with this situation? Conan's --output-folder= isn't helpful here imo.

memsharded commented 2 weeks ago

Hi @ibis-hdl

This might be a different issue. In your case, it seems that you are using Ninja, even for msvc compiler, is it? In that case, it is necessary to tell Conan:

conan install .... -c tools.cmake.cmaketoolchain:generator=Ninja

So Conan can define the correct layout and location of files. Please try that and let us know (in fact it would be better if this is posted as a new question, so it can be closed if solved)

Otherwise, @amine-aboufirass any feedback about the above here? Thanks!

ibis-hdl commented 2 weeks ago

Yes, I use on Windows && macOS "generator": "Ninja Multi-Config", on Linux simply "generator": "Ninja". Unfortunately, it doesn't work on Windows and Linux as expected:

$ conan install . --settings build_type=Release -c='tools.cmake.cmaketoolchain:generator=Ninja' --build=missing --profile:all=gcc
...
$ cmake --preset gcc-release
...
CMake Error at /opt/cmake/share/cmake-3.30/Modules/CMakeDetermineSystem.cmake:152 (message):
  Could not find toolchain file: generators/conan_toolchain.cmake

or even

CMake Error at C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.29/Modules/CMakeDetermineSystem.cmake:152 (message):
  Could not find toolchain file: generators/conan_toolchain.cmake

If you like, you can move this to a new issue/question. I have no idea how to and preserve the conversation.

memsharded commented 2 weeks ago

Then sorry, if you are using Ninja Multi-Config, the conf should match, please try:

conan install ... -c "tools.cmake.cmaketoolchain:generator=Ninja Multi-Config"

If that doesn't work, then yes, please create a new ticket, and copy a link to your initial comment in the first comment.

ibis-hdl commented 2 weeks ago

Thank you, please see at #17324

memsharded commented 2 weeks ago

Thanks @ibis-hdl we will follow up there

Hi @amine-aboufirass, please let us know about the above.