2
2
// SPDX-License-Identifier: Apache-2.0
3
3
// SPDX-License-Identifier: MIT
4
4
5
- use std:: { io:: Cursor , process:: Command } ;
6
-
7
- #[ derive( Debug , serde:: Deserialize ) ]
8
- struct TargetSpec {
9
- #[ serde( rename = "llvm-target" ) ]
10
- llvm_target : String ,
11
- }
5
+ use std:: process:: Command ;
12
6
13
7
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
14
8
// SPDX-License-Identifier: Apache-2.0
15
9
// SPDX-License-Identifier: MIT
16
10
11
+ #[ derive( Debug , PartialEq , Eq ) ]
12
+ struct RustCfg {
13
+ target_arch : Option < String > ,
14
+ }
15
+
16
+ fn parse_rust_cfg ( cfg : String ) -> RustCfg {
17
+ let target_line = "target_arch=\" " ;
18
+ let mut target_arch = None ;
19
+ for line in cfg. split ( '\n' ) {
20
+ if line. starts_with ( target_line) {
21
+ let len = target_line. len ( ) ;
22
+ let arch = line. chars ( ) . skip ( len) . take ( line. len ( ) - len - 1 ) . collect ( ) ;
23
+ target_arch. replace ( arch) ;
24
+ }
25
+ }
26
+ RustCfg { target_arch }
27
+ }
28
+
17
29
/// Try to determine the current target triple.
18
30
///
19
31
/// Returns a target triple (e.g. `x86_64-unknown-linux-gnu` or `i686-pc-windows-msvc`) or an
@@ -24,18 +36,12 @@ struct TargetSpec {
24
36
/// * Errors:
25
37
/// * Unexpected system config
26
38
pub fn target_triple ( ) -> Result < String , crate :: Error > {
27
- let output = Command :: new ( "rustc" )
28
- . args ( & [ "-Z" , "unstable-options" , "--print" , "target-spec-json" ] )
29
- . env ( "RUSTC_BOOTSTRAP" , "1" )
30
- . output ( ) ?;
39
+ let output = Command :: new ( "rustc" ) . args ( & [ "--print" , "cfg" ] ) . output ( ) ?;
40
+
31
41
let arch = if output. status . success ( ) {
32
- let target_spec: TargetSpec = serde_json:: from_reader ( Cursor :: new ( output. stdout ) ) ?;
33
- target_spec
34
- . llvm_target
35
- . split ( '-' )
36
- . next ( )
37
- . unwrap ( )
38
- . to_string ( )
42
+ parse_rust_cfg ( String :: from_utf8_lossy ( & output. stdout ) . into_owned ( ) )
43
+ . target_arch
44
+ . expect ( "could not find `target_arch` when running `rustc --print cfg`." )
39
45
} else {
40
46
super :: common:: print_info ( & format ! (
41
47
"failed to determine target arch using rustc, error: `{}`. The fallback is the architecture of the machine that compiled this crate." ,
@@ -90,3 +96,34 @@ pub fn target_triple() -> Result<String, crate::Error> {
90
96
91
97
Ok ( format ! ( "{}-{}" , arch, os) )
92
98
}
99
+
100
+ #[ cfg( test) ]
101
+ mod tests {
102
+ use super :: RustCfg ;
103
+
104
+ #[ test]
105
+ fn parse_rust_cfg ( ) {
106
+ assert_eq ! (
107
+ super :: parse_rust_cfg( "target_arch" . into( ) ) ,
108
+ RustCfg { target_arch: None }
109
+ ) ;
110
+
111
+ assert_eq ! (
112
+ super :: parse_rust_cfg(
113
+ r#"debug_assertions
114
+ target_arch="aarch64"
115
+ target_endian="little"
116
+ target_env=""
117
+ target_family="unix"
118
+ target_os="macos"
119
+ target_pointer_width="64"
120
+ target_vendor="apple"
121
+ unix"#
122
+ . into( )
123
+ ) ,
124
+ RustCfg {
125
+ target_arch: Some ( "aarch64" . into( ) )
126
+ }
127
+ ) ;
128
+ }
129
+ }
0 commit comments