Search code examples
c++dockerconan

Is it possible to use docker-containers as build-requires in conan?


we use conan to build our libraries and applications as a package/dependency management tool.

We want to build our projects with two different toolchains. As an example:

  • visual_studio toolchain
  • gcc but my question is independent of that specific compiler.

According to the documentation I've a few options to do so with conan. https://docs.conan.io/en/1.35/systems_cross_building/cross_building.html#cross-building-with-conan

  1. install the toolchains local and create conan profiles referring to the toolchain.
  2. define "build requires", which will automatically download the tool-chain and conan will use it, similar to the profile mechanism.

Is it possible to pack the "build requires" tools into a docker container and get conan somehow to invoke the build command through the docker?

The reason I want to do so:

  • visual studio toolchain can be run in a windows-container
  • gcc toolchain can be run in linux container
  • it is possible to encapsulate the complete toolchain

The typical usage of docker+conan looks a little bit different: https://docs.conan.io/1/howtos/run_conan_in_docker.html#docker-conan In that case, conan runs inside of the container.

Why I want to manage the toolchain-docker-images through build-requires recipes? We have different versions of toolchains (images). It would be nice if we can manage tool-chain versions with build-requires recipes.

I want to understand, why it's the recommended way to install conan inside of docker combined with the toolchain. Are there technical limitations / performance limitations if we implement it as described above?

Thanks :). Toni


Solution

  • This is not possible as built-in. tool_requires (build_requires) can only provide information for the consumer recipes, they can generate environment files, cmake files, etc.

    But it might be possible to do it extending the Conan capabilities. You could create Conan packages that wrap the information for your docker images, mostly the image name, plus maybe some configuration, and you could inject those images via [tool_requires] in your profiles, for example.

    Then, there could be different methods to leverage that information. For example, if recipes inherit from a python_requires that implement a def run() method that overrides the Conanfile.run() default one, you can customize there the injection of docker .... commands that would be pre-pended.

    Or you could use the new 2.0 command wrapper, and via the conanfile.dependencies.build access the tool_requires information, so they can inject the necessary docker ... modifications to the commands.

    It should be noted however that when dependencies are installed, they will be installed in the host system, not in the docker container, so it would be necessary that each docker invocation correctly mounts the host Conan cache and configures it to use it.

    Overall, it sounds possible, but probably quite complicated, and it could have unexpected rough edges. Using Conan inside the containers works very well and it is quite simple, it is used a lot in ConanCenter, and many Conan users do that too.