預(yù)編譯好的三方庫也分兩大種情況:有cmake config文件和沒有cmake config文件的庫,然后每種情況再分是否僅有頭文件,下面我們每個場景描述何如托管:
1. 僅有頭文件的庫且無自帶cmake config文件的情況
目錄結(jié)構(gòu)如下:
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 = []
如果此頭庫本來已經(jīng)提供了cmake config文件
目錄結(jié)構(gòu)如下:
rapidjson
└── 1.1.0
├── conan_cmds.sh
├── conanfile.py
├── include
│ └── rapidjson
│ ├── allocators.h
│ ├── cursorstreamwrapper.h
│ ├── document.h
│ ├── ...
└── lib
└── cmake
└── rapidjson
則可以統(tǒng)一用下面的模板:
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.有二進(jìn)制庫但無cmake config文件的情況
目錄結(jié)構(gòu)如下:
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類型找到對應(yīng)的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里定義當(dāng)前庫內(nèi)所包含的所有庫文件名字。
如果此庫自帶cmake config文件,即結(jié)構(gòu)如下:
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. 如何方便地創(chuàng)建conan庫和上傳到j(luò)frog
在上面提到的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里創(chuàng)建的repo的名字
pkgName: 你的包名字