准备构建

之前的教程章节中,我们向 Conan 软件包添加了 fmt 依赖项,以便为我们的 “Hello World” C++ 库提供彩色输出。在本节中,我们将重点介绍 recipe 的 generate() 方法。此方法的目的是生成构建步骤运行时可能需要的所有信息。这意味着如下内容:

  • 写入将在构建步骤中使用的文件,例如注入环境变量的脚本、传递给构建系统的文件等。

  • 配置工具链以基于设置和选项提供额外信息,或从 Conan 默认生成的工具链中删除信息,这些信息可能不适用于某些情况。

我们将解释如何针对基于先前教程章节的简单示例使用此方法。我们向 recipe 添加一个 with_fmt 选项,根据该值,我们是否需要 fmt 库。我们使用 generate() 方法来修改工具链,以便它将一个变量传递给 CMake,这样我们就可以有条件地添加该库,并在源代码中是否使用 fmt

请首先克隆源代码以重新创建此项目。您可以在 GitHub 上的 examples2 仓库中找到它们

$ git clone https://github.com/conan-io/examples2.git
$ cd examples2/tutorial/creating_packages/preparing_the_build

您会注意到先前 recipe 的 conanfile.py 文件中的一些更改。让我们检查相关部分

...
from conan.tools.build import check_max_cppstd, check_min_cppstd
...

class helloRecipe(ConanFile):
    name = "hello"
    version = "1.0"

    ...
    options = {"shared": [True, False],
               "fPIC": [True, False],
               "with_fmt": [True, False]}

    default_options = {"shared": False,
                       "fPIC": True,
                       "with_fmt": True}
    ...

    def validate(self):
        if self.options.with_fmt:
            check_min_cppstd(self, "11")
            check_max_cppstd(self, "14")

    def source(self):
        git = Git(self)
        git.clone(url="https://github.com/conan-io/libhello.git", target=".")
        # Please, be aware that using the head of the branch instead of an immutable tag
        # or commit is not a good practice in general
        git.checkout("optional_fmt")

    def requirements(self):
        if self.options.with_fmt:
            self.requires("fmt/8.1.1")

    def generate(self):
        tc = CMakeToolchain(self)
        if self.options.with_fmt:
            tc.variables["WITH_FMT"] = True
        tc.generate()

    ...

正如您所见

  • 我们声明一个新的 with_fmt 选项,默认值设置为 True

  • 基于 with_fmt 选项的值

    • 我们安装或不安装 fmt/8.1.1 Conan 软件包。

    • 我们要求或不要求最低和最高 C++ 标准,因为 fmt 库至少需要 C++11,并且如果我们尝试使用高于 C++14 的标准,它将无法编译(只是一个示例,fmt 可以使用更现代的标准构建)

    • 我们将 WITH_FMT 变量与值 True 注入到 CMakeToolchain 中,以便我们可以在 hello 库的 CMakeLists.txt 中使用它,有条件地添加 CMake fmt::fmt 目标。

  • 我们正在克隆库的另一个分支。optional_fmt 分支包含代码中的一些更改。让我们看看 CMake 方面发生了什么变化

CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(hello CXX)

add_library(hello src/hello.cpp)
target_include_directories(hello PUBLIC include)
set_target_properties(hello PROPERTIES PUBLIC_HEADER "include/hello.h")

if (WITH_FMT)
    find_package(fmt)
    target_link_libraries(hello fmt::fmt)
    target_compile_definitions(hello PRIVATE USING_FMT=1)
endif()

install(TARGETS hello)

正如您所见,我们使用了在 CMakeToolchain 中注入的 WITH_FMT。根据该值,我们将尝试查找 fmt 库并将我们的 hello 库与其链接。此外,检查我们是否添加了 USING_FMT=1 编译定义,我们在源代码中使用它,具体取决于我们是否选择添加对 fmt 的支持。

hello.cpp
#include <iostream>
#include "hello.h"

#if USING_FMT == 1
#include <fmt/color.h>
#endif

void hello(){
    #if USING_FMT == 1
        #ifdef NDEBUG
        fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "hello/1.0: Hello World Release! (with color!)\n");
        #else
        fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "hello/1.0: Hello World Debug! (with color!)\n");
        #endif
    #else
        #ifdef NDEBUG
        std::cout << "hello/1.0: Hello World Release! (without color)" << std::endl;
        #else
        std::cout << "hello/1.0: Hello World Debug! (without color)" << std::endl;
        #endif
    #endif
}

让我们首先使用 with_fmt=True 然后 with_fmt=False 从源代码构建软件包。当 test_package 运行时,它将根据选项的值显示不同的消息。

$ conan create . --build=missing -o with_fmt=True
-------- Exporting the recipe ----------
...

-------- Testing the package: Running test() ----------
hello/1.0 (test package): Running test()
hello/1.0 (test package): RUN: ./example
hello/1.0: Hello World Release! (with color!)

$ conan create . --build=missing -o with_fmt=False
-------- Exporting the recipe ----------
...

-------- Testing the package: Running test() ----------
hello/1.0 (test package): Running test()
hello/1.0 (test package): RUN: ./example
hello/1.0: Hello World Release! (without color)

这只是一个简单的示例,说明如何使用 generate() 方法根据一个选项的值自定义工具链,但在 generate() 方法中,您可以执行许多其他操作,例如

  • 根据您的需求创建完整的自定义工具链,以便在您的构建中使用。

  • 访问有关软件包依赖项的某些信息,例如
    • 配置访问定义的 conf_info

    • 访问依赖项选项。

    • 使用 copy 工具 从依赖项导入文件。您还可以导入文件,为软件包创建清单,收集所有依赖项版本和许可证。

  • 使用 Environment 工具 为系统环境生成信息。

  • 除了 ReleaseDebug 之外,还添加自定义配置,考虑到设置,例如 ReleaseSharedDebugShared

另请参阅