conan-io / docs

conan.io reStructuredText documentation
http://docs.conan.io
MIT License
110 stars 361 forks source link

Configure dependencies page #3907

Open memsharded opened 6 days ago

memsharded commented 6 days ago

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

Feedback by @mpusz

mpusz commented 6 days ago

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: ```bash conan inspect -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: ```ini [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: ```python 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: ```bash 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: ```ini [options] zlib:shared=True ``` Use the profile with: ```bash 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: ```ini [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:

vulder commented 6 days ago

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.