conan use (four) - packaged binaries

Summed up front how to package a deposit library header files, that case is very simple, because only need to copy the source file on the line. How to package now under study would normally generate a DLL or static package library. Reference document: https://docs.conan.io/en/latest/creating_packages/getting_started.html#creating-the-package-recipe  .

1. Prepare Source

Here I use a small tool written before: https://github.com/243286065/lz-string-cpp/tree/dev  . In its changed on the dev branch library.

2. Package

In windows, likewise, we need to use conan command creates a template conanfile.py out:

conan new LZStringcpp/1.0.0 

Then modify confile.py:

from conans import ConanFile, CMake, tools


class LzstringcppConan(ConanFile):
    name = "LZStringcpp"
    version = "1.0.0"
    license = "MIT"
    author = "xl"
    url = "https://github.com/243286065/lz-string-cpp/tree/dev"
    description = "C++ Class implementation of lz-string (based on https://github.com/pieroxy/lz-string)"
    topics = ("LZS", "Compress")
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake"

    def source(self):
        self.run("git clone -b dev https://github.com/243286065/lz-string-cpp.git")
        # This small hack might be useful to guarantee proper /MT /MD linkage
        # in MSVC if the packaged project doesn't have variables to set it
        # properly
        tools.replace_in_file("lz-string-cpp/CMakeLists.txt", "project(LZStringcpp)",
                              '''project(LZStringcpp)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()''')

    def build(self):
        cmake = CMake(self)
        cmake.configure(source_folder="lz-string-cpp")
        cmake.build()

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

    def package(self):
        self.copy("LZString.h", dst="include", src="lz-string-cpp/src")
        self.copy("*.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 = ["LZStringcpp"]

Here to explain the meaning of the above script:

  • settings Field defines the configuration different binary packages, require different configurations will produce different binary packages. If you want to do cross-compilation, use self.settings.os and self.settings.arc
    if platform.system() == "Windows":
        cmake = CMake(self)
        cmake.configure(source_folder="hello")
        cmake.build()
    else:
        env_build = AutoToolsBuildEnvironment(self)
        env_build.configure()
        env_build.make()
  • default_options Field indicates the default static library is provided;
  • source Function is used to prepare the source code, here is executed git clone , you can also perform http download something.
  • build Use cmake to build, you can also call directly to make, MSBuild and other build.
  • package The method of copy header files, library files to the final package.
  • package_info Method defines the library must be linked LZStringcpp the user to use this package.

Then execution:

conan create . xl/stable

Will be successfully executed under normal circumstances, then you can go .conan directory view, you can see a new library, and is compiled lib files.
We can upload it to the server:

conan upload LZStringcpp/1.0.0@xl/stable -r develope --all

image.png

3. Test

We went on Ubuntu, the last time we tested conanfile.txt rapidjson modify the test program:

[requires]
rapidjson/1.1.0@tencent/stable
LZStringcpp/1.0.0@xl/stable

[generators]
cmake

[imports]
include, * -> ./include
lib, * -> ./lib

[options]
LZStringcpp:shared=True

Also performed in the build directory:

conan install ..

It will prompt:
image.png
This is because we are packed on the windows, and the default is to compile a static library package, so we ask that rely on dynamic libraries in Ubuntu, it would not otherwise exist, then it would suggest that you use --build LZString be Source building, so we re-run:

conan install .. --build LZStringcpp

Results:
image.png
In this way we can directly use on Linux, is not very convenient. As long as the conanfile.py settingsfield attributes not perfectly match, it can be constructed from the source.

Can be built directly from the source, which is a very nice feature, it allows us to avoid having to maintain a large number of versions.

Guess you like

Origin www.cnblogs.com/xl2432/p/11906219.html