conan 上传预编译的库(artifact)
我们经常需要从现有二进制文件创建包,比如第三方或供应商提供的C/C 库(只有include和lib),或在引入conan管理包之前手工编译编译好C/C 库。在这种情况下,我们并不需要conan从源代码编译,费时费事或根本不可能。所以以下情况我们可以考虑直接将本地已经编译好的二进制文件生成conan包:
- 当您无法从源代码构建包时(当只有预编译的库可用时)。
- 在工件(artifact)开发阶段需要频繁打包提供另外的应用程序使用时。这时我只需要将编译好的工件快速提供给使用方而不需要重新编译,因此您不想调用 conan create。如果您使用 IDE 或在本地调用 conan build命令,此方法将保留您的构建缓存。
本文在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
使用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
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:Usersguyadong.conandatacjson1.7.15cjson1.7.15export
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:Usersguyadong.conandatacjson1.7.15cjson1.7.15package755fc07adbed235a7d3eadec4b6882d4912f09bc
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
读取默认值 这些字段不是随便填的,每个字段都一个可选值列表:
- os: [‘AIX’, ‘Android’, ‘Arduino’, ‘Emscripten’, ‘FreeBSD’, ‘Linux’, ‘Macos’, ‘Neutrino’, ‘SunOS’, ‘VxWorks’, ‘Windows’, ‘WindowsCE’, ‘WindowsStore’, ‘baremetal’, ‘iOS’, ‘tvOS’, ‘watchOS’]
- compiler: Possible values are [‘Visual Studio’, ‘apple-clang’, ‘clang’, ‘gcc’, ‘intel’, ‘intel-cc’, ‘mcst-lcc’, ‘msvc’, ‘qcc’, ‘sun-cc’]
- arch: Possible values are [‘x86’, ‘x86_64’, ‘ppc32be’, ‘ppc32’, ‘ppc64le’, ‘ppc64’, ‘armv4’, ‘armv4i’, ‘armv5el’, ‘armv5hf’, ‘armv6’, ‘armv7’, ‘armv7hf’, ‘armv7s’, ‘armv7k’, ‘armv8’, ‘armv8_32’, ‘armv8.3’, ‘sparc’, ‘sparcv9’, ‘mips’, ‘mips64’, ‘avr’, ‘s390’, ‘s390x’, ‘asm.js’, ‘wasm’, ‘sh4le’, ‘e2k-v2’, ‘e2k-v3’, ‘e2k-v4’, ‘e2k-v5’, ‘e2k-v6’, ‘e2k-v7’, ‘xtensalx6’, ‘xtensalx106’] 如果要获取准确的所有字段的可用值列表,参见HOME/.conan/settings.yml,其中包含conan配置文件中 settings允许值的默认定义 关于HOME/.conan/settings.yml参见 https://docs.conan.io/en/latest/extending/custom_settings.html#custom-settings
conan upload
执行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可以看到刚才上传的包