Skip to content

Commit 6b4d656

Browse files
rlehsalkinium
andcommitted
[ide] VSCode support: Tasks, IntelliSense, Debug
Co-authored-by: Niklas Hauser <[email protected]>
1 parent 151d2e1 commit 6b4d656

File tree

6 files changed

+260
-0
lines changed

6 files changed

+260
-0
lines changed

tools/ide/module.lb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2021, Raphael Lehmann
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
# -----------------------------------------------------------------------------
12+
13+
# -----------------------------------------------------------------------------
14+
def init(module):
15+
module.name = ":ide"
16+
module.description = "IDE Support"
17+
18+
19+
def prepare(module, options):
20+
return True
21+
22+
23+
def build(env):
24+
pass
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"configurations": [
3+
%% for profile, config in profiles.items()
4+
{
5+
"name": "{{ platform }}GCC ({{ profile | capitalize }})",
6+
"includePath": [
7+
%% for path in include_paths | sort
8+
"${workspaceFolder}/{{ path | modm.windowsify(escape_level=1) }}"{% if not loop.last%},{% endif %}
9+
%% endfor
10+
],
11+
"defines": [
12+
%% for define in config.cppdefines | sort
13+
"{{ define }}"{% if not loop.last%},{% endif %}
14+
%% endfor
15+
],
16+
"compilerPath": "{{ compiler_path }}",
17+
"compilerArgs": [
18+
%% for flag in config.archflags | sort
19+
"{{ flag }}"{% if not loop.last%},{% endif %}
20+
%% endfor
21+
],
22+
"cStandard": "c17",
23+
"cppStandard": "c++20",
24+
"intelliSenseMode": "gcc-arm"
25+
}{% if not loop.last%},{% endif %}
26+
%% endfor
27+
],
28+
"version": 4
29+
}

tools/ide/vscode/launch.json.in

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
%% for config in configs
5+
{
6+
"name": "Debug ({{ config.profile | capitalize }}, {{ config.tool | capitalize }}, {{ partname }})",
7+
"cwd": "${workspaceFolder}",
8+
"executable": "{{ config.executable }}",
9+
"request": "launch",
10+
"type": "cortex-debug",
11+
"servertype": "openocd",
12+
"device": "{{ partname }}",
13+
"runToMain": true,
14+
%% if with_freertos
15+
"rtos": "FreeRTOS",
16+
%% endif
17+
"configFiles": [
18+
%% for cfg in openocd_cfg
19+
"{{ cfg }}",
20+
%% endfor
21+
],
22+
%% if with_rtt
23+
"rttConfig": {
24+
"enabled": true,
25+
"address": "0x{{"%0x" % rtt_ram.start}}",
26+
"searchSize": {{ rtt_ram.size }},
27+
"searchId": "modm.rtt.modm",
28+
"polling_interval": 1,
29+
"decoders": [
30+
%% for id in rtt_channels
31+
{
32+
"port": {{id}},
33+
"type": "console"
34+
},
35+
%% endfor
36+
]
37+
},
38+
%% endif
39+
},
40+
%% endfor
41+
]
42+
}

tools/ide/vscode/module.lb

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2021, Raphael Lehmann
5+
# Copyright (c) 2021, Niklas Hauser
6+
#
7+
# This file is part of the modm project.
8+
#
9+
# This Source Code Form is subject to the terms of the Mozilla Public
10+
# License, v. 2.0. If a copy of the MPL was not distributed with this
11+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
12+
# -----------------------------------------------------------------------------
13+
14+
import subprocess
15+
import platform
16+
17+
def init(module):
18+
module.name = ":ide:vscode"
19+
module.description = FileReader("module.md")
20+
21+
22+
def prepare(module, options):
23+
module.depends(":build")
24+
return True
25+
26+
27+
def build(env):
28+
pass
29+
30+
31+
def post_build(env):
32+
env.outbasepath = env.relcwdoutpath(".vscode/")
33+
34+
configs = []
35+
build_path = env.relative_outpath(env[":build:build.path"])
36+
for buildtool in ["scons", "make"]:
37+
for profile in ["release", "debug"]:
38+
if env.has_module(":build:" + buildtool):
39+
configs.append({
40+
"tool": buildtool,
41+
"profile": profile,
42+
"executable": "".join([build_path, "/", buildtool, "-", profile, "/", env[":build:project.name"], ".elf"]),
43+
})
44+
45+
compiler = ""
46+
core = ""
47+
if env[":target"].has_driver("core:cortex-m*"):
48+
compiler += "arm-none-eabi-"
49+
core = "Arm "
50+
elif env[":target"].has_driver("core:avr*"):
51+
compiler += "avr-"
52+
core = "AVR "
53+
compiler += "g++"
54+
55+
# Find out the toolchain location
56+
if "Windows" in platform.platform():
57+
# FIXME: how do I query this on Windows?
58+
compiler_path = compiler + ".exe"
59+
else:
60+
compiler_path = subprocess.run("which " + compiler, shell=True, stdout=subprocess.PIPE)
61+
compiler_path = compiler_path.stdout.decode("ascii").strip()
62+
63+
flags = env.query(":build:collect_flags")(env)[None]
64+
profiles = {
65+
p: {f: (flags[f][""] + flags[f][p]) for f in ["cppdefines", "archflags"]}
66+
for p in ["release", "debug"]
67+
}
68+
69+
env.substitutions = {
70+
"configs": configs,
71+
"partname": env[":target"].partname.upper(),
72+
"with_freertos": env.has_module(":freertos"),
73+
"platform": core,
74+
"profiles": profiles,
75+
"include_paths": env.collector_values("::path.include"),
76+
"compiler_path": compiler_path,
77+
# FIXME: RTT block is searched for too early.
78+
# See https://github.com/Marus/cortex-debug/wiki/SEGGER-RTT-support#known-issues
79+
"with_rtt": False, # env.has_module(":platform:rtt"),
80+
}
81+
# IntelliSense config
82+
env.template("c_cpp_properties.json.in")
83+
84+
# Only generate the tasks file for non-CMake build systems!
85+
if configs:
86+
env.template("tasks.json.in")
87+
88+
# Debugging support for Cortex-M only
89+
if env[":target"].has_driver("core:cortex-m*"):
90+
# rtt_ram = env.query(":platform:cortex-m:linkerscript", {})
91+
# rtt_ram = rtt_ram.get("cont_ram_regions", [{"start": 0x20000000, "size": 4096}])[0]
92+
# env.substitutions["rtt_ram"] = rtt_ram
93+
# env.substitutions["rtt_channels"] = range(len(env.get(":platform:rtt:buffer.tx", [])))
94+
95+
openocd_cfg = ["modm/openocd.cfg"]
96+
if env.get(":build:openocd.cfg", ""):
97+
openocd_cfg.append(env[":build:openocd.cfg"])
98+
env.substitutions["openocd_cfg"] = openocd_cfg
99+
100+
env.template("launch.json.in")

tools/ide/vscode/module.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Visual Studio Code (VSCode)
2+
3+
[Visual Studio Code (VSCode)][VSCode] is a popular IDE with an integrated
4+
debugger UI.
5+
6+
This module provides configuration files to integrate building, debugging,
7+
uploading and code completion into the IDE:
8+
9+
- `.vscode/tasks.json`: If the `modm:build:scons` and `modm:build:make` module
10+
are included, this file wraps the building and uploading commands by profile.
11+
You can invoke them by Ctl+Shift+B (macOS: Cmd+Shift+B).
12+
- `.vscode/c_cpp_properties.json`: Configures IntelliSense for better code
13+
completion and for using the correct system headers. You need the
14+
[C/C++ extension][] installed.
15+
- `.vscode/launch.json`: Configures the [Cortex-Debug][] extension to start the
16+
debugger with a single click from the IDE.
17+
18+
Note that not all build system features are provided in the IDE, only the most
19+
common. You can call all build system tools from the command line.
20+
In addition, the `modm:build:cmake` module is supported natively by the
21+
[CMake Tools][] extension and is therefore not wrapped.
22+
23+
We recommend adding this module from the command line so that you don't
24+
accidentally overwrite your modified files later: `lbuild build -m ::vscode`.
25+
26+
Note that some configuration options may be specific to your environment (such
27+
as the compiler path) and may not work in other environments.
28+
29+
30+
[VSCode]: https://code.visualstudio.com/
31+
[C/C++ extension]: https://github.com/Microsoft/vscode-cpptools
32+
[CMake Tools]: https://github.com/microsoft/vscode-cmake-tools
33+
[Cortex-Debug]: https://github.com/Marus/cortex-debug#readme

tools/ide/vscode/tasks.json.in

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
// See https://code.visualstudio.com/docs/editor/tasks
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
%% for config in configs
7+
{
8+
"type": "shell",
9+
"label": "Build ({{ config.profile | capitalize }})",
10+
"command": "{{ config.tool }} build profile={{ config.profile }}",
11+
"group": "build",
12+
"presentation": {
13+
"reveal": "silent",
14+
"showReuseMessage": false
15+
},
16+
"problemMatcher": "$gcc"
17+
},
18+
%% endfor
19+
%% for config in configs
20+
{
21+
"type": "shell",
22+
"label": "Upload ({{ config.profile | capitalize }})",
23+
"command": "{{ config.tool }} program profile={{ config.profile }}",
24+
"group": "build",
25+
"presentation": {
26+
"reveal": "silent",
27+
"showReuseMessage": false
28+
}
29+
},
30+
%% endfor
31+
]
32+
}

0 commit comments

Comments
 (0)