标签搜索

目 录CONTENT

文章目录

conan的安装及其nexus的conan制品库使用

陈铭
2021-08-15 / 0 评论 / 0 点赞 / 435 阅读 / 1,175 字 / 正在检测是否收录...

设置nexus

开启conan的hosted库,默认只开启proxy库;接着nexus打开Conan Bearer Token Realm

# $nexus_home就是nexus安装的路径
vi $nexus_home/etc/nexus-default.properties
加上:nexus.conan.hosted.enabled=true

conan安装及其设置

pip install conan

一般来说,linux安装pypi后,模块的二进制命令并不会自动加到环境变量中

find / -name conan
# 找到conan的二进制命令
ln -s /usr/local/python3/bin/conan /usr/bin/conan

设置nexus库

conan remote add nexus_local http://cmjava.ltd:8081/repository/conan_local/ false
conan remote add nexus_proxy http://cmjava.ltd:8081/repository/conan_proxy/ false

打包并上传

生成打包模板

# -t:生成测试文件
conan new <model_name>/<version> -t

生成模板后会出现conanfile.py文件,需要修改,基本上只要改:

  • (1)是否需要加上requires
  • (2)哪里拉代码
  • (3)tools.replace_in_file修改
  • (3)cmake.configure的源码位置
  • (4)self.copy的各类文件复制位置
from conans import ConanFile, CMake, tools

class HelloConan(ConanFile):
    name = "<model_name>"
    version = "<version>"
    license = "<Put the package license here>"
    author = "<Put your name here> <And your email here>"
    url = "<Package recipe repository url here, for issues about the package>"
    description = "<Description of Hello here>"
    topics = ("<Put some tag here>", "<here>", "<and here>")
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False]}
    default_options = {"shared": False, "fPIC": True}
    generators = "cmake"
    requires = ["myjson/v1.0.0@ar/stable"]  # 考虑加上,即该模块的依赖

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def source(self):
        # 拉取源码,在~/.conan/data/<model_name>/<version>/<company>/<channel>/source下存放源码,也就是hello文件夹
        self.run("git clone https://github.com/conan-io/hello.git") 
        # 如果模块下有CMakeLists.txt,需要修改。
		# 因为打包会进行编译链接测试,所以可能需要外部依赖,选择用conan的依赖
        tools.replace_in_file("<model_name>/CMakeLists.txt", "PROJECT(<model_project_name>)",
                              '''PROJECT(<model_project_name>)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()''')

    # 定义编译和构建
    def build(self):
        cmake = CMake(self)
        # source_folder=~/.conan/data/<model_name>/<version>/<company>/<channel>/source/
        # 对应~/.conan/data/<model_name>/<version>/<company>/<channel>/source/hello
        cmake.configure(source_folder="hello")
        cmake.build()

        # Explicit way:
        # self.run('cmake %s/hello %s'
        #          % (self.source_folder, cmake.command_line))
        # self.run("cmake --build . %s" % cmake.build_config)

    # 定义打包
    def package(self):
        # 头文件和lib文件位置需要修改
        # dst=~/.conan/data/<model_name>/<version>/<company>/<channel>/package/
        # src=~/.conan/data/<model_name>/<version>/<company>/<channel>/build/
        self.copy("*.h", dst="include", src="hello")
        self.copy("*hello.lib", dst="lib", keep_path=False)
        self.copy("*.dll", dst="bin", keep_path=False)
        self.copy("*.so", dst="lib", keep_path=False)
        self.copy("*.dylib", dst="lib", keep_path=False)
        self.copy("*.a", dst="lib", keep_path=False)

    def package_info(self):
        self.cpp_info.libs = ["<model_name>"]

打包

conan create . <company>/<channel>

上传至私库

# <model_name>/<version>@<company>/<channel>:制品限定名
# --all:全部文件
conan upload <model_name>/<version>@<company>/<channel> --all -r=nexus_local

安装依赖

假定我们有个模块,需要用conan安装它的依赖。那么模块根路径除了源代码,要有conanfile.txt和CMakeLists.txt。前者conan安装依赖用,后者cmake读取conan的依赖进行编译用。

conan安装依赖

# 进入有conanfile.txt和CMakeLists.txt的根路径
cd <model_root>
# 会读取conanfile.txt
conan install .

因此conanfile.txt我们需要定义该模块依赖了什么

# 依赖的模块
[requires]
<model_name>/<version>@<company>/<channel>
# 编译器
[generators]
cmake

编译模块

# 会读取CMakeLists.txt
cmake . -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release

CMakeLists.txt的内容如下

# 最少的cmake版本
cmake_minimum_required(VERSION 2.8.12)
# 模块名
project(example)
# 相当于#define,可选
add_definitions("-std=c++11")

# 定义编译时所需的依赖,去读取conanbuildinfo.cmake寻找依赖,里面都是conan安装的。
# ${CMAKE_BINARY_DIR}:执行cmake命令的路径,也就是模块根路径
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
# 执行conanbuildinfo.cmake,必须的
conan_basic_setup()

# 可执行文件,参数(可执行文件, 源码文件)
add_executable(example example.cpp)
# 链接的库,参数(可执行文件, conan安装的依赖)
target_link_libraries(example ${CONAN_LIBS})

模块安装

安装好的文件都在./bin下

cmake --build .

我们就已经可以进入./bin执行文件了

0

评论区