Skip to content
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

Escape whitespace and special characters in generated compile_commands.json #1726

Closed
xq114 opened this issue Oct 5, 2021 · 7 comments
Closed
Milestone

Comments

@xq114
Copy link
Contributor

xq114 commented Oct 5, 2021

描述讨论详情

microsoft/vscode-cpptools#4781

https://clang.llvm.org/docs/JSONCompilationDatabase.html

LLVM对compile_commands.json的"command": ...作出了明确规定,也就是需要显示处理空格和特殊字符。但"arguments": [...]没作特别说明,vscode认为要求和command是一样的,也就是需要escape这些特殊字符。现在xmake生成的compile_commands如果含有空格等,则不会进行转义,从而无法被vscode正确处理。例如xmake会生成这样的compile_commands

"arguments": ["<compiler>", "-I", "path with whitespace"]

但vscode要求这样(cmake采用这种形式)

"command": "<compiler> -I \"path with whitespace\""

或者这样

"arguments": ["<compiler>", "-I", "\"path with whitespace\""]
@waruqi
Copy link
Member

waruqi commented Oct 5, 2021

我这里 mac下测试 ok,没啥问题

commands 是完整string ,所以有转义要求也是合理的,为了便于拆分解析,通常的命令执行都是如此

而 args 都拆分了,每一项都已经分开了,还要求空格转义,这本身就有点另类。。

[
{
  "directory": "/Users/ruki/projects/personal/xmake/tests/projects/c/static library with spaces",
  "arguments": ["/usr/local/bin/ccache", "/usr/bin/xcrun", "-sdk", "macosx", "clang", "-c", "-Qunused-arguments", "-arch", "x86_64", "-mmacosx-version-min=10.15", "-isysroot", "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk", "-fvisibility=hidden", "-O3", "-I", "/Users/ruki/projects/personal/xmake/tests/projects/c/static library with spaces/i n c", "-DNDEBUG", "-o", "build/.objs/demo/macosx/x86_64/release/s r c/test.c.o", "s r c/test.c"],
  "file": "s r c/test.c"
},
{
  "directory": "/Users/ruki/projects/personal/xmake/tests/projects/c/static library with spaces",
  "arguments": ["/usr/local/bin/ccache", "/usr/bin/xcrun", "-sdk", "macosx", "clang", "-c", "-Qunused-arguments", "-arch", "x86_64", "-mmacosx-version-min=10.15", "-isysroot", "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk", "-fvisibility=hidden", "-O3", "-I", "/Users/ruki/projects/personal/xmake/tests/projects/c/static library with spaces/i n c", "-DNDEBUG", "-o", "build/.objs/test/macosx/x86_64/release/s r c/interface.c.o", "s r c/interface.c"],
  "file": "s r c/interface.c"
}]

@xq114
Copy link
Contributor Author

xq114 commented Oct 5, 2021

目前看c/c++的flag vscode会处理,只有cuda的flag有问题, microsoft/vscode-cpptools#8091

@xq114
Copy link
Contributor Author

xq114 commented Oct 6, 2021

vscode解析cuda时需要把这些参数传给nvcc,而nvcc不会处理这个问题;解析c++的时候内部就完成了,不需要传给外部程序,所以没问题。vscode对空格的处理还要等他们完成不同shell的不同escaping行为处理工作才行。在这之前xmake能否提供一个兼容模式,生成类似cmake的"command": ... 描述,而不是arguments?或者针对nvcc的flag特殊处理一下空格问题?

@waruqi
Copy link
Member

waruqi commented Oct 6, 2021

按理来讲 arguments 应该比 commands 兼容性更好才对,避免了 xmake层额外的转义拼string,也省去了 vscode 那边的转义解析

这边改成 commands 改动量不少,短期内也可能引入其他问题 等搞稳定发版 说不定vscode那边都支持上nvcc空格了

这边可以对nvcc flag特殊处理,不过加了即使当前临时兼容了,后面vscode那边改进完了,那这边现有版本不是反而又不兼容了?还得同步改回来?如果这样的话 还不如直接等那边改进完就好

@xq114
Copy link
Contributor Author

xq114 commented Oct 6, 2021

不会不兼容,现在cpp的-I flag也是同时兼容"\"path with space\"""path with space"两种写法的,问题是对cuda它只是简单把参数拼起来传给nvcc,没有解析-I -D等语义。之后加上语义解析之后理论上也完全可以同时支持两种写法。为了保持最大兼容可以只对nvcc的-I flag加quote,其他不动,也就是现在的

"-I", "path with space"

改成

"-I \"path with space\""

这样写无论LLVM和vscode那边怎么变化都不会影响的。这个问题主要影响的就是windows上cuda intellisense(因为cuda sdk默认安装位置含空格),做这样一个改动就没问题了

@waruqi
Copy link
Member

waruqi commented Oct 6, 2021

改了下,再试试

@waruqi waruqi added this to the v2.5.8 milestone Oct 6, 2021
@xq114
Copy link
Contributor Author

xq114 commented Oct 6, 2021

现在可以了

@xq114 xq114 closed this as completed Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants