find_package OPTIONAL_COMPONENT

That doesn’t mean you can pass invalid components. The ambiguous part here is that you don’t know if a component is valid until you know what Qt version it will find. One approach would be to use multiple calls, taking advantage of the fact that the first one that succeeds will effectively turn later ones into no-ops, as long as constraints are still satisfied. Conceptually, something like the following is what I’m thinking:

# NOTE: First call has no REQUIRED keyword but specifies a VERSION
find_package(Qt6
    VERSION 6.5    # Set this to the version the QHttpServer module was added to Qt
    COMPONENTS Concurrent Gui OpenGL Svg Widgets Xml
    OPTIONAL_COMPONENTS Charts HttpServer WebSockets
)
find_package(Qt6
    REQUIRED
    COMPONENTS Concurrent Gui OpenGL Svg Widgets Xml
    OPTIONAL_COMPONENTS Charts WebSockets    # NOTE: No HttpServer here
)

If you want to support the find_package(Qt${QT_VERSION_MAJOR} ...) part, you’d need to tweak the above further. I’ve just assumed Qt6 for simplicity in this example.

1 Like