Skip to content

Commit

Permalink
Add parse target triple test
Browse files Browse the repository at this point in the history
Fixes microsoft#1916

Signed-off-by: Yonggang Luo <[email protected]>
  • Loading branch information
lygstate committed Jun 26, 2021
1 parent b71fb38 commit faa8ea3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
48 changes: 29 additions & 19 deletions src/triple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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;
}
}
}
Expand All @@ -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;
}
}
}
Expand Down
22 changes: 16 additions & 6 deletions test/unit-tests/kit-scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand Down

0 comments on commit faa8ea3

Please sign in to comment.