Ninja equivalent of Make's "build from this directory down" feature (with CMake)?

cmake, ninja

Solution

`ninja <DIR>/all` works with recent versions of Ninja (1.7.2). Version 1.3.4 does not allow this.

I could not find a reference to this on the manual. However, CMake has this documented here:

Recent versions of the ninja program can build the project through the “all” target. An “install” target is also provided.

For each subdirectory sub/dir of the project, additional targets are generated:

- sub/dir/all Depends on all targets required by the subdirectory.

- sub/dir/install Runs the install step in the subdirectory, if any.

- sub/dir/test Runs the test step in the subdirectory, if any.

- sub/dir/package Runs the package step in the subdirectory, if any.

Problem

When building a project using CMake and Make, you can execute `make` from a subdirectory of your build tree (i.e. from a directory below whatever directory contains your top-level `Makefile`), and `make` will (as far as I can tell) build all targets at or below that directory. This is because CMake generates a `Makefile` for every directory that contains targets, so when you're in a directory with targets, `make` finds the `Makefile` for building those targets. When CMake generates Ninja files, however, it only generates one `build.ninja` file, which is at the top level of the build tree. So calling `ninja` from a directory other than the top-level directory fails (even the `-f` option doesn't work because `ninja` can't find the `rules.ninja` file). Is there any way to emulate the "make-like" behavior of building targets at and below a directory? As far as I can tell, there are no Ninja targets that correspond to "all targets at and below a particular directory." (This could be emulated using phony targets named after each directory that depend on all targets at and below that directory, but CMake does not generate such targets by default.)

Original source