What's the best way to configure an out-of-tree build with Ninja?

56 views
Skip to first unread message

Mattia Maldini

unread,
Apr 15, 2024, 9:56:44 PMApr 15
to ninja-build
I'm fiddling with ninja as a backend for a build file generator, and I want to give the option to build out of the source tree in a custom directory. What would be the best way to achieve this?
For example, how would one replicate the directory tree of the sources in the build directory? My idea at the moment is to add a "mkdir" rule to create directories and then for each source file add the base directory as an implicit dependencies. Is there a more idiomatic way to do so, or is it one of the responsibilities left out for higher level layers?

David Turner

unread,
Apr 16, 2024, 8:23:42 AMApr 16
to ninja-build
It is up to the generator to decide how to organize the file paths that appear in the Ninja build plan. For example with GN, the tool will generate relative paths from the build directory to the source tree, i.e. if you do something like `gn gen out/default`, then the file at `out/default/build.ninja` will contain references to source files such as `../../src/foo/foo.cc` directly.

Another example is CMake, which will use absolute paths for source files instead (i.e. /abs/path/to/src/foo/foo.cc) in the build plan, at least in the few examples I looked at.

None of these tool replicate the source tree inside the build directory. This is in general a bad idea (since now you need to ensure, on  each invocation, that your replication is in sync with whatever changed in the source tree).

Hope this helps,

- Digit
Message has been deleted

David Turner

unread,
Apr 16, 2024, 10:10:32 AMApr 16
to Mattia Maldini, ninja-build
Sorry, I deleted Mattia's answer from the mailing list by mistake (I was trying to delete a draft I was writing on the web site).
Here's it is again for archival / searchability purposes:

------------------------------------------------------------------

Mattia Maldini New message

3:06 PM (1 hour ago)
Reply to all
to ninja-build
To clarify, I don't mean replicating the complete source tree (by copying (or linking?) source files), just the folders.

More specifically, my question is about how object files end up organized in the build directory. I think CMake replicates the sources' folder structure.
I fiddled a little with gn too and noticed that it seems to replicate the source's path in the object's name, which is an interesting approach and I think I'm going to follow it.

Thank you!


On Tue, Apr 16, 2024 at 3:06 PM Mattia Maldini <mattia51...@gmail.com> wrote:
To clarify, I don't mean replicating the complete source tree (by copying (or linking?) source files), just the folders.

More specifically, my question is about how object files end up organized in the build directory. I think CMake replicates the sources' folder structure.
I fiddled a little with gn too and noticed that it seems to replicate the source's path in the object's name, which is an interesting approach and I think I'm going to follow it.

Thank you!
--
You received this message because you are subscribed to the Google Groups "ninja-build" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ninja-build...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ninja-build/250bee04-0b3f-4fe0-93cd-6365bcb53ec1n%40googlegroups.com.

Evan Martin

unread,
Apr 16, 2024, 11:36:23 AMApr 16
to David Turner, Mattia Maldini, ninja-build
For example, how would one replicate the directory tree of the sources in the build directory? My idea at the moment is to add a "mkdir" rule to create directories and then for each source file add the base directory as an implicit dependencies. Is there a more idiomatic way to do so, or is it one of the responsibilities left out for higher level layers?

A small note on this idea, from the manual: "Output directories are always implicitly created before running the command that relies on them."
This means you likely do not need to worry about creating directories, just construct the desired output paths and let Ninja handle it.

Ben Boeckel

unread,
Apr 17, 2024, 11:52:24 AMApr 17
to David Turner, ninja-build
On Tue, Apr 16, 2024 at 05:23:42 -0700, 'David Turner' via ninja-build wrote:
> Another example is CMake, which will use absolute paths for source files
> instead (i.e. /abs/path/to/src/foo/foo.cc) in the build plan, at least in
> the few examples I looked at.

This MR changed from relative to absolute paths:

https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6148

Explanation for why CMake switched is provided in the description there.

--Ben
Reply all
Reply to author
Forward
0 new messages