避免在 Conan 配置文件和 CMake 之间重复配置
Avoid repeating config between Conan profile and CMake
我一直在为个人项目学习柯南,但我有一个问题无法通过查看文档和示例来回答。
Conan 的“配置文件”和 CMake 之间似乎缺乏互动,这很尴尬。我的意思是:有许多不同的构建参数必须手动保持同步 - 运行 使用与 conan install
期间选择的参数不同的参数的 CMake 将导致构建错误。
举一个更具体的例子,这是我的两个配置以及我将如何使用它们来构建:
$ cat ~/.conan/profiles/gcc_debug
[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=10
compiler.libcxx=libstdc++11
build_type=Debug
[options]
[build_requires]
[env]
$ export CC=/usr/bin/gcc
$ export CXX=/usr/bin/g++
$ conan install ../src --profile gcc_debug
$ cmake ../src -GNinja -DCMAKE_BUILD_TYPE=Debug
$ cmake --build .
$ cat ~/.conan/profiles/clang_cl_release
[settings]
os=Windows
os_build=Windows
arch=x86_64
arch_build=x86_64
compiler=Visual Studio
compiler.version=16
compiler.toolset=ClangCL
build_type=Release
[options]
[build_requires]
[env]
$ conan install ../src --profile clang_cl_release
$ cmake ../src -G"Visual Studio 16 2019" -TClangCL
$ cmake --build . --config=Release
在第一个示例中,我必须重复 gcc
和 Debug
。
在第二个中,我必须重复 Visual Studio
、16
、ClangCL
和 Release
。
不得不重复自己这么多次感觉很臭,尤其是当这些选项分布在多个长 运行 命令中时,而忘记一个选项可能会导致不明显的错误消息。在我看来,当您调用 cmake_basic_setup()
时,柯南不只是强制执行(覆盖)适当的 CMake 变量,这对我来说也很奇怪;它已经知道它期望的值是什么,如果它们不匹配就会爆炸。
推荐的处理方法是什么?
我刚开始研究 conan build
, which seems promising. I had originally skipped over this because it looked like it only applied to people who were publishing a Conan package, but now I'm thinking it is meant for consumers as well. In fact, this page 甚至明确提到了我的问题陈述:
This conan build
will use the settings used in the conan install
which have been cached in the local conaninfo.txt
and file in your build folder. This simplifies the process and reduces the errors of mismatches between the installed packages and the current project configuration. [...] Implementing and using the conanfile.py build()
method ensures that we always use the same settings both in the installation of requirements and the build of the project, and simplifies calling the build system.
通常您会使用 Conan 设置所有外部库,然后从 CMake 中使用它们。但是,您可以采用另一种方式:让 CMake 调用 Conan 来安装外部依赖项。
来自 How to launch conan install from cmake 的示例代码:
cmake_minimum_required(VERSION 2.8)
project(myproject CXX)
# Download automatically, you can also just copy the conan.cmake file
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/master/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake")
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_run(REQUIRES Catch2/2.6.0@catchorg/stable
BASIC_SETUP)
add_executable(main main.cpp)
target_link_libraries(main ${CONAN_LIBS})
查看推荐的柯南用法:https://github.com/conan-io/cmake-conan
set(CONAN_PROFILE_NAME ${CMAKE_SYSTEM_NAME}-${CMAKE_GENERATOR})
execute_process(COMMAND ${CONAN_CMD} profile new ${CONAN_PROFILE_NAME} --detect ERROR_QUIET)
conan_cmake_autodetect(settings)
# REPEAT THIS FOR EACH OF YOUR REMOTES
conan_cmake_configure(REQUIRES zlib/1.2.0 GENERATORS cmake_find_package)
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
PROFILE ${CONAN_PROFILE_NAME}
SETTINGS ${settings})
find_package(zlib)
我一直在为个人项目学习柯南,但我有一个问题无法通过查看文档和示例来回答。
Conan 的“配置文件”和 CMake 之间似乎缺乏互动,这很尴尬。我的意思是:有许多不同的构建参数必须手动保持同步 - 运行 使用与 conan install
期间选择的参数不同的参数的 CMake 将导致构建错误。
举一个更具体的例子,这是我的两个配置以及我将如何使用它们来构建:
$ cat ~/.conan/profiles/gcc_debug
[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=10
compiler.libcxx=libstdc++11
build_type=Debug
[options]
[build_requires]
[env]
$ export CC=/usr/bin/gcc
$ export CXX=/usr/bin/g++
$ conan install ../src --profile gcc_debug
$ cmake ../src -GNinja -DCMAKE_BUILD_TYPE=Debug
$ cmake --build .
$ cat ~/.conan/profiles/clang_cl_release
[settings]
os=Windows
os_build=Windows
arch=x86_64
arch_build=x86_64
compiler=Visual Studio
compiler.version=16
compiler.toolset=ClangCL
build_type=Release
[options]
[build_requires]
[env]
$ conan install ../src --profile clang_cl_release
$ cmake ../src -G"Visual Studio 16 2019" -TClangCL
$ cmake --build . --config=Release
在第一个示例中,我必须重复 gcc
和 Debug
。
在第二个中,我必须重复 Visual Studio
、16
、ClangCL
和 Release
。
不得不重复自己这么多次感觉很臭,尤其是当这些选项分布在多个长 运行 命令中时,而忘记一个选项可能会导致不明显的错误消息。在我看来,当您调用 cmake_basic_setup()
时,柯南不只是强制执行(覆盖)适当的 CMake 变量,这对我来说也很奇怪;它已经知道它期望的值是什么,如果它们不匹配就会爆炸。
推荐的处理方法是什么?
我刚开始研究 conan build
, which seems promising. I had originally skipped over this because it looked like it only applied to people who were publishing a Conan package, but now I'm thinking it is meant for consumers as well. In fact, this page 甚至明确提到了我的问题陈述:
This
conan build
will use the settings used in theconan install
which have been cached in the localconaninfo.txt
and file in your build folder. This simplifies the process and reduces the errors of mismatches between the installed packages and the current project configuration. [...] Implementing and using the conanfile.pybuild()
method ensures that we always use the same settings both in the installation of requirements and the build of the project, and simplifies calling the build system.
通常您会使用 Conan 设置所有外部库,然后从 CMake 中使用它们。但是,您可以采用另一种方式:让 CMake 调用 Conan 来安装外部依赖项。
来自 How to launch conan install from cmake 的示例代码:
cmake_minimum_required(VERSION 2.8)
project(myproject CXX)
# Download automatically, you can also just copy the conan.cmake file
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/master/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake")
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_run(REQUIRES Catch2/2.6.0@catchorg/stable
BASIC_SETUP)
add_executable(main main.cpp)
target_link_libraries(main ${CONAN_LIBS})
查看推荐的柯南用法:https://github.com/conan-io/cmake-conan
set(CONAN_PROFILE_NAME ${CMAKE_SYSTEM_NAME}-${CMAKE_GENERATOR})
execute_process(COMMAND ${CONAN_CMD} profile new ${CONAN_PROFILE_NAME} --detect ERROR_QUIET)
conan_cmake_autodetect(settings)
# REPEAT THIS FOR EACH OF YOUR REMOTES
conan_cmake_configure(REQUIRES zlib/1.2.0 GENERATORS cmake_find_package)
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
PROFILE ${CONAN_PROFILE_NAME}
SETTINGS ${settings})
find_package(zlib)