Introduction to conan: conanfile txt conanfile. The difference between PY

Posted by moe on Sat, 19 Feb 2022 06:55:08 +0100

conan conanfile. txt conanfile. The difference between PY

In previous blogs< Introduction to conan (4): conan refers to an example of a third-party library >In, we take cJSON as an example to illustrate how to reference a conan package in the project.

conanfile.txt

In this blog, we created a conanfile to reference the cjson/1.7.13 library Txt file is used to specify json_test.c program dependency library cjson

[requires]
cjson/1.7.13

[generators]
cmake

About conanfile Txt for details, see Conan's official documents <conanfile.txt> and <Installing dependencies>

conanafile.txt has other fields: options,imports...

You can understand this conanfile Txt is used to define the conan configuration of the current project. conan's install, create, export, export PKG, upload And other commands will be executed according to this configuration file.

conanfile.py

In< Introduction to conan (3): upload precompiled artifacts >In, we use create new to create a new package:

Use the conan new command to create a basic configuration:

$ cd cjson.build/release/
$ conan new cjson/1.7.15 --bare
File saved: conanfile.py

conan new will generate conanfile in the current folder py,

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"]

This conanfile Py is also used to define the conan configuration of the current project. It defines all the configurations of a conan package in the form of python class, which is obviously relative to conanfile Txt, the content of this python script is much richer and more flexible (because you can use methods to define logic).

difference

Both are used for configuration Txt and conan file What's the difference between py? Why design two configuration definition methods? What happens if there are two definition files in a project?

These questions confused me when I first started conan.

I searched Google and found that someone asked conan official the same question as me:< Mix conanfile.py and conanfile.txt in the same project #307>

The following is the author's reply:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-7pm8CCXH-1645197377826)(cnblog/conan16.png)]

conanfile.txt is a simple conan configuration definition method. Not every program understands python script (I don't understand it). If developers want to use conan to simplify the reference method of third-party libraries just as a consumer, in most cases, conan can work normally as long as requirements and generators are set correctly, Then conanfile is preferred Txt this configuration file definition method, because it is more intuitive and easy to understand.

If a developer wants to act as a * * producer * * to package his project into a conan package and upload it to the conan server for use by a third party, conanfile Txt cannot meet the requirements. You must use the full-featured config Py script to define package configuration. In fact, conan distributes packages based on the flexibility of python script through conanfile Py to define the full configuration of the package. So when we execute the conan new command to create a new conan configuration, the conan file is automatically generated Py script.

Therefore, conanfile is not allowed in a project Txt and conanfile Py coexists (it's all package definitions, who's listening to), and an error will be reported when executing the conan command.

conan series

Introduction to conan (I): installation of conan and jfrog artifact
Introduction to conan (II): conan service configuration - password management and policy
Introduction to conan (III): uploading precompiled artifacts
Introduction to conan (IV): examples of conan referencing third-party libraries
Introduction to conan (V): examples of conan cross compiling and referencing third-party libraries
Introduction to conan (VI): the difference between conanfile.txt and conanfile.py