@@ -226,16 +226,13 @@ def format_build_time(duration):
226
226
227
227
def default_build_triple (verbose ):
228
228
"""Build triple as in LLVM"""
229
- # If the user already has a host build triple with an existing `rustc`
230
- # install, use their preference. This fixes most issues with Windows builds
231
- # being detected as GNU instead of MSVC.
229
+ # If we're on Windows and have an existing `rustc` toolchain, use `rustc --version --verbose`
230
+ # to find our host target triple. This fixes an issue with Windows builds being detected
231
+ # as GNU instead of MSVC.
232
+ # Otherwise, detect it via `uname`
232
233
default_encoding = sys .getdefaultencoding ()
233
234
234
- if sys .platform == 'darwin' :
235
- if verbose :
236
- print ("not using rustc detection as it is unreliable on macOS" , file = sys .stderr )
237
- print ("falling back to auto-detect" , file = sys .stderr )
238
- else :
235
+ if platform_is_win32 ():
239
236
try :
240
237
version = subprocess .check_output (["rustc" , "--version" , "--verbose" ],
241
238
stderr = subprocess .DEVNULL )
@@ -253,19 +250,17 @@ def default_build_triple(verbose):
253
250
print ("falling back to auto-detect" , file = sys .stderr )
254
251
255
252
required = not platform_is_win32 ()
256
- ostype = require (["uname" , "-s" ], exit = required )
257
- cputype = require (['uname' , '-m' ], exit = required )
253
+ uname = require (["uname" , "-smp" ], exit = required )
258
254
259
255
# If we do not have `uname`, assume Windows.
260
- if ostype is None or cputype is None :
256
+ if uname is None :
261
257
return 'x86_64-pc-windows-msvc'
262
258
263
- ostype = ostype .decode (default_encoding )
264
- cputype = cputype .decode (default_encoding )
259
+ kernel , cputype , processor = uname .decode (default_encoding ).split ()
265
260
266
261
# The goal here is to come up with the same triple as LLVM would,
267
262
# at least for the subset of platforms we're willing to target.
268
- ostype_mapper = {
263
+ kerneltype_mapper = {
269
264
'Darwin' : 'apple-darwin' ,
270
265
'DragonFly' : 'unknown-dragonfly' ,
271
266
'FreeBSD' : 'unknown-freebsd' ,
@@ -275,17 +270,18 @@ def default_build_triple(verbose):
275
270
}
276
271
277
272
# Consider the direct transformation first and then the special cases
278
- if ostype in ostype_mapper :
279
- ostype = ostype_mapper [ostype ]
280
- elif ostype == 'Linux' :
281
- os_from_sp = subprocess .check_output (
282
- ['uname' , '-o' ]).strip ().decode (default_encoding )
283
- if os_from_sp == 'Android' :
284
- ostype = 'linux-android'
273
+ if kernel in kerneltype_mapper :
274
+ kernel = kerneltype_mapper [kernel ]
275
+ elif kernel == 'Linux' :
276
+ # Apple doesn't support `-o` so this can't be used in the combined
277
+ # uname invocation above
278
+ ostype = require (["uname" , "-o" ], exit = required ).decode (default_encoding )
279
+ if ostype == 'Android' :
280
+ kernel = 'linux-android'
285
281
else :
286
- ostype = 'unknown-linux-gnu'
287
- elif ostype == 'SunOS' :
288
- ostype = 'pc-solaris'
282
+ kernel = 'unknown-linux-gnu'
283
+ elif kernel == 'SunOS' :
284
+ kernel = 'pc-solaris'
289
285
# On Solaris, uname -m will return a machine classification instead
290
286
# of a cpu type, so uname -p is recommended instead. However, the
291
287
# output from that option is too generic for our purposes (it will
@@ -294,34 +290,34 @@ def default_build_triple(verbose):
294
290
cputype = require (['isainfo' , '-k' ]).decode (default_encoding )
295
291
# sparc cpus have sun as a target vendor
296
292
if 'sparc' in cputype :
297
- ostype = 'sun-solaris'
298
- elif ostype .startswith ('MINGW' ):
293
+ kernel = 'sun-solaris'
294
+ elif kernel .startswith ('MINGW' ):
299
295
# msys' `uname` does not print gcc configuration, but prints msys
300
296
# configuration. so we cannot believe `uname -m`:
301
297
# msys1 is always i686 and msys2 is always x86_64.
302
298
# instead, msys defines $MSYSTEM which is MINGW32 on i686 and
303
299
# MINGW64 on x86_64.
304
- ostype = 'pc-windows-gnu'
300
+ kernel = 'pc-windows-gnu'
305
301
cputype = 'i686'
306
302
if os .environ .get ('MSYSTEM' ) == 'MINGW64' :
307
303
cputype = 'x86_64'
308
- elif ostype .startswith ('MSYS' ):
309
- ostype = 'pc-windows-gnu'
310
- elif ostype .startswith ('CYGWIN_NT' ):
304
+ elif kernel .startswith ('MSYS' ):
305
+ kernel = 'pc-windows-gnu'
306
+ elif kernel .startswith ('CYGWIN_NT' ):
311
307
cputype = 'i686'
312
- if ostype .endswith ('WOW64' ):
308
+ if kernel .endswith ('WOW64' ):
313
309
cputype = 'x86_64'
314
- ostype = 'pc-windows-gnu'
315
- elif sys . platform == 'win32' :
310
+ kernel = 'pc-windows-gnu'
311
+ elif platform_is_win32 () :
316
312
# Some Windows platforms might have a `uname` command that returns a
317
313
# non-standard string (e.g. gnuwin32 tools returns `windows32`). In
318
314
# these cases, fall back to using sys.platform.
319
315
return 'x86_64-pc-windows-msvc'
320
316
else :
321
- err = "unknown OS type: {}" .format (ostype )
317
+ err = "unknown OS type: {}" .format (kernel )
322
318
sys .exit (err )
323
319
324
- if cputype in ['powerpc' , 'riscv' ] and ostype == 'unknown-freebsd' :
320
+ if cputype in ['powerpc' , 'riscv' ] and kernel == 'unknown-freebsd' :
325
321
cputype = subprocess .check_output (
326
322
['uname' , '-p' ]).strip ().decode (default_encoding )
327
323
cputype_mapper = {
@@ -354,24 +350,23 @@ def default_build_triple(verbose):
354
350
cputype = cputype_mapper [cputype ]
355
351
elif cputype in {'xscale' , 'arm' }:
356
352
cputype = 'arm'
357
- if ostype == 'linux-android' :
358
- ostype = 'linux-androideabi'
359
- elif ostype == 'unknown-freebsd' :
360
- cputype = subprocess .check_output (
361
- ['uname' , '-p' ]).strip ().decode (default_encoding )
362
- ostype = 'unknown-freebsd'
353
+ if kernel == 'linux-android' :
354
+ kernel = 'linux-androideabi'
355
+ elif kernel == 'unknown-freebsd' :
356
+ cputype = processor
357
+ kernel = 'unknown-freebsd'
363
358
elif cputype == 'armv6l' :
364
359
cputype = 'arm'
365
- if ostype == 'linux-android' :
366
- ostype = 'linux-androideabi'
360
+ if kernel == 'linux-android' :
361
+ kernel = 'linux-androideabi'
367
362
else :
368
- ostype += 'eabihf'
363
+ kernel += 'eabihf'
369
364
elif cputype in {'armv7l' , 'armv8l' }:
370
365
cputype = 'armv7'
371
- if ostype == 'linux-android' :
372
- ostype = 'linux-androideabi'
366
+ if kernel == 'linux-android' :
367
+ kernel = 'linux-androideabi'
373
368
else :
374
- ostype += 'eabihf'
369
+ kernel += 'eabihf'
375
370
elif cputype == 'mips' :
376
371
if sys .byteorder == 'big' :
377
372
cputype = 'mips'
@@ -387,14 +382,14 @@ def default_build_triple(verbose):
387
382
else :
388
383
raise ValueError ('unknown byteorder: {}' .format (sys .byteorder ))
389
384
# only the n64 ABI is supported, indicate it
390
- ostype += 'abi64'
385
+ kernel += 'abi64'
391
386
elif cputype == 'sparc' or cputype == 'sparcv9' or cputype == 'sparc64' :
392
387
pass
393
388
else :
394
389
err = "unknown cpu type: {}" .format (cputype )
395
390
sys .exit (err )
396
391
397
- return "{}-{}" .format (cputype , ostype )
392
+ return "{}-{}" .format (cputype , kernel )
398
393
399
394
400
395
@contextlib .contextmanager
0 commit comments