Hi Folks,
The build manifest (build.ninja) is encouraged to use relative paths,
especially for files that appear beneath the top of the build tree.
Therefore running `ninja` in `<dir>` or `ninja -C <dir>` can only work
with a given build manifest if `<dir>` matches what the build manifest
expects. Using `ninja -f /some/other/build.ninja` does not work unless
the current working directory happens to match what that manifest expects.
I propose that we add a new top-level binding that generators can use
to specify the expected working directory. This can also be interpreted
as the starting point for relative paths in the build manifest. For
example:
```
ninja_workdir = /absolute/path/to/expected/working/directory
```
Ninja could chdir to this location after reading the build manifest(s),
thus ensuring that all the relative paths work as intended.
---------------------------------------------------------------------------
Furthermore, the above feature will help fix a problem with depfile
loading. The ninja manual currently has a warning for depfile content:
```
File paths are compared as is, which means that an absolute path and a
relative path, pointing to the same file, are considered different by Ninja.
```
See issue 1251 [1] for some problems this causes in practice. We can
fix it by using `ninja_workdir`. If a path in the depfile does not
exist in the build manifest then we can check for alternatives matches:
* If the depfile path is absolute and starts with the workdir then we
can strip off the workdir prefix and then check if the relative
path exists instead.
* If the depfile path is relative we can prepend the workdir and then
check if the absolute path exists instead (after CanonicalizePath).
Note that we cannot use `getcwd` for this because it would not
preserve symbolic links that the generator may have used in absolute
paths written to the build manifest. The `ninja_workdir` binding
allows the generator to tell ninja exactly what logical path it used.
I have changes locally using this approach to resolve issue 1251, but
I'd like to get some feedback here before polishing it up for a PR.
Thanks,
-Brad
[1]
https://github.com/ninja-build/ninja/issues/1251