Modelling directory structures correctly

11 views
Skip to first unread message

nullplan01

unread,
Dec 22, 2025, 12:09:37 PM (4 days ago) Dec 22
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 PM (2 days ago) Dec 23
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 AM (2 days ago) Dec 24
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
Reply all
Reply to author
Forward
0 new messages