Leon Wennekers
Leon Wennekers

Reputation: 35

Having a different version of a dependency based on a conan profile

We have several projects for an embedded Linux environments. We are switching to Conan for our dependency management. The shared libraries for these projects are turned into conan packages. These packages are build for multiple platforms using the same code base. For these projects we use googletest as our unit testing framework.

For these embedded environments we are tied to the cross compilers delivered to us by our supplier. We are tied to gcc 4.4.3 and 5.2.0 to cross compile our code resulting in different behaviour building packages. As we are using googletest I cross compile a version of googletest and add it to my artifactory so dependencies can be resolved. I just made a simple conanfile.py for googletest to build it calling the normal cmake, but unfortunately I can't build the 1.8.1 and 1.10.0 version using the gcc 4.4.3 cross compiler.

Possible solutions:

  1. Keep using 1.8.0 version of googletest for every project. Possible, but with the disadvantage not being able to use new googletest features.
  2. No unit tests when using the gcc 4.4.2 profile. Possible, but due to the fact we defined a test_package it wants to build unit tests. We can work around is to specify a non existing test folder when creating the package. But that means a different command to for creating a package.
  3. Use version 1.8.0 when building for the gcc 4.4.3 profile and use a newer version for other profiles. Possible?

Although building for different platforms I have on code base and one conanfile.py to define the package. Using the --profile options I cross compile for different architectures. I would like to keep it this way.

I tried specifying the version of the googletest package as [>=1.8.0] in the hope that for the gcc 4.4.3 profile it picks up the 1.8.0 version as that is the only version with binaries for that architecture. Unfortunately it finds the newer versions and throws an error that it is missing the prebuilt package for the newer version.

How can I make one profile use version 1.8.0 of a dependency an make the other profile use version 1.10.0 for the dependency?

One thing I want to prevent is to do checks based on profile settings in my conanfile.py, as that would create a dependency between the files. This may cause me to change the conanfile.py when adding or removing profiles in the future.

Upvotes: 3

Views: 4154

Answers (2)

drodri
drodri

Reputation: 5972

You can directly declare build_requires in profiles, something like:

[settings]
compiler=gcc
compiler.version=4.4.3

[build_requires]
gtest/1.8.0

And

[settings]
compiler=gcc
compiler.version=5.2.0

[build_requires]
gtest/1.10.0

These build requires are applied to all packages that need to be built, but you can also define which packages do they apply to. You can check in the recipes whether this build-requires is used, and if your profile doesn't declare gtest as build-requires, then skip testing.

Check https://docs.conan.io/en/latest/reference/profiles.html

Upvotes: 1

uilianries
uilianries

Reputation: 3887

Use version 1.8.0 when building for the gcc 4.4.3 profile and use a newer version for other profiles. Possible?

Yes, totally possible.

But first you need to understand that test_package is not for your project unit test. Tests packages are created to validate your package only. If you want to test your project using Conan, it should be done during the build step:


class LibConan(ConanFile):
    ...

    def build_requirements(self):
        if self.settings.compiler == "gcc" and tools.Version(self.settings.compiler.version) < "5":
            self.build_requires("gtest/1.8.1")
        elif self.settings.compiler == "gcc":
            self.build_requires("gtest/1.10.1")

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()
        if self.develop:
            cmake.test()

You can obtain more information about develop and unit test in Conan docs.

Now about this example, build_requirements installs the GTest package according your compiler version and its name, in this case, GCC < 5. Besides that, self.develop is only available when building the package (conan create command), thus the unit tests only runs when creating, not when installing.

Upvotes: 1

Related Questions