conan.tools.files 补丁

conan.tools.files.patch()

patch(conanfile, base_path=None, patch_file=None, patch_string=None, strip=0, fuzz=False, **kwargs)

在 `conanfile.source_folder` 目录中应用来自文件(`patch_file`)或字符串(`patch_string`)的差异。可以通过 `layout(self)` 方法中的 `self.folders` 属性自定义包含源代码的文件夹。

参数:
  • conanfile – 当前配方,始终传递“self”

  • base_path – 该路径是相对于 `conanfile.export_sources_folder` 的相对路径,除非提供了绝对路径。

  • patch_file – 应应用的补丁文件。该路径是相对于 `conanfile.source_folder` 的相对路径,除非提供了绝对路径。

  • patch_string – 应应用的补丁字符串。

  • strip – 从路径中剥离的文件夹数量。

  • fuzz – 是否应接受模糊补丁。

  • kwargs – 可以添加并有助于输出信息的额外参数

用法

from conan.tools.files import patch

def build(self):
    for it in self.conan_data.get("patches", {}).get(self.version, []):
        patch(self, **it)

conan.tools.files.apply_conandata_patches()

apply_conandata_patches(conanfile)

应用存储在 `conanfile.conan_data`(从 `conandata.yml` 文件读取)中的补丁。它将应用 `patches` 条目下与给定 `conanfile.version` 匹配的所有补丁。如果 `conandata.yml` 中未定义版本,它将直接应用 `patches` 关键字下的所有补丁。

键条目将作为 `kwargs` 传递给 `patch` 函数。

用法

from conan.tools.files import apply_conandata_patches

def source(self):
    apply_conandata_patches(self)

`conandata.yml` 示例

patches:
- patch_file: "patches/0001-buildflatbuffers-cmake.patch"
- patch_file: "patches/0002-implicit-copy-constructor.patch"
  base_path: "subfolder"
  patch_type: backport
  patch_source: https://github.com/google/flatbuffers/pull/5650
  patch_description: Needed to build with modern clang compilers.

针对不同版本的不同补丁

patches:
  "1.11.0":
    - patch_file: "patches/0001-buildflatbuffers-cmake.patch"
    - patch_file: "patches/0002-implicit-copy-constructor.patch"
      base_path: "subfolder"
      patch_type: backport
      patch_source: https://github.com/google/flatbuffers/pull/5650
      patch_description: Needed to build with modern clang compilers.
  "1.12.0":
    - patch_file: "patches/0001-buildflatbuffers-cmake.patch"
    - patch_string: |
        --- a/tests/misc-test.c
        +++ b/tests/misc-test.c
        @@ -1232,6 +1292,8 @@ main (int argc, char **argv)
              g_test_add_func ("/misc/pause-cancel", do_pause_cancel_test);
              g_test_add_data_func ("/misc/stealing/async", GINT_TO_POINTER (FALSE), do_stealing_test);
              g_test_add_data_func ("/misc/stealing/sync", GINT_TO_POINTER (TRUE), do_stealing_test);
        +     g_test_add_func ("/misc/response/informational/content-length", do_response_informational_content_length_test);
        +

        ret = g_test_run ();
    - patch_file: "patches/0003-fix-content-length-calculation.patch"

对于每个补丁,必须提供 `patch_file`、`patch_string` 或 `patch_user` 字段。前两者由 `apply_conandata_patches()` 自动应用,而 `patch_user` 会被忽略,必须由用户直接在 `conanfile.py` 配方中处理。

conan.tools.files.export_conandata_patches()

export_conandata_patches(conanfile)

导出存储在“`conanfile.conan_data`”(从“`conandata.yml`”文件读取)中的补丁。它将导出“`patches`”条目下与给定“`conanfile.version`”匹配的所有补丁。如果“`conandata.yml`”中未定义版本,它将直接导出“`patches`”关键字下的所有补丁。

未定义版本的 `conandata.yml` 示例

from conan.tools.files import export_conandata_patches
def export_sources(self):
    export_conandata_patches(self)

core.sources.patch:extra_path

警告

此功能是实验性的,可能会发生重大更改。有关更多信息,请参阅 Conan 稳定性 部分。

`export_conandata_patches()` 工具可以使用 `core.sources.patch:extra_path` 核心配置在包创建时自动从外部路径注入补丁。

这意味着 `conan-center-index` 仓库中的 `conan create` 命令可以注入和应用补丁,而无需将补丁放在同一仓库中,也无需修改 `conandata.yml` 文件。

`core.sources.patch:extra_path` 配置应指向一个文件夹,其中包含所有可能包的所有可能的额外补丁,按包名称组织,遵循与 `conan-center-index` 仓库相同的约定。

extra_folder
    pkgname1
       conandata.yml
       patches
          mypatch.path
    pkgname2
       ...

`conandata.yml` 也应遵循相同的结构。

patches:
    "1.0":
        - patch_file: "patches/mypatch.patch"

注意

在安装任意依赖项(`conan install --build=xxx`)时不可能应用补丁,因为可能注入的补丁是包的“源”标识的一部分,并且必须在其配方修订版中表示。缓存或远程服务器中已存在的包已经导出了其文件并计算了配方修订版,因此如果不违反包的标识(以及由此产生的可重现性和可追溯性),就不能在那里应用补丁。因此,这意味着 `core.sources.patch:extra_path` 只能在 `conan create` 时工作。