jq 在 compile_commands.json "file"字段中插入 .o 而不是 .c 文件



我设法创建了一个CommandsFile.txt,如下所示:

/cygwin/c/Dev/bin/tricore-gcc.exe   -isystem     all_inc     -W     -Wall  (plus a bunch of other flags)  -c     software/ProjectA/src/file1.c     -o     _objs/ccLib/file1.o
/cygwin/c/Dev/bin/tricore-gcc.exe   -isystem     all_inc     -W     -Wall  (plus a bunch of other flags)  -c     software/ProjectA/src/file2.c     -o     _objs/ccLib/file2.o
...

并且我利用CCD_ 2在下面的脚本中生成CCD_

#!/bin/bash
cat CommandsFile.txt 
| grep -wE 'gcc|g++' 
| grep -w '-c' 
| ./jq-win64 -nR '[inputs|{directory:".", command:., file: match(" [^ ]+$").string[1:]}]' 
> compile_commands.json 

它的工作方式很有魅力,但脚本用.o而不是.c文件填充字段"file"

...
{
"directory": ".",
"command": "  /cygwin/c/Dev/bin/tricore-gcc.exe   -isystem     all_inc     -W     -Wall  (plus a bunch of other flags)  -c     software/ProjectA/src/file1.c     -o     _objs/ccLib/file1.o,
"file": "_objs/ccLib/file1.o"
},
{
"directory": ".",
"command": "  /cygwin/c/Dev/bin/tricore-gcc.exe   -isystem     all_inc     -W     -Wall  (plus a bunch of other flags)  -c     software/ProjectA/src/file2.c     -o     _objs/ccLib/file2.o,
"file": "_objs/ccLib/file2.o"  },
...

我应该如何修改脚本以使字段"file"中有.c文件而不是.o文件?

这里的问题的关键在于用于捕获.c文件的regex,所以我将重点讨论它。

如果知道最多有一个.c文件,并且它的名称合理(例如,没有嵌入空间(,那么";捕获";文件名如下:

capture("(^| )(?<c>[^ ]+[.]c)( |$)")

然而,如果可能有多个.c文件,那么您可以迭代地使用以下";捕获";表达式:

capture("(^| )(?<c>[^ ]+[.]c)( |$)(?<etc>.*)")

示例:

echo 'a.c b.c' |
jq -R 'capture("(^| )(?<c>[^ ]+[.]c)( |$)(?<etc>.*)")'

生产:

{
"c": "a.c",
"etc": "b.c"
}

多个.c文件

echo "a.c b.c" |
jq -R '
# emit a stream of filenames ending in .$suffix
def filenames($suffix):
def one:
capture("(^| )(?<c>[^ ]+[.]"+$suffix+")( |$) *(?<etc>.*)");
def names:
one | .c, (.etc | names);
names;
filenames("c")'

生产:

"a.c"
"b.c"

最新更新