fix(cli): parse host target triple from rustc -vV (#1191)

This commit is contained in:
messense 2022-05-22 13:43:35 +08:00 committed by GitHub
parent 5be415d3d9
commit beb75111fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 48 deletions

View file

@ -1,8 +1,6 @@
import { platform } from 'os'
import test from 'ava'
import { parseTriple, getDefaultTargetTriple } from '../parse-triple'
import { parseTriple } from '../parse-triple'
const triples = [
{
@ -122,23 +120,3 @@ for (const triple of triples) {
t.deepEqual(parseTriple(triple.name), triple.expected)
})
}
const MaybeTest =
process.arch !== 'x64' && platform() === 'linux' ? test.skip : test
MaybeTest('should parse default triple from rustup show active', (t) => {
t.deepEqual(
getDefaultTargetTriple(
`x86_64-unknown-linux-gnu (directory override for '/home/runner/work/fast-escape/fast-escape')`,
),
parseTriple('x86_64-unknown-linux-gnu'),
)
t.deepEqual(
getDefaultTargetTriple(`stable-x86_64-apple-darwin (default)`),
parseTriple(`x86_64-apple-darwin`),
)
t.deepEqual(
getDefaultTargetTriple(`nightly-2020-08-29-x86_64-apple-darwin (default)`),
parseTriple('x86_64-apple-darwin'),
)
})

View file

@ -12,7 +12,7 @@ import toml from 'toml'
import { getNapiConfig } from './consts'
import { debugFactory } from './debug'
import { createJsBinding } from './js-binding-template'
import { getDefaultTargetTriple, parseTriple } from './parse-triple'
import { getHostTargetTriple, parseTriple } from './parse-triple'
import {
copyFileAsync,
mkdirAsync,
@ -241,11 +241,7 @@ export class BuildCommand extends Command {
const binFlag = this.bin ? `--bin ${this.bin}` : ''
const triple = this.targetTripleDir
? parseTriple(this.targetTripleDir)
: getDefaultTargetTriple(
execSync('rustup show active-toolchain', {
env: process.env,
}).toString('utf8'),
)
: getHostTargetTriple()
debug(`Current triple is: ${chalk.green(triple.raw)}`)
const pFlag = this.project ? `-p ${this.project}` : ''
const externalFlags = [

View file

@ -98,30 +98,16 @@ export function parseTriple(rawTriple: string): PlatformDetail {
}
}
// x86_64-unknown-linux-gnu (directory override for '/home/runner/work/fast-escape/fast-escape')
// stable-x86_64-apple-darwin (default)
// nightly-2020-08-29-x86_64-apple-darwin (default)
export function getDefaultTargetTriple(rustcfg: string): PlatformDetail {
const currentTriple = rustcfg
.trim()
.replace(/\(.*?\)/, '')
.trim()
const allTriples = execSync(`rustup target list`, {
export function getHostTargetTriple(): PlatformDetail {
const host = execSync(`rustc -vV`, {
env: process.env,
})
.toString('utf8')
.split('\n')
.map((line) =>
line
.trim()
// remove (installed) from x86_64-apple-darwin (installed)
.replace(/\(.*?\)/, '')
.trim(),
)
.filter((line) => line.length)
const triple = allTriples.find((triple) => currentTriple.indexOf(triple) > -1)
.find((line) => line.startsWith('host: '))
const triple = host?.slice('host: '.length)
if (!triple) {
throw new TypeError(`Can not parse target triple from ${currentTriple}`)
throw new TypeError(`Can not parse target triple from host`)
}
return parseTriple(triple)
}