如何使用 Conan 选择 Ninja 作为 CMake 生成器?
How to choose Ninja as CMake generator with Conan?
在尝试按照 Conan 的入门指南创建包时,我在尝试 运行 conan create . demo/testing
时遇到了困难。它不断尝试使用 MinGW Makefiles
作为 CMake 生成器,但我想改用 Ninja。
我的食谱如下:
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
class HelloConan(ConanFile):
name = "hello"
version = "0.1"
# Optional metadata
license = "<Put the package license here>"
author = "<Put your name here> <And your email here>"
url = "<Package recipe repository url here, for issues about the package>"
description = "<Description of Hello here>"
topics = ("<Put some tag here>", "<here>", "<and here>")
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}
# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "CMakeLists.txt", "src/*", "include/*"
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def layout(self):
cmake_layout(self)
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
def package_info(self):
self.cpp_info.libs = ["hello"]
在 vcvars64.bat 环境中 运行ning 时的输出:
D:\microsoft_visual_studio22\Preview\VC\Auxiliary\Build>vcvars64.bat
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.2.0-pre.1.0
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
D:\Programming\hellopkg>conan create . demo/testing
Exporting package recipe
hello/0.1@demo/testing exports_sources: Copied 1 '.txt' file: CMakeLists.txt
hello/0.1@demo/testing exports_sources: Copied 1 '.cpp' file: hello.cpp
hello/0.1@demo/testing exports_sources: Copied 1 '.h' file: hello.h
hello/0.1@demo/testing: The stored package has not changed
hello/0.1@demo/testing: Exported revision: 4fc80e430f8244be40256fab755f8c43
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=clang
compiler.libcxx=libstdc++
compiler.version=13
os=Windows
os_build=Windows
[options]
[build_requires]
[env]
CC=D:/microsoft_visual_studio/2022/Preview/VC/Tools/Llvm/x64/bin/clang.exe
CXX=D:/microsoft_visual_studio/2022/Preview/VC/Tools/Llvm/x64/bin/clang++.exe
hello/0.1@demo/testing: Forced build from source
hello/0.1@demo/testing (test package): Installing package
Requirements
hello/0.1@demo/testing from local cache - Cache
Packages
hello/0.1@demo/testing:e12ca8b919921eecef92b328d38021194388175a - Build
Installing (downloading, building) binaries...
hello/0.1@demo/testing: WARN: Build folder is dirty, removing it: C:\Users\Zantox\.conan\data\hello[=11=].1\demo\testing\build\e12ca8b919921eecef92b328d38021194388175a
hello/0.1@demo/testing: Copying sources to build folder
hello/0.1@demo/testing: Building your package in C:\Users\Zantox\.conan\data\hello[=11=].1\demo\testing\build\e12ca8b919921eecef92b328d38021194388175a
hello/0.1@demo/testing: Generator txt created conanbuildinfo.txt
hello/0.1@demo/testing: Calling generate()
hello/0.1@demo/testing: WARN: Using the new toolchains and generators without specifying a build profile (e.g: -pr:b=default) is discouraged and might cause failures and unexpected behavior
hello/0.1@demo/testing: Aggregating env generators
hello/0.1@demo/testing: Calling build()
hello/0.1@demo/testing: CMake command: cmake -G "MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE="C:/Users/Zantox/.conan/data/hello/0.1/demo/testing/build/e12ca8b919921eecef92b328d38021194388175a/cmake-build-release/conan/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="C:/Users/Zantox/.conan/data/hello/0.1/demo/testing/package/e12ca8b919921eecef92b328d38021194388175a" -DCMAKE_SH="CMAKE_SH-NOTFOUND" "C:\Users\Zantox\.conan\data\hello[=11=].1\demo\testing\build\e12ca8b919921eecef92b328d38021194388175a\."
-- Using Conan toolchain: C:/Users/Zantox/.conan/data/hello/0.1/demo/testing/build/e12ca8b919921eecef92b328d38021194388175a/cmake-build-release/conan/conan_toolchain.cmake
-- Conan toolchain: Setting BUILD_SHARED_LIBS = OFF
-- The CXX compiler identification is Clang 13.0.0 with GNU-like command-line
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/microsoft_visual_studio/2022/Preview/VC/Tools/Llvm/x64/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
wiki 指定将柯南设置 cmake_generator
设置为 Ninja
,如下所示:
$ conan config set general.cmake_generator=Ninja
我已经确认此设置现在在我的 [general]
类别下的 conan.conf
文件中,但似乎没有效果。与 CONAN_CMAKE_GENERATOR
环境变量相同。
我尝试将配方的构建部分更改为:
def build(self):
cmake = CMake(self, generator="Ninja")
cmake.configure()
cmake.build()
但这只会在尝试 运行:
时出现此错误
ERROR: hello/0.1@demo/testing: Error in build() method, line 36
cmake = CMake(self, generator="Ninja")
TypeError: __init__() got an unexpected keyword argument 'generator'
作为参考,我使用的是Python版本3.10.1
和柯南版本1.46.0
您正在遵循为柯南 2.0 设计的 CMake helper imported directly from conans
. However, you imported the new CMake 助手的方法签名。它们不兼容。
您可以保留新的助手,但您需要在 conanbuild.conf
中定义您的生成器。 (尚未记录)
但是,您也可以直接在 CMakeToolchain 构造函数中设置它:
tc = CMakeToolchain(self, generator="Ninja")
但是它很无聊并且硬编码在你的食谱上,所以作为替代方案,你可以直接在你的 global.conf 文件中配置它。
echo tools.cmake.cmaketoolchain:generator=Ninja >> %USERPROFILE%\.conan\global.conf
这些功能是实验性的,应该尽快改进,因为它们将成为柯南 2.0 的标准。阅读文档以防行为发生变化。
在尝试按照 Conan 的入门指南创建包时,我在尝试 运行 conan create . demo/testing
时遇到了困难。它不断尝试使用 MinGW Makefiles
作为 CMake 生成器,但我想改用 Ninja。
我的食谱如下:
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
class HelloConan(ConanFile):
name = "hello"
version = "0.1"
# Optional metadata
license = "<Put the package license here>"
author = "<Put your name here> <And your email here>"
url = "<Package recipe repository url here, for issues about the package>"
description = "<Description of Hello here>"
topics = ("<Put some tag here>", "<here>", "<and here>")
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}
# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "CMakeLists.txt", "src/*", "include/*"
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def layout(self):
cmake_layout(self)
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
def package_info(self):
self.cpp_info.libs = ["hello"]
在 vcvars64.bat 环境中 运行ning 时的输出:
D:\microsoft_visual_studio22\Preview\VC\Auxiliary\Build>vcvars64.bat
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.2.0-pre.1.0
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
D:\Programming\hellopkg>conan create . demo/testing
Exporting package recipe
hello/0.1@demo/testing exports_sources: Copied 1 '.txt' file: CMakeLists.txt
hello/0.1@demo/testing exports_sources: Copied 1 '.cpp' file: hello.cpp
hello/0.1@demo/testing exports_sources: Copied 1 '.h' file: hello.h
hello/0.1@demo/testing: The stored package has not changed
hello/0.1@demo/testing: Exported revision: 4fc80e430f8244be40256fab755f8c43
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=clang
compiler.libcxx=libstdc++
compiler.version=13
os=Windows
os_build=Windows
[options]
[build_requires]
[env]
CC=D:/microsoft_visual_studio/2022/Preview/VC/Tools/Llvm/x64/bin/clang.exe
CXX=D:/microsoft_visual_studio/2022/Preview/VC/Tools/Llvm/x64/bin/clang++.exe
hello/0.1@demo/testing: Forced build from source
hello/0.1@demo/testing (test package): Installing package
Requirements
hello/0.1@demo/testing from local cache - Cache
Packages
hello/0.1@demo/testing:e12ca8b919921eecef92b328d38021194388175a - Build
Installing (downloading, building) binaries...
hello/0.1@demo/testing: WARN: Build folder is dirty, removing it: C:\Users\Zantox\.conan\data\hello[=11=].1\demo\testing\build\e12ca8b919921eecef92b328d38021194388175a
hello/0.1@demo/testing: Copying sources to build folder
hello/0.1@demo/testing: Building your package in C:\Users\Zantox\.conan\data\hello[=11=].1\demo\testing\build\e12ca8b919921eecef92b328d38021194388175a
hello/0.1@demo/testing: Generator txt created conanbuildinfo.txt
hello/0.1@demo/testing: Calling generate()
hello/0.1@demo/testing: WARN: Using the new toolchains and generators without specifying a build profile (e.g: -pr:b=default) is discouraged and might cause failures and unexpected behavior
hello/0.1@demo/testing: Aggregating env generators
hello/0.1@demo/testing: Calling build()
hello/0.1@demo/testing: CMake command: cmake -G "MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE="C:/Users/Zantox/.conan/data/hello/0.1/demo/testing/build/e12ca8b919921eecef92b328d38021194388175a/cmake-build-release/conan/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="C:/Users/Zantox/.conan/data/hello/0.1/demo/testing/package/e12ca8b919921eecef92b328d38021194388175a" -DCMAKE_SH="CMAKE_SH-NOTFOUND" "C:\Users\Zantox\.conan\data\hello[=11=].1\demo\testing\build\e12ca8b919921eecef92b328d38021194388175a\."
-- Using Conan toolchain: C:/Users/Zantox/.conan/data/hello/0.1/demo/testing/build/e12ca8b919921eecef92b328d38021194388175a/cmake-build-release/conan/conan_toolchain.cmake
-- Conan toolchain: Setting BUILD_SHARED_LIBS = OFF
-- The CXX compiler identification is Clang 13.0.0 with GNU-like command-line
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/microsoft_visual_studio/2022/Preview/VC/Tools/Llvm/x64/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
wiki 指定将柯南设置 cmake_generator
设置为 Ninja
,如下所示:
$ conan config set general.cmake_generator=Ninja
我已经确认此设置现在在我的 [general]
类别下的 conan.conf
文件中,但似乎没有效果。与 CONAN_CMAKE_GENERATOR
环境变量相同。
我尝试将配方的构建部分更改为:
def build(self):
cmake = CMake(self, generator="Ninja")
cmake.configure()
cmake.build()
但这只会在尝试 运行:
时出现此错误ERROR: hello/0.1@demo/testing: Error in build() method, line 36
cmake = CMake(self, generator="Ninja")
TypeError: __init__() got an unexpected keyword argument 'generator'
作为参考,我使用的是Python版本3.10.1
和柯南版本1.46.0
您正在遵循为柯南 2.0 设计的 CMake helper imported directly from conans
. However, you imported the new CMake 助手的方法签名。它们不兼容。
您可以保留新的助手,但您需要在 conanbuild.conf
中定义您的生成器。 (尚未记录)
但是,您也可以直接在 CMakeToolchain 构造函数中设置它:
tc = CMakeToolchain(self, generator="Ninja")
但是它很无聊并且硬编码在你的食谱上,所以作为替代方案,你可以直接在你的 global.conf 文件中配置它。
echo tools.cmake.cmaketoolchain:generator=Ninja >> %USERPROFILE%\.conan\global.conf
这些功能是实验性的,应该尽快改进,因为它们将成为柯南 2.0 的标准。阅读文档以防行为发生变化。