From faa8ea3b7159d082cb4cfec89397e2d48c3e4908 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Sat, 26 Jun 2021 13:58:34 +0800 Subject: [PATCH] Add parse target triple test Fixes https://github.com/microsoft/vscode-cmake-tools/issues/1916 Signed-off-by: Yonggang Luo --- src/triple.ts | 48 +++++++++++++++++++------------- test/unit-tests/kit-scan.test.ts | 22 +++++++++++---- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/triple.ts b/src/triple.ts index 627629dc0a..791b96de49 100644 --- a/src/triple.ts +++ b/src/triple.ts @@ -89,14 +89,27 @@ const TriplePossibleABI: {[index: string]: RegExp} = { eabisim: /^eabisim$/ }; -const TriplePossibleLibC: {[index: string]: RegExp} = { - musl: /^musl$/, - glibc: /^(gnu|msys|cygwin)$/, - msvcrt: /^msvc$/, - mingw: /^(mingw32|mingw|mingw64|w64)/ -// 'llvm': /^llvm$/, TODO:https://github.com/llvm/llvm-project/tree/master/libc/src/stdio -// otherwise system libc -}; +const TriplePossibleLibC: {key: string; regexp: RegExp}[] = [ + /* mingw have higher priority than glibc */ + { + key: 'mingw', + regexp: /^(mingw32|mingw|mingw64|w64)/ + }, + { + key: 'musl', + regexp: /^musl$/ + }, + { + key: 'glibc', + regexp: /^(gnu|msys|cygwin)$/ + }, + { + key: 'msvcrt', + regexp: /^msvc$/ + } + // TODO llvm-libc :https://github.com/llvm/llvm-project/tree/main/libc + // otherwise system libc +]; export function computeTargetTriple(target: TargetTriple): string { let triple = target.targetArch; @@ -128,8 +141,7 @@ export function parseTargetTriple(triple: string): TargetTriple | undefined { elementToSkip.push(tripleElement); if (foundArch === "unknow") { foundArch = key; - } else if (foundArch !== key) { - return undefined; + break; } } } @@ -139,9 +151,9 @@ export function parseTargetTriple(triple: string): TargetTriple | undefined { if (osReg.exec(tripleElement) !== null) { elementToSkip.push(tripleElement); if (foundOs === "unknow" || foundOs === 'none') { + // os other than none have higher priority + // so we not break foundOs = key; - } else if (foundOs !== key && key !== 'none') { - return undefined; } } } @@ -152,20 +164,18 @@ export function parseTargetTriple(triple: string): TargetTriple | undefined { elementToSkip.push(tripleElement); if (foundAbi === "unknow") { foundAbi = key; - } else if (foundAbi !== key) { - return undefined; + break; } } } - for (const key of Object.keys(TriplePossibleLibC)) { - const libcReg = TriplePossibleLibC[key]; + for (const possibleLibC of TriplePossibleLibC) { + const libcReg = possibleLibC.regexp; if (libcReg.exec(tripleElement) !== null) { elementToSkip.push(tripleElement); if (foundLibc === "unknow") { - foundLibc = key; - } else if (foundLibc !== key) { - return undefined; + foundLibc = possibleLibC.key; + break; } } } diff --git a/test/unit-tests/kit-scan.test.ts b/test/unit-tests/kit-scan.test.ts index 2af44f3f3e..33473a9e8b 100644 --- a/test/unit-tests/kit-scan.test.ts +++ b/test/unit-tests/kit-scan.test.ts @@ -10,7 +10,7 @@ import * as triple from '../../src/triple'; import {fs} from '../../src/pr'; import {CMakeTools} from '@cmt/cmake-tools'; -import {clearExistingKitConfigurationFile, DefaultEnvironment,} from '@test/util'; +import {clearExistingKitConfigurationFile, DefaultEnvironment} from '@test/util'; const here = __dirname; function getTestRootFilePath(filename: string): string { @@ -66,6 +66,19 @@ suite('Kits scan test', async () => { .to.equal('arm-none-linux-gnueabi'); expect(triple.findTargetTriple('Target: arm-linux-gnueabihf')) .to.equal('arm-linux-gnueabihf'); + expect(triple.findTargetTriple('Target: x86_64-w64-windows-gnu')) + .to.equal('x86_64-w64-windows-gnu'); + }); + + test('parse target triple', () => { + expect(triple.parseTargetTriple('x86_64-w64-windows-gnu')).equal({ + triple: 'x86_64-w64-windows-gnu', + targetOs: 'win32', + targetArch: 'x64', + vendors: [], + abi: 'pe', + libc: 'mingw' + }); }); test('Detect system kits never throws', @@ -125,10 +138,8 @@ suite('Kits scan test', async () => { expect(compkit!.name).to.eq('Clang 8.1.0 x86_64-apple-darwin16.7.0'); }); - test('Detect an MinGW compiler file on linux', async () => { - if (process.platform === 'win32') - return; + if (process.platform === 'win32') { return; } await disableMingwMake(); @@ -151,8 +162,7 @@ suite('Kits scan test', async () => { // Test is broken, the use of env path has changed test.skip('Detect an MinGW compiler file on windows', async () => { - if (process.platform !== 'win32') - return; + if (process.platform !== 'win32') { return; } const compiler = path.join(fakebin, 'mingw32-gcc'); const compkit = await kit.kitIfCompiler(compiler);