如何选择 Ninja 作为 Conan 的 CMake 生成器?

Zan*_*tox 3 c++ cmake conan

在尝试按照柯南的入门指南创建包时,我在尝试运行conan create . demo/testing. 它不断尝试用作MinGW MakefilesCMake 生成器,但我想改用 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"]
Run Code Online (Sandbox Code Playgroud)

在 vcvars64.bat 环境中运行时的输出:

D:\microsoft_visual_studio\2022\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\0.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\0.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\0.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
Run Code Online (Sandbox Code Playgroud)

wiki指定柯南设置设置cmake_generatorNinja如下:

$ conan config set general.cmake_generator=Ninja
Run Code Online (Sandbox Code Playgroud)

我已经确认这个设置现在在我的conan.conf文件的类别下[general],但它似乎没有效果。与环境变量相同CONAN_CMAKE_GENERATOR

我尝试将配方的构建部分更改为:

def build(self):
    cmake = CMake(self, generator="Ninja")
    cmake.configure()
    cmake.build()
Run Code Online (Sandbox Code Playgroud)

但这只会在尝试运行时出现此错误:

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'
Run Code Online (Sandbox Code Playgroud)

作为参考,我使用的是Python版本3.10.1和Conan版本1.46.0

uil*_*ies 10

您正在遵循直接从 导入的CMake帮助程序的方法签名conans。但是,您导入了为 Conan 2.0 设计的新CMake帮助程序。它们是不相容的。

您可以保留新的助手,但您需要在 中定义生成器conanbuild.conf。(尚未记录)

但是,您也可以直接在CMakeToolchain构造函数中设置它:

tc = CMakeToolchain(self, generator="Ninja")
Run Code Online (Sandbox Code Playgroud)

但它很无聊并且硬编码在您的配方中,因此作为替代方案,您可以直接在global.conf文件中配置它。

echo tools.cmake.cmaketoolchain:generator=Ninja >> %USERPROFILE%\.conan\global.conf
Run Code Online (Sandbox Code Playgroud)

这些功能是实验性的,应该很快就会得到改进,因为它们将成为 Conan 2.0 的标准。如果行为发生变化,请阅读文档。