Adding implicit dependencies to all instances of a rule

349 views
Skip to first unread message

Iain Merrick

unread,
Apr 26, 2017, 2:18:43 PM4/26/17
to ninja-build
Hi ninja-build,

I have a possible feature request, unless there's some clever way to do what I want that I can't figure out... I'm looking for a way to add one or more implicit dependencies to every "build" line that references a particular "rule".

The motivation is build tools that are themselves built as part of the build process. For example let's say I have a "foo" tool that I compile as follows:

  rule cc
    command = cc $in -o $out

  build bin/foo : cc bin/foo.c

Now I want to use "foo" to process "bar.txt":

  rule foo
    command = bin/foo $in > $out

  build bar : foo bar.txt

But that doesn't work correctly -- bar also needs an implicit dependency on bin/foo to ensure that the tool is compiled first:

  build bar : foo bar.txt | bin/foo

If I'm using foo a lot, that gets tedious, because I need to add bin/foo to every "build" line. It's also error-prone, because if I forget to add that implicit dependency, the build *might* work but it will probably pick up stale dependencies.

It would be nice to declare the implicit dependency just once, in the rule itself:

  rule foo | bin/foo
    command = bin/foo $in > $out

Would it be worth adding that syntax to Ninja? Or is there already another way to do it?

I'm happy to try implementing this feature myself, but I figured I'd check here first whether it's necessary and/or useful.

Iain

Evan Martin

unread,
Apr 26, 2017, 2:20:51 PM4/26/17
to Iain Merrick, ninja-build
In general, the guideline for Ninja features is "if it can be done outside of Ninja without impacting performance", then it should.  In this case I think your build file generator ought to handle this.

(One reason to not implement features like this within Ninja is that Ninja can't know all the requirements.  If bin/foo is a shell script, for example, the rule might need more dependencies than just the single file.  Higher-level tooling can better model this requirement.)

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Iain Merrick

unread,
Apr 27, 2017, 12:30:09 AM4/27/17
to Evan Martin, ninja-build
That's fair enough, I'll look into doing it in a wrapper.

One reason this *might* still be worth considering  as a core feature is compression. If I have one rule but call it 1000 times, it's wasteful to repeat the same dependencies over and over. In practice if you're generating ninja.build files I imagine the size is not very important though.

Iain
Reply all
Reply to author
Forward
0 new messages