Ninja: Feature request: more flexible rule name matching in the compdb tool

Created on 20 Sep 2015  ·  12Comments  ·  Source: ninja-build/ninja

Currently, to use the compdb tool you need to know the exact name of the rules which you want to query. This works OK provided these names are predictable, but this not necessarily the case for some generators.

Most notably, in version 3.2 (I think) CMake's ninja generator has started generating rules of the form <lang>_COMPILER__<directory>. This means that in order to query the compilation database, you need to first scan the project directory tree, or resort to other hacks. CMake prior to 3.2 generates rules of the form <lang>_COMPILER which made them possible to query from generic tools - without specific knowledge of the project.

Hence this feature request is for more flexible matching of rule names against command line arguments. A minimally-useful implementation would allow prefix-only matching. Alternatively regex matching would also be useful.

Most helpful comment

I find the compdb tool pretty hard to use -- to me, it's surprising that it needs a list of rules (it took me a while until I figured that out). Maybe if no explicit rules are explicitly passed that could just mean "all rules"? Would that be enough for cmake-based builds?

(Unrelated: I think you can pass CMAKE_EXPORT_COMPILE_COMMANDS to cmake to have it write a compilation database directly.)

All 12 comments

In the past there was a "rules" tool (7b67e48a1479c6fc7326c55756088bd57d64fc82) that could print the list of all rule. With that tool we could have done something like that:

ninja -t compdb `ninja -t rules | grep 'CXX_COMPILER_'`

I think it would be a good idea to resurrect this tool since it could also help to write shell completion script. I will try to do that in the next few days.

That would also work, thanks.

I find the compdb tool pretty hard to use -- to me, it's surprising that it needs a list of rules (it took me a while until I figured that out). Maybe if no explicit rules are explicitly passed that could just mean "all rules"? Would that be enough for cmake-based builds?

(Unrelated: I think you can pass CMAKE_EXPORT_COMPILE_COMMANDS to cmake to have it write a compilation database directly.)

Is there anyone here? 🙂

the rules tool seems removed from the build?

This would be really helpful.

I like @nico's suggestion to just print all the rules by default. In future something more sophisticated (like wildcard matching) could be added if needed.

I note @sarcasm's suggested workarounds here are rather ugly, motivating a more beautiful solution in ninja itself :)

There's now #1377 for dumping all rules by default.

Wanted to add a small note here, it seems that for some CMake projects [0], ninja -t compdb (no args) will not dump anything (the output is an empty list []).

cmake -G Ninja .
ninja -t compdb
[
]

However, using CMAKE_EXPORT_COMPILE_COMMANDS as nico suggested will:

cmake -G Ninja . -DCMAKE_EXPORT_COMPILE_COMMANDS=1
ninja -t compdb
[
]

ninja -t compdb still returns empty, but compile_commands.json is already generated.

Thanks nico!

[0] for Binaryen at least

What does ninja --version say?

1.8.2, btw this ninja is coming from depot_tools

That version is missing #1377.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Qix- picture Qix-  ·  8Comments

gjasny picture gjasny  ·  10Comments

MRYangY picture MRYangY  ·  23Comments

mdempsky picture mdempsky  ·  9Comments

nico picture nico  ·  20Comments