1 import { exec } from "child_process";
2 import { workspace, type Uri, window, ProgressLocation } from "vscode";
5 showRquirementsNotMetErrorMessage,
6 } from "./requirementsUtil.mjs";
7 import { join } from "path";
8 import { getSDKAndToolchainPath } from "./picoSDKUtil.mjs";
9 import type Settings from "../settings.mjs";
10 import { SettingsKey } from "../settings.mjs";
11 import { readFileSync, writeFileSync } from "fs";
12 import Logger from "../logger.mjs";
14 export async function configureCmakeNinja(
20 // check if CMakeLists.txt exists in the root folder
21 await workspace.fs.stat(
22 folder.with({ path: join(folder.fsPath, "CMakeLists.txt") })
25 const rquirementsAvailable = await checkForRequirements(settings);
27 if (!rquirementsAvailable) {
28 void showRquirementsNotMetErrorMessage();
33 void window.withProgress(
35 location: ProgressLocation.Notification,
37 title: "Configuring CMake...",
39 // eslint-disable-next-line @typescript-eslint/require-await
40 async (progress, token) => {
41 const sdkPaths = await getSDKAndToolchainPath(settings);
42 const cmake = settings.getString(SettingsKey.cmakePath) || "cmake";
44 // TODO: analyze command result
45 // TODO: option for the user to choose the generator
47 `${cmake} -DCMAKE_BUILD_TYPE=Debug ` +
48 `-G Ninja -B ./build "${folder.fsPath}"`,
52 ...(process.env as { [key: string]: string }),
53 // eslint-disable-next-line @typescript-eslint/naming-convention
54 [`PICO_SDK_PATH_${envSuffix}`]: sdkPaths?.[0],
55 // eslint-disable-next-line @typescript-eslint/naming-convention
56 [`PICO_TOOLCHAIN_PATH_${envSuffix}`]: sdkPaths?.[1],
61 child.on("error", err => {
65 //child.stdout?.on("data", data => {});
66 child.on("close", () => {
67 progress.report({ increment: 100 });
69 child.on("exit", code => {
71 console.error(`CMake exited with code ${code ?? "unknown"}`);
73 progress.report({ increment: 100 });
76 token.onCancellationRequested(() => {
88 export function cmakeUpdateSuffix(
89 cmakeFilePath: string,
93 /^set\(PICO_SDK_PATH \$ENV\{PICO_SDK_PATH_[A-Za-z0-9]+\}\)$/m;
94 const toolchainPathRegex =
95 /^set\(PICO_TOOLCHAIN_PATH \$ENV\{PICO_TOOLCHAIN_PATH_[A-Za-z0-9]+\}\)$/m;
98 const content = readFileSync(cmakeFilePath, "utf8");
99 const modifiedContent = content
102 `set(PICO_SDK_PATH $ENV{PICO_SDK_PATH_${newSuffix}})`
106 `set(PICO_TOOLCHAIN_PATH $ENV{PICO_TOOLCHAIN_PATH_${newSuffix}})`
109 writeFileSync(cmakeFilePath, modifiedContent, "utf8");
110 Logger.log("Updated envSuffix in CMakeLists.txt successfully.");
112 Logger.log("Updating cmake envSuffix!");