-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow 'ninja -t compdb' accept targets #2319
Conversation
3eb2357
to
96ebb17
Compare
0655263
to
cafc5eb
Compare
3a0cf3c
to
bd0ff62
Compare
@digit-google thanks for your comments, they're addressed. |
This looks good to me, but will require @jhasse 's approval. Can I ask you to add some regression test in misc/output_test.py for this feature though? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for having another go at this :)
Test: ./ninja -t compdb --target wrong_target (fail) ./ninja -t compdb --target test_target (empty due to lack of rules) ./ninja -t compdb --target ninja,ninja_test cc cxx (ok) ./ninja -t compdb --target test_target cc cxx (ok) ./ninja -t compdb cxx (ok, regression test) ./ninja ninja_test && ./ninja_test (passed) Co-authored-by: Linkun Chen <lkchen@google.com> Co-authored-by: csmoe <csmoe@msn.com>
fwiw, i really like this feature and look forward to this PR being merged! one obvious use case for C/C++ users is to produce the database consumed by clangd. It means i get more concise & relevant search hits when e.g. asking clangd for all references to some symbol it also means the CPU fans in my PC no longer go crazy as a consequence of clangd's attempt to background-index a large number of source files that i didn't even want to browse (because they don't participate in building the executable thank you, @csmoe! |
side note (for anyone who, like me, only learned about the jq command recently): in addition to compile commands, ninja -t compdb --target foo | \
jq '[.[] | select( .command | test("/bin/clang.* -c"))]' > compile_commands.json (or |
In some future PR, it might be nice if the compilation database format were extended to allow a field like { "directory": "/home/user/llvm/build",
"command": "clang-cl.exe [args...] /c /o t.o t.cc",
"command_type": "compile_CXX",
"file": "t.cc" }, ...which generator programs might communicate to Ninja in the form of a rule some_cxx_rule
command = clang-cl.exe [args...] /o $out /c $in
command_type = compile_CXX
description = Building CXX object $out ...so that our ninja -t compdb --target foo | \
jq '[.[] | select( .command_type | test("^compile_(CXX|C)$"))]' > compile_commands.json (But again, this would be a separate PR, and afaict the current PR is complete (or, it looks like it will be, after #2479 is merged).) |
Would it be better to shift out arguments so that no flag is required for the targets list? so instead of
you would have
or if a flag is really necessary to break out of flag-only parsing, use syntax like in
so they can be space separated instead of comma separated |
Prior to the commits in this PR, the output of
so |
i guess this would work:
if this is happening in a bash script, and if you've defined an array parameter where each element is the name of a target, then this alternative syntax might save users from learning how to join strings in bash: ninja -t compdb --target $(targets=( $(cat $WORKSPACE/active-executables.txt)); IFS=','; echo "${targets[*]}") ...though i guess it's not so bad if you use the paste command: ninja -t compdb --target $(paste -s -d , $WORKSPACE/active-executables.txt) Note, in the case where the generator program is cmake, you can get a list of all executable targets with a script like the following (zsh): prepare_build_environment # a shell function that sets $BUILD_DIR
cmake_root=${$(realpath $(which cmake)):h:h}
cd -q $BUILD_DIR
cmake . --trace-expand \
|& rg 'add_executable\(' \
| rg -v -e "^(${BUILD_DIR}|${cmake_root})" \
| sed -E -e 's/.*add_executable\([[:space:]]*([^[:space:]]+) .*/\1/' if you redirect the output of this script to a file
to select the set of executables that are relevant to your current task, and redirect that to |
so, boiling down the above, it seems like the only open question is whether we want:
...where TARGETS is a comma-separated list of names of targets; or:
i prefer option 1 (as already implemented), because option 2 requires me to remember that |
minor nit: if we go with option 1 ( |
in case we need more motivation to merge this PR: i just wrote a small extension for Telescope (which is pretty straightforward) that reads a compilation database and hands the list of all primary source files from that database over to Telescope. For example, if i cd to the build directory produced by cmake for building ninja itself and then run: ninja -t compdb --target ninja > compile_commands.json ...here's what i see when i invoke my Telescope extension: For anyone starting to read the Ninja source code for the first time, this seems like a good starting point. (And as soon as i hit By contrast, when i use an IDE to load project files generated by cmake, it is not at all obvious which source files are linked into the executable in which i'm currently interested. To me, this means that i'll have an easier time browsing a C/C++ program |
I like Feel free to modify anything or recreate one, but preserve lkchen's contribution record, he completed the main part. |
@csmoe sure! How should we proceed? (Does github's PR interface have something like a 'bequeath' button so that you can bequeath the PR to me, or should i open a new PR like you did, or...?) |
github seems not support that, so create your pr😃 |
Fix #1544
Cherry-picked #1546 and addressed the comments.
May I have your code review @jhasse ? Thanks