Skip to content
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

Using Host and Build Profile from CMakePresets #678

Open
ibis-hdl opened this issue Dec 1, 2024 · 2 comments
Open

Using Host and Build Profile from CMakePresets #678

ibis-hdl opened this issue Dec 1, 2024 · 2 comments

Comments

@ibis-hdl
Copy link

ibis-hdl commented Dec 1, 2024

I like to start with the positive things: it works as intended, but if you have other ideas, problems rises as usually :)
Anyway, I use CMakePresets.json in that way:

{
    ...
    "configurePresets": [
        {
            "name": "conan-provider",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "${sourceDir}/cmake/cmake-conan/conan_provider.cmake"
            }
        },

where I clone the git repo cmake-conan into ${sourceDir}/cmake/cmake-conan and inherits to all my presets, which works as expected.
To support macOS users with clang and libc++ on my Linux box, I have a special target clang-libc++ (since I don't own a Mac).

My intention is to use cmake-conan with this CMake preset.

The first step was to write a default profile for conan:

$ cat ~/.conan2/profiles/default 
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=18
os=Linux

[buildenv]
CC=clang
CXX=clang++

[conf]
tools.build:compiler_executables={ "c": "/usr/bin/clang", "cpp": "/usr/bin/clang++" }

The conan provider should take this into account for host and build profile, but doesn't.

Second try, I wrote a CMake cache file clang-libc++-cachevars.cmake:

# pre-fill
set(CONAN_HOST_PROFILE:STRING  "clang-libc++")
set(CONAN_BUILD_PROFILE:STRING "clang-libc++")

to inject this into the CMake cache as it is done with the CMakePresets.json from above, and call it:

$ cmake -C clang-libc++-cachevars.cmake --preset clang-libc++-release
loading initial cache file clang-libc++-cachevars.cmake
Preset CMake variables:

  CMAKE_BUILD_TYPE="Release"
  CMAKE_CONFIGURATION_TYPES="Debug;Release;RelWithDebInfo"
  CMAKE_CXX_COMPILER="clang++"
  CMAKE_C_COMPILER="clang"
  CMAKE_DEFAULT_BUILD_TYPE="Release"
...
-- CMake-Conan: Checking if a default profile exists
/home/vscode/.conan2/profiles/default
-- CMake-Conan: cmake_system_name=Linux
-- CMake-Conan: cmake_system_processor=x86_64
-- CMake-Conan: CMake compiler=Clang
-- CMake-Conan: CMake compiler version=18.1.3
-- CMake-Conan: [settings] compiler=clang
-- CMake-Conan: [settings] compiler.version=18
-- Performing Test _conan_is_libcxx
-- Performing Test _conan_is_libcxx - Failed
-- Performing Test _conan_is_gnu_libstdcxx
-- Performing Test _conan_is_gnu_libstdcxx - Success
...
-- CMake-Conan: Creating profile /workspaces/gh-actions-test/build/conan_host_profile
-- CMake-Conan: Profile: 
[settings]
arch=x86_64
os=Linux
compiler=clang
compiler.version=18
compiler.cppstd=20
compiler.libcxx=libstdc++11
[conf]
tools.cmake.cmaketoolchain:generator=Ninja Multi-Config
tools.build:compiler_executables={"c":"clang","cpp":"/usr/bin/clang++"}

-- CMake-Conan: Installing both Debug and Release
-- CMake-Conan: conan install /workspaces/gh-actions-test -of=/workspaces/gh-actions-test/build/conan --profile:host=default;--profile:host=/workspaces/gh-actions-test/build/conan_host_profile;--profile:build=default;-s;build_type=Release;--build=missing

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=20
compiler.libcxx=libstdc++11
compiler.version=18
os=Linux
[conf]
tools.build:compiler_executables={'c': 'clang', 'cpp': '/usr/bin/clang++'}
tools.cmake.cmaketoolchain:generator=Ninja Multi-Config
[buildenv]
CC=clang
CXX=clang++

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=18
os=Linux
[conf]
tools.build:compiler_executables={'c': '/usr/bin/clang', 'cpp': '/usr/bin/clang++'}
[buildenv]
CC=clang
CXX=clang++
...
$ cmake -C clang-libc++-cachevars.cmake --build --preset clang-libc++-release
...

Finally, the provider is always creating the build profile:

-- CMake-Conan: Creating profile /home/runner/work/gh-actions-test/gh-actions-test/build/conan_host_profile
-- CMake-Conan: Profile: 
[settings]
arch=x86_64
os=Linux
compiler=clang
compiler.version=18
compiler.cppstd=20
compiler.libcxx=libstdc++11
[conf]
tools.cmake.cmaketoolchain:generator=Ninja Multi-Config
tools.build:compiler_executables={"c":"clang","cpp":"/usr/bin/clang++"}

-- CMake-Conan: Installing both Debug and Release
-- CMake-Conan: conan install /home/runner/work/gh-actions-test/gh-actions-test -of=/home/runner/work/gh-actions-test/gh-actions-test/build/conan --profile:host=default;--profile:host=/home/runner/work/gh-actions-test/gh-actions-test/build/conan_host_profile;--profile:build=default;-s;build_type=Release;--build=missing

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=20
compiler.libcxx=libstdc++11
compiler.version=18
os=Linux
[conf]
tools.build:compiler_executables={'c': 'clang', 'cpp': '/usr/bin/clang++'}
tools.cmake.cmaketoolchain:generator=Ninja Multi-Config

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=20
compiler.libcxx=libc++
compiler.version=18
os=Linux

and the CMakeCache.txt contains always:

...
//Conan build profile
CONAN_BUILD_PROFILE:STRING=default
...

//Conan host profile
CONAN_HOST_PROFILE:STRING=default;auto-cmake

//Command line arguments for conan install
CONAN_INSTALL_ARGS:STRING=--build=missing
...

To me, it looks as the variables CONAN_BUILD_PROFILEand CONAN_HOST_PROFILEaren't honored as described in the docs, isn't it or do I use it not in the right way?

@ibis-hdl
Copy link
Author

ibis-hdl commented Dec 1, 2024

I found a smart solution, which finally doesn't work as expected.

$ conan profile show -pr clang-libc++
Host profile:
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=18
os=Linux
[conf]
tools.build:compiler_executables={'c': '/usr/bin/clang', 'cpp': '/usr/bin/clang++'}
[buildenv]
CC=clang
CXX=clang++

Build profile:
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=18
os=Linux
[conf]
tools.build:compiler_executables={'c': '/usr/bin/clang', 'cpp': '/usr/bin/clang++'}
[buildenv]
CC=clang
CXX=clang++

And CMakePresets.json

{
    ...
    "configurePresets": [
        {
            "name": "conan-provider",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "${sourceDir}/cmake/cmake-conan/conan_provider.cmake"
            }
        },
        ...
        {
            "name": "clang-libc++",
            "inherits": [
                ...
                "conan-provider",
                ...
            ],
            "cacheVariables": {
                "CONAN_HOST_PROFILE": "clang-libc++",
                "CONAN_BUILD_PROFILE": "clang-libc++"            }
        },
        ...

With this, CMakeCache variables are set expected, but CMake build failed for some reason.

$ cmake --preset clang-libc++
Preset CMake variables:

  CMAKE_BUILD_TYPE="Release"
  CMAKE_CONFIGURATION_TYPES="Debug;Release;RelWithDebInfo"
  CMAKE_CXX_COMPILER="clang++"
  CMAKE_C_COMPILER="clang"
  CMAKE_DEFAULT_BUILD_TYPE="Release"
  ...
  CMAKE_PROJECT_TOP_LEVEL_INCLUDES="/workspaces/gh-actions-test/cmake/cmake-conan/conan_provider.cmake"
  CONAN_BUILD_PROFILE="clang-libc++"
  CONAN_HOST_PROFILE="clang-libc++"
...
-- The CXX compiler identification is Clang 18.1.3
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- CMake-Conan: first find_package() found. Installing dependencies with Conan
-- CMake-Conan: Installing both Debug and Release
-- CMake-Conan: conan install /workspaces/gh-actions-test -of=/workspaces/gh-actions-test/build/conan --profile:host=clang-libc++;--profile:build=clang-libc++;-s;build_type=Release;--build=missing

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=18
os=Linux
[conf]
tools.build:compiler_executables={'c': '/usr/bin/clang', 'cpp': '/usr/bin/clang++'}
[buildenv]
CC=clang
CXX=clang++

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=18
os=Linux
[conf]
tools.build:compiler_executables={'c': '/usr/bin/clang', 'cpp': '/usr/bin/clang++'}
[buildenv]
CC=clang
CXX=clang++


======== Computing dependency graph ========
Graph root
    conanfile.py: /workspaces/gh-actions-test/conanfile.py
Requirements
    catch2/3.7.1#431d772165ed0bc5adaabaa44a9f53ca - Cache

======== Computing necessary packages ========
Requirements
    catch2/3.7.1#431d772165ed0bc5adaabaa44a9f53ca:33ee53aab1b9bfd22e13914fa502a98606b452d9#e5783f218c56ede35621a61c9e45edc5 - Cache

======== Installing packages ========
catch2/3.7.1: Already installed! (1 of 1)
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.filenames' used in: catch2/3.7.1
WARN: deprecated:     'cpp_info.names' used in: catch2/3.7.1
WARN: deprecated:     'cpp_info.build_modules' used in: catch2/3.7.1

======== Finalizing install (deploy, generators) ========
conanfile.py: Calling generate()
conanfile.py: Generators folder: /workspaces/gh-actions-test/build/conan/build/Release/generators
conanfile.py: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
    find_package(Catch2)
    target_link_libraries(... Catch2::Catch2WithMain)
conanfile.py: CMakeToolchain generated: conan_toolchain.cmake
conanfile.py: CMakeToolchain: Preset 'conan-release' added to CMakePresets.json.
    (cmake>=3.23) cmake --preset conan-release
    (cmake<3.23) cmake <path> -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=generators/conan_toolchain.cmake  -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Release
conanfile.py: CMakeToolchain generated: /workspaces/gh-actions-test/build/conan/build/Release/generators/CMakePresets.json
conanfile.py: CMakeToolchain generated: /workspaces/gh-actions-test/ConanCMakePresets.json
conanfile.py: Generating aggregated env files
conanfile.py: Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
Install finished successfully
-- CMake-Conan: CONAN_GENERATORS_FOLDER=/workspaces/gh-actions-test/build/conan/build/Release/generators
-- CMake-Conan: CONANFILE=/workspaces/gh-actions-test/conanfile.py
-- CMake-Conan: conan install /workspaces/gh-actions-test -of=/workspaces/gh-actions-test/build/conan --profile:host=clang-libc++;--profile:build=clang-libc++;-s;build_type=Debug;--build=missing

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Debug
compiler=clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=18
os=Linux
[conf]
tools.build:compiler_executables={'c': '/usr/bin/clang', 'cpp': '/usr/bin/clang++'}
[buildenv]
CC=clang
CXX=clang++

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=18
os=Linux
[conf]
tools.build:compiler_executables={'c': '/usr/bin/clang', 'cpp': '/usr/bin/clang++'}
[buildenv]
CC=clang
CXX=clang++


======== Computing dependency graph ========
Graph root
    conanfile.py: /workspaces/gh-actions-test/conanfile.py
Requirements
    catch2/3.7.1#431d772165ed0bc5adaabaa44a9f53ca - Cache

======== Computing necessary packages ========
Requirements
    catch2/3.7.1#431d772165ed0bc5adaabaa44a9f53ca:9976135ac672b25daf671f2b7a42dcb840fc7db9#6446d545cd569e43f6f07f32b7c42dc3 - Cache

======== Installing packages ========
catch2/3.7.1: Already installed! (1 of 1)
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.filenames' used in: catch2/3.7.1
WARN: deprecated:     'cpp_info.names' used in: catch2/3.7.1
WARN: deprecated:     'cpp_info.build_modules' used in: catch2/3.7.1

======== Finalizing install (deploy, generators) ========
conanfile.py: Calling generate()
conanfile.py: Generators folder: /workspaces/gh-actions-test/build/conan/build/Debug/generators
conanfile.py: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
    find_package(Catch2)
    target_link_libraries(... Catch2::Catch2WithMain)
conanfile.py: CMakeToolchain generated: conan_toolchain.cmake
conanfile.py: CMakeToolchain: Preset 'conan-debug' added to CMakePresets.json.
    (cmake>=3.23) cmake --preset conan-debug
    (cmake<3.23) cmake <path> -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=generators/conan_toolchain.cmake  -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Debug
conanfile.py: CMakeToolchain generated: /workspaces/gh-actions-test/build/conan/build/Debug/generators/CMakePresets.json
conanfile.py: CMakeToolchain generated: /workspaces/gh-actions-test/ConanCMakePresets.json
conanfile.py: Generating aggregated env files
conanfile.py: Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
Install finished successfully
-- CMake-Conan: CONAN_GENERATORS_FOLDER=/workspaces/gh-actions-test/build/conan/build/Debug/generators
-- CMake-Conan: CONANFILE=/workspaces/gh-actions-test/conanfile.py
-- Conan: Component target declared 'Catch2::Catch2'
-- Conan: Component target declared 'Catch2::Catch2WithMain'
-- Configuring done (1.8s)
-- Generating done (0.6s)
-- Build files have been written to: /workspaces/gh-actions-test/build

and build:

 $ cmake --build --preset clang-libc++-release
[1/8 (0.082s)] Scanning /workspaces/gh-actions-test/testsuite/test_me.cpp for CXX dependencies
FAILED: testsuite/CMakeFiles/gh-action-test_unit_tests.dir/Release/test_me.cpp.o.ddi 
"/usr/bin/clang-scan-deps-18" -format=p1689 -- /usr/bin/clang++ -DCMAKE_INTDIR=\"Release\" -I/workspaces/gh-actions-test/src -O3 -DNDEBUG -std=c++20 -x c++ /workspaces/gh-actions-test/testsuite/test_me.cpp -c -o testsuite/CMakeFiles/gh-action-test_unit_tests.dir/Release/test_me.cpp.o -resource-dir "/usr/lib/llvm-18/lib/clang/18" -MT testsuite/CMakeFiles/gh-action-test_unit_tests.dir/Release/test_me.cpp.o.ddi -MD -MF testsuite/CMakeFiles/gh-action-test_unit_tests.dir/Release/test_me.cpp.o.ddi.d > testsuite/CMakeFiles/gh-action-test_unit_tests.dir/Release/test_me.cpp.o.ddi.tmp && mv testsuite/CMakeFiles/gh-action-test_unit_tests.dir/Release/test_me.cpp.o.ddi.tmp testsuite/CMakeFiles/gh-action-test_unit_tests.dir/Release/test_me.cpp.o.ddi
Error while scanning dependencies for /workspaces/gh-actions-test/testsuite/test_me.cpp:
/workspaces/gh-actions-test/testsuite/test_me.cpp:1:10: fatal error: 'catch2/catch_test_macros.hpp' file not found
[2/8 (0.138s)] Scanning /workspaces/gh-actions-test/source/main.cpp for CXX dependencies
ninja: build stopped: subcommand failed.

The output directory for Conan is default, the recipe isn't any special

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()

@ibis-hdl
Copy link
Author

ibis-hdl commented Dec 4, 2024

During reducing the problem, it compiles now for some circumstances. I'm not sure what the problem is, but it seems to be related to selected compiler by Conan and/or the profile...
Anyway, here a striped down version of this repo.

I continued with my experiments:

> conan remove '*'
...
> rm -rf build; CMAKE_CXX_COMPILER=clang++ CXX=clang++ cmake --preset clang-libc++
...
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is Clang 18.1.3
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - broken
CMake Error at /opt/cmake/share/cmake-3.30/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "/usr/bin/cc"

  is not able to compile a simple test program.
...
> conan list 'catch*/*:*' --format=compact
Local Cache
  catch2/3.7.1
    catch2/3.7.1#431d772165ed0bc5adaabaa44a9f53ca%1726584452.35 (2024-09-17 14:47:32 UTC)
      catch2/3.7.1#431d772165ed0bc5adaabaa44a9f53ca:33ee53aab1b9bfd22e13914fa502a98606b452d9
        settings: Linux, x86_64, Release, clang, gnu17, libc++, 18
        options(diff): 
      catch2/3.7.1#431d772165ed0bc5adaabaa44a9f53ca:9976135ac672b25daf671f2b7a42dcb840fc7db9
        settings: Linux, x86_64, Debug, clang, gnu17, libc++, 18
        options(diff): 

So Conan related settings seems to be correct to me :(

But:

> rm -rf build; CC=clang CXX=clang++ cmake --preset clang-libc++ 
...
# all fine with explicit given CC - even unused !!!!!
> cmake --build --preset clang-libc++-release
...
Error while scanning dependencies for /workspaces/conan-cmake-issue/testsuite/test_me.cpp:
/workspaces/conan-cmake-issue/testsuite/test_me.cpp:1:10: fatal error: 'catch2/catch_test_macros.hpp' file not found
[2/8] Scanning /workspaces/conan-cmake-issue/source/main.cpp for CXX dependencies
ninja: build stopped: subcommand failed.

Anyway, somewhere I've got lost ... I created a branch conan-explicit with call of conan install (without conan-cmake) and failed completely now :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant