conan-io / docs

conan.io reStructuredText documentation
http://docs.conan.io
MIT License
104 stars 346 forks source link

[question] How to set options with hyphens in conanfile.py? #3768

Open gouriano opened 3 weeks ago

gouriano commented 3 weeks ago

What is your question?

Hi I am trying to customize aws-sdk-cpp package in my conanfile.py. The problem is that it uses hyphens in option names See the recipe

My conanfile looks as follows:

from conan import ConanFile
class ConanApp(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    generators = "CMakeDeps"
#    default_options = {"aws-sdk-cpp*:text-to-speech": False}
    def requirements(self):
        self.requires("aws-sdk-cpp/1.9.234")
    def configure(self):
        self.options["aws-sdk-cpp/*"].text_to_speech = False

If I use "self.options["aws-sdk-cpp/"].text-to-speech = False", I get wrong python If I use "self.options["aws-sdk-cpp/"].text_to_speech = False", Conan2 does not complain, but the setting does not seem to have any effect (I know this because my app still requires pulseaudio package) If I use "default_options = {"aws-sdk-cpp*:text-to-speech": False}", everything seems to work fine.

My question: is it possible to specify an option with hyphen in configure() method?

Have you read the CONTRIBUTING guide?

memsharded commented 3 weeks ago

Hi @gouriano

Can you please clarify the Conan version you are using?

Also, please note that setting options in recipes is not the most recommended approach, but setting them in profile files, and having recipes to check them in validate() method, raising ConanInvalidConfiguration if something is strictly necessary.

gouriano commented 3 weeks ago

$ conan version version: 2.3.2 conan_path: /home/gouriano/venv_conan2/bin/conan python version: 3.9.19 sys_version: 3.9.19 (main, Apr 4 2024, 12:39:35) [GCC 8.5.0 20210514 (Red Hat 8.5.0-20)] sys_executable: /home/gouriano/venv_conan2/bin/python is_frozen: False architecture: x86_64 system version: conan-io/conan#1 SMP Mon Apr 8 11:23:13 EDT 2024 platform: Linux-4.18.0-513.24.1.el8_9.x86_64-x86_64-with-glibc2.28 system: Linux release: 4.18.0-513.24.1.el8_9.x86_64 cpu: x86_64

memsharded commented 3 weeks ago

I think this is mostly an overlook of the recipe, using hyphens that if is is true that it is not strictly forbidden, it is inconvenient for this use case.

I'd say that you can do something like setattr(self.options["aws-sdk-cpp"], "text-to-speech", False), please try that and let us know.

gouriano commented 3 weeks ago

It works! Thank you! Maybe, this should be documented.

memsharded commented 3 weeks ago

Maybe, this should be documented.

Thanks for the feedback. The truth is that this sounds a bit as a workaround, but it is also true that there is a loophole there, as hyphens are not forbidden. But forbidding them now wouldn't be great, as that would mean breaking recipes in ConanCenter, or at the very least having some painful upgrades that would be also painful for users of those recipes.

As the general recommendation is to define options values in profiles, not in recipes, I think it might be better to leave it as-is. Please read https://docs.conan.io/2/knowledge/faq.html#defining-options-for-dependencies-in-conanfile-py-recipes-doesn-t-work, and try to define your options in profiles as much as possible (and let recipes validate() things)

I'll move it to the docs repo to try to add a note there about this.