]> Git Repo - pico-vscode.git/blob - src/commands/getPaths.mts
Fix #36, Downgrading SDK >= 2.0.0 with unsupported PICO_BOARD breaks cmake
[pico-vscode.git] / src / commands / getPaths.mts
1 import { CommandWithResult } from "./command.mjs";
2 import { commands, workspace } from "vscode";
3 import {
4   getPythonPath, getPath,
5   cmakeGetSelectedToolchainAndSDKVersions, cmakeGetPicoVar
6 } from "../utils/cmakeUtil.mjs";
7 import { join } from "path";
8 import { buildToolchainPath } from "../utils/download.mjs";
9 import Settings, { SettingsKey } from "../settings.mjs";
10
11 export class GetPythonPathCommand
12                       extends CommandWithResult<string> {
13   constructor() {
14     super("getPythonPath");
15   }
16
17   async execute(): Promise<string> {
18     if (
19       workspace.workspaceFolders === undefined ||
20       workspace.workspaceFolders.length === 0
21     ) {
22       return "";
23     }
24
25     const pythonPath = await getPythonPath();
26
27     return pythonPath;
28   }
29 }
30
31 export class GetEnvPathCommand
32                       extends CommandWithResult<string> {
33   constructor() {
34     super("getEnvPath");
35   }
36
37   async execute(): Promise<string> {
38     if (
39       workspace.workspaceFolders === undefined ||
40       workspace.workspaceFolders.length === 0
41     ) {
42       return "";
43     }
44
45     const path = await getPath();
46
47     return path;
48   }
49 }
50
51 export class GetGDBPathCommand
52                       extends CommandWithResult<string> {
53   constructor() {
54     super("getGDBPath");
55   }
56
57   execute(): string {
58     if (
59       workspace.workspaceFolders === undefined ||
60       workspace.workspaceFolders.length === 0
61     ) {
62       return "";
63     }
64
65     const workspaceFolder = workspace.workspaceFolders?.[0];
66
67     const selectedToolchainAndSDKVersions =
68     cmakeGetSelectedToolchainAndSDKVersions(
69       join(workspaceFolder.uri.fsPath, "CMakeLists.txt")
70     );
71     if (selectedToolchainAndSDKVersions === null) {
72       return "";
73     }
74     const toolchainVersion = selectedToolchainAndSDKVersions[1];
75
76     let triple = "arm-none-eabi";
77     if (toolchainVersion.includes("RISCV")) {
78       if (toolchainVersion.includes("COREV")) {
79         triple = "riscv32-corev-elf";
80       } else {
81         triple = "riscv32-unknown-elf";
82       }
83     } else if (process.platform === "linux") {
84       // Arm toolchains have incorrect libncurses versions on Linux (specifically RPiOS)
85       if (process.arch === "arm64") {
86         return "gdb";
87       } else {
88         return "gdb-multiarch";
89       }
90     }
91
92     return join(buildToolchainPath(toolchainVersion), "bin", triple + "-gdb");
93   }
94 }
95
96 export class GetChipCommand
97                       extends CommandWithResult<string> {
98   constructor() {
99     super("getChip");
100   }
101
102   async execute(): Promise<string> {
103     if (
104       workspace.workspaceFolders === undefined ||
105       workspace.workspaceFolders.length === 0
106     ) {
107       return "";
108     }
109
110     const workspaceFolder = workspace.workspaceFolders?.[0];
111
112     const settings = Settings.getInstance();
113     let buildDir = join(workspaceFolder.uri.fsPath, "build");
114     if (
115       settings !== undefined && settings.getBoolean(SettingsKey.useCmakeTools)
116     ) {
117       // Compiling with CMake Tools
118       const cmakeBuildDir: string = await commands.executeCommand(
119         "cmake.buildDirectory"
120       );
121
122       if (cmakeBuildDir) {
123         buildDir = cmakeBuildDir;
124       }
125     }
126
127     const platform =
128     cmakeGetPicoVar(
129       join(buildDir, "CMakeCache.txt"),
130       "PICO_PLATFORM"
131     );
132     if (platform === null) {
133       return "rp2040";
134     }
135
136     if (platform === "rp2350-arm-s" || platform === "rp2350-riscv") {
137       return "rp2350";
138     } else {
139       return "rp2040";
140     }
141   }
142 }
143
144 export class GetTargetCommand
145                       extends CommandWithResult<string> {
146   constructor() {
147     super("getTarget");
148   }
149
150   async execute(): Promise<string> {
151     if (
152       workspace.workspaceFolders === undefined ||
153       workspace.workspaceFolders.length === 0
154     ) {
155       return "";
156     }
157
158     const workspaceFolder = workspace.workspaceFolders?.[0];
159
160     const settings = Settings.getInstance();
161     let buildDir = join(workspaceFolder.uri.fsPath, "build");
162     if (
163       settings !== undefined && settings.getBoolean(SettingsKey.useCmakeTools)
164     ) {
165       // Compiling with CMake Tools
166       const cmakeBuildDir: string = await commands.executeCommand(
167         "cmake.buildDirectory"
168       );
169
170       if (cmakeBuildDir) {
171         buildDir = cmakeBuildDir;
172       }
173     }
174
175     const platform =
176     cmakeGetPicoVar(
177       join(buildDir, "CMakeCache.txt"),
178       "PICO_PLATFORM"
179     );
180     if (platform === null) {
181       return "rp2040";
182     }
183
184     if (platform === "rp2350-arm-s") {
185       return "rp2350";
186     } else if (platform === "rp2350-riscv") {
187       return "rp2350-riscv";
188     } else {
189       return "rp2040";
190     }
191   }
192 }
This page took 0.043468 seconds and 4 git commands to generate.