创建conan包-不同/相同repo中的配方和来源

本文是基于对conan官方文档Recipe and Sources in a Different Repo - Recipe and Sources in the Same Repo翻译而来, 更详细的信息可以去查阅conan官方文档。

1 Recipe and Sources in a Different Repo

In the previous section, we fetched the sources of our library from an external repository. It is a typical workflow for packaging third party libraries.
在上一节中,我们从外部资源库获取了库的源代码。这是打包第三方库的典型工作流程。

There are two different ways to fetch the sources from an external repository:
从外部资源库获取源文件有两种不同的方法:

1.1 source()的方法

Using the source() method as we displayed in the previous section:
使用我们在上一节中展示的 source() 方法:

from conans import ConanFile, CMake, tools

class HelloConan(ConanFile):
    ...

    def source(self):
        self.run("git clone https://github.com/conan-io/hello.git")
        ...

You can also use the tools.Git class:
您还可以使用 tools.Git 类:

from conans import ConanFile, CMake, tools

class HelloConan(ConanFile):
    ...

    def source(self):
        git = tools.Git(folder="hello")
        git.clone("https://github.com/conan-io/hello.git", "master")
        ...

1.2 使用scm 属性

Using the scm attribute of the ConanFile:
使用 ConanFile 的 scm 属性:

Warning
This is a deprecated feature. Please refer to the Migration Guidelines to find the feature that replaces this one.
这是一项已废弃的功能。请参阅 "迁移指南 "查找替代此功能的功能。

from conans import ConanFile, CMake, tools

class HelloConan(ConanFile):
     scm = {
        "type": "git",
        "subfolder": "hello",
        "url": "https://github.com/conan-io/hello.git",
        "revision": "master"
     }
    ...

Conan will clone the scm url and will checkout the scm revision. Head to creating package documentation to know more details about SCM feature.
conan将克隆 scm 网址并签出 scm 修订版。前往创建软件包文档,了解有关 SCM 功能的更多详情。

The source() method will be called after the checkout process, so you can still use it to patch something or retrieve more sources, but it is not necessary in most cases.
source() 方法将在checkout过程后调用,因此您仍可使用它来修补或检索更多来源,但在大多数情况下并无必要。

2 Recipe and Sources in the Same Repo

Caution
We are actively working to finalize the Conan 2.0 Release. Some of the information on this page references deprecated features which will not be carried forward with the new release. It’s important to check the Migration Guidelines to ensure you are using the most up to date features.
我们正在积极努力完成conan 2.0 版本的最终定稿。本页面中的部分信息引用了过时的功能,这些功能将不会在新版本中继续使用。请务必查看 “迁移指南”,以确保您使用的是最新功能。

Sometimes it is more convenient to have the recipe and source code together in the same repository. This is true especially if you are developing and packaging your own library, and not one from a third-party.
有时,将配方和源代码放在同一个资源库中会更方便。尤其是在开发和打包自己的库,而不是第三方库时更是如此。

There are two different approaches:
有两种不同的方法:

  • Using the exports sources attribute of the conanfile to export the source code together with the recipe. This way the recipe is self-contained and will not need to fetch the code from external origins when building from sources. It can be considered a “snapshot” of the source code.
  • 使用 conanfileexports sources 属性将源代码与配方一起导出。这样,配方就自成一体,在从源代码构建时无需从外部获取代码。它可以被视为源代码的 “snapshot”。
  • Using the scm attribute of the conanfile to capture the remote and commit of your repository automatically.
  • 使用 conanfilescm 属性自动捕获版本库的remote和commit。

2.1 Exporting the Sources with the Recipe: exports_sources

This could be an appropriate approach if we want the package recipe to live in the same repository as the source code it is packaging.
如果我们希望软件包配方与打包的源代码位于同一版本库中,这可能是一种合适的方法。

First, let’s get the initial source code and create the basic package recipe:
首先,让我们获取初始源代码并创建基本软件包配方:

$ conan new hello/0.1 -t -s

A src folder will be created with the same “hello” source code as in the previous example. You can have a look at it and see that the code is straightforward.
将创建一个 src 文件夹,其中包含与上一个示例中相同的 "hello "源代码。你可以看一看,代码非常简单。

Now let’s have a look at conanfile.py:
现在,让我们来看看 conanfile.py:

from conans import ConanFile, CMake

class HelloConan(ConanFile):
    name = "hello"
    version = "0.1"
    license = "<Put the package license here>"
    url = "<Package recipe repository url here, for issues about the package>"
    description = "<Description of hello here>"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake"
    exports_sources = "src/*"

    def build(self):
        cmake = CMake(self)
        cmake.configure(source_folder="src")
        cmake.build()

        # Explicit way:
        # self.run('cmake "%s/src" %s' % (self.source_folder, cmake.command_line))
        # self.run("cmake --build . %s" % cmake.build_config)

    def package(self):
        self.copy("*.h", dst="include", src="src")
        self.copy("*.lib", dst="lib", keep_path=False)
        self.copy("*.dll", dst="bin", keep_path=False)
        self.copy("*.dylib*", dst="lib", keep_path=False)
        self.copy("*.so", dst="lib", keep_path=False)
        self.copy("*.a", dst="lib", keep_path=False)

    def package_info(self):
        self.cpp_info.libs = ["hello"]

There are two important changes:
有两个重要的变化:

  • Added the exports_sources field, indicating to Conan to copy all the files from the local src folder into the package recipe.
  • 添加 exports_sources 字段,指示conan将本地 src 文件夹中的所有文件复制到软件包配方中。
  • Removed the source() method, since it is no longer necessary to retrieve external sources.
  • 删除了 source() 方法,因为不再需要检索外部来源。

Also, you can notice the two CMake lines:

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

They are not added in the package recipe, as they can be directly added to the src/CMakeLists.txt file.
它们不会添加到软件包配方中,因为它们可以直接添加到 src/CMakeLists.txt 文件中。

And simply create the package for user and channel demo/testing as described previously:
只需按照前面所述,为user和channel演示/测试创建软件包即可:

$ conan create . demo/testing
...
hello/0.1@demo/testing test package: Running test()
Hello world Release!

2.2 Capturing the Remote and Commit: scm

Warning
This is a deprecated feature. Please refer to the Migration Guidelines to find the feature that replaces this one.
这是一项已废弃的功能。请参阅 "迁移指南 "查找替代此功能的功能。

You can use the scm attribute with the url and revision field set to auto. When you export the recipe (or when conan create is called) the exported recipe will capture the remote and commit of the local repository:
你可以使用 scm 属性,并将 urlrevision字段设置为自动。导出配方时(或调用 conan create 时),导出的配方将捕获本地版本库的remote和commit:

import os
 from conans import ConanFile, CMake, tools

 class HelloConan(ConanFile):
     scm = {
         "type": "git",  # Use "type": "svn", if local repo is managed using SVN
         "subfolder": "hello",
         "url": "auto",
         "revision": "auto",
         "password": os.environ.get("SECRET", None)
     }
     ...

You can commit and push the conanfile.py to your origin repository, which will always preserve the auto values. When the file is exported to the Conan local cache (except you have uncommitted changes, read below), these data will be stored in the conanfile.py itself (Conan will modify the file) or in a special file conandata.yml that will be stored together with the recipe, depending on the value of the configuration parameter scm_to_conandata.
您可以提交 conanfile.py 并将其推送到您的源版本库,这样将始终保留自动值。当文件导出到 Conan 本地缓存时(除非您有未提交的修改,请阅读下文),这些数据将存储在 conanfile.py 本身(Conan 将修改该文件)或一个特殊文件 conandata.yml,该文件将与配方一起存储,具体取决于配置参数 scm_to_conandata 的值。

  • If the scm_to_conandata is not activated (default behavior in Conan v1.x) Conan will store a modified version of the conanfile.py with the values of the fields in plain text:
  • 如果未激活 scm_to_conandata(Conan v1.x 中的默认行为),Conan 将以纯文本形式存储字段值的 conanfile.py 修改版:
    import os
    from conans import ConanFile, CMake, tools

    class HelloConan(ConanFile):
        scm = {
            "type": "git",
            "subfolder": "hello",
            "url": "https://github.com/conan-io/hello.git",
            "revision": "437676e15da7090a1368255097f51b1a470905a0",
            "password": "MY_SECRET"
        }
        ...

So when you upload the recipe to a Conan remote, the recipe will contain the “resolved” URL and commit.
因此,当您将配方上传到conan远程服务器时,配方将包含 "已解析 "的 URL 和提交。

  • If scm_to_conandata is activated, the value of these fields (except username and password) will be stored in the conandata.yml file that will be automatically exported with the recipe.
  • 如果激活了 scm_to_conandata,这些字段的值(用户名和密码除外)将保存在 conandata.yml 文件中,该文件将随配方自动导出。

Whichever option you choose, the data resolved will be assigned by Conan to the corresponding field when the recipe file is loaded, and they will be available for all the methods defined in the recipe. Also, if building the package from sources, Conan will fetch the code in the captured url/commit before running the method source() in the recipe (if defined).
无论您选择哪个选项,在加载配方文件时,conan都会将已解析的数据分配到相应的字段,这些数据将用于配方中定义的所有方法。此外,如果从源代码构建软件包,在运行配方中的 source() 方法(如果已定义)之前,conan将在捕获的 url/commit 中获取代码。

As SCM attributes are evaluated in the local directory context (see scm attribute), you can write more complex functions to retrieve the proper values, this source conanfile.py will be valid too:
由于 SCM 属性是在本地目录上下文中评估的(参见 scm 属性),你可以编写更复杂的函数来获取适当的值,这样 conanfile.py 源文件也会有效:

 import os
 from conans import ConanFile, CMake, tools

 def get_remote_url():
      """ Get remote url regardless of the cloned directory """
      here = os.path.dirname(__file__)
      svn = tools.SVN(here)
      return svn.get_remote_url()

 class HelloConan(ConanFile):
      scm = {
         "type": "svn",
         "subfolder": "hello",
         "url": get_remote_url(),
         "revision": "auto"
      }
     ...

Tip
When doing a conan create or conan export, Conan will capture the sources of the local scm project folder in the local cache.
在创建或导出 Conan 时,Conan 会在本地缓存中捕获本地 scm 项目文件夹的源代码。

This allows building packages making changes to the source code without the need of committing them and pushing them to the remote repository. This convenient to speed up the development of your packages when cloning from a local repository.
这允许在构建软件包时修改源代码,而无需提交并推送到远程版本库。当从本地版本库克隆软件包时,这可以加快软件包的开发速度。

So, if you are using the scm feature, with some auto field for url and/or revision and you have uncommitted changes in your repository a warning message will be printed:
因此,如果您使用 scm 功能,并自动输入网址和/或修订版本,而版本库中有未提交的更改,则会打印一条警告信息:

$ conan export . hello/0.1@demo/testing

 hello/0.1@demo/testing: WARN: There are uncommitted changes, skipping the replacement of 'scm.url'
 and 'scm.revision' auto fields. Use --ignore-dirty to force it.
 The 'conan upload' command will prevent uploading recipes with 'auto' values in these fields.

As the warning message explains, the auto fields won’t be replaced unless you specify --ignore-dirty, and by default, the conan upload will block the upload of the recipe. This prevents recipes to be uploaded with incorrect scm values exported. You can use conan upload --force to force uploading the recipe with the auto values un-replaced.
正如警告信息所述,除非指定--ignore-dirty,否则自动字段不会被替换。这可以防止在上传配方时导出错误的 scm 值。你可以使用 conan upload --force 来强制上传未替换自动值的配方。

  • 27
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 46
    评论
评论 46
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值