@@ -226,13 +226,16 @@ def format_build_time(duration):
226226
227227def default_build_triple (verbose ):
228228 """Build triple as in LLVM"""
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`
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.
233232 default_encoding = sys .getdefaultencoding ()
234233
235- if platform_is_win32 ():
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 :
236239 try :
237240 version = subprocess .check_output (["rustc" , "--version" , "--verbose" ],
238241 stderr = subprocess .DEVNULL )
@@ -250,17 +253,19 @@ def default_build_triple(verbose):
250253 print ("falling back to auto-detect" , file = sys .stderr )
251254
252255 required = not platform_is_win32 ()
253- uname = require (["uname" , "-smp" ], exit = required )
256+ ostype = require (["uname" , "-s" ], exit = required )
257+ cputype = require (['uname' , '-m' ], exit = required )
254258
255259 # If we do not have `uname`, assume Windows.
256- if uname is None :
260+ if ostype is None or cputype is None :
257261 return 'x86_64-pc-windows-msvc'
258262
259- kernel , cputype , processor = uname .decode (default_encoding ).split ()
263+ ostype = ostype .decode (default_encoding )
264+ cputype = cputype .decode (default_encoding )
260265
261266 # The goal here is to come up with the same triple as LLVM would,
262267 # at least for the subset of platforms we're willing to target.
263- kerneltype_mapper = {
268+ ostype_mapper = {
264269 'Darwin' : 'apple-darwin' ,
265270 'DragonFly' : 'unknown-dragonfly' ,
266271 'FreeBSD' : 'unknown-freebsd' ,
@@ -270,18 +275,17 @@ def default_build_triple(verbose):
270275 }
271276
272277 # Consider the direct transformation first and then the special cases
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'
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'
281285 else :
282- kernel = 'unknown-linux-gnu'
283- elif kernel == 'SunOS' :
284- kernel = 'pc-solaris'
286+ ostype = 'unknown-linux-gnu'
287+ elif ostype == 'SunOS' :
288+ ostype = 'pc-solaris'
285289 # On Solaris, uname -m will return a machine classification instead
286290 # of a cpu type, so uname -p is recommended instead. However, the
287291 # output from that option is too generic for our purposes (it will
@@ -290,34 +294,34 @@ def default_build_triple(verbose):
290294 cputype = require (['isainfo' , '-k' ]).decode (default_encoding )
291295 # sparc cpus have sun as a target vendor
292296 if 'sparc' in cputype :
293- kernel = 'sun-solaris'
294- elif kernel .startswith ('MINGW' ):
297+ ostype = 'sun-solaris'
298+ elif ostype .startswith ('MINGW' ):
295299 # msys' `uname` does not print gcc configuration, but prints msys
296300 # configuration. so we cannot believe `uname -m`:
297301 # msys1 is always i686 and msys2 is always x86_64.
298302 # instead, msys defines $MSYSTEM which is MINGW32 on i686 and
299303 # MINGW64 on x86_64.
300- kernel = 'pc-windows-gnu'
304+ ostype = 'pc-windows-gnu'
301305 cputype = 'i686'
302306 if os .environ .get ('MSYSTEM' ) == 'MINGW64' :
303307 cputype = 'x86_64'
304- elif kernel .startswith ('MSYS' ):
305- kernel = 'pc-windows-gnu'
306- elif kernel .startswith ('CYGWIN_NT' ):
308+ elif ostype .startswith ('MSYS' ):
309+ ostype = 'pc-windows-gnu'
310+ elif ostype .startswith ('CYGWIN_NT' ):
307311 cputype = 'i686'
308- if kernel .endswith ('WOW64' ):
312+ if ostype .endswith ('WOW64' ):
309313 cputype = 'x86_64'
310- kernel = 'pc-windows-gnu'
311- elif platform_is_win32 () :
314+ ostype = 'pc-windows-gnu'
315+ elif sys . platform == 'win32' :
312316 # Some Windows platforms might have a `uname` command that returns a
313317 # non-standard string (e.g. gnuwin32 tools returns `windows32`). In
314318 # these cases, fall back to using sys.platform.
315319 return 'x86_64-pc-windows-msvc'
316320 else :
317- err = "unknown OS type: {}" .format (kernel )
321+ err = "unknown OS type: {}" .format (ostype )
318322 sys .exit (err )
319323
320- if cputype in ['powerpc' , 'riscv' ] and kernel == 'unknown-freebsd' :
324+ if cputype in ['powerpc' , 'riscv' ] and ostype == 'unknown-freebsd' :
321325 cputype = subprocess .check_output (
322326 ['uname' , '-p' ]).strip ().decode (default_encoding )
323327 cputype_mapper = {
@@ -350,23 +354,24 @@ def default_build_triple(verbose):
350354 cputype = cputype_mapper [cputype ]
351355 elif cputype in {'xscale' , 'arm' }:
352356 cputype = 'arm'
353- if kernel == 'linux-android' :
354- kernel = 'linux-androideabi'
355- elif kernel == 'unknown-freebsd' :
356- cputype = processor
357- kernel = 'unknown-freebsd'
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'
358363 elif cputype == 'armv6l' :
359364 cputype = 'arm'
360- if kernel == 'linux-android' :
361- kernel = 'linux-androideabi'
365+ if ostype == 'linux-android' :
366+ ostype = 'linux-androideabi'
362367 else :
363- kernel += 'eabihf'
368+ ostype += 'eabihf'
364369 elif cputype in {'armv7l' , 'armv8l' }:
365370 cputype = 'armv7'
366- if kernel == 'linux-android' :
367- kernel = 'linux-androideabi'
371+ if ostype == 'linux-android' :
372+ ostype = 'linux-androideabi'
368373 else :
369- kernel += 'eabihf'
374+ ostype += 'eabihf'
370375 elif cputype == 'mips' :
371376 if sys .byteorder == 'big' :
372377 cputype = 'mips'
@@ -382,14 +387,14 @@ def default_build_triple(verbose):
382387 else :
383388 raise ValueError ('unknown byteorder: {}' .format (sys .byteorder ))
384389 # only the n64 ABI is supported, indicate it
385- kernel += 'abi64'
390+ ostype += 'abi64'
386391 elif cputype == 'sparc' or cputype == 'sparcv9' or cputype == 'sparc64' :
387392 pass
388393 else :
389394 err = "unknown cpu type: {}" .format (cputype )
390395 sys .exit (err )
391396
392- return "{}-{}" .format (cputype , kernel )
397+ return "{}-{}" .format (cputype , ostype )
393398
394399
395400@contextlib .contextmanager
0 commit comments