Search code examples
pythonc++cmakeconan

Using Conan to build multiple libraries and packaging them to be used without Conan


I am very new to Conan and I am quite lost in the documentation at the moment and I cannot really find a way to do what I want.

Maybe I am not using the right tool for this, I am very open to suggestions.

Let's say my C++ project needs opencv and nlohmann_json to be built. I want to be able to create an archive of all my project's dependencies to be used as is, so it can be redistributed on our gitlab instance.

I want to have a repository which roughly looks like this:

├── conanfile.py
├── json
│   └── 3.10.4
│       └── conanfile.py
└── opencv
    ├── 4.4.0
    │   └── conanfile.py
    └── 5.1.0
        └── conanfile.py 

Where invoking the root conanfile.py would automatically build the required libs and copy their files as to have something like this:

├── conanfile.py
├── json
│   └── 3.10.4
│       └── conanfile.py
├── opencv
│   ├── 4.4.0
│   │   └── conanfile.py
│   └── 5.1.0
│       └── conanfile.py
└── win64_x86
    ├── include
    │   ├── nlohmann
    │   └── opencv2
    └── lib
        └── <multiple files>

and the CI/CD would archive the directory and make it available for download.

I have managed to build and package a very simple repo (https://github.com/jameskbride/cmake-hello-world) independently using the git tools and doing something like this:

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

    def source(self):
        git = tools.Git(folder="repo")
        git.clone("https://github.com/jameskbride/cmake-hello-world", "master")

    def build(self):
        cmake = CMake(self)
        cmake.configure(source_folder="repo")
        cmake.build()

However, I cannot figure out how to get the files that I want to package from my main conanfile.py. All of the approaches I have tried so far needed the package to be built separately (ie, going to the folder, using conan source . conan build . and conan export-pkg .. I am, 100% sure I am not doing this properly but I can't really find a way to do this. I found something about deploy() in the docs but can't figure out how to use it...

NB: I would really, really prefer not using a remote with prebuilt binaries for dependencies. I want to build everything from source with the CMake configurations I have defined. I also explicitely don't want a conan package in the end. I just want an aggregation of all the files needed to be shipped directly to the machines.


Solution

  • This question has been answered by Conan's co-founder james here: https://github.com/conan-io/conan/issues/9874

    Basically:

    • Use the CMakeDeps generator instead of cmake / cmake_find_package
    • Patch the resulting cmake config files at install time inside the generate() method from the main conanfile.py
    • Edit recipes accordingly (but should be fine most of the time)