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

2023-11-30 07:36

本文主要是介绍创建conan包-不同/相同repo中的配方和来源,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

  • 1 Recipe and Sources in a Different Repo
    • 1.1 source()的方法
    • 1.2 使用scm 属性
  • 2 Recipe and Sources in the Same Repo
    • 2.1 Exporting the Sources with the Recipe: exports_sources
    • 2.2 Capturing the Remote and Commit: scm

本文是基于对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, toolsclass 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, toolsclass 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, toolsclass 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, CMakeclass 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 osfrom conans import ConanFile, CMake, toolsclass 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 osfrom conans import ConanFile, CMake, toolsclass 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 osfrom conans import ConanFile, CMake, toolsdef 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/testinghello/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 来强制上传未替换自动值的配方。

这篇关于创建conan包-不同/相同repo中的配方和来源的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/436185

相关文章

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

Macos创建python虚拟环境的详细步骤教学

《Macos创建python虚拟环境的详细步骤教学》在macOS上创建Python虚拟环境主要通过Python内置的venv模块实现,也可使用第三方工具如virtualenv,下面小编来和大家简单聊聊... 目录一、使用 python 内置 venv 模块(推荐)二、使用 virtualenv(兼容旧版 P

Linux lvm实例之如何创建一个专用于MySQL数据存储的LVM卷组

《Linuxlvm实例之如何创建一个专用于MySQL数据存储的LVM卷组》:本文主要介绍使用Linux创建一个专用于MySQL数据存储的LVM卷组的实例,具有很好的参考价值,希望对大家有所帮助,... 目录在Centos 7上创建卷China编程组并配置mysql数据目录1. 检查现有磁盘2. 创建物理卷3. 创

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定