Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support explicit typing in cmake.configureSettings #2868

Merged
merged 3 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Improvements:
- Use upper case drive letters on Windows for `cmake.sourceDirectory`. [PR #2665](https://github.com/microsoft/vscode-cmake-tools/pull/2665) [@Danielmelody](https://github.com/Danielmelody)
- Custom browse configuration should not include (redundant) per-file arguments. [#2645](https://github.com/microsoft/vscode-cmake-tools/issues/2645)
- Support optional generator in `configurePresets` for version 3 and higher. [#2734](https://github.com/microsoft/vscode-cmake-tools/issues/2734) [@jochil](https://github.com/jochil)
- Add a public API for extension authors that depend on CMake Tools. [#494]((https://github.com/microsoft/vscode-cmake-tools/issues/494)
- Add a public API for extension authors that depend on CMake Tools. [#494](https://github.com/microsoft/vscode-cmake-tools/issues/494)
- Support explicit typing in `cmake.configureSettings`. [#1457](https://github.com/microsoft/vscode-cmake-tools/issues/1457)

Bug Fixes:
- Fix warning message that appears when using a default build preset with a multi-config generator. [#2353](https://github.com/microsoft/vscode-cmake-tools/issues/2353)
Expand Down
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,38 @@
"cmake.configureSettings": {
"type": "object",
"default": {},
"additionalProperties": {
"oneOf": [
{
"type": "string"
},
{
"type": "boolean"
},
{
"type": "number"
},
{
"type": "array",
"items": {
"type": "string"
}
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [ "", "BOOL", "STRING", "FILEPATH", "PATH"]
},
"value": {
}
},
"additionalProperties": false,
"required": ["type", "value"]
}
]
},
"description": "%cmake-tools.configuration.cmake.configureSettings.description%",
"scope": "resource"
},
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface ExtensionConfigurationSettings {
saveBeforeBuild: boolean;
buildBeforeRun: boolean;
clearOutputBeforeBuild: boolean;
configureSettings: { [key: string]: any };
configureSettings: { [key: string]: boolean | number | string | string[] | util.CMakeValue };
cacheInit: string | string[] | null;
preferredGenerators: string[];
generator: string | null;
Expand Down Expand Up @@ -257,7 +257,7 @@ export class ConfigurationReader implements vscode.Disposable {
get clearOutputBeforeBuild(): boolean {
return !!this.configData.clearOutputBeforeBuild;
}
get configureSettings(): any {
get configureSettings(): {[key: string]: boolean | number | string | string[] | util.CMakeValue} {
return this.configData.configureSettings;
}
get cacheInit() {
Expand Down
16 changes: 10 additions & 6 deletions src/drivers/cmakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1467,10 +1467,13 @@ export abstract class CMakeDriver implements vscode.Disposable {
private generateCMakeSettingsFlags(): string[] {
const settingMap: { [key: string]: util.CMakeValue } = {};

util.objectPairs(this.config.configureSettings)
.forEach(([key, value]) => settingMap[key] = util.cmakeify(value as string));
util.objectPairs(this._variantConfigureSettings)
.forEach(([key, value]) => settingMap[key] = util.cmakeify(value as string));
try {
util.objectPairs(this.config.configureSettings).forEach(([key, value]) => settingMap[key] = util.cmakeify(value));
} catch (e: any) {
log.error(e.message);
throw e;
}
util.objectPairs(this._variantConfigureSettings).forEach(([key, value]) => settingMap[key] = util.cmakeify(value as string));
if (this._variantLinkage !== null) {
settingMap.BUILD_SHARED_LIBS = util.cmakeify(this._variantLinkage === 'shared');
}
Expand Down Expand Up @@ -1503,12 +1506,12 @@ export abstract class CMakeDriver implements vscode.Disposable {
log.debug(localize('using.compilers.in.for.configure', 'Using compilers in {0} for configure', this._kit.name));
for (const lang in this._kit.compilers) {
const compiler = this._kit.compilers[lang];
settingMap[`CMAKE_${lang}_COMPILER`] = { type: 'FILEPATH', value: compiler } as util.CMakeValue;
settingMap[`CMAKE_${lang}_COMPILER`] = { type: 'FILEPATH', value: compiler };
}
}
if (this._kit.toolchainFile) {
log.debug(localize('using.cmake.toolchain.for.configure', 'Using CMake toolchain {0} for configuring', this._kit.name));
settingMap.CMAKE_TOOLCHAIN_FILE = { type: 'FILEPATH', value: this._kit.toolchainFile } as util.CMakeValue;
settingMap.CMAKE_TOOLCHAIN_FILE = { type: 'FILEPATH', value: this._kit.toolchainFile };
}
if (this._kit.cmakeSettings) {
util.objectPairs(this._kit.cmakeSettings)
Expand All @@ -1518,6 +1521,7 @@ export abstract class CMakeDriver implements vscode.Disposable {
return util.objectPairs(settingMap).map(([key, value]) => {
switch (value.type) {
case 'UNKNOWN':
case '':
return `-D${key}=${value.value}`;
default:
return `-D${key}:${value.type}=${value.value}`;
Expand Down
11 changes: 7 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,19 @@ export function product<T>(arrays: T[][]): T[][] {
}

export interface CMakeValue {
type: ('UNKNOWN' | 'BOOL' | 'STRING' | 'FILEPATH'); // There are more types, but we don't care ATM
type: ('UNKNOWN' | 'BOOL' | 'STRING' | 'FILEPATH' | 'PATH' | ''); // There are more types, but we don't care ATM
value: string;
}

export function cmakeify(value: (string | boolean | number | string[])): CMakeValue {
export function cmakeify(value: (string | boolean | number | string[] | CMakeValue)): CMakeValue {
const ret: CMakeValue = {
type: 'UNKNOWN',
value: ''
};
if (value === true || value === false) {
ret.type = 'BOOL';
ret.value = value ? 'TRUE' : 'FALSE';
} else if (typeof (value) === 'string') {
} else if (isString(value)) {
ret.type = 'STRING';
ret.value = replaceAll(value, ';', '\\;');
} else if (typeof value === 'number') {
Expand All @@ -267,8 +267,11 @@ export function cmakeify(value: (string | boolean | number | string[])): CMakeVa
} else if (value instanceof Array) {
ret.type = 'STRING';
ret.value = value.join(';');
} else if (Object.getOwnPropertyNames(value).filter(e => e === 'type' || e === 'value').length === 2) {
ret.type = value.type;
ret.value = value.value;
} else {
throw new Error(`Invalid value to convert to cmake value: ${value}`);
throw new Error(localize('invalid.value', 'Invalid value to convert to cmake value: {0}', JSON.stringify(value)));
}
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ suite('Variable Substitution', () => {
workspaceRootFolderName: '${workspaceRootFolderName}',
workspaceFolderBasename: '${workspaceFolderBasename}',
generator: '${generator}',
userHome: '${userHome}'
userHome: '${userHome}',
test1: true,
test2: 123,
test3: ["1", "2", "3"],
test4: {"type": "PATH", "value": "/usr/bin"}
},
installPrefix: '${workspaceFolder}/build/dist'
});
Expand Down Expand Up @@ -133,6 +137,21 @@ suite('Variable Substitution', () => {
'[cmakeInstallPrefix] substitution incorrect');
expect(typeof cacheEntry.value).to.eq('string', '[cmakeInstallPrefix] unexpected cache entry value type');

cacheEntry = cache.get('test1') as CacheEntry;
expect(cacheEntry.type).to.eq(CacheEntryType.Bool);
expect(cacheEntry.value).to.eq(true);

cacheEntry = cache.get('test2') as CacheEntry;
expect(cacheEntry.type).to.eq(CacheEntryType.String);
expect(cacheEntry.value).to.eq('123');

cacheEntry = cache.get('test3') as CacheEntry;
expect(cacheEntry.type).to.eq(CacheEntryType.String);
expect(cacheEntry.value).to.eq('1;2;3');

cacheEntry = cache.get('test4') as CacheEntry;
expect(cacheEntry.type).to.eq(CacheEntryType.Path);
expect(cacheEntry.value).to.eq('/usr/bin');
}).timeout(100000);

test('Check substitution for variant names', async () => {
Expand Down