# Query available version(s) of package before find\_package()

**URL:** <https://discourse.cmake.org/t/query-available-version-s-of-package-before-find-package/14310>\
**Category:** Code\
**Created:** [July 2, 2025, 8:13pm UTC](https://discourse.cmake.org/t/query-available-version-s-of-package-before-find-package/14310 "2025-07-02T20:13:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dgobbi/32/5647_2.png) [@dgobbi](https://discourse.cmake.org/u/dgobbi)\
**Post date:** [July 2, 2025, 8:13pm UTC](https://discourse.cmake.org/t/query-available-version-s-of-package-before-find-package/14310/1 "2025-07-02T20:13:14Z")

</div>

Let’s say that I want to do something like this:

```plaintext
find_package(pkg REQUIRED COMPONENTS x y z)

```

However, the components that I need depend on what version of `pkg` I get, because (for example) the maintainers have shuffled things around between versions, or perhaps they only recently split their package into multiple components.

I don’t want to force a specific version of `pkg` in my `find_package()` call, because my users expect to be able to use my software in a wide range of environments.

How can I find out what version(s) of `pkg` are available before I do the `find_package()` call? Should my CMakeLists.txt run cmake as a COMMAND to do a test configuration that runs `find_package(pkg QUIET)`, and then use the discovered version information to call the main `find_package(pkg COMPONENTS ...)` with the version-specific list of components?

---

<div class="post-metadata">

**Author:** ![nickvdbroeck](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/85f322/32.png) [@nickvdbroeck](https://discourse.cmake.org/u/nickvdbroeck)\
**Post date:** [July 16, 2025, 9:33am UTC](https://discourse.cmake.org/t/query-available-version-s-of-package-before-find-package/14310/2 "2025-07-16T09:33:53Z")

</div>

As a suggestion, would it not make sense to simply use fall through logic?

You first write find\_package with the most likely, or most desired version (since cmake 3.19 you can use ranges, otherwise you need to do it newest version first).  
If the find\_package fails, then you provide the next find\_package with the support for the older component. And so on.  
If your code needs to know then you can define compiler flags inside those if statements as well.

Of course, you can’t put the REQUIRED in the first package calls. But ‘worst case’ you can put a message(FATAL\_ERROR “”) at the end if none of the find\_packages found something.

The biggest downside I see is that it will be slower for the variations further down list.
