@@ -247,6 +247,8 @@ mod tool;
247247pub use tool:: Tool ;
248248use tool:: ToolFamily ;
249249
250+ mod target_info;
251+
250252/// A builder for compilation of a native library.
251253///
252254/// A `Build` is the main type of the `cc` crate and is used to control all the
@@ -312,6 +314,8 @@ enum ErrorKind {
312314 ToolNotFound ,
313315 /// One of the function arguments failed validation.
314316 InvalidArgument ,
317+ /// Invalid target
318+ InvalidTarget ,
315319 #[ cfg( feature = "parallel" ) ]
316320 /// jobserver helpthread failure
317321 JobserverHelpThreadError ,
@@ -1900,6 +1904,13 @@ impl Build {
19001904 && !( target. contains ( "android" )
19011905 && android_clang_compiler_uses_target_arg_internally ( & cmd. path ) )
19021906 {
1907+ let ( arch, rest) = target. split_once ( '-' ) . ok_or_else ( || {
1908+ Error :: new (
1909+ ErrorKind :: InvalidTarget ,
1910+ format ! ( "Invalid target `{}`: no `-` in it" , target) ,
1911+ )
1912+ } ) ?;
1913+
19031914 if target. contains ( "darwin" ) {
19041915 if let Some ( arch) =
19051916 map_darwin_target_from_rust_to_compiler_architecture ( target)
@@ -1983,39 +1994,17 @@ impl Build {
19831994 format ! ( "--target={}-apple-tvos{}" , arch, deployment_target) . into ( ) ,
19841995 ) ;
19851996 }
1986- } else if target. starts_with ( "riscv64gc-" ) {
1987- cmd. args . push (
1988- format ! ( "--target={}" , target. replace( "riscv64gc" , "riscv64" ) ) . into ( ) ,
1989- ) ;
1990- } else if target. starts_with ( "riscv64imac-" ) {
1991- cmd. args . push (
1992- format ! ( "--target={}" , target. replace( "riscv64imac" , "riscv64" ) ) . into ( ) ,
1993- ) ;
1994- } else if target. starts_with ( "riscv32gc-" ) {
1997+ } else if let Ok ( index) = target_info:: RISCV_ARCH_MAPPING
1998+ . binary_search_by_key ( & arch, |( arch, _) | & arch)
1999+ {
19952000 cmd. args . push (
1996- format ! ( "--target={}" , target. replace( "riscv32gc" , "riscv32" ) ) . into ( ) ,
2001+ format ! (
2002+ "--target={}-{}" ,
2003+ target_info:: RISCV_ARCH_MAPPING [ index] . 1 ,
2004+ rest
2005+ )
2006+ . into ( ) ,
19972007 ) ;
1998- } else if target. starts_with ( "riscv32i-" ) {
1999- cmd. args . push (
2000- format ! ( "--target={}" , target. replace( "riscv32i" , "riscv32" ) ) . into ( ) ,
2001- )
2002- } else if target. starts_with ( "riscv32im-" ) {
2003- cmd. args . push (
2004- format ! ( "--target={}" , target. replace( "riscv32im" , "riscv32" ) ) . into ( ) ,
2005- )
2006- } else if target. starts_with ( "riscv32imc-" ) {
2007- cmd. args . push (
2008- format ! ( "--target={}" , target. replace( "riscv32imc" , "riscv32" ) ) . into ( ) ,
2009- )
2010- } else if target. starts_with ( "riscv32imac-" ) {
2011- cmd. args . push (
2012- format ! ( "--target={}" , target. replace( "riscv32imac" , "riscv32" ) ) . into ( ) ,
2013- )
2014- } else if target. starts_with ( "riscv32imafc-" ) {
2015- cmd. args . push (
2016- format ! ( "--target={}" , target. replace( "riscv32imafc" , "riscv32" ) )
2017- . into ( ) ,
2018- )
20192008 } else if target. contains ( "uefi" ) {
20202009 if target. contains ( "x86_64" ) {
20212010 cmd. args . push ( "--target=x86_64-unknown-windows-gnu" . into ( ) ) ;
0 commit comments