[cmake-developers] [PATCH] Ninja: Optionally skip library dependencies for object targets

Ben Boeckel ben.boeckel at kitware.com
Fri Mar 25 13:59:21 EDT 2016


On Tue, Mar 15, 2016 at 10:06:12 -0400, Brad King wrote:
>   add_custom_command(OUTPUT foo.h ...)
>   add_library(foo foo.c foo.h)
>   add_library(bar bar.c)
>   target_link_libraries(bar foo)
> 
> The order-only dependency of `bar.c.o` on the target `foo` is just
> in case `bar.c` needs to include `foo.h`.  It is a conservative
> approach and does generate many unneeded dependencies in practice.

FWIW, I've looked into the logic which needs to be done here before and
as far as I can tell, it is (pseudocode):

    deps = direct_deps_of(bar)
    cc_deps = filter(is_custom_command, deps)

    obj_deps = []
    foreach dep in deps
        r_deps = all_deps_of(dep)
        r_cc_deps = filter(is_custom_command, r_deps)

        if r_cc_deps is not subset of cc_deps
            obj_deps.add(dep) # alternatively, just pass on the
                              # missing custom command dependencies
                              # (optionally with a warning).

    # objects of bar should depend on the obj_deps targets for any
    # potential implicit custom command dependencies.

> With a bit of analysis the Ninja generator could detect when `foo`
> has no custom commands and does not have a target-level dependency
> on other targets with custom commands.  Then dependencies on `foo`
> from compilations in `bar` could be omitted automatically.

This algorithm, I believe, can be applied to all generators, not just
Ninja.

--Ben


More information about the cmake-developers mailing list