conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
7.95k stars 951 forks source link

[question] How to access `self.options` of a parent library package from its test package? #16491

Closed PauloCarvalhoRJ closed 1 week ago

PauloCarvalhoRJ commented 2 weeks ago

Greetings,

So, I have a test package (test_package sub-directory in the source repository) inside a package named zlib/1.2.11. In zlib's own recipe, I have:

    options = {"shared": [True, False],
               "fPIC": [True, False],
               "minizip": [True, False],
               "os_version": ["linux", "windows", "other"],
               }

I need to access zlib's self.options.minizip value in test package's recipe:

cmake.definitions["WITH_MINIZIP"] = self.options["zlib"].minizip

The code above works with Conan 1, but in Conan 2 I get this error:

ERROR: zlib/1.2.11 (test package): Error in build() method, line 20
        cmake.definitions["WITH_MINIZIP"] = self.options["zlib"].minizip
        ConanException: option 'minizip' doesn't exist
Possible options are []

Then the question is: How can one now (Conan 2) access options values of library packages in their respective test packages?

Thanks in advance,

PC

Have you read the CONTRIBUTING guide?

AbrilRBS commented 2 weeks ago

Hi @PauloCarvalhoRJ thanks a lot for your question.

In Conan 2 (And new enough Conan 1 versions!) you'll be looking for the self.dependencies (docs here) accessor

With this, you can query your tested package options like so in a test_package: self.dependencies[self.tested_reference_str].options.minizip (Or just replace the self.tested_reference_str for the library name for any other dependency if needed in general recipes)

I hope this helps :)

PauloCarvalhoRJ commented 1 week ago

Hello,

Thank you. The new API is more readable in Conan 2. The error shifted to the cmake.dependencies side, but I'll start another question for the issue.

best,

PC