(#7621) docs: how to validate dep version and options

* docs: how to validate dep version and options

* Apply suggestions from code review

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>
This commit is contained in:
Chris Mc
2021-10-12 01:02:16 -04:00
committed by GitHub
parent c1158f9dd8
commit 5bee8bb8e9

View File

@@ -202,6 +202,29 @@ pacman, brew, choco) and install packages which are missing on Conan Center but
As example there are [glu](https://github.com/conan-io/conan-center-index/blob/master/recipes/glu/all/conanfile.py) and [OpenGL](https://github.com/conan-io/conan-center-index/blob/master/recipes/opengl/all/conanfile.py). Also, it will require an exception rule for [conan-center hook](https://github.com/conan-io/hooks#conan-center), a [pull request](https://github.com/conan-io/hooks/pulls) should be open to allow it over the KB-H032.
### Verifying Dependency Version
Some project requirements need to respect a version constraint. This can be enforced in a recipe by accessing the [`deps_cpp_info`](https://docs.conan.io/en/latest/reference/conanfile/attributes.html#deps-cpp-info) attribute.
An exaple of this can be found in the [spdlog recipe](https://github.com/conan-io/conan-center-index/blob/9618f31c4d9b4da5d06f905befe9691cf105a1fc/recipes/spdlog/all/conanfile.py#L92-L94).
```py
if tools.Version(self.deps_cpp_info["liba"].version) < "7":
raise ConanInvalidConfiguration(f"The project {self.name}/{self.version} requires liba > 7.x")
```
In Conan version 1.x this needs to be done in the `build` method, in future release is should be done in the `validate` method.
### Verifying Dependency Options
Certain projects are dependant on the configuration (a.k.a options) of a dependency. This can be enforced in a recipe by accessing the [`options`](https://docs.conan.io/en/latest/reference/conanfile/attributes.html#options) attribute.
An example of this can be found in the [kealib recipe](https://github.com/conan-io/conan-center-index/blob/9618f31c4d9b4da5d06f905befe9691cf105a1fc/recipes/kealib/all/conanfile.py#L44-L46).
```py
def validate(self):
if not self.options["liba"].enable_feature:
raise ConanInvalidConfiguration(f"The project {self.name}/{self.version} requires liba.enable_feature=True.")
```
## Test the recipe locally
The system will use the [conan-center hook](https://github.com/conan-io/hooks) to perform some quality checks. You can install the hook running: