]> Git Repo - pico-vscode.git/blob - src/utils/cmakeUtil.mts
Add global env vars and compile button
[pico-vscode.git] / src / utils / cmakeUtil.mts
1 import { exec } from "child_process";
2 import { workspace, type Uri, window, ProgressLocation } from "vscode";
3 import {
4   checkForRequirements,
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";
13
14 export async function configureCmakeNinja(
15   folder: Uri,
16   settings: Settings,
17   envSuffix: string
18 ): Promise<boolean> {
19   try {
20     // check if CMakeLists.txt exists in the root folder
21     await workspace.fs.stat(
22       folder.with({ path: join(folder.fsPath, "CMakeLists.txt") })
23     );
24
25     const rquirementsAvailable = await checkForRequirements(settings);
26
27     if (!rquirementsAvailable) {
28       void showRquirementsNotMetErrorMessage();
29
30       return false;
31     }
32
33     void window.withProgress(
34       {
35         location: ProgressLocation.Notification,
36         cancellable: true,
37         title: "Configuring CMake...",
38       },
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";
43
44         // TODO: analyze command result
45         // TODO: option for the user to choose the generator
46         const child = exec(
47           `${cmake} -DCMAKE_BUILD_TYPE=Debug ` +
48             `-G Ninja -B ./build "${folder.fsPath}"`,
49           {
50             cwd: folder.fsPath,
51             env: {
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],
57             },
58           }
59         );
60
61         child.on("error", err => {
62           console.error(err);
63         });
64
65         //child.stdout?.on("data", data => {});
66         child.on("close", () => {
67           progress.report({ increment: 100 });
68         });
69         child.on("exit", code => {
70           if (code !== 0) {
71             console.error(`CMake exited with code ${code ?? "unknown"}`);
72           }
73           progress.report({ increment: 100 });
74         });
75
76         token.onCancellationRequested(() => {
77           child.kill();
78         });
79       }
80     );
81
82     return true;
83   } catch (e) {
84     return false;
85   }
86 }
87
88 export function cmakeUpdateSuffix(
89   cmakeFilePath: string,
90   newSuffix: string
91 ): void {
92   const sdkPathRegex =
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;
96
97   try {
98     const content = readFileSync(cmakeFilePath, "utf8");
99     const modifiedContent = content
100       .replace(
101         sdkPathRegex,
102         `set(PICO_SDK_PATH $ENV{PICO_SDK_PATH_${newSuffix}})`
103       )
104       .replace(
105         toolchainPathRegex,
106         `set(PICO_TOOLCHAIN_PATH $ENV{PICO_TOOLCHAIN_PATH_${newSuffix}})`
107       );
108
109     writeFileSync(cmakeFilePath, modifiedContent, "utf8");
110     Logger.log("Updated envSuffix in CMakeLists.txt successfully.");
111   } catch (error) {
112     Logger.log("Updating cmake envSuffix!");
113   }
114 }
This page took 0.038865 seconds and 4 git commands to generate.