]> git.lizzy.rs Git - rust.git/blob - src/librustc_target/spec/apple_ios_base.rs
Skip checking for unused mutable locals that have no name
[rust.git] / src / librustc_target / spec / apple_ios_base.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::io;
12 use std::process::Command;
13 use spec::{LinkArgs, LinkerFlavor, TargetOptions};
14
15 use self::Arch::*;
16
17 #[allow(non_camel_case_types)]
18 #[derive(Copy, Clone)]
19 pub enum Arch {
20     Armv7,
21     Armv7s,
22     Arm64,
23     I386,
24     X86_64
25 }
26
27 impl Arch {
28     pub fn to_string(&self) -> &'static str {
29         match self {
30             &Armv7 => "armv7",
31             &Armv7s => "armv7s",
32             &Arm64 => "arm64",
33             &I386 => "i386",
34             &X86_64 => "x86_64"
35         }
36     }
37 }
38
39 pub fn get_sdk_root(sdk_name: &str) -> Result<String, String> {
40     let res = Command::new("xcrun")
41                       .arg("--show-sdk-path")
42                       .arg("-sdk")
43                       .arg(sdk_name)
44                       .output()
45                       .and_then(|output| {
46                           if output.status.success() {
47                               Ok(String::from_utf8(output.stdout).unwrap())
48                           } else {
49                               let error = String::from_utf8(output.stderr);
50                               let error = format!("process exit with error: {}",
51                                                   error.unwrap());
52                               Err(io::Error::new(io::ErrorKind::Other,
53                                                  &error[..]))
54                           }
55                       });
56
57     match res {
58         Ok(output) => Ok(output.trim().to_string()),
59         Err(e) => Err(format!("failed to get {} SDK path: {}", sdk_name, e))
60     }
61 }
62
63 fn build_pre_link_args(arch: Arch) -> Result<LinkArgs, String> {
64     let sdk_name = match arch {
65         Armv7 | Armv7s | Arm64 => "iphoneos",
66         I386 | X86_64 => "iphonesimulator"
67     };
68
69     let arch_name = arch.to_string();
70
71     let sdk_root = get_sdk_root(sdk_name)?;
72
73     let mut args = LinkArgs::new();
74     args.insert(LinkerFlavor::Gcc,
75                 vec!["-arch".to_string(),
76                      arch_name.to_string(),
77                      "-Wl,-syslibroot".to_string(),
78                      sdk_root]);
79
80     Ok(args)
81 }
82
83 fn target_cpu(arch: Arch) -> String {
84     match arch {
85         Armv7 => "cortex-a8", // iOS7 is supported on iPhone 4 and higher
86         Armv7s => "cortex-a9",
87         Arm64 => "cyclone",
88         I386 => "yonah",
89         X86_64 => "core2",
90     }.to_string()
91 }
92
93 pub fn opts(arch: Arch) -> Result<TargetOptions, String> {
94     let pre_link_args = build_pre_link_args(arch)?;
95     Ok(TargetOptions {
96         cpu: target_cpu(arch),
97         dynamic_linking: false,
98         executables: true,
99         pre_link_args,
100         has_elf_tls: false,
101         // The following line is a workaround for jemalloc 4.5 being broken on
102         // ios. jemalloc 5.0 is supposed to fix this.
103         // see https://github.com/rust-lang/rust/issues/45262
104         exe_allocation_crate: None,
105         .. super::apple_base::opts()
106     })
107 }