From ce3ae5ab42f65d58fa436919a65f5dfc15984a4e Mon Sep 17 00:00:00 2001 From: paulober <44974737+paulober@users.noreply.github.com> Date: Mon, 12 Aug 2024 16:06:08 +0200 Subject: [PATCH] Fix #30, c_cpp_properties sdk >=2.0.0 force include moved Signed-off-by: paulober <44974737+paulober@users.noreply.github.com> --- scripts/pico_project.py | 13 +++++++++++- src/utils/semverUtil.mts | 37 ++++++++++++++++++++++++++++++++++ src/utils/vscodeConfigUtil.mts | 19 +++++++++++------ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/scripts/pico_project.py b/scripts/pico_project.py index 768c6b4..3eb4363 100644 --- a/scripts/pico_project.py +++ b/scripts/pico_project.py @@ -400,6 +400,16 @@ def propertiesToolchainPath(toolchainVersion, force_windows=False, force_non_win def codeToolchainPath(toolchainVersion): return f"${{userHome}}{relativeToolchainPath(toolchainVersion)}" +def semver_compare_ge(first, second): + """ Compare two semantic version strings and return True if the first is greater or equal to the second """ + first_tuple = tuple(map(int, first.split("."))) + second_tuple = tuple(map(int, second.split("."))) + + assert len(first_tuple) == 3 + assert len(second_tuple) == 3 + + return first_tuple >= second_tuple + def CheckPrerequisites(): global isMac, isWindows, isx86 isMac = (platform.system() == 'Darwin') @@ -877,6 +887,7 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger, }} ''' + base_headers_folder_name = "pico_base_headers" if semver_compare_ge(sdkVersion, "2.0.0") else "pico_base" properties = f'''{{ "configurations": [ {{ @@ -886,7 +897,7 @@ def generateProjectFiles(projectPath, projectName, sdkPath, projects, debugger, "{codeSdkPath(sdkVersion)}/**" ], "forcedInclude": [ - "{codeSdkPath(sdkVersion)}/src/common/pico_base/include/pico.h", + "{codeSdkPath(sdkVersion)}/src/common/{base_headers_folder_name}/include/pico.h", "${{workspaceFolder}}/build/generated/pico_base/pico/config_autogen.h" ], "defines": [], diff --git a/src/utils/semverUtil.mts b/src/utils/semverUtil.mts index 0d7eaa9..0c6609d 100644 --- a/src/utils/semverUtil.mts +++ b/src/utils/semverUtil.mts @@ -34,3 +34,40 @@ export function compare(a: string, b: string): number { return 0; } + +/** + * Function to compare two semantic version strings. + * Returns true if the first version is greater than or equal to the second version. + * + * @param first - The first version string. + * @param second - The second version string. + * @returns boolean - True if the first version is greater than or equal to the second version. + * Defaults to false if the version strings are invalid. (behaviour will be removed in + * a future version) + */ +export function compareGe(first: string, second: string): boolean { + // Split the version strings into parts and convert them to numbers + const firstParts = first.split(".").map(Number); + const secondParts = second.split(".").map(Number); + + // Ensure both versions have three parts + if (firstParts.length !== 3 || secondParts.length !== 3) { + // TODO: use proper error handling in switch SDK + /*throw new Error( + "Version strings must have exactly three parts (major.minor.patch)" + );*/ + return false; + } + + // Compare the version parts + for (let i = 0; i < 3; i++) { + if (firstParts[i] > secondParts[i]) { + return true; + } else if (firstParts[i] < secondParts[i]) { + return false; + } + } + + // If all parts are equal + return true; +} diff --git a/src/utils/vscodeConfigUtil.mts b/src/utils/vscodeConfigUtil.mts index e0498e4..bc472f5 100644 --- a/src/utils/vscodeConfigUtil.mts +++ b/src/utils/vscodeConfigUtil.mts @@ -4,6 +4,7 @@ import { join } from "path"; import { SettingsKey } from "../settings.mjs"; import { type WorkspaceConfiguration, workspace } from "vscode"; import { dirname } from "path/posix"; +import { compareGe } from "./semverUtil.mjs"; interface Configuration { includePath: string[]; @@ -38,10 +39,13 @@ async function updateCppPropertiesFile( config.forcedInclude = config.forcedInclude.filter( item => !item.startsWith("${userHome}/.pico-sdk") ); + const baseHeadersFolderName = compareGe(newSDKVersion, "2.0.0") + ? "pico_base_headers" + : "pico_base"; // Add the new pico-sdk forcedInclude config.forcedInclude.push( `\${userHome}/.pico-sdk/sdk/${newSDKVersion}` + - "/src/common/pico_base/include/pico.h" + `/src/common/${baseHeadersFolderName}/include/pico.h` ); // Update the compilerPath @@ -137,15 +141,16 @@ async function updateSettingsFile( // PATH // replace env: with env_ before splitting at : - const oldPath = currentValue[key.includes("windows") ? "Path" : "PATH"] - .replaceAll("${env:", "${env_"); + const oldPath = currentValue[ + key.includes("windows") ? "Path" : "PATH" + ].replaceAll("${env:", "${env_"); Logger.log(`Oldpath ${oldPath}`); const pathList = oldPath.split(key.includes("windows") ? ";" : ":"); let toolchainIdx = -1; let cmakeIdx = -1; let ninjaIdx = -1; - for (let i=0; i < pathList.length; i++) { + for (let i = 0; i < pathList.length; i++) { pathList[i] = pathList[i].replaceAll("${env_", "${env:"); Logger.log(pathList[i]); const item = pathList[i]; @@ -205,7 +210,8 @@ async function updateSettingsFile( if (newNinjaVersion) { await config.update( "raspberry-pi-pico." + SettingsKey.ninjaPath, - buildNinjaHomePath(newNinjaVersion), null + buildNinjaHomePath(newNinjaVersion), + null ); } if (newCMakeVersion) { @@ -216,7 +222,8 @@ async function updateSettingsFile( ); await config.update( "raspberry-pi-pico." + SettingsKey.cmakePath, - buildCMakeHomePath(newCMakeVersion), null + buildCMakeHomePath(newCMakeVersion), + null ); } }