Skip to content

Commit

Permalink
Fix #30, c_cpp_properties sdk >=2.0.0 force include moved
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Aug 12, 2024
1 parent 54ec080 commit ce3ae5a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
13 changes: 12 additions & 1 deletion scripts/pico_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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": [
{{
Expand All @@ -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": [],
Expand Down
37 changes: 37 additions & 0 deletions src/utils/semverUtil.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
19 changes: 13 additions & 6 deletions src/utils/vscodeConfigUtil.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand All @@ -216,7 +222,8 @@ async function updateSettingsFile(
);
await config.update(
"raspberry-pi-pico." + SettingsKey.cmakePath,
buildCMakeHomePath(newCMakeVersion), null
buildCMakeHomePath(newCMakeVersion),
null
);
}
}
Expand Down

0 comments on commit ce3ae5a

Please sign in to comment.