From 64e566dbb98fd28d27445dee8ff88694c5b42f27 Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Fri, 9 Aug 2024 12:26:42 +0100 Subject: [PATCH] Fix #29 - remove CMakeCache.txt before generating if build dir has been moved --- src/utils/cmakeUtil.mts | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/utils/cmakeUtil.mts b/src/utils/cmakeUtil.mts index 301a5d9a..ec917d80 100644 --- a/src/utils/cmakeUtil.mts +++ b/src/utils/cmakeUtil.mts @@ -1,10 +1,10 @@ import { exec } from "child_process"; import { workspace, type Uri, window, ProgressLocation } from "vscode"; import { showRequirementsNotMetErrorMessage } from "./requirementsUtil.mjs"; -import { dirname, join } from "path"; +import { dirname, join, resolve } from "path"; import Settings from "../settings.mjs"; import { HOME_VAR, SettingsKey } from "../settings.mjs"; -import { readFileSync } from "fs"; +import { existsSync, readFileSync, rmSync } from "fs"; import Logger from "../logger.mjs"; import { readFile, writeFile } from "fs/promises"; import { rimraf, windows as rimrafWindows } from "rimraf"; @@ -110,6 +110,35 @@ export async function configureCmakeNinja(folder: Uri): Promise { return false; } + if (existsSync(join(folder.fsPath, "build", "CMakeCache.txt"))) { + // check if the build directory has been moved + + const buildDir = join(folder.fsPath, "build"); + + const cacheBuildDir = cmakeGetPicoVar( + join(buildDir, "CMakeCache.txt"), + "CMAKE_CACHEFILE_DIR" + ); + + if (cacheBuildDir !== null) { + let p1 = resolve(buildDir); + let p2 = resolve(cacheBuildDir); + if (process.platform === "win32") { + p1 = p1.toLowerCase(); + p2 = p2.toLowerCase(); + } + + if (p1 !== p2) { + console.warn( + `Build directory has been moved from ${p1} to ${p2}` + + ` - Deleting CMakeCache.txt and regenerating.` + ); + + rmSync(join(buildDir, "CMakeCache.txt")); + } + } + } + try { // check if CMakeLists.txt exists in the root folder await workspace.fs.stat(