为第三方库创建软件包时声明布局

请先克隆源代码以重新创建此项目。您可以在 GitHub 的examples2 存储库中找到它们

$ git clone https://github.com/conan-io/examples2.git
$ cd examples2/examples/conanfile/layout/third_party_libraries

如果我们有这个项目,目的是为代码位于外部的第三方库创建一个软件包

.
├── conanfile.py
└── patches
    └── mypatch

conanfile.py 将如下所示

...

class Pkg(ConanFile):
    name = "hello"
    version = "1.0"
    exports_sources = "patches*"

    ...

    def layout(self):
        cmake_layout(self, src_folder="src")
        # if you are declaring your own layout, just declare:
        # self.folders.source = "src"

    def source(self):
        # we are inside a "src" subfolder, as defined by layout
        # the downloaded soures will be inside the "src" subfolder
        get(self, "https://github.com/conan-io/libhello/archive/refs/heads/main.zip",
            strip_root=True)
        # Please, be aware that using the head of the branch instead of an immutable tag
        # or commit is not a good practice in general as the branch may change the contents

        # patching, replacing, happens here
        patch(self, patch_file=os.path.join(self.export_sources_folder, "patches/mypatch"))

    def build(self):
        # If necessary, the build() method also has access to the export_sources_folder
        # for example if patching happens in build() instead of source()
        #patch(self, patch_file=os.path.join(self.export_sources_folder, "patches/mypatch"))
        cmake = CMake(self)
        cmake.configure()
        cmake.build()
        ...

我们可以看到 ConanFile.export_sources_folder 属性可以提供对源代码根文件夹的访问

  • 在本地,它将是 conanfile.py 所在的文件夹

  • 在缓存中,它将是“source”文件夹,该文件夹将包含 CMakeLists.txtpatches 的副本,而“source/src”文件夹将包含实际下载的源代码。

我们可以检查一下,现在一切运行良好

$ conan create .
...
Downloading main.zip
hello/1.0: Unzipping 3.7KB
Unzipping 100 %
...
[ 50%] Building CXX object CMakeFiles/hello.dir/src/hello.cpp.o
[100%] Linking CXX static library libhello.a
[100%] Built target hello
...
$ conan list hello/1.0
Local Cache
hello
    hello/1.0

另请参阅