我们经常需要从现有二进制文件创建包,比如第三方或供应商提供的C/C++库(只有include和lib),或在引入conan管理包之前手工编译编译好C/C++库。在这种情况下,我们并不需要conan从源代码编译,费时费事或根本不可能。所以以下情况我们可以考虑直接将本地已经编译好的二进制文件生成conan包:
本文在cJSON为例,说明如何上传预编译的二进制库(artifact)
cjson.build是我之前用于编译cJSON而写的脚本,可以使用armcc,gcc,MSVC编统linux-arm,linux-armhf,linux-x86_64,windows-x86_64平台的库。
现在我打算用conan来做包管理,对于我来说,将这些已经预编译好的库上传到conan私有制品库,是最省事的办法 。
所以我参照conan的官方文档《Packaging Existing Binaries》来实现arm平台预编译库上传到私有制品库
你可以用cjson.build编译出自己当前平台的库,然后参照本文来做测试。
使用conan new
命令创建基本配置:
$ cd cjson.build/release/
$ conan new cjson/1.7.15 --bare
File saved: conanfile.py
–bare -b 创建不需要编译的配置文件
conan new
会在当前文件夹下生成conanfile.py,我修改了其中的description,url,license,author,topics
改为有意义的值
from conans import ConanFile, tools
class CjsonConan(ConanFile):
name = "cjson"
version = "1.7.15"
settings = "os", "compiler", "build_type", "arch"
description = "Ultralightweight JSON parser in ANSI C."
url = "https://github.com/DaveGamble/cJSON"
license = "https://github.com/DaveGamble/cJSON/blob/master/LICENSE"
author = "DaveGamble"
topics = "json"
def package(self):
self.copy("*")
def package_info(self):
self.cpp_info.libs = tools.collect_libs(self)
def configure(self):
del self.settings.compiler.libcxx
因为cJSON是个纯C的库,所以需要添加最后两行代码,
conan export-pkg
否则会报错: ERROR: cjson/1.7.15: ‘settings.compiler.libcxx’ value not defined 参见《ERROR: Invalid setting》https://docs.conan.io/en/latest/faq/troubleshooting.html#error-setting-value-not-defined
conan export-pkg
命令将release/cJSON_arm-linux-gnueabihf
(适用于linux-armhf平台的库)下生成的arm平台库打包保存到本地仓库($HOME/.conan/data
)
$ cd cjson.build/release/
$ conan export-pkg -pf cJSON_arm-linux-gnueabihf conanfile.py -s os=Linux -s compiler=gcc -s compiler.version=4.8 -s arch=armv7 --force
Exporting package recipe
cjson/1.7.15@cjson/1.7.15: A new conanfile.py version was exported
cjson/1.7.15@cjson/1.7.15: Folder: C:\Users\guyadong\.conan\data\cjson\1.7.15\cjson\1.7.15\export
cjson/1.7.15@cjson/1.7.15: Exported revision: a7a0296958b0d29e5e4f34434b1808cd
cjson/1.7.15@cjson/1.7.15: Forced build from source
Packaging to 755fc07adbed235a7d3eadec4b6882d4912f09bc
cjson/1.7.15@cjson/1.7.15: Exporting to cache existing package from user folder
cjson/1.7.15@cjson/1.7.15: Package folder C:\Users\guyadong\.conan\data\cjson\1.7.15\cjson\1.7.15\package\755fc07adbed235a7d3eadec4b6882d4912f09bc
cjson/1.7.15@cjson/1.7.15: Packaged 1 '.h' file: cJSON.h
cjson/1.7.15@cjson/1.7.15: Packaged 1 '.a' file: libcjson.a
cjson/1.7.15@cjson/1.7.15: Packaged 4 '.cmake' files: cjson-release.cmake, cjson.cmake, cJSONConfig.cmake, cJSONConfigVersion.cmake
cjson/1.7.15@cjson/1.7.15: Packaged 1 '.pc' file: libcjson.pc
cjson/1.7.15@cjson/1.7.15: Package '755fc07adbed235a7d3eadec4b6882d4912f09bc' created
cjson/1.7.15@cjson/1.7.15: Created package revision 86d7c021c2a785a1b8ea1fd00af1893c
-pf --package-folder 指定要打包的文件夹 -f --force 强制上传,如果指定参数,会强制覆盖原来存在的包。未指定的,如果本地仓库已经存在同名名则报错退出。
os,compiler,compiler.version,arch
参数用于指定该包的交叉编译环境,如果未指定这些参数,则从$HOME/.conan/profiles/default
读取默认值 这些字段不是随便填的,每个字段都一个可选值列表:
执行conan upload
完成cjson/1.7.15
包上传私有制品库
$ conan upload cjson/1.7.15 -r=privrepo --all
Are you sure you want to upload 'cjson/1.7.15' to 'facelib'? (yes/no): yes
Uploading to remote 'privrepo':
Uploading cjson/1.7.15 to remote 'privrepo'
Uploading conanfile.py -> cjson/1.7.15
Uploading conanmanifest.txt -> cjson/1.7.15
Uploaded conan recipe 'cjson/1.7.15' to 'privrepo': http://127.0.0.1:8082/artifactory/api/conan/stable
Uploading package 1/1: 51dcb8c35db66a86148c7ec8052bb9971de45522 to 'privrepo'
Compressing package...
Uploading conan_package.tgz -> cjson/1.7.15:51dc
Uploading conaninfo.txt -> cjson/1.7.15:51dc
Uploading conanmanifest.txt -> cjson/1.7.15:51dc
-r 指定要上传的制品仓库名 –all 上传所有文件(package recipe and package)
登录入JFrog Artifactory可以看到刚才上传的包