Modelling directory structures correctly

28 views
Skip to first unread message

nullplan01

unread,
Dec 22, 2025, 12:09:37 PM12/22/25
to ninja-build
Evening all,

I have created a small libc implementation, and to build it, I am using a Python script to generate a build.ninja file. The full project can be seen here: https://github.com/nullplan/sirius

For the most part, I am satisfied with the result, but using the clean tool causes error messages. The project is written in C, and I want to support out-of-tree builds, so I have created a build rule "md" which just creates a directory, then listed all directories as being built with the md rule with no inputs, and finally all object files get an order-only dependency on their output directory.

This reliably creates the directories before running the compilers, but the clean tool tries to remove the directories before removing the object files, which doesn't work and fails with ENOTEMPTY. If I run the clean tool multiple times, eventually it gets rid of all directories, and of course I can just remove the build directory, or if building in the git repo, I can clean it with git.

Still, I wonder if I can change something about the generated build file to make the clean tool work. Specifically, it would have to know that it cannot delete a directory while there are still files in there, so not before deleting the dependents, and not before the subdirectories (which right now are not modeled in the build.ninja at all).

Ciao,
Markus

Ben Boeckel

unread,
Dec 23, 2025, 9:26:30 PM12/23/25
to nullplan01, ninja-build
On Mon, Dec 22, 2025 at 09:08:30 -0800, nullplan01 wrote:
> For the most part, I am satisfied with the result, but using the clean tool
> causes error messages. The project is written in C, and I want to support
> out-of-tree builds, so I have created a build rule "md" which just creates
> a directory, then listed all directories as being built with the md rule
> with no inputs, and finally all object files get an order-only dependency
> on their output directory.

This is unnecessary with `ninja`. Output directories for any output will
be created before a rule is executed.

https://github.com/ninja-build/ninja/blob/c74beb43695ff8cae955f05a6cbe1753aa9517f8/src/build.cc#L838

--Ben

nullplan01

unread,
Dec 24, 2025, 5:00:56 AM12/24/25
to ninja-build
Hi,

thanks for the info. Indeed, I tried it and the directories are being created. However, the clean tool does not remove them. Oh well, at least I am no longer getting error messages in cleaning.

Happy holidays,
Markus

Dirk Pranke

unread,
Dec 27, 2025, 1:50:31 PM12/27/25
to nullplan01, ninja-build
I don't think there's a good way to implement what you're trying to do. 

AFAIK, ninja has no concept of tracking the dependencies between targets in the way that would be needed to delete the directories only after the files in them had been deleted. Perhaps more importantly, ninja also doesn't track whether the directories were created by it or whether they already existed (you might not want to delete them in the latter case), and it's maybe kinda unclear whether you should try to delete the directories only if there are no other stray files (i.e., files ninja didn't create) in them. Changing clean to try to do those things might be a fairly incompatible change as a result, unless we added a flag to the ninja file to tell it which semantics to use.

Alternatively, one could perhaps add another command to ninja to do a "fuller" clean that perhaps explicitly deleted everything except for the build files (something equivalent to `gn clean`, for example), but that same logic could easily be done by a different tool (like GN).

So, I'm not sure there are compelling reasons to add either of these features to ninja.

-- Dirk

-- Dirk

--
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 visit https://groups.google.com/d/msgid/ninja-build/cf509ce2-0825-4efc-bdbd-51b5998f727en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages