Skip to content

Ninja: Always compile sources using absolute paths

Brad King requested to merge brad.king/cmake:ninja-absolute-paths into master

The Ninja generator traditionally referenced source files and include directories using paths relative to the build directory if they could be expressed without a ../ sequence that leaves the build and source directories. For example, when using a build/ directory inside the source tree, sources would be compiled as -c ../src.c and include directories would be referenced as -I ../include. This approach matches the traditional Ninja convention of using relative paths whenever possible, but has undesirable side effects such as:

  • Compiler diagnostic messages may not use absolute paths, making it harder for IDEs/editors to find the referenced sources or headers.

  • Debug symbols may not use absolute paths, making it harder for debuggers to find the referenced sources or headers.

  • Different results depending on the path to the build tree relative to the source tree.

  • Inconsistent with the Makefile generators, which use absolute paths.

Switch to always using absolute paths to reference source files and include directories on compiler command lines. While alternative solutions for diagnostic messages and debug symbols may exist with specific tooling, this is the simplest and most consistent approach.

Note that a previous attempt to do this in commit 955c2a63 was reverted by !520 (merged) due to problems hooking up depfile dependencies on generated files. This time, the changes in !6143 (merged) should avoid those problems.

Fixes: #13894 (closed), #17450 (closed)

Edited by Brad King

Merge request reports