Skip to content

Commit 3978c84

Browse files
Magpie Embeddedpaulober
authored andcommitted
Improve CMake flexibility for Zephyr setup
1 parent 42d46b6 commit 3978c84

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

src/utils/setupZephyr.mts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync, readdirSync, rmSync } from "fs";
22
import { window, workspace, ProgressLocation, Uri } from "vscode";
33
import { type ExecOptions, exec, spawnSync } from "child_process";
4+
import { dirname } from "path";
45
import { join as joinPosix } from "path/posix";
56
import { homedir } from "os";
67
import Logger from "../logger.mjs";
@@ -82,7 +83,7 @@ function _runCommand(
8283

8384
export async function setupZephyr(
8485
data: ZephyrSetupValue
85-
): Promise<ZephyrSetupOutputs | string | undefined> {
86+
): Promise<ZephyrSetupOutputs | undefined> {
8687
const settings = Settings.getInstance();
8788
if (settings === undefined) {
8889
_logger.error("Settings not initialized.");
@@ -531,13 +532,7 @@ export async function setupZephyr(
531532
const customEnv = process.env;
532533

533534
const customPath = [
534-
joinPosix(
535-
homedir().replaceAll("\\", "/"),
536-
".pico-sdk",
537-
"cmake",
538-
"v3.31.5",
539-
"bin"
540-
),
535+
dirname(output.cmakeExecutable.replaceAll("\\", "/")),
541536
joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk", "dtc", "bin"),
542537
joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk", "git", "cmd"),
543538
joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk", "gperf", "bin"),

src/webview/newZephyrProjectPanel.mts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import {
2323
} from "./newProjectPanel.mjs";
2424
import which from "which";
2525
import { existsSync } from "fs";
26-
import { join } from "path";
26+
import { join, dirname } from "path";
2727
import { PythonExtension } from "@vscode/python-extension";
2828
import { unknownErrorToString } from "../utils/errorHelper.mjs";
2929
import { buildZephyrWorkspacePath } from "../utils/download.mjs";
30-
import { setupZephyr } from "../utils/setupZephyr.mjs";
30+
import { setupZephyr, type ZephyrSetupOutputs } from "../utils/setupZephyr.mjs";
3131
import VersionBundlesLoader, {
3232
type VersionBundle,
3333
} from "../utils/versionBundles.mjs";
@@ -142,7 +142,18 @@ export class NewZephyrProjectPanel {
142142

143143
// Create settings.json file with correct subsitution for tools such as
144144
// CMake, Ninja, Python, etc
145-
private static createSettingsJson(): string {
145+
private static createSettingsJson(
146+
homePath: string,
147+
cmakePath: string
148+
): string {
149+
// Helper functions
150+
const getDirName = (s: string): string => dirname(joinPosix(s));
151+
152+
const subbedCmakePath = getDirName(
153+
cmakePath.replace(homePath, "${userHome}")
154+
);
155+
console.log(subbedCmakePath);
156+
146157
const settingsJson = {
147158
/* eslint-disable @typescript-eslint/naming-convention */
148159
"cmake.options.statusBarVisibility": "hidden",
@@ -161,27 +172,37 @@ export class NewZephyrProjectPanel {
161172
"cmake.automaticReconfigure": false,
162173
"cmake.configureOnOpen": false,
163174
"cmake.generator": "Ninja",
164-
"cmake.cmakePath": "${userHome}/.pico-sdk/cmake/v3.31.5/bin/cmake",
175+
"cmake.cmakePath": `${cmakePath.replace(homePath, "${userHome}")}`,
165176
"C_Cpp.debugShortcut": false,
166177
"terminal.integrated.env.windows": {
167178
PICO_SDK_PATH: "${env:USERPROFILE}/.pico-sdk/sdk/2.1.1",
168179
PICO_TOOLCHAIN_PATH: "${env:USERPROFILE}/.pico-sdk/toolchain/14_2_Rel1",
169-
Path: "${env:USERPROFILE}/.pico-sdk/toolchain/14_2_Rel1/bin;${env:USERPROFILE}/.pico-sdk/picotool/2.1.1/picotool;${env:USERPROFILE}/.pico-sdk/cmake/v3.31.5/bin;${env:USERPROFILE}/.pico-sdk/ninja/v1.12.1;${env:PATH}",
180+
Path:
181+
"${env:USERPROFILE}/.pico-sdk/toolchain/14_2_Rel1/bin;${env:USERPROFILE}/.pico-sdk/picotool/2.1.1/picotool;" +
182+
`${getDirName(cmakePath.replace(homePath, "${env:USERPROFILE}"))};` +
183+
"${env:USERPROFILE}/.pico-sdk/ninja/v1.12.1;${env:PATH}",
170184
},
171185
"terminal.integrated.env.osx": {
172186
PICO_SDK_PATH: "${env:HOME}/.pico-sdk/sdk/2.1.1",
173187
PICO_TOOLCHAIN_PATH: "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1",
174-
PATH: "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.1.1/picotool:${env:HOME}/.pico-sdk/cmake/v3.31.5/bin:${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}",
188+
PATH:
189+
"${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.1.1/picotool:" +
190+
`${getDirName(cmakePath.replace(homePath, "{env:HOME}"))}:` +
191+
"${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}",
175192
},
176193
"terminal.integrated.env.linux": {
177194
PICO_SDK_PATH: "${env:HOME}/.pico-sdk/sdk/2.1.1",
178195
PICO_TOOLCHAIN_PATH: "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1",
179-
PATH: "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.1.1/picotool:${env:HOME}/.pico-sdk/cmake/v3.31.5/bin:${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}",
196+
PATH:
197+
"${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.1.1/picotool:" +
198+
`${getDirName(cmakePath.replace(homePath, "{env:HOME}"))}:` +
199+
"${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}",
180200
},
181201
"raspberry-pi-pico.cmakeAutoConfigure": true,
182202
"raspberry-pi-pico.useCmakeTools": false,
183-
"raspberry-pi-pico.cmakePath":
184-
"${HOME}/.pico-sdk/cmake/v3.31.5/bin/cmake",
203+
"raspberry-pi-pico.cmakePath": `${getDirName(
204+
cmakePath.replace(homePath, "${HOME}")
205+
)};`,
185206
"raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.12.1/ninja",
186207
};
187208

@@ -497,9 +518,12 @@ export class NewZephyrProjectPanel {
497518
cmakeVersion: data.cmakeVersion,
498519
});
499520

500-
if (zephyrSetupOutputs !== null) {
501-
this._logger.info(zephyrSetupOutputs);
521+
if (zephyrSetupOutputs === null) {
522+
return;
502523
}
524+
525+
this._logger.info(JSON.stringify(zephyrSetupOutputs));
526+
503527
this._logger.info("Generating new Zephyr Project");
504528

505529
// Create a new directory to put the project in
@@ -593,7 +617,10 @@ export class NewZephyrProjectPanel {
593617
);
594618

595619
// Create settings JSON and write to new folder
596-
const settingsJson = NewZephyrProjectPanel.createSettingsJson();
620+
const settingsJson = NewZephyrProjectPanel.createSettingsJson(
621+
homeDirectory.replaceAll("\\", "/"),
622+
zephyrSetupOutputs?.cmakeExecutable || ""
623+
);
597624
await workspace.fs.writeFile(
598625
Uri.file(settingJsonFile),
599626
Buffer.from(settingsJson)

0 commit comments

Comments
 (0)