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

请先克隆源代码以重新创建此项目。您可以在 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

另请参阅