Skip to content

Commit

Permalink
Merge pull request #10 from raspberrypi/cmake-tools-compatibility
Browse files Browse the repository at this point in the history
Add support for cmake-tools extension
  • Loading branch information
will-v-pi authored Apr 17, 2024
2 parents cef1ea1 + ccb76a5 commit 0bb5661
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 65 deletions.
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@
"category": "Raspberry Pi Pico",
"enablement": "false"
},
{
"command": "raspberry-pi-pico.getPythonPath",
"title": "Get python path",
"category": "Raspberry Pi Pico",
"enablement": "false"
},
{
"command": "raspberry-pi-pico.getEnvPath",
"title": "Get environment path",
"category": "Raspberry Pi Pico",
"enablement": "false"
},
{
"command": "raspberry-pi-pico.compileProject",
"title": "Compile Pico Project",
Expand Down Expand Up @@ -151,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": "",
Expand Down
33 changes: 27 additions & 6 deletions scripts/pico_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

VSCODE_LAUNCH_FILENAME = 'launch.json'
VSCODE_C_PROPERTIES_FILENAME = 'c_cpp_properties.json'
VSCODE_CMAKE_KITS_FILENAME ='cmake-kits.json'
VSCODE_SETTINGS_FILENAME ='settings.json'
VSCODE_EXTENSIONS_FILENAME ='extensions.json'
VSCODE_TASKS_FILENAME ='tasks.json'
Expand Down Expand Up @@ -740,7 +741,7 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger,
# Need to escape windows files paths backslashes
# TODO: env in currently not supported in compilerPath var
#cPath = f"${{env:PICO_TOOLCHAIN_PATH_{envSuffix}}}" + os.path.sep + os.path.basename(str(compilerPath).replace('\\', '\\\\' ))
cPath = compilerPath.as_posix()
cPath = compilerPath.as_posix() + (".exe" if isWindows else "")

# if this is a path in the .pico-sdk homedir tell the settings to use the homevar
user_home = os.path.expanduser("~").replace("\\", "/")
Expand Down Expand Up @@ -780,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)",
Expand All @@ -799,8 +799,7 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger,
"postRestartCommands": [
"break main",
"continue"
],
"preLaunchTask": "Compile Project"
]
}},
{{
"name": "Pico Debug (C++ Debugger)",
Expand Down Expand Up @@ -852,6 +851,24 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger,

pythonExe = sys.executable.replace("\\", "/").replace(user_home, "${HOME}") if use_home_var else sys.executable

# kits
kits = f'''[
{{
"name": "Pico",
"compilers": {{
"C": "{cPath}",
"CXX": "{cPath}"
}},
"toolchainFile": "{propertiesSdkPath(sdkVersion)}/cmake/preload/toolchains/pico_arm_gcc.cmake",
"environmentVariables": {{
"PATH": "${{command:raspberry-pi-pico.getEnvPath}};${{env:PATH}}"
}},
"cmakeSettings": {{
"Python3_EXECUTABLE": "${{command:raspberry-pi-pico.getPythonPath}}"
}}
}}
]'''

# settings
settings = f'''{{
"cmake.options.statusBarVisibility": "hidden",
Expand Down Expand Up @@ -888,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}"'''

Expand Down Expand Up @@ -930,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",
Expand Down Expand Up @@ -965,6 +982,10 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger,
file.write(properties)
file.close()

file = open(VSCODE_CMAKE_KITS_FILENAME, 'w')
file.write(kits)
file.close()

file = open(VSCODE_SETTINGS_FILENAME, 'w')
file.write(settings)
file.close()
Expand Down
15 changes: 14 additions & 1 deletion src/commands/compileProject.mts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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);
Expand Down
43 changes: 43 additions & 0 deletions src/commands/getPaths.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { CommandWithResult } from "./command.mjs";
import { workspace } from "vscode";
import { getPythonPath, getPath } from "../utils/cmakeUtil.mjs";

export class GetPythonPathCommand
extends CommandWithResult<string> {
constructor() {
super("getPythonPath");
}

async execute(): Promise<string> {
if (
workspace.workspaceFolders === undefined ||
workspace.workspaceFolders.length === 0
) {
return "";
}

const pythonPath = await getPythonPath();

return pythonPath;
}
}

export class GetEnvPathCommand
extends CommandWithResult<string> {
constructor() {
super("getEnvPath");
}

async execute(): Promise<string> {
if (
workspace.workspaceFolders === undefined ||
workspace.workspaceFolders.length === 0
) {
return "";
}

const path = await getPath();

return path;
}
}
19 changes: 18 additions & 1 deletion src/commands/launchTargetPath.mts
Original file line number Diff line number Diff line change
@@ -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<string> {
constructor() {
Expand Down Expand Up @@ -60,6 +61,19 @@ export default class LaunchTargetPathCommand extends CommandWithResult<string> {
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(
Expand All @@ -70,6 +84,9 @@ export default class LaunchTargetPathCommand extends CommandWithResult<string> {
return "";
}

// Compile before returning
await commands.executeCommand("raspberry-pi-pico.compileProject");

return join(fsPathFolder, "build", projectName + ".elf").replaceAll(
"\\",
"/"
Expand Down
6 changes: 6 additions & 0 deletions src/extension.mts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import { existsSync, readFileSync } from "fs";
import { basename, join } from "path";
import CompileProjectCommand from "./commands/compileProject.mjs";
import LaunchTargetPathCommand from "./commands/launchTargetPath.mjs";
import {
GetPythonPathCommand,
GetEnvPathCommand
} from "./commands/getPaths.mjs";
import {
downloadAndInstallCmake,
downloadAndInstallNinja,
Expand Down Expand Up @@ -80,6 +84,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
new NewProjectCommand(context.extensionUri),
new SwitchSDKCommand(ui, context.extensionUri),
new LaunchTargetPathCommand(),
new GetPythonPathCommand(),
new GetEnvPathCommand(),
new CompileProjectCommand(),
new ClearGithubApiCacheCommand(),
new ConditionalDebuggingCommand(),
Expand Down
1 change: 1 addition & 0 deletions src/settings.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum SettingsKey {
gitPath = "gitPath",
cmakeAutoConfigure = "cmakeAutoConfigure",
githubToken = "githubToken",
useCmakeTools = "useCmakeTools",
}

/**
Expand Down
Loading

0 comments on commit 0bb5661

Please sign in to comment.