[cmake-developers] FindFoo.cmake and COMPONENTS

Ben Boeckel ben.boeckel at kitware.com
Wed Mar 26 19:01:11 EDT 2014


On Wed, Mar 26, 2014 at 22:38:51 +0000, Alex Merry wrote:
> Actually, yeah, it's mainly the dependency handling that's hairy.  There
> are couple of other things you have to be aware of; for instance, I've
> seen several find modules try to give all the components to
> pkg_check_modules() at once, but you can't do that (pkg_check_modules
> finds everything or nothing).

OK, good to know I'm not missing anything :) . My thought was to do the
following (in that linked module):

    set(${component}_deps ...)

    set(extra_deps)
    foreach (component IN LISTS Derelict_FIND_COMPONENTS)
        foreach (dep IN LISTS ${component}_deps)
            # if dep not in Derelict_FIND_COMPONENTS
                list(APPEND extra_deps "${component}")
        endforeach ()
    endforeach ()

and then to also search for the deps and finally do:

    set(valid_components)
    foreach (component IN LISTS Derelict_FIND_COMPONENTS)
        set(missing)
        foreach (dep IN LISTS ${component}_deps)
            # if dep not found
                list(APPEND missing "${component}")
        endforeach ()

        if (missing)
            # if REQUIRED
                # error out
        else ()
            # Add imported target.
            list(APPEND valid_components "${component}")
        endif ()
    endforeach ()

    foreach (component IN LISTS valid_components)
        # Add dependency links here.
    endforeach ()

This keeps the "find this component" logic free of any of the dependency
logic and falls back gracefully if components aren't found and such.

It is a little verbose though :( . If there were a nice way to do
callbacks[1] (to do the "find a component", "add the import target", and
"add the dependency link" bits), this would actually wrap up into a
module nicely.

--Ben

[1]I don't really like the "file(WRITE path); include(path)" route...



More information about the cmake-developers mailing list