Skip to content

Commit

Permalink
[vscode] Support tasks and intelliSense
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Oct 27, 2021
1 parent 0722111 commit f2bf916
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 33 deletions.
29 changes: 29 additions & 0 deletions tools/ide/vscode/c_cpp_properties.json.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"configurations": [
%% for profile, config in profiles.items()
{
"name": "{{ platform }} GCC ({{ profile | capitalize }})",
"includePath": [
%% for path in include_paths | sort
"${workspaceFolder}/{{ path | modm.windowsify(escape_level=1) }}"{% if not loop.last%},{% endif %}
%% endfor
],
"defines": [
%% for define in config.defines | sort
"{{ define }}"{% if not loop.last%},{% endif %}
%% endfor
],
"compilerPath": "{{ compiler_path }}",
"compilerArgs": [
%% for flag in config.archflags | sort
"{{ flag }}"{% if not loop.last%},{% endif %}
%% endfor
],
"cStandard": "c17",
"cppStandard": "c++20",
"intelliSenseMode": "gcc-arm"
}{% if not loop.last%},{% endif %}
%% endfor
],
"version": 4
}
18 changes: 11 additions & 7 deletions tools/ide/vscode/launch.json.in
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
{
"version": "0.2.0",
"configurations": [
%% for profile in profiles
%% for config in configs
{
"name": "modm debug configuration ({{ profile.name }}) for {{ partname }}",
"cwd": "${workspaceRoot}",
"executable": "{{ profile.executable }}",
"name": "Debug ({{ config.profile | capitalize }}, {{ config.tool | capitalize }}, {{ partname }})",
"cwd": "${workspaceFolder}",
"executable": "{{ config.executable }}",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"device": "{{ partname }}",
"runToMain": true,
%% if with_freertos
"rtos": "FreeRTOS",
%% endif
"configFiles": [
%% for openocd_config_file in openocd_config_files
"{{ openocd_config_file }}"{% if not loop.last %},{% endif %}
%% for cfg in openocd_cfg
"{{ cfg }}"{% if not loop.last %},{% endif %}
%% endfor
]
}{% if not loop.last %},{% endif %}
},
%% endfor
]
}
84 changes: 67 additions & 17 deletions tools/ide/vscode/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021, Raphael Lehmann
# Copyright (c) 2021, Niklas Hauser
#
# This file is part of the modm project.
#
Expand All @@ -10,38 +11,87 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

import subprocess
import platform

def init(module):
module.name = ":ide:vscode"
module.description = FileReader("module.md")


def prepare(module, options):
module.depends(":build")
return options[":target"].has_driver("core:cortex-m*")
return True


def build(env):
debug_profiles = list()
configs = []
build_path = env.relative_outpath(env[":build:build.path"])
for buildtool in ["scons", "make", "cmake"]:
for buildtool in ["scons", "make"]:
for profile in ["release", "debug"]:
if env.has_module(":build:" + buildtool):
debug_profiles.append({
"name": buildtool + " profile=" + profile,
configs.append({
"tool": buildtool,
"profile": profile,
"executable": "".join([build_path, "/", buildtool, "-", profile, "/", env[":build:project.name"], ".elf"]),
})

openocd_config_files = [
"modm/openocd.cfg",
]
openocd_cfg = ["modm/openocd.cfg"]
if env[":build:openocd.cfg"]:
openocd_config_files.append(env[":build:openocd.cfg"])

env.substitutions.update({
"profiles": debug_profiles,
"partname": env[":target"].partname,
"openocd_config_files": openocd_config_files,
})
# these files are placed into the CWD
openocd_cfg.append(env[":build:openocd.cfg"])

env.substitutions = {
"configs": configs,
"partname": env[":target"].partname.upper(),
"openocd_cfg": openocd_cfg,
"with_freertos": env.has_module(":freertos"),
}

env.outbasepath = env.relcwdoutpath(".vscode/")
# Only generate the tasks file for non-CMake build systems!
if configs:
env.template("tasks.json.in")
# Debugging support for Cortex-M only
if env[":target"].has_driver("core:cortex-m*"):
env.template("launch.json.in")


def post_build(env):
# Derive correct compiler path
compiler = ""
core = ""
if env[":target"].has_driver("core:cortex-m*"):
compiler += "arm-none-eabi-"
core = "Arm "
elif env[":target"].has_driver("core:avr*"):
compiler += "avr-"
core = "AVR "
compiler += "g++"

if "Windows" in platform.platform():
# FIXME: Determine toolchain location on Windows
compiler_path = compiler + ".exe"
else:
compiler_path = subprocess.run("which " + compiler, shell=True, stdout=subprocess.PIPE)
compiler_path = compiler_path.stdout.decode("ascii").strip()

flags = env.query("::collect_flags")(env)[None]
profiles = {
"release": {
"defines": flags["cppdefines"][""] + flags["cppdefines"]["release"],
"archflags": flags["archflags"][""] + flags["archflags"]["release"],
},
"debug": {
"defines": flags["cppdefines"][""] + flags["cppdefines"]["debug"],
"archflags": flags["archflags"][""] + flags["archflags"]["debug"],
}
}

env.substitutions = {
"platform": core,
"profiles": profiles,
"include_paths": env.collector_values("::path.include"),
"compiler_path": compiler_path,
}
env.outbasepath = env.relcwdoutpath(".vscode/")
env.template("launch.json.in")
env.template("c_cpp_properties.json.in")
27 changes: 18 additions & 9 deletions tools/ide/vscode/module.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
# Visual Studio Code (VSCode)

[Visual Studio Code (VSCode)][VSCode] is a popular IDE with an integrated debugger UI.
Together with the [Cortex-Debug][] extension it works great for Cortex-M targets.
[Visual Studio Code (VSCode)][VSCode] is a popular IDE with an integrated
debugger UI.

The blog [mcuoneclipse.com has a nice post][mcuoneclipse-vscode-debug] on how to setup
and use Visual Studio Code for C/C++ with ARM Cortex-M properly.
The blog [mcuoneclipse.com has a nice post][mcuoneclipse-vscode-debug] on how to
setup and use Visual Studio Code for C/C++ with ARM Cortex-M properly.

This module generates a `.vscode/` folder with the following files:

- `launch.json`: configuration file to launch the debugger with a single click
(or keyboard shortcut) from the IDE.
## Building

This module provides a `.vscode/tasks.json` file that wraps your selected build
system


## Debugging

This module generates a configuration file to use with the [Cortex-Debug][]
extension for Cortex-M targets:

- `.vscode/launch.json`: launches the debugger with a single click from the IDE.

[VSCode]: https://code.visualstudio.com/
[Cortex-Debug]: https://github.com/Marus/cortex-debug#readme
[mcuoneclipse-vscode-debug]: https://mcuoneclipse.com/2021/10/23/visual-studio-code-for-c-c-with-arm-cortex-m-part-10-assembly-stepping/

[VSCode]: https://code.visualstudio.com/
[mcuoneclipse-vscode-debug]: https://mcuoneclipse.com/2021/05/01/visual-studio-code-for-c-c-with-arm-cortex-m-part-1/
36 changes: 36 additions & 0 deletions tools/ide/vscode/tasks.json.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

{
// See https://code.visualstudio.com/docs/editor/tasks
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
%% for config in configs
{
"type": "shell",
"label": "Build ({{ config.profile | capitalize }})",
"command": "{{ config.tool }} build profile={{ config.profile }}",
"group": "build",
"presentation": {
"reveal": "silent",
"showReuseMessage": false
},
"problemMatcher": {
"base": "$gcc",
"fileLocation": ["relative", "${workspaceFolder}"]
}
},
%% endfor
%% for config in configs
{
"type": "shell",
"label": "Upload ({{ config.profile | capitalize }})",
"command": "{{ config.tool }} program profile={{ config.profile }}",
"group": "build",
"presentation": {
"reveal": "silent",
"showReuseMessage": false
}
},
%% endfor
]
}

0 comments on commit f2bf916

Please sign in to comment.