前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >conan入门(六):conanfile.txt conanfile.py的区别

conan入门(六):conanfile.txt conanfile.py的区别

作者头像
10km
发布2022-04-13 12:01:25
8370
发布2022-04-13 12:01:25
举报
文章被收录于专栏:10km的专栏

conan conanfile.txt conanfile.py的区别

在之前的博客《conan入门(四):conan 引用第三方库示例》中我们以cJSON为例说明了如何在项目中引用一个conan 包。

conanfile.txt

在这篇博客中我们为了引用cjson/1.7.13库,创建了一个conanfile.txt文件用于指定json_test.c程序的依赖库cjson

代码语言:javascript
复制
[requires]
cjson/1.7.13

[generators]
cmake

关于conanfile.txt的详细说明参见Conan官方文档《conanfile.txt》《Installing dependencies》

conanafile.txt还有一些别的字段:options,imports。。。

可以理解这个conanfile.txt的作用就是定义当前项目的conan配置。conan 的install,create,export,export-pkg,upload...等命令都会用依据这个配置文件来执行。

conanfile.py

在《conan入门(三):上传预编译的库(artifact)》中,我们用到了create new创建一个新的包:

使用conan new命令创建基本配置:

代码语言:javascript
复制
$ cd cjson.build/release/
$ conan new cjson/1.7.15 --bare
File saved: conanfile.py

conan new会在当前文件夹下生成conanfile.py,

代码语言:javascript
复制
from conans import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout


class HelloConan(ConanFile):
    name = "hello"
    version = "0.1"

    # Optional metadata
    license = "<Put the package license here>"
    author = "<Put your name here> <And your email here>"
    url = "<Package recipe repository url here, for issues about the package>"
    description = "<Description of Hello here>"
    topics = ("<Put some tag here>", "<here>", "<and here>")

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False]}
    default_options = {"shared": False, "fPIC": True}

    # Sources are located in the same place as this recipe, copy them to the recipe
    exports_sources = "CMakeLists.txt", "src/*"

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def layout(self):
        cmake_layout(self)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()

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

这个conanfile.py也是用于定义当前项目的conan配置,它以python 类的方式定义了一个conan包的所有配置,显然相对于conanfile.txt ,这个python脚本的内容要丰富许多,灵活许多(因为可以使用方法定义逻辑)。

difference

都是用于conan配置,conanfile.txt和conanfile.py有何区别?为何要设计两种配置定义方式?如果一个项目中存在两个定义文件文件会怎么样?

这些问题把刚conan入门的我搞糊涂了。

谷歌上搜索了一下,发现有人与我有同样的问题问了conan官方:《Mix conanfile.py and conanfile.txt in the same project #307

以下是作者的回复:

conanfile.txt是一种简单的conan配置定义方式,并不是每个程序员都懂python脚本(我就不懂),对于开发人员如果只是作为一个消费者角色(consumer),想使用conan简化第三方库的引用方式,在大多数情况下,一般只要正确设置requires,generators,conan就可以正常工作,那么首选使用conanfile.txt这种配置文件定义方式,因为它是更加直观易懂的。

如果开发人员要作为生产者角色(producer),把自己的项目也封装成conan包上传到conan服务器供第三方使用,conanfile.txt是不能满足要求的,必须使用全能的confile.py脚本来定义包的配置,事实上conan在分发包时就是基于python脚本的灵活性通过conanfile.py来定义包的全部配置的。所以当我们执行conan new命令创建一个新的conan配置时,自动生成的是conanfile.py脚本。

所以一个项目中是不允许conanfile.txt和conanfile.py共存的(都来是包定义,到底听谁的呢),执行conan命令会报错。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022/04/06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • conan conanfile.txt conanfile.py的区别
    • conanfile.txt
      • conanfile.py
        • difference
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档