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 test from 'ava'
import { parseTriple, getDefaultTargetTriple } from '../parse-triple' import { parseTriple } from '../parse-triple'
const triples = [ const triples = [
{ {
@ -122,23 +120,3 @@ for (const triple of triples) {
t.deepEqual(parseTriple(triple.name), triple.expected) 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 { getNapiConfig } from './consts'
import { debugFactory } from './debug' import { debugFactory } from './debug'
import { createJsBinding } from './js-binding-template' import { createJsBinding } from './js-binding-template'
import { getDefaultTargetTriple, parseTriple } from './parse-triple' import { getHostTargetTriple, parseTriple } from './parse-triple'
import { import {
copyFileAsync, copyFileAsync,
mkdirAsync, mkdirAsync,
@ -241,11 +241,7 @@ export class BuildCommand extends Command {
const binFlag = this.bin ? `--bin ${this.bin}` : '' const binFlag = this.bin ? `--bin ${this.bin}` : ''
const triple = this.targetTripleDir const triple = this.targetTripleDir
? parseTriple(this.targetTripleDir) ? parseTriple(this.targetTripleDir)
: getDefaultTargetTriple( : getHostTargetTriple()
execSync('rustup show active-toolchain', {
env: process.env,
}).toString('utf8'),
)
debug(`Current triple is: ${chalk.green(triple.raw)}`) debug(`Current triple is: ${chalk.green(triple.raw)}`)
const pFlag = this.project ? `-p ${this.project}` : '' const pFlag = this.project ? `-p ${this.project}` : ''
const externalFlags = [ 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') export function getHostTargetTriple(): PlatformDetail {
// stable-x86_64-apple-darwin (default) const host = execSync(`rustc -vV`, {
// 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`, {
env: process.env, env: process.env,
}) })
.toString('utf8') .toString('utf8')
.split('\n') .split('\n')
.map((line) => .find((line) => line.startsWith('host: '))
line const triple = host?.slice('host: '.length)
.trim()
// remove (installed) from x86_64-apple-darwin (installed)
.replace(/\(.*?\)/, '')
.trim(),
)
.filter((line) => line.length)
const triple = allTriples.find((triple) => currentTriple.indexOf(triple) > -1)
if (!triple) { 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) return parseTriple(triple)
} }