From ccb76a5211d630e316cf3bc3f94f9372f365c6f6 Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Mon, 8 Apr 2024 16:29:22 +0100 Subject: [PATCH] Add compilation with CMake Tools Adds support for compiling with CMake Tools, instead of this extension Also compile project whenever raspberry-pi-pico.launchTargetPath is called, to remove need for the preLaunchTask when debugging Signed-off-by: William Vinnicombe --- package.json | 5 +++++ scripts/pico_project.py | 8 +++----- src/commands/compileProject.mts | 15 ++++++++++++++- src/commands/launchTargetPath.mts | 19 ++++++++++++++++++- src/settings.mts | 1 + src/utils/cmakeUtil.mts | 9 +++++++++ 6 files changed, 50 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index d29259c..6915337 100644 --- a/package.json +++ b/package.json @@ -163,6 +163,11 @@ "default": true, "markdownDescription": "Automatically run configure when opening a Pico project" }, + "raspberry-pi-pico.useCmakeTools": { + "type": "boolean", + "default": false, + "markdownDescription": "Use the CMake Tools extension for CMake configuration, instead of this extension" + }, "raspberry-pi-pico.githubToken": { "type": "string", "default": "", diff --git a/scripts/pico_project.py b/scripts/pico_project.py index 2638914..b9eb08a 100644 --- a/scripts/pico_project.py +++ b/scripts/pico_project.py @@ -781,8 +781,7 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger, ], "openOCDLaunchCommands": [ "adapter speed 5000" - ], - "preLaunchTask": "Compile Project" + ] }}, {{ "name": "Pico Debug (Cortex-Debug with external OpenOCD)", @@ -800,8 +799,7 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger, "postRestartCommands": [ "break main", "continue" - ], - "preLaunchTask": "Compile Project" + ] }}, {{ "name": "Pico Debug (C++ Debugger)", @@ -907,6 +905,7 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger, "PATH": "{propertiesToolchainPath(toolchainVersion, force_non_windows=True)}/bin:{os.path.dirname(cmakePath.replace(user_home, "${env:HOME}") if use_home_var else cmakePath)}:{os.path.dirname(ninjaPath.replace(user_home, "${env:HOME}") if use_home_var else ninjaPath)}:${{env:PATH}}" }}, "raspberry-pi-pico.cmakeAutoConfigure": true, + "raspberry-pi-pico.useCmakeTools": false, "raspberry-pi-pico.cmakePath": "{cmakePath.replace(user_home, "${HOME}") if use_home_var else cmakePath}", "raspberry-pi-pico.ninjaPath": "{ninjaPath.replace(user_home, "${HOME}") if use_home_var else ninjaPath}"''' @@ -949,7 +948,6 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger, {{ "label": "Flash", "type": "process", - "dependsOn": "Compile Project", "command": "{openocd_path if openocd_path else "openocd"}", "args": [ "-f", diff --git a/src/commands/compileProject.mts b/src/commands/compileProject.mts index c1f2097..0ef49a8 100644 --- a/src/commands/compileProject.mts +++ b/src/commands/compileProject.mts @@ -1,6 +1,7 @@ -import { tasks, window } from "vscode"; +import { commands, tasks, window } from "vscode"; import { Command } from "./command.mjs"; import Logger from "../logger.mjs"; +import Settings, { SettingsKey } from "../settings.mjs"; export default class CompileProjectCommand extends Command { private _logger: Logger = new Logger("CompileProjectCommand"); @@ -19,6 +20,18 @@ export default class CompileProjectCommand extends Command { return task.name === "Compile Project"; }); + const settings = Settings.getInstance(); + if ( + settings !== undefined && settings.getBoolean(SettingsKey.useCmakeTools) + ) { + // Compile with CMake Tools + await commands.executeCommand( + "cmake.launchTargetPath" + ); + + return; + } + if (task) { // Execute the task await tasks.executeTask(task); diff --git a/src/commands/launchTargetPath.mts b/src/commands/launchTargetPath.mts index da9631c..a588807 100644 --- a/src/commands/launchTargetPath.mts +++ b/src/commands/launchTargetPath.mts @@ -1,7 +1,8 @@ import { readFileSync } from "fs"; import { CommandWithResult } from "./command.mjs"; -import { window, workspace } from "vscode"; +import { commands, window, workspace } from "vscode"; import { join } from "path"; +import Settings, { SettingsKey } from "../settings.mjs"; export default class LaunchTargetPathCommand extends CommandWithResult { constructor() { @@ -60,6 +61,19 @@ export default class LaunchTargetPathCommand extends CommandWithResult { return ""; } + const settings = Settings.getInstance(); + if ( + settings !== undefined && settings.getBoolean(SettingsKey.useCmakeTools) + ) { + // Compile with CMake Tools + const path: string = await commands.executeCommand( + "cmake.launchTargetPath" + ); + if (path) { + return path; + } + } + const fsPathFolder = workspace.workspaceFolders[0].uri.fsPath; const projectName = await this.readProjectNameFromCMakeLists( @@ -70,6 +84,9 @@ export default class LaunchTargetPathCommand extends CommandWithResult { return ""; } + // Compile before returning + await commands.executeCommand("raspberry-pi-pico.compileProject"); + return join(fsPathFolder, "build", projectName + ".elf").replaceAll( "\\", "/" diff --git a/src/settings.mts b/src/settings.mts index d42b10e..424a743 100644 --- a/src/settings.mts +++ b/src/settings.mts @@ -12,6 +12,7 @@ export enum SettingsKey { gitPath = "gitPath", cmakeAutoConfigure = "cmakeAutoConfigure", githubToken = "githubToken", + useCmakeTools = "useCmakeTools", } /** diff --git a/src/utils/cmakeUtil.mts b/src/utils/cmakeUtil.mts index b2f9d08..a75d03a 100644 --- a/src/utils/cmakeUtil.mts +++ b/src/utils/cmakeUtil.mts @@ -101,6 +101,15 @@ export async function configureCmakeNinja(folder: Uri): Promise { return false; } + if (settings.getBoolean(SettingsKey.useCmakeTools)) { + await window.showErrorMessage( + "You must use the CMake Tools extension to configure your build. " + + "To use this extension instead, change the useCmakeTools setting." + ); + + return false; + } + try { // check if CMakeLists.txt exists in the root folder await workspace.fs.stat(