打包文件:package() 方法

我们已经在 hello 包中使用了 package() 方法来调用 CMake 的安装步骤。在本教程中,我们将更详细地解释 CMake.install() 的用法,以及如何修改此方法以完成以下工作:

  • 使用 conan.tools.files 工具集将生成的构建产物从构建文件夹复制到包文件夹

  • 复制包许可证

  • 管理软链接打包

请先克隆源文件以重新创建此项目。您可以在 GitHub 上的 examples2 仓库 中找到它们

$ git clone https://github.com/conan-io/examples2.git
$ cd examples2/tutorial/creating_packages/package_method

在 package() 方法中使用 CMake 安装步骤

当您已在 CMakeLists.txt 中定义了将构建产物(头文件、库、二进制文件)从构建和源文件夹提取到预定位置并可能对这些产物进行后处理的功能时,这是最简单的选择。这无需修改您的 CMakeLists.txt 即可工作,因为 Conan 会将 CMAKE_INSTALL_PREFIX CMake 变量设置为指向 recipe 的 package_folder 属性。然后,只需在 CMakeLists.txt 中对创建的目标调用 install(),就足以让 Conan 将构建好的产物移动到 Conan 本地缓存中的正确位置。

CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(hello CXX)

add_library(hello src/hello.cpp)
target_include_directories(hello PUBLIC include)
set_target_properties(hello PROPERTIES PUBLIC_HEADER "include/hello.h")

...

install(TARGETS hello)
conanfile.py
def package(self):
    cmake = CMake(self)
    cmake.install()

让我们再次构建我们的包,并注意 Conan 本地缓存中有关文件打包的行

$ conan create . --build=missing -tf=""
...
hello/1.0: Build folder /Users/user/.conan2/p/tmp/b5857f2e70d1b2fd/b/build/Release
hello/1.0: Generated conaninfo.txt
hello/1.0: Generating the package
hello/1.0: Temporary package folder /Users/user/.conan2/p/tmp/b5857f2e70d1b2fd/p
hello/1.0: Calling package()
hello/1.0: CMake command: cmake --install "/Users/user/.conan2/p/tmp/b5857f2e70d1b2fd/b/build/Release" --prefix "/Users/user/.conan2/p/tmp/b5857f2e70d1b2fd/p"
hello/1.0: RUN: cmake --install "/Users/user/.conan2/p/tmp/b5857f2e70d1b2fd/b/build/Release" --prefix "/Users/user/.conan2/p/tmp/b5857f2e70d1b2fd/p"
-- Install configuration: "Release"
-- Installing: /Users/user/.conan2/p/tmp/b5857f2e70d1b2fd/p/lib/libhello.a
-- Installing: /Users/user/.conan2/p/tmp/b5857f2e70d1b2fd/p/include/hello.h
hello/1.0 package(): Packaged 1 '.h' file: hello.h
hello/1.0 package(): Packaged 1 '.a' file: libhello.a
hello/1.0: Package 'fd7c4113dad406f7d8211b3470c16627b54ff3af' created
hello/1.0: Created package revision bf7f5b9a3bb2c957742be4be216dfcbb
hello/1.0: Full package reference: hello/1.0#25e0b5c00ae41ef9fbfbbb1e5ac86e1e:fd7c4113dad406f7d8211b3470c16627b54ff3af#bf7f5b9a3bb2c957742be4be216dfcbb
hello/1.0: Package folder /Users/user/.conan2/p/47b4c4c61c8616e5/p

如您所见,调用 cmake.install() 方法后,includelibrary 文件都被复制到了包文件夹。

在 package() 方法中使用 conan.tools.files.copy() 并打包许可证

对于您不想依赖 CMake 的安装功能或正在使用其他构建系统的情况,Conan 提供了将选定的文件复制到 package_folder 的工具。在这种情况下,您可以使用 tools.files.copy 函数进行复制。我们可以将之前的 cmake.install() 步骤替换为自定义的文件复制,结果将是相同的。

请注意,我们还将库源文件中的 LICENSE 文件打包到了 licenses 文件夹中。这是 Conan 包中的常见模式,也可以添加到使用 cmake.install() 的前一个示例中,因为 CMakeLists.txt 不会将此文件复制到 package folder

conanfile.py
def package(self):
    copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
    copy(self, pattern="*.h", src=os.path.join(self.source_folder, "include"), dst=os.path.join(self.package_folder, "include"))
    copy(self, pattern="*.a", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
    copy(self, pattern="*.so", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
    copy(self, pattern="*.lib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
    copy(self, pattern="*.dll", src=self.build_folder, dst=os.path.join(self.package_folder, "bin"), keep_path=False)
    copy(self, pattern="*.dylib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)

让我们再次构建我们的包,并注意 Conan 本地缓存中有关文件打包的行

$ conan create . --build=missing -tf=""
...
hello/1.0: Build folder /Users/user/.conan2/p/tmp/222db0532bba7cbc/b/build/Release
hello/1.0: Generated conaninfo.txt
hello/1.0: Generating the package
hello/1.0: Temporary package folder /Users/user/.conan2/p/tmp/222db0532bba7cbc/p
hello/1.0: Calling package()
hello/1.0: Copied 1 file: LICENSE
hello/1.0: Copied 1 '.h' file: hello.h
hello/1.0: Copied 1 '.a' file: libhello.a
hello/1.0 package(): Packaged 1 file: LICENSE
hello/1.0 package(): Packaged 1 '.h' file: hello.h
hello/1.0 package(): Packaged 1 '.a' file: libhello.a
hello/1.0: Package 'fd7c4113dad406f7d8211b3470c16627b54ff3af' created
hello/1.0: Created package revision 50f91e204d09b64b24b29df3b87a2f3a
hello/1.0: Full package reference: hello/1.0#96ed9fb1f78bc96708b1abf4841523b0:fd7c4113dad406f7d8211b3470c16627b54ff3af#50f91e204d09b64b24b29df3b87a2f3a
hello/1.0: Package folder /Users/user/.conan2/p/21ec37b931782de8/p

检查 includelibrary 文件是如何打包的。LICENSE 文件也如我们前面所解释的那样被复制了。