预编译好的三方库也分两大种情况:有cmake config
文件和没有cmake config
文件的库,然后每种情况再分是否仅有头文件,下面我们每个场景描述何如托管:
1. 仅有头文件的库且无自带cmake config文件的情况
目录结构如下:
rapidjson
└── 1.1.0
├── conan_cmds.sh
├── conanfile.py
└── include
└── rapidjson
├── allocators.h
├── cursorstreamwrapper.h
├── document.h
├── ...
conanfile.py如下:
import os
from conan import ConanFile
from conan.tools.files import copy
class LibraryRecipe(ConanFile):
name = "rapidjson"
version = "1.1.0"
# No settings/options are necessary, this is header only
exports_sources = "include/*"
# We can avoid copying the sources to the build folder in the cache
no_copy_source = True
def package(self):
copy(self, "include/*.h*", self.source_folder, self.package_folder, keep_path=True)
def package_info(self):
# For header-only packages, libdirs and bindirs are not used
# so it's necessary to set those as empty.
self.cpp_info.bindirs = []
self.cpp_info.libdirs = []
如果此头库本来已经提供了cmake config文件
目录结构如下:
rapidjson
└── 1.1.0
├── conan_cmds.sh
├── conanfile.py
├── include
│ └── rapidjson
│ ├── allocators.h
│ ├── cursorstreamwrapper.h
│ ├── document.h
│ ├── ...
└── lib
└── cmake
└── rapidjson
则可以统一用下面的模板:
import os
from conan import ConanFile
from conan.tools.files import copy
class LibraryRecipe(ConanFile):
name = "rapidjson" # -------- 这里的库名需要自行修改
version = "1.0.0" # -------- 这里的库版本需要自行修改
settings = "os", "compiler", "build_type", "arch"
def layout(self):
_arch = str(self.settings.arch).lower()
_os = str(self.settings.os).lower()
_current = os.path.abspath(os.getcwd())
self.folders.build = _arch + "-" + _os
self.folders.source = self.folders.build
self.package_dir = os.path.join(_current, _arch + "-" + _os)
def package(self):
_include = os.path.join(self.package_dir, "include")
_lib = os.path.join(self.package_dir, "lib")
_bin = os.path.join(self.package_dir, "bin")
copy(self, "*", _include, os.path.join(self.package_folder, "include"), keep_path=True)
copy(self, "*", _lib, os.path.join(self.package_folder, "lib"), keep_path=True)
copy(self, "*", _bin, os.path.join(self.package_folder, "bin"), keep_path=True)
def package_info(self):
# Disable the config package that would otherwise be generated by CMakeDeps
self.cpp_info.set_property("cmake_find_mode", "none")
self.cpp_info.builddirs.append(os.path.join('lib', 'cmake'))
2.有二进制库但无cmake config文件的情况
目录结构如下:
ehp-hdi
└── 2.0.7
├── armv8-linux
│ ├── include
│ └── lib
├── conan_cmds.sh
├── conanfile.py
└── x86_64-linux
├── include
└── lib
如果支持很多平台,通过目录隔离,如:armv8-linux和x86_64-linux,conanfile.py会通过arch和os类型找到对应的platform目录;
conanfile.py如下:
import os
from conan import ConanFile
from conan.tools.files import copy
class LibraryRecipe(ConanFile):
name = "ehp-hdi"
version = "2.0.7"
settings = "os", "compiler", "build_type", "arch"
def layout(self):
_arch = str(self.settings.arch).lower()
_os = str(self.settings.os).lower()
_current = os.path.abspath(os.getcwd())
self.folders.build = _arch + "-" + _os
self.folders.source = self.folders.build
self.package_dir = os.path.join(_current, _arch + "-" + _os)
def package(self):
_include = os.path.join(self.package_dir, "include")
_lib = os.path.join(self.package_dir, "lib")
_bin = os.path.join(self.package_dir, "bin")
copy(self, "*", _include, os.path.join(self.package_folder, "include"), keep_path=True)
copy(self, "*", _lib, os.path.join(self.package_folder, "lib"), keep_path=True)
copy(self, "*", _bin, os.path.join(self.package_folder, "bin"), keep_path=True)
def package_info(self):
self.cpp_info.libs = [
"BaseLib",
"cjson",
"DataIO",
"ehr-common",
"ehr-decision-map-base",
"ehr-decision-map-restricted-area",
"ehr-location-map-base",
"hdicoresdk",
"hdimapapi",
"hdimapengine",
"nds_sqlite3",
"nds_sqlite3_crypto",
"nds_update"
]
在package_info里的self.cpp_info.libs里定义当前库内所包含的所有库文件名字。
如果此库自带cmake config文件,即结构如下:
ehp-hdi
└── 2.0.7
├── armv8-linux
│ ├── include
│ └── lib
├── conan_cmds.sh
├── conanfile.py
└── x86_64-linux
├── include
└── lib
└── cmake
└──ehp-hdi
├── ehp-hdiConfig.cmake
├── ehp-hdiConfigVersion.cmake
├── ehp-hdiTargets.cmake
└── ehp-hdiTargets-release.cmake
则上面的package_info就可以替换成下面的方式:
def package_info(self):
# Disable the config package that would otherwise be generated by CMakeDeps
self.cpp_info.set_property("cmake_find_mode", "none")
self.cpp_info.builddirs.append(os.path.join('lib', 'cmake'))
3. 如何方便地创建conan库和上传到jfrog
在上面提到的conan_command.sh就是做这个事情,只是将常见的conan操作封装在shell命令里:
#!/bin/bash
profile=$1
user=admin
channel=stable
repo=myrepo
pkgName=myPkgName
# Read `CONAN_CHANNEL` from ENV, default is `stable`
if [[ -n "$CONAN_CHANNEL" && ("$CONAN_CHANNEL" != "stable" && "$CONAN_CHANNEL" != "testing") ]]; then
echo "Error: CONAN_CHANNEL must be either 'stable' or 'testing'."
exit 1
elif [ -n "$CONAN_CHANNEL" ]; then
channel=$CONAN_CHANNEL
fi
# Check if need to create and upload library in batch mode.
if [[ -n "$CONAN_CREATE_UPLOAD" && $CONAN_CREATE_UPLOAD == "ON" ]]; then
echo "Performing create library..."
conan create . --user=$user --channel=$channel -s build_type=Debug --profile=${profile}
conan create . --user=$user --channel=$channel -s build_type=Release --profile=${profile}
echo "Performing upload library to remote repo..."
conan upload $pkgName -r=$repo
exit 0
fi
# Read user input choise and execute matched commands.
echo "Welcome to the conan cmds..."
echo "Please select an option:"
echo "1. Create library"
echo "2. Upload library to remote repo"
echo "3. List local libraries"
echo "4. Search remote libraries"
echo "5. Remove local libraries"
read choice
case $choice in
1)
echo "Performing create library..."
conan create . --user=$user --channel=$channel -s build_type=Debug --profile=${profile}
conan create . --user=$user --channel=$channel -s build_type=Release --profile=${profile}
;;
2)
echo "Performing upload library to remote repo..."
conan upload $pkgName -r=$repo
;;
3)
echo "Performing list local libraries..."
conan list $pkgName
;;
4)
echo "Performing search remote libraries..."
conan search $pkgName -r=$repo
;;
5)
echo "Performing remove local libraries..."
conan remove -c $pkgName
;;
*)
echo "Invalid selection!"
;;
esac
user:你的jfrog的账户名
repo: 你在jfrog里创建的repo的名字
pkgName: 你的包名字