diff mbox series

add Visual Studio Code configuration

Message ID 20210512100906.621504-1-pbonzini@redhat.com (mailing list archive)
State New, archived
Headers show
Series add Visual Studio Code configuration | expand

Commit Message

Paolo Bonzini May 12, 2021, 10:09 a.m. UTC
Add configurations to build files with Visual Studio Code and
to retrieve the search path for headers from the compile_commands.json
file.

Using this configuration requires installing the Meson extension and
using a build subdirectory that matches the one configured in the
Meson extension itself.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 .vscode/c_cpp_properties.json | 13 +++++++++++++
 .vscode/settings.json         | 11 +++++++++++
 .vscode/tasks.json            | 23 +++++++++++++++++++++++
 scripts/rebuild.py            | 22 ++++++++++++++++++++++
 4 files changed, 69 insertions(+)
 create mode 100644 .vscode/c_cpp_properties.json
 create mode 100644 .vscode/settings.json
 create mode 100644 .vscode/tasks.json
 create mode 100755 scripts/rebuild.py

Comments

Peter Maydell May 12, 2021, 11:02 a.m. UTC | #1
On Wed, 12 May 2021 at 11:10, Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> Add configurations to build files with Visual Studio Code and
> to retrieve the search path for headers from the compile_commands.json
> file.
>
> Using this configuration requires installing the Meson extension and
> using a build subdirectory that matches the one configured in the
> Meson extension itself.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>> diff --git a/scripts/rebuild.py b/scripts/rebuild.py
> new file mode 100755
> index 0000000000..e35e08f42d
> --- /dev/null
> +++ b/scripts/rebuild.py
> @@ -0,0 +1,22 @@
> +#! /usr/bin/env python3
> +#
> +# Author: Paolo Bonzini <pbonzini@redhat.com>
> +#
> +# This program compiles the input files using commands from the
> +# compile_commands.json file.  (Unlike Make/ninja, the _source_
> +# file is passed to the program rather than the targe).  It is
> +# mostly intended to be called from editors.

This seems weird. I don't think we should try to support multiple
ways of building QEMU -- editors should just run make the same
way everybody else does...

-- PMM
Philippe Mathieu-Daudé May 12, 2021, 11:08 a.m. UTC | #2
Hi Paolo,

On 5/12/21 12:09 PM, Paolo Bonzini wrote:
> Add configurations to build files with Visual Studio Code and
> to retrieve the search path for headers from the compile_commands.json
> file.
> 
> Using this configuration requires installing the Meson extension

maybe add "(@id:asabil.meson)" because there is another extension
called meson.

> and
> using a build subdirectory that matches the one configured in the
> Meson extension itself.

But first we need the ms-vscode.cpptools extension.

> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  .vscode/c_cpp_properties.json | 13 +++++++++++++
>  .vscode/settings.json         | 11 +++++++++++
>  .vscode/tasks.json            | 23 +++++++++++++++++++++++
>  scripts/rebuild.py            | 22 ++++++++++++++++++++++
>  4 files changed, 69 insertions(+)
>  create mode 100644 .vscode/c_cpp_properties.json
>  create mode 100644 .vscode/settings.json
>  create mode 100644 .vscode/tasks.json
>  create mode 100755 scripts/rebuild.py
> 
> diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
> new file mode 100644
> index 0000000000..43f5fc1b4d
> --- /dev/null
> +++ b/.vscode/c_cpp_properties.json
> @@ -0,0 +1,13 @@
> +{
> +    "configurations": [
> +        {
> +            "name": "qemu",
> +            "includePath": [ "${default}", "${workspaceFolder}/linux-headers/**", "${workspaceFolder}/include/**", "${workspaceFolder}/+build/**"],

Not sure about +build... This produces:

Cannot find "/home/phil/source/qemu/+build/".

> +            "compileCommands": "${workspaceFolder}/${config:mesonbuild.buildFolder}/compile_commands.json",
> +            "intelliSenseMode": "linux-gcc-x64",
> +            "cStandard": "c11",
> +            "cppStandard": "c++14"
> +        }
> +    ],
> +    "version": 4
> +}
> diff --git a/.vscode/settings.json b/.vscode/settings.json
> new file mode 100644
> index 0000000000..efbbb4f88b
> --- /dev/null
> +++ b/.vscode/settings.json
> @@ -0,0 +1,11 @@
> +{
> +	"files.associations": {
> +		"*.mak": "makefile",
> +		"*.c.inc": "c",
> +		"*.h.inc": "c",
> +		"*.json": "python",

When opening .json I get many:

"true" is not defined (Pylance reportUndefinedVariable)
"false" is not defined (Pylance reportUndefinedVariable)

> +		"*.rst.inc": "restructuredtext",
> +		"*.vert": "glsl",
> +		"*.frag": "glsl"
> +	}
> +}
> diff --git a/.vscode/tasks.json b/.vscode/tasks.json
> new file mode 100644
> index 0000000000..362821043e
> --- /dev/null
> +++ b/.vscode/tasks.json
> @@ -0,0 +1,23 @@
> +{
> +	"version": "2.0.0",
> +	"tasks": [
> +		{
> +			"type": "cppbuild",
> +			"label": "C/C++: build active file with compile_commands.json",
> +			"command": "python3",
> +			"args": [
> +				"${workspaceFolder}/scripts/rebuild.py", "${file}"
> +			],
> +			"options": {
> +				"cwd": "${workspaceFolder}/${config:mesonbuild.buildFolder}"
> +			},
> +			"problemMatcher": [
> +				"$gcc"
> +			],
> +			"group": {
> +				"kind": "build",
> +				"isDefault": true
> +			}
> +		}
> +	]
> +}
> diff --git a/scripts/rebuild.py b/scripts/rebuild.py
> new file mode 100755
> index 0000000000..e35e08f42d
> --- /dev/null
> +++ b/scripts/rebuild.py
> @@ -0,0 +1,22 @@
> +#! /usr/bin/env python3
> +#
> +# Author: Paolo Bonzini <pbonzini@redhat.com>
> +#
> +# This program compiles the input files using commands from the
> +# compile_commands.json file.  (Unlike Make/ninja, the _source_
> +# file is passed to the program rather than the targe).  It is

Typo "target"

> +# mostly intended to be called from editors.
> +
> +import os
> +import sys
> +import json
> +
> +with open('compile_commands.json') as f:
> +    cc_json = json.load(f)
> +
> +paths = set((os.path.relpath(i) for i in sys.argv[1:]))
> +for i in cc_json:
> +    if i['file'] in paths:
> +        os.chdir(i['directory'])
> +        print(i['command'])
> +        os.system(i['command'])
> 

I got these warnings:

[5/12/2021, 12:24:04 PM]
"${workspaceFolder}/builddir/compile_commands.json" could not be found.
'includePath' from c_cpp_properties.json in folder 'qemu' will be used
instead.
[5/12/2021, 12:24:04 PM] For C source files, IntelliSenseMode was
changed from "linux-gcc-x64" to "linux-clang-x64" based on compiler args
and querying compilerPath: "/usr/lib64/ccache/clang"
[5/12/2021, 12:24:04 PM] For C++ source files, IntelliSenseMode was
changed from "linux-gcc-x64" to "linux-clang-x64" based on compiler args
and querying compilerPath: "/usr/lib64/ccache/clang"

Then when pressing Ctrl+Shift+B to run the build task:

> Executing task: C/C++: build active file with compile_commands.json <

Starting build...
python3 /home/phil/source/qemu/scripts/rebuild.py
/home/phil/source/qemu/builddir/meson-info/meson-info.json
Traceback (most recent call last):
  File "/home/phil/source/qemu/scripts/rebuild.py", line 14, in <module>
    with open('compile_commands.json') as f:
FileNotFoundError: [Errno 2] No such file or directory:
'compile_commands.json'

Build finished with error(s).
The terminal process failed to launch (exit code: -1).

I don't see any target in the Meson view.

BTW I also installed the 'ms-python.python' extension and
got 'ms-azuretools.vscode-docker' suggested.

Regards,

Phil.
Paolo Bonzini May 12, 2021, 6:16 p.m. UTC | #3
On 12/05/21 13:08, Philippe Mathieu-Daudé wrote:
>> --- /dev/null
>> +++ b/.vscode/c_cpp_properties.json
>> @@ -0,0 +1,13 @@
>> +{
>> +    "configurations": [
>> +        {
>> +            "name": "qemu",
>> +            "includePath": [ "${default}", "${workspaceFolder}/linux-headers/**", "${workspaceFolder}/include/**", "${workspaceFolder}/+build/**"],
> 
> Not sure about +build... This produces:

Oops, should be ${workspaceFolder}/${config:mesonbuild.buildFolder}/**.

>> diff --git a/.vscode/settings.json b/.vscode/settings.json
>> new file mode 100644
>> index 0000000000..efbbb4f88b
>> --- /dev/null
>> +++ b/.vscode/settings.json
>> @@ -0,0 +1,11 @@
>> +{
>> +	"files.associations": {
>> +		"*.mak": "makefile",
>> +		"*.c.inc": "c",
>> +		"*.h.inc": "c",
>> +		"*.json": "python",
> 
> When opening .json I get many:
> 
> "true" is not defined (Pylance reportUndefinedVariable)
> "false" is not defined (Pylance reportUndefinedVariable)

I'll remove this then.

>> diff --git a/.vscode/tasks.json b/.vscode/tasks.json
>> new file mode 100644
>> index 0000000000..362821043e
>> --- /dev/null
>> +++ b/.vscode/tasks.json
>> @@ -0,0 +1,23 @@
>> +{
>> +	"version": "2.0.0",
>> +	"tasks": [
>> +		{
>> +			"type": "cppbuild",
>> +			"label": "C/C++: build active file with compile_commands.json",
>> +			"command": "python3",
>> +			"args": [
>> +				"${workspaceFolder}/scripts/rebuild.py", "${file}"
>> +			],
>> +			"options": {
>> +				"cwd": "${workspaceFolder}/${config:mesonbuild.buildFolder}"
>> +			},
>> +			"problemMatcher": [
>> +				"$gcc"
>> +			],
>> +			"group": {
>> +				"kind": "build",
>> +				"isDefault": true
>> +			}
>> +		}
>> +	]
>> +}
>> diff --git a/scripts/rebuild.py b/scripts/rebuild.py
>> new file mode 100755
>> index 0000000000..e35e08f42d
>> --- /dev/null
>> +++ b/scripts/rebuild.py
>> @@ -0,0 +1,22 @@
>> +#! /usr/bin/env python3
>> +#
>> +# Author: Paolo Bonzini <pbonzini@redhat.com>
>> +#
>> +# This program compiles the input files using commands from the
>> +# compile_commands.json file.  (Unlike Make/ninja, the _source_
>> +# file is passed to the program rather than the targe).  It is
> 
> Typo "target"
> 
>> +# mostly intended to be called from editors.
>> +
>> +import os
>> +import sys
>> +import json
>> +
>> +with open('compile_commands.json') as f:
>> +    cc_json = json.load(f)
>> +
>> +paths = set((os.path.relpath(i) for i in sys.argv[1:]))
>> +for i in cc_json:
>> +    if i['file'] in paths:
>> +        os.chdir(i['directory'])
>> +        print(i['command'])
>> +        os.system(i['command'])
>>
> 
> I got these warnings:
> 
> [5/12/2021, 12:24:04 PM]
> "${workspaceFolder}/builddir/compile_commands.json" could not be found.
> 'includePath' from c_cpp_properties.json in folder 'qemu' will be used
> instead.

"builddir" must be your mesonbuild.buildFolder.  You must first run 
"configure" manually in that directory (or adjust the configuration of 
the Meson extension if that's what you prefer).

Once you do that, Ctrl+Shift+B works.

Paolo
Paolo Bonzini May 12, 2021, 6:29 p.m. UTC | #4
On 12/05/21 13:02, Peter Maydell wrote:
>> --- /dev/null
>> +++ b/scripts/rebuild.py
>> @@ -0,0 +1,22 @@
>> +#! /usr/bin/env python3
>> +#
>> +# Author: Paolo Bonzini <pbonzini@redhat.com>
>> +#
>> +# This program compiles the input files using commands from the
>> +# compile_commands.json file.  (Unlike Make/ninja, the _source_
>> +# file is passed to the program rather than the targe).  It is
>> +# mostly intended to be called from editors.
> 
> This seems weird. I don't think we should try to support multiple
> ways of building QEMU -- editors should just run make the same
> way everybody else does...

Right, it's possible to include a "whole build" task in tasks.json, and 
it will invoke Make/ninja (I haven't done it yet though so it's not 
included in this first attempt).

This script instead is used to rebuild the one file that is being 
edited, for example to check quickly for syntax errors.  I did find it 
quite surprising that VS Code could not do this on its own, since it can 
use compile_commands.json to retrieve the header file paths for example; 
still, this script is not a replacement for make, as this functionality 
of reverse-mapping from .c to .o is not available natively in either 
Make or ninja.  It might even be handy for vim or Emacs users.

(This series really should have been tagged as RFC).

Paolo
diff mbox series

Patch

diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
new file mode 100644
index 0000000000..43f5fc1b4d
--- /dev/null
+++ b/.vscode/c_cpp_properties.json
@@ -0,0 +1,13 @@ 
+{
+    "configurations": [
+        {
+            "name": "qemu",
+            "includePath": [ "${default}", "${workspaceFolder}/linux-headers/**", "${workspaceFolder}/include/**", "${workspaceFolder}/+build/**"],
+            "compileCommands": "${workspaceFolder}/${config:mesonbuild.buildFolder}/compile_commands.json",
+            "intelliSenseMode": "linux-gcc-x64",
+            "cStandard": "c11",
+            "cppStandard": "c++14"
+        }
+    ],
+    "version": 4
+}
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000000..efbbb4f88b
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,11 @@ 
+{
+	"files.associations": {
+		"*.mak": "makefile",
+		"*.c.inc": "c",
+		"*.h.inc": "c",
+		"*.json": "python",
+		"*.rst.inc": "restructuredtext",
+		"*.vert": "glsl",
+		"*.frag": "glsl"
+	}
+}
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
new file mode 100644
index 0000000000..362821043e
--- /dev/null
+++ b/.vscode/tasks.json
@@ -0,0 +1,23 @@ 
+{
+	"version": "2.0.0",
+	"tasks": [
+		{
+			"type": "cppbuild",
+			"label": "C/C++: build active file with compile_commands.json",
+			"command": "python3",
+			"args": [
+				"${workspaceFolder}/scripts/rebuild.py", "${file}"
+			],
+			"options": {
+				"cwd": "${workspaceFolder}/${config:mesonbuild.buildFolder}"
+			},
+			"problemMatcher": [
+				"$gcc"
+			],
+			"group": {
+				"kind": "build",
+				"isDefault": true
+			}
+		}
+	]
+}
diff --git a/scripts/rebuild.py b/scripts/rebuild.py
new file mode 100755
index 0000000000..e35e08f42d
--- /dev/null
+++ b/scripts/rebuild.py
@@ -0,0 +1,22 @@ 
+#! /usr/bin/env python3
+#
+# Author: Paolo Bonzini <pbonzini@redhat.com>
+#
+# This program compiles the input files using commands from the
+# compile_commands.json file.  (Unlike Make/ninja, the _source_
+# file is passed to the program rather than the targe).  It is
+# mostly intended to be called from editors.
+
+import os
+import sys
+import json
+
+with open('compile_commands.json') as f:
+    cc_json = json.load(f)
+
+paths = set((os.path.relpath(i) for i in sys.argv[1:]))
+for i in cc_json:
+    if i['file'] in paths:
+        os.chdir(i['directory'])
+        print(i['command'])
+        os.system(i['command'])