conan: 封装只包含头文件(header_only)的库示例
有的C/C 项目只包含头文件,不需要编译,对于这种情况如何封装为Conan的包呢?
Conan官方文档 《Package scaffolding for conan new command》给出了样例
我就有这样一个项目 common_source_cpp收集了工作中常用的代码,以C/C 头文件形式为主,本文就以common_source_cpp为例说明如何实现conanfile.py将它封装为一个不需要编译只有头文件的Conan包。
conanfile.py
conanfile.py 码云地址: https://gitee.com/l0km/common_source_cpp/blob/master/conanfile.py
代码语言:javascript复制from conans import ConanFile, tools
class CommonSourceCppConan(ConanFile):
name = "common_source_cpp"
version = "0.0.0-DEV"
settings = "os", "compiler", "build_type", "arch"
description = "C/C /C 11公用代码"
url = "https://gitee.com/l0km/common_source_cpp"
license = "BSD-2-Clause"
author = "guyadong 10km0811@sohu.com"
topics = ("C", "C ", "C 11", "common")
settings = "os", "arch", "compiler", "build_type"
exports_sources = "*.h", "*.cpp", "*.hpp", "*.hh", "*.c", "LICENSE"
def package(self):
self.copy("*.h", dst="include")
self.copy("*.cpp", dst="include")
self.copy("*.hpp", dst="include")
self.copy("*.hh", dst="include")
self.copy("*.c", dst="include")
self.copy("LICENSE", dst="include")
def package_id(self):
# 重要:指定项目只有头文件
self.info.header_only()
def package_info(self):
if self.settings.compiler == "Visual Studio":
self.cpp_info.includedirs.append("include/dirent")
conanfile_master.py
如果需要项目的最新版本则实现如下
conanfile_master.py 码云地址: https://gitee.com/l0km/common_source_cpp/blob/master/conanfile_master.py
代码语言:javascript复制from conans import ConanFile, tools
class CommonSourceCppConanConan(ConanFile):
name = "common_source_cpp"
version = "master"
settings = "os", "compiler", "build_type", "arch"
description = "C/C /C 11公用代码"
url = "https://gitee.com/l0km/common_source_cpp"
license = "BSD-2-Clause"
author = "guyadong 10km0811@sohu.com"
topics = ("C", "C ", "C 11", "common")
settings = "os", "arch", "compiler", "build_type"
def export(self):
self.copy("conanfile.py",src="conanfile_master.py")
def source(self):
# 从远端仓库下载指定分支代码
self.run("git clone -b " self.version " " self.url ".git")
# git = tools.Git(folder=self.name)
# git.clone(self.url ".git")
# git.checkout(self.version)
def package(self):
# 确保当前仓库与远程仓库同步
self.run("cd common_source_cpp && git checkout " self.version " && git pull")
self.copy("*.h", dst="include", src="common_source_cpp")
self.copy("*.cpp", dst="include", src="common_source_cpp")
self.copy("*.hpp", dst="include", src="common_source_cpp")
self.copy("*.hh", dst="include", src="common_source_cpp")
self.copy("*.c", dst="include", src="common_source_cpp")
self.copy("LICENSE", dst="include", src="common_source_cpp")
def package_id(self):
self.info.header_only()
def package_info(self):
if self.settings.compiler == "Visual Studio":
self.cpp_info.includedirs.append("include/dirent")
conan create
有了上面的conanfile.py,就可以执行conan create
命令将cpp_redis生成到本地的conan仓库了
$ conan create .
Exporting package recipe
common_source_cpp/0.0.0-DEV exports_sources: Copied 43 '.h' files
common_source_cpp/0.0.0-DEV exports_sources: Copied 8 '.cpp' files
common_source_cpp/0.0.0-DEV exports_sources: Copied 11 '.hpp' files
common_source_cpp/0.0.0-DEV exports_sources: Copied 1 '.hh' file: uri.hh
common_source_cpp/0.0.0-DEV exports_sources: Copied 2 '.c' files: debug_printf.c, if_utilits_c.c
common_source_cpp/0.0.0-DEV exports_sources: Copied 1 file: LICENSE
common_source_cpp/0.0.0-DEV: The stored package has not changed
common_source_cpp/0.0.0-DEV: Exported revision: 50a7e6024f11c1e9b6bbab7e45c4253e
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=Visual Studio
compiler.runtime=MD
compiler.version=14
os=Windows
os_build=Windows
[options]
[build_requires]
[env]
common_source_cpp/0.0.0-DEV: Forced build from source
Installing package: common_source_cpp/0.0.0-DEV
Requirements
common_source_cpp/0.0.0-DEV from local cache - Cache
Packages
common_source_cpp/0.0.0-DEV:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Build
Installing (downloading, building) binaries...
common_source_cpp/0.0.0-DEV: Copying sources to build folder
。。。
common_source_cpp/0.0.0-DEV: Calling package()
common_source_cpp/0.0.0-DEV package(): Packaged 43 '.h' files
common_source_cpp/0.0.0-DEV package(): Packaged 11 '.hpp' files
common_source_cpp/0.0.0-DEV package(): Packaged 2 '.c' files: debug_printf.c, if_utilits_c.c
common_source_cpp/0.0.0-DEV package(): Packaged 8 '.cpp' files
common_source_cpp/0.0.0-DEV package(): Packaged 1 file: LICENSE
common_source_cpp/0.0.0-DEV package(): Packaged 1 '.hh' file: uri.hh
common_source_cpp/0.0.0-DEV: Package '5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9' created
common_source_cpp/0.0.0-DEV: Created package revision 6b32a82748a8366bb24b66daef5296df
common_source_cpp/0.0.0-DEV: WARN: This conanfile has no build step
conan create .
根据配置文件 (同级文件夹下的conanfile.py) 构建二进制包conan create
命令行用法参见Conan官方文档《conan create》
执行 conann create
指定conanfile_master.py
则会从远端仓库下载最新的maste分支代码
$ conan create conanfile_master.py
Exporting package recipe
common_source_cpp/master: Calling export()
common_source_cpp/master: The stored package has not changed
common_source_cpp/master: Exported revision: 4115d7716d69dce3edff00b6da3560ab
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=Visual Studio
compiler.runtime=MD
compiler.version=14
os=Windows
os_build=Windows
[options]
[build_requires]
[env]
common_source_cpp/master: Forced build from source
Installing package: common_source_cpp/master
Requirements
common_source_cpp/master from local cache - Cache
Packages
common_source_cpp/master:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Build
Installing (downloading, building) binaries...
common_source_cpp/master: Copying sources to build folder
common_source_cpp/master: Building your package in
。。。
common_source_cpp/master: Calling package()
Already on 'master'
Your branch is up to date with 'origin/master'.
Already up to date.
common_source_cpp/master package(): Packaged 43 '.h' files
common_source_cpp/master package(): Packaged 11 '.hpp' files
common_source_cpp/master package(): Packaged 2 '.c' files: debug_printf.c, if_utilits_c.c
common_source_cpp/master package(): Packaged 8 '.cpp' files
common_source_cpp/master package(): Packaged 1 file: LICENSE
common_source_cpp/master package(): Packaged 1 '.hh' file: uri.hh
common_source_cpp/master: Package '5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9' created
common_source_cpp/master: Created package revision 778c04901c633169b88ac94242cc152f
common_source_cpp/master: WARN: This conanfile has no build step
执行conan search
会显示本地仓库二进制包的信息
$ conan search common_source_cpp/0.0.0-DEV@
Existing packages for recipe common_source_cpp/0.0.0-DEV:
Package_ID: 5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9
Outdated from recipe: False
代码语言:javascript复制$ conan search common_source_cpp/master@
Existing packages for recipe common_source_cpp/master:
Package_ID: 5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9
Outdated from recipe: False
conan upload
执行conan upload
上传到私有制品库了:
conan upload common_source_cpp/0.0.0-DEV -r ${repo} -all
# ${repo}为私有制品库的名字
–all 指定上传所有内容(配置文件conanfile.py,源码和打包的文件–package),如果不指定些选项,只上传除package之外的所有文件
执行conan upload
上传到私有制品库了:
conan upload common_source_cpp/master -r ${repo}
# ${repo}为私有制品库的名字
注意:因为我们需要common_source_cpp/master保持与远程端同步的最新代码,所以这里没有使用–all 指定上传所有内容(配置文件conanfile.py,源码和package),只上传了conanfile.py,引用该项目时执行
conan install common_source_cpp/master@ --build common_source_cpp
会自动从远程仓库克隆代码在本地仓库生成package 关于conan upload
命令的详细说明参见Conan官方文档:《conan upload》
参考资料
《Recipe and Sources in a Different Repo》
《conan create》
《package()》
《source()》
《self.info.header_only()》