Файли
openidea/src/platform/cmakeSettings.ts
T
Yohann CATHERINEтаyohannlog 5debc5297b feat(cmake): CLion-style profiles, presets and toolchains (#7262)
CMake gets CLion-style profiles (build type, generator, build directory, CMake options, environment), CMake presets as profiles, and toolchains (compilers, build tool, debugger, environment script). Only the active profile is detected; target ids have their own namespace, configurations from the older per-profile ids are migrated with their suppressions and before-launch references, the toolchain's build program is passed only to a compatible generator, and a profile switch refreshes the derived executable and build arguments while keeping user edits.

Co-authored-by: yohannlog <[email protected]>
2026-12-02 15:35:21 +08:00

98 рядки
3.2 KiB
TypeScript

import type { CMakeProfile, Toolchain, ToolchainKind } from "@/bindings";
/** A profile as saved in the settings; `preset` only exists on the profiles made from presets. */
export type CMakeProfileValid = Required<Omit<CMakeProfile, "preset">>;
export interface CMakeValid {
profiles?: CMakeProfileValid[];
ignorePresets?: boolean;
/** Presets are listed disabled until named here, as in CLion. */
enabledPresets?: string[];
/** The profile the toolbar switcher picked; empty = the first enabled one. */
activeProfile?: string;
}
export type ToolchainValid = Required<Toolchain>;
export const DEFAULT_TOOLCHAIN: ToolchainValid = {
name: "Default",
kind: "system",
path: "",
cmake: "",
buildTool: "",
cCompiler: "",
cxxCompiler: "",
debugger: "",
};
const TOOLCHAIN_KINDS: ToolchainKind[] = ["system", "mingw"];
export function validateToolchains(raw: unknown): ToolchainValid[] {
if (!Array.isArray(raw)) return [];
const string = (value: unknown) => (typeof value === "string" ? value : "");
return raw
.filter((t) => t && typeof t === "object")
.map((t) => ({
name: string(t.name),
kind: TOOLCHAIN_KINDS.includes(t.kind) ? t.kind : "system",
path: string(t.path),
cmake: string(t.cmake),
buildTool: string(t.buildTool),
cCompiler: string(t.cCompiler),
cxxCompiler: string(t.cxxCompiler),
debugger: string(t.debugger),
}));
}
export const DEFAULT_CMAKE_PROFILE: CMakeProfileValid = {
name: "Debug",
buildType: "Debug",
generator: "",
cmakeArgs: [],
buildDir: "",
buildArgs: [],
env: {},
enabled: true,
toolchain: "",
};
export function validateCMake(raw: unknown): CMakeValid {
const cmake = (raw ?? {}) as {
profiles?: unknown;
ignorePresets?: unknown;
enabledPresets?: unknown;
activeProfile?: unknown;
};
const presets: CMakeValid = {};
if (typeof cmake.ignorePresets === "boolean") presets.ignorePresets = cmake.ignorePresets;
if (Array.isArray(cmake.enabledPresets)) {
presets.enabledPresets = cmake.enabledPresets.filter((name): name is string => typeof name === "string");
}
if (typeof cmake.activeProfile === "string") presets.activeProfile = cmake.activeProfile;
const profiles = cmake.profiles;
if (!Array.isArray(profiles)) return presets;
const string = (value: unknown, fallback = "") => (typeof value === "string" ? value : fallback);
const args = (value: unknown): string[] =>
Array.isArray(value) ? value.filter((arg): arg is string => typeof arg === "string") : [];
return {
...presets,
profiles: profiles
.filter((p) => p && typeof p === "object")
.map((p) => ({
name: string(p.name, "Debug"),
buildType: string(p.buildType, "Debug"),
generator: string(p.generator),
cmakeArgs: args(p.cmakeArgs),
buildDir: string(p.buildDir),
buildArgs: args(p.buildArgs),
enabled: p.enabled !== false,
toolchain: string(p.toolchain),
env: Object.fromEntries(
Object.entries(p.env && typeof p.env === "object" ? p.env : {}).filter(
(entry): entry is [string, string] => typeof entry[1] === "string",
),
),
})),
};
}