创建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

相关文章

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

spoj705( 求不相同的子串个数)

题意:求串s的不同子串的个数 解题思路:任何子串都是某个后缀的前缀,对n个后缀排序,求某个后缀的前缀的个数,减去height[i](第i个后缀与第i-1 个后缀有相同的height[i]个前缀)。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstrin

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

Java 创建图形用户界面(GUI)入门指南(Swing库 JFrame 类)概述

概述 基本概念 Java Swing 的架构 Java Swing 是一个为 Java 设计的 GUI 工具包,是 JAVA 基础类的一部分,基于 Java AWT 构建,提供了一系列轻量级、可定制的图形用户界面(GUI)组件。 与 AWT 相比,Swing 提供了许多比 AWT 更好的屏幕显示元素,更加灵活和可定制,具有更好的跨平台性能。 组件和容器 Java Swing 提供了许多

顺序表之创建,判满,插入,输出

文章目录 🍊自我介绍🍊创建一个空的顺序表,为结构体在堆区分配空间🍊插入数据🍊输出数据🍊判断顺序表是否满了,满了返回值1,否则返回0🍊main函数 你的点赞评论就是对博主最大的鼓励 当然喜欢的小伙伴可以:点赞+关注+评论+收藏(一键四连)哦~ 🍊自我介绍   Hello,大家好,我是小珑也要变强(也是小珑),我是易编程·终身成长社群的一名“创始团队·嘉宾”

Maven创建项目中的groupId, artifactId, 和 version的意思

文章目录 groupIdartifactIdversionname groupId 定义:groupId 是 Maven 项目坐标的第一个部分,它通常表示项目的组织或公司的域名反转写法。例如,如果你为公司 example.com 开发软件,groupId 可能是 com.example。作用:groupId 被用来组织和分组相关的 Maven artifacts,这样可以避免

速了解MySQL 数据库不同存储引擎

快速了解MySQL 数据库不同存储引擎 MySQL 提供了多种存储引擎,每种存储引擎都有其特定的特性和适用场景。了解这些存储引擎的特性,有助于在设计数据库时做出合理的选择。以下是 MySQL 中几种常用存储引擎的详细介绍。 1. InnoDB 特点: 事务支持:InnoDB 是一个支持 ACID(原子性、一致性、隔离性、持久性)事务的存储引擎。行级锁:使用行级锁来提高并发性,减少锁竞争