1- use crate :: spec:: { LinkArgs , LinkerFlavor , TargetOptions } ;
2- use std:: env;
3- use std:: io;
4- use std:: path:: Path ;
5- use std:: process:: Command ;
1+ use crate :: spec:: TargetOptions ;
62
73use Arch :: * ;
84#[ allow( non_camel_case_types) ]
@@ -16,108 +12,6 @@ pub enum Arch {
1612 X86_64_macabi ,
1713}
1814
19- #[ allow( non_camel_case_types) ]
20- #[ derive( Copy , Clone ) ]
21- pub enum AppleOS {
22- tvOS,
23- iOS,
24- }
25-
26- impl Arch {
27- pub fn to_string ( self ) -> & ' static str {
28- match self {
29- Armv7 => "armv7" ,
30- Armv7s => "armv7s" ,
31- Arm64 => "arm64" ,
32- I386 => "i386" ,
33- X86_64 => "x86_64" ,
34- X86_64_macabi => "x86_64" ,
35- }
36- }
37- }
38-
39- pub fn get_sdk_root ( sdk_name : & str ) -> Result < String , String > {
40- // Following what clang does
41- // (https://github.com/llvm/llvm-project/blob/
42- // 296a80102a9b72c3eda80558fb78a3ed8849b341/clang/lib/Driver/ToolChains/Darwin.cpp#L1661-L1678)
43- // to allow the SDK path to be set. (For clang, xcrun sets
44- // SDKROOT; for rustc, the user or build system can set it, or we
45- // can fall back to checking for xcrun on PATH.)
46- if let Ok ( sdkroot) = env:: var ( "SDKROOT" ) {
47- let p = Path :: new ( & sdkroot) ;
48- match sdk_name {
49- // Ignore `SDKROOT` if it's clearly set for the wrong platform.
50- "appletvos"
51- if sdkroot. contains ( "TVSimulator.platform" )
52- || sdkroot. contains ( "MacOSX.platform" ) => { }
53- "appletvsimulator"
54- if sdkroot. contains ( "TVOS.platform" ) || sdkroot. contains ( "MacOSX.platform" ) => { }
55- "iphoneos"
56- if sdkroot. contains ( "iPhoneSimulator.platform" )
57- || sdkroot. contains ( "MacOSX.platform" ) => { }
58- "iphonesimulator"
59- if sdkroot. contains ( "iPhoneOS.platform" ) || sdkroot. contains ( "MacOSX.platform" ) => {
60- }
61- "macosx10.15"
62- if sdkroot. contains ( "iPhoneOS.platform" )
63- || sdkroot. contains ( "iPhoneSimulator.platform" ) => { }
64- // Ignore `SDKROOT` if it's not a valid path.
65- _ if !p. is_absolute ( ) || p == Path :: new ( "/" ) || !p. exists ( ) => { }
66- _ => return Ok ( sdkroot) ,
67- }
68- }
69- let res =
70- Command :: new ( "xcrun" ) . arg ( "--show-sdk-path" ) . arg ( "-sdk" ) . arg ( sdk_name) . output ( ) . and_then (
71- |output| {
72- if output. status . success ( ) {
73- Ok ( String :: from_utf8 ( output. stdout ) . unwrap ( ) )
74- } else {
75- let error = String :: from_utf8 ( output. stderr ) ;
76- let error = format ! ( "process exit with error: {}" , error. unwrap( ) ) ;
77- Err ( io:: Error :: new ( io:: ErrorKind :: Other , & error[ ..] ) )
78- }
79- } ,
80- ) ;
81-
82- match res {
83- Ok ( output) => Ok ( output. trim ( ) . to_string ( ) ) ,
84- Err ( e) => Err ( format ! ( "failed to get {} SDK path: {}" , sdk_name, e) ) ,
85- }
86- }
87-
88- fn build_pre_link_args ( arch : Arch , os : AppleOS ) -> Result < LinkArgs , String > {
89- let sdk_name = match ( arch, os) {
90- ( Arm64 , AppleOS :: tvOS) => "appletvos" ,
91- ( X86_64 , AppleOS :: tvOS) => "appletvsimulator" ,
92- ( Armv7 , AppleOS :: iOS) => "iphoneos" ,
93- ( Armv7s , AppleOS :: iOS) => "iphoneos" ,
94- ( Arm64 , AppleOS :: iOS) => "iphoneos" ,
95- ( I386 , AppleOS :: iOS) => "iphonesimulator" ,
96- ( X86_64 , AppleOS :: iOS) => "iphonesimulator" ,
97- ( X86_64_macabi , AppleOS :: iOS) => "macosx10.15" ,
98- _ => unreachable ! ( ) ,
99- } ;
100-
101- let arch_name = arch. to_string ( ) ;
102-
103- let sdk_root = get_sdk_root ( sdk_name) ?;
104-
105- let mut args = LinkArgs :: new ( ) ;
106- args. insert (
107- LinkerFlavor :: Gcc ,
108- vec ! [
109- "-arch" . to_string( ) ,
110- arch_name. to_string( ) ,
111- "-isysroot" . to_string( ) ,
112- sdk_root. clone( ) ,
113- "-Wl,-syslibroot" . to_string( ) ,
114- sdk_root,
115- ] ,
116- ) ;
117-
118- Ok ( args)
119- }
120-
12115fn target_cpu ( arch : Arch ) -> String {
12216 match arch {
12317 Armv7 => "cortex-a8" , // iOS7 is supported on iPhone 4 and higher
@@ -137,15 +31,13 @@ fn link_env_remove(arch: Arch) -> Vec<String> {
13731 }
13832}
13933
140- pub fn opts ( arch : Arch , os : AppleOS ) -> Result < TargetOptions , String > {
141- let pre_link_args = build_pre_link_args ( arch, os) ?;
142- Ok ( TargetOptions {
34+ pub fn opts ( arch : Arch ) -> TargetOptions {
35+ TargetOptions {
14336 cpu : target_cpu ( arch) ,
14437 executables : true ,
145- pre_link_args,
14638 link_env_remove : link_env_remove ( arch) ,
14739 has_elf_tls : false ,
14840 eliminate_frame_pointer : false ,
14941 ..super :: apple_base:: opts ( )
150- } )
42+ }
15143}
0 commit comments