docs icon indicating copy to clipboard operation
docs copied to clipboard

Configure dependencies page

Open memsharded opened this issue 1 year ago • 2 comments

It is a bit difficult to see best practices around using and defining options.

  • accesing info via self.dependencies not self.options["dependency"]
  • Pattern usages
  • Best in profiles

Feedback by @mpusz

memsharded avatar Nov 20 '24 08:11 memsharded

Thanks! My customers are trying to achieve this in different ways, and they often suffer.

The approach people often use is to force something in configure(). Also, they often use the old self.options["mp-units"].cxx_modules = True or mp-units:cxx_modules=True as, despite them being deprecated, they can still easily be Googled in some blogs or StackOverlfow. If you will ask ChatGPT it will provide plenty of invalid answers:

ChatGPT: How to set options for dependencies is Conan?

In Conan, you can set options for dependencies to customize their behavior or features when building your project. Here’s a step-by-step guide on how to configure options for dependencies in Conan:


1. Understand Conan Options

Conan packages often define options to toggle features (e.g., enabling shared libraries or specific modules). You can view available options for a package using the command:

conan inspect <package_name> -a options

For example, for zlib, you might see options like shared, fPIC, etc.


2. Set Options in conanfile.txt

If you're using a conanfile.txt file, specify the options under the [options] section:

[requires]
zlib/1.2.11

[options]
zlib:shared=True

Here:

  • zlib is the package name.
  • shared=True sets the option for the zlib package to build as a shared library.

3. Set Options in conanfile.py

If you're using a conanfile.py, options can be set programmatically in the build_requirements or requirements methods:

from conan import ConanFile

class MyProject(ConanFile):
    requires = "zlib/1.2.11"

    def configure(self):
        self.options["zlib"].shared = True

4. Set Options on the Command Line

You can override options when installing dependencies by using the -o flag:

conan install . -o zlib:shared=True

This is useful for testing or quick builds.


5. Set Global Options in a Profile

You can set options in a Conan profile for consistent configuration across multiple projects:

  • Create or edit a profile (e.g., ~/.conan/profiles/default or custom profiles).
  • Add the [options] section:
[options]
zlib:shared=True

Use the profile with:

conan install . --profile=default

6. Multi-Level Option Management

Options in Conan can be overridden at different levels:

  1. Command-line overrides everything.
  2. Conanfile options (conanfile.txt or conanfile.py) take precedence over profiles.
  3. Profiles provide global configuration defaults.

7. Example: Combine Multiple Options

For multiple dependencies with options:

[requires]
zlib/1.2.11
openssl/1.1.1s

[options]
zlib:shared=True
openssl:shared=False

This configuration ensures zlib is built as a shared library, while openssl remains static.


By understanding these mechanisms, you can fine-tune Conan's dependency management for your project's needs.

This is why a good page that will be easy to find is really needed here. It should discuss and compare various options of doing it:

  • default_options
  • configure
  • requires
  • setting of the same option in more than one recipe at a time
  • profiles
  • command line and possibly some more.

mpusz avatar Nov 20 '24 09:11 mpusz

I'll can second what @mpusz said. In addition, I would suggest to add a few lines on pros/cons of certain approaches (e.g., I was not aware that the configure way prevents users from overriding the values, which per say I don't see as nothing strictly bad/good, as it depends on what one wants to achieve. However, it should be easy for an author to judge what pattern is needed/wanted.). Furthermore, maybe a note on the ordering/precedence different ways have would also be helpful.

vulder avatar Nov 20 '24 09:11 vulder