]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014-2015 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 | //! [Flexible target specification.](https://github.com/rust-lang/rfcs/pull/131) | |
12 | //! | |
13 | //! Rust targets a wide variety of usecases, and in the interest of flexibility, | |
14 | //! allows new target triples to be defined in configuration files. Most users | |
15 | //! will not need to care about these, but this is invaluable when porting Rust | |
16 | //! to a new platform, and allows for an unprecedented level of control over how | |
17 | //! the compiler works. | |
18 | //! | |
19 | //! # Using custom targets | |
20 | //! | |
21 | //! A target triple, as passed via `rustc --target=TRIPLE`, will first be | |
22 | //! compared against the list of built-in targets. This is to ease distributing | |
23 | //! rustc (no need for configuration files) and also to hold these built-in | |
24 | //! targets as immutable and sacred. If `TRIPLE` is not one of the built-in | |
25 | //! targets, rustc will check if a file named `TRIPLE` exists. If it does, it | |
26 | //! will be loaded as the target configuration. If the file does not exist, | |
27 | //! rustc will search each directory in the environment variable | |
28 | //! `RUST_TARGET_PATH` for a file named `TRIPLE.json`. The first one found will | |
29 | //! be loaded. If no file is found in any of those directories, a fatal error | |
30 | //! will be given. `RUST_TARGET_PATH` includes `/etc/rustc` as its last entry, | |
31 | //! to be searched by default. | |
32 | //! | |
33 | //! Projects defining their own targets should use | |
34 | //! `--target=path/to/my-awesome-platform.json` instead of adding to | |
35 | //! `RUST_TARGET_PATH`. | |
36 | //! | |
37 | //! # Defining a new target | |
38 | //! | |
39 | //! Targets are defined using [JSON](http://json.org/). The `Target` struct in | |
40 | //! this module defines the format the JSON file should take, though each | |
41 | //! underscore in the field names should be replaced with a hyphen (`-`) in the | |
42 | //! JSON file. Some fields are required in every target specification, such as | |
54a0048b SL |
43 | //! `llvm-target`, `target-endian`, `target-pointer-width`, `data-layout`, |
44 | //! `arch`, and `os`. In general, options passed to rustc with `-C` override | |
45 | //! the target's settings, though `target-feature` and `link-args` will *add* | |
46 | //! to the list specified by the target, rather than replace. | |
1a4d82fc JJ |
47 | |
48 | use serialize::json::Json; | |
1a4d82fc | 49 | use std::default::Default; |
c34b1796 | 50 | use std::io::prelude::*; |
7453a54e | 51 | use syntax::abi::Abi; |
1a4d82fc | 52 | |
d9579d0f | 53 | mod android_base; |
1a4d82fc | 54 | mod apple_base; |
85aaf69f | 55 | mod apple_ios_base; |
c34b1796 | 56 | mod bitrig_base; |
d9579d0f AL |
57 | mod dragonfly_base; |
58 | mod freebsd_base; | |
59 | mod linux_base; | |
85aaf69f | 60 | mod openbsd_base; |
c1a9b12d | 61 | mod netbsd_base; |
7453a54e | 62 | mod solaris_base; |
d9579d0f | 63 | mod windows_base; |
62682a34 | 64 | mod windows_msvc_base; |
1a4d82fc | 65 | |
7453a54e SL |
66 | macro_rules! supported_targets { |
67 | ( $(($triple:expr, $module:ident)),+ ) => ( | |
68 | $(mod $module;)* | |
69 | ||
70 | /// List of supported targets | |
71 | pub const TARGETS: &'static [&'static str] = &[$($triple),*]; | |
72 | ||
73 | // this would use a match if stringify! were allowed in pattern position | |
74 | fn load_specific(target: &str) -> Option<Target> { | |
75 | let target = target.replace("-", "_"); | |
76 | if false { } | |
77 | $( | |
78 | else if target == stringify!($module) { | |
54a0048b SL |
79 | let mut t = $module::target(); |
80 | t.options.is_builtin = true; | |
7453a54e SL |
81 | debug!("Got builtin target: {:?}", t); |
82 | return Some(t); | |
83 | } | |
84 | )* | |
85 | ||
86 | None | |
87 | } | |
88 | ) | |
89 | } | |
90 | ||
91 | supported_targets! { | |
92 | ("x86_64-unknown-linux-gnu", x86_64_unknown_linux_gnu), | |
93 | ("i686-unknown-linux-gnu", i686_unknown_linux_gnu), | |
94 | ("i586-unknown-linux-gnu", i586_unknown_linux_gnu), | |
95 | ("mips-unknown-linux-gnu", mips_unknown_linux_gnu), | |
96 | ("mipsel-unknown-linux-gnu", mipsel_unknown_linux_gnu), | |
97 | ("powerpc-unknown-linux-gnu", powerpc_unknown_linux_gnu), | |
98 | ("powerpc64-unknown-linux-gnu", powerpc64_unknown_linux_gnu), | |
99 | ("powerpc64le-unknown-linux-gnu", powerpc64le_unknown_linux_gnu), | |
100 | ("arm-unknown-linux-gnueabi", arm_unknown_linux_gnueabi), | |
101 | ("arm-unknown-linux-gnueabihf", arm_unknown_linux_gnueabihf), | |
102 | ("armv7-unknown-linux-gnueabihf", armv7_unknown_linux_gnueabihf), | |
103 | ("aarch64-unknown-linux-gnu", aarch64_unknown_linux_gnu), | |
104 | ("x86_64-unknown-linux-musl", x86_64_unknown_linux_musl), | |
105 | ("i686-unknown-linux-musl", i686_unknown_linux_musl), | |
106 | ("mips-unknown-linux-musl", mips_unknown_linux_musl), | |
107 | ("mipsel-unknown-linux-musl", mipsel_unknown_linux_musl), | |
108 | ||
109 | ("i686-linux-android", i686_linux_android), | |
110 | ("arm-linux-androideabi", arm_linux_androideabi), | |
111 | ("aarch64-linux-android", aarch64_linux_android), | |
112 | ||
113 | ("i686-unknown-freebsd", i686_unknown_freebsd), | |
114 | ("x86_64-unknown-freebsd", x86_64_unknown_freebsd), | |
115 | ||
116 | ("i686-unknown-dragonfly", i686_unknown_dragonfly), | |
117 | ("x86_64-unknown-dragonfly", x86_64_unknown_dragonfly), | |
118 | ||
119 | ("x86_64-unknown-bitrig", x86_64_unknown_bitrig), | |
120 | ("x86_64-unknown-openbsd", x86_64_unknown_openbsd), | |
121 | ("x86_64-unknown-netbsd", x86_64_unknown_netbsd), | |
122 | ("x86_64-rumprun-netbsd", x86_64_rumprun_netbsd), | |
123 | ||
124 | ("x86_64-apple-darwin", x86_64_apple_darwin), | |
125 | ("i686-apple-darwin", i686_apple_darwin), | |
126 | ||
127 | ("i386-apple-ios", i386_apple_ios), | |
128 | ("x86_64-apple-ios", x86_64_apple_ios), | |
129 | ("aarch64-apple-ios", aarch64_apple_ios), | |
130 | ("armv7-apple-ios", armv7_apple_ios), | |
131 | ("armv7s-apple-ios", armv7s_apple_ios), | |
132 | ||
133 | ("x86_64-sun-solaris", x86_64_sun_solaris), | |
134 | ||
135 | ("x86_64-pc-windows-gnu", x86_64_pc_windows_gnu), | |
136 | ("i686-pc-windows-gnu", i686_pc_windows_gnu), | |
137 | ||
138 | ("x86_64-pc-windows-msvc", x86_64_pc_windows_msvc), | |
139 | ("i686-pc-windows-msvc", i686_pc_windows_msvc), | |
54a0048b | 140 | ("i586-pc-windows-msvc", i586_pc_windows_msvc), |
7453a54e SL |
141 | |
142 | ("le32-unknown-nacl", le32_unknown_nacl), | |
143 | ("asmjs-unknown-emscripten", asmjs_unknown_emscripten) | |
144 | } | |
145 | ||
1a4d82fc JJ |
146 | /// Everything `rustc` knows about how to compile for a specific target. |
147 | /// | |
148 | /// Every field here must be specified, and has no default value. | |
85aaf69f | 149 | #[derive(Clone, Debug)] |
1a4d82fc | 150 | pub struct Target { |
1a4d82fc JJ |
151 | /// Target triple to pass to LLVM. |
152 | pub llvm_target: String, | |
153 | /// String to use as the `target_endian` `cfg` variable. | |
154 | pub target_endian: String, | |
155 | /// String to use as the `target_pointer_width` `cfg` variable. | |
156 | pub target_pointer_width: String, | |
157 | /// OS name to use for conditional compilation. | |
158 | pub target_os: String, | |
d9579d0f AL |
159 | /// Environment name to use for conditional compilation. |
160 | pub target_env: String, | |
b039eaaf SL |
161 | /// Vendor name to use for conditional compilation. |
162 | pub target_vendor: String, | |
7453a54e SL |
163 | /// Architecture to use for ABI considerations. Valid options: "x86", |
164 | /// "x86_64", "arm", "aarch64", "mips", "powerpc", and "powerpc64". | |
1a4d82fc | 165 | pub arch: String, |
54a0048b SL |
166 | /// [Data layout](http://llvm.org/docs/LangRef.html#data-layout) to pass to LLVM. |
167 | pub data_layout: String, | |
1a4d82fc JJ |
168 | /// Optional settings with defaults. |
169 | pub options: TargetOptions, | |
170 | } | |
171 | ||
172 | /// Optional aspects of a target specification. | |
173 | /// | |
174 | /// This has an implementation of `Default`, see each field for what the default is. In general, | |
175 | /// these try to take "minimal defaults" that don't assume anything about the runtime they run in. | |
85aaf69f | 176 | #[derive(Clone, Debug)] |
1a4d82fc | 177 | pub struct TargetOptions { |
54a0048b SL |
178 | /// Whether the target is built-in or loaded from a custom target specification. |
179 | pub is_builtin: bool, | |
180 | ||
1a4d82fc JJ |
181 | /// Linker to invoke. Defaults to "cc". |
182 | pub linker: String, | |
62682a34 SL |
183 | /// Archive utility to use when managing archives. Defaults to "ar". |
184 | pub ar: String, | |
92a42be0 | 185 | |
d9579d0f AL |
186 | /// Linker arguments that are unconditionally passed *before* any |
187 | /// user-defined libraries. | |
1a4d82fc | 188 | pub pre_link_args: Vec<String>, |
92a42be0 SL |
189 | /// Objects to link before all others, always found within the |
190 | /// sysroot folder. | |
191 | pub pre_link_objects_exe: Vec<String>, // ... when linking an executable | |
192 | pub pre_link_objects_dll: Vec<String>, // ... when linking a dylib | |
193 | /// Linker arguments that are unconditionally passed after any | |
194 | /// user-defined but before post_link_objects. Standard platform | |
195 | /// libraries that should be always be linked to, usually go here. | |
196 | pub late_link_args: Vec<String>, | |
197 | /// Objects to link after all others, always found within the | |
198 | /// sysroot folder. | |
199 | pub post_link_objects: Vec<String>, | |
d9579d0f AL |
200 | /// Linker arguments that are unconditionally passed *after* any |
201 | /// user-defined libraries. | |
1a4d82fc | 202 | pub post_link_args: Vec<String>, |
92a42be0 | 203 | |
d9579d0f AL |
204 | /// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults |
205 | /// to "default". | |
1a4d82fc | 206 | pub cpu: String, |
d9579d0f AL |
207 | /// Default target features to pass to LLVM. These features will *always* be |
208 | /// passed, and cannot be disabled even via `-C`. Corresponds to `llc | |
209 | /// -mattr=$features`. | |
1a4d82fc JJ |
210 | pub features: String, |
211 | /// Whether dynamic linking is available on this target. Defaults to false. | |
212 | pub dynamic_linking: bool, | |
213 | /// Whether executables are available on this target. iOS, for example, only allows static | |
214 | /// libraries. Defaults to false. | |
215 | pub executables: bool, | |
1a4d82fc JJ |
216 | /// Relocation model to use in object file. Corresponds to `llc |
217 | /// -relocation-model=$relocation_model`. Defaults to "pic". | |
218 | pub relocation_model: String, | |
219 | /// Code model to use. Corresponds to `llc -code-model=$code_model`. Defaults to "default". | |
220 | pub code_model: String, | |
221 | /// Do not emit code that uses the "red zone", if the ABI has one. Defaults to false. | |
222 | pub disable_redzone: bool, | |
223 | /// Eliminate frame pointers from stack frames if possible. Defaults to true. | |
224 | pub eliminate_frame_pointer: bool, | |
225 | /// Emit each function in its own section. Defaults to true. | |
226 | pub function_sections: bool, | |
227 | /// String to prepend to the name of every dynamic library. Defaults to "lib". | |
228 | pub dll_prefix: String, | |
229 | /// String to append to the name of every dynamic library. Defaults to ".so". | |
230 | pub dll_suffix: String, | |
231 | /// String to append to the name of every executable. | |
232 | pub exe_suffix: String, | |
233 | /// String to prepend to the name of every static library. Defaults to "lib". | |
234 | pub staticlib_prefix: String, | |
235 | /// String to append to the name of every static library. Defaults to ".a". | |
236 | pub staticlib_suffix: String, | |
92a42be0 SL |
237 | /// OS family to use for conditional compilation. Valid options: "unix", "windows". |
238 | pub target_family: Option<String>, | |
1a4d82fc JJ |
239 | /// Whether the target toolchain is like OSX's. Only useful for compiling against iOS/OS X, in |
240 | /// particular running dsymutil and some other stuff like `-dead_strip`. Defaults to false. | |
241 | pub is_like_osx: bool, | |
7453a54e SL |
242 | /// Whether the target toolchain is like Solaris's. |
243 | /// Only useful for compiling against Illumos/Solaris, | |
244 | /// as they have a different set of linker flags. Defaults to false. | |
245 | pub is_like_solaris: bool, | |
1a4d82fc | 246 | /// Whether the target toolchain is like Windows'. Only useful for compiling against Windows, |
c34b1796 | 247 | /// only really used for figuring out how to find libraries, since Windows uses its own |
1a4d82fc JJ |
248 | /// library naming convention. Defaults to false. |
249 | pub is_like_windows: bool, | |
62682a34 | 250 | pub is_like_msvc: bool, |
85aaf69f SL |
251 | /// Whether the target toolchain is like Android's. Only useful for compiling against Android. |
252 | /// Defaults to false. | |
253 | pub is_like_android: bool, | |
1a4d82fc JJ |
254 | /// Whether the linker support GNU-like arguments such as -O. Defaults to false. |
255 | pub linker_is_gnu: bool, | |
256 | /// Whether the linker support rpaths or not. Defaults to false. | |
257 | pub has_rpath: bool, | |
62682a34 SL |
258 | /// Whether to disable linking to compiler-rt. Defaults to false, as LLVM |
259 | /// will emit references to the functions that compiler-rt provides. | |
1a4d82fc | 260 | pub no_compiler_rt: bool, |
b039eaaf SL |
261 | /// Whether to disable linking to the default libraries, typically corresponds |
262 | /// to `-nodefaultlibs`. Defaults to true. | |
263 | pub no_default_libraries: bool, | |
62682a34 SL |
264 | /// Dynamically linked executables can be compiled as position independent |
265 | /// if the default relocation model of position independent code is not | |
266 | /// changed. This is a requirement to take advantage of ASLR, as otherwise | |
267 | /// the functions in the executable are not randomized and can be used | |
268 | /// during an exploit of a vulnerability in any code. | |
1a4d82fc | 269 | pub position_independent_executables: bool, |
c1a9b12d SL |
270 | /// Format that archives should be emitted in. This affects whether we use |
271 | /// LLVM to assemble an archive or fall back to the system linker, and | |
272 | /// currently only "gnu" is used to fall into LLVM. Unknown strings cause | |
273 | /// the system linker to be used. | |
274 | pub archive_format: String, | |
e9174d1e SL |
275 | /// Is asm!() allowed? Defaults to true. |
276 | pub allow_asm: bool, | |
c1a9b12d SL |
277 | /// Whether the target uses a custom unwind resumption routine. |
278 | /// By default LLVM lowers `resume` instructions into calls to `_Unwind_Resume` | |
279 | /// defined in libgcc. If this option is enabled, the target must provide | |
280 | /// `eh_unwind_resume` lang item. | |
281 | pub custom_unwind_resume: bool, | |
e9174d1e SL |
282 | |
283 | /// Default crate for allocation symbols to link against | |
284 | pub lib_allocation_crate: String, | |
285 | pub exe_allocation_crate: String, | |
9cc50fc6 SL |
286 | |
287 | /// Flag indicating whether ELF TLS (e.g. #[thread_local]) is available for | |
288 | /// this target. | |
289 | pub has_elf_tls: bool, | |
7453a54e SL |
290 | // This is mainly for easy compatibility with emscripten. |
291 | // If we give emcc .o files that are actually .bc files it | |
292 | // will 'just work'. | |
293 | pub obj_is_bitcode: bool, | |
1a4d82fc JJ |
294 | } |
295 | ||
296 | impl Default for TargetOptions { | |
62682a34 SL |
297 | /// Create a set of "sane defaults" for any target. This is still |
298 | /// incomplete, and if used for compilation, will certainly not work. | |
1a4d82fc JJ |
299 | fn default() -> TargetOptions { |
300 | TargetOptions { | |
54a0048b | 301 | is_builtin: false, |
e9174d1e SL |
302 | linker: option_env!("CFG_DEFAULT_LINKER").unwrap_or("cc").to_string(), |
303 | ar: option_env!("CFG_DEFAULT_AR").unwrap_or("ar").to_string(), | |
1a4d82fc JJ |
304 | pre_link_args: Vec::new(), |
305 | post_link_args: Vec::new(), | |
306 | cpu: "generic".to_string(), | |
307 | features: "".to_string(), | |
308 | dynamic_linking: false, | |
309 | executables: false, | |
1a4d82fc JJ |
310 | relocation_model: "pic".to_string(), |
311 | code_model: "default".to_string(), | |
312 | disable_redzone: false, | |
313 | eliminate_frame_pointer: true, | |
314 | function_sections: true, | |
315 | dll_prefix: "lib".to_string(), | |
316 | dll_suffix: ".so".to_string(), | |
317 | exe_suffix: "".to_string(), | |
318 | staticlib_prefix: "lib".to_string(), | |
319 | staticlib_suffix: ".a".to_string(), | |
92a42be0 | 320 | target_family: None, |
1a4d82fc | 321 | is_like_osx: false, |
7453a54e | 322 | is_like_solaris: false, |
1a4d82fc | 323 | is_like_windows: false, |
85aaf69f | 324 | is_like_android: false, |
62682a34 | 325 | is_like_msvc: false, |
1a4d82fc JJ |
326 | linker_is_gnu: false, |
327 | has_rpath: false, | |
328 | no_compiler_rt: false, | |
b039eaaf | 329 | no_default_libraries: true, |
1a4d82fc | 330 | position_independent_executables: false, |
92a42be0 SL |
331 | pre_link_objects_exe: Vec::new(), |
332 | pre_link_objects_dll: Vec::new(), | |
d9579d0f | 333 | post_link_objects: Vec::new(), |
92a42be0 | 334 | late_link_args: Vec::new(), |
7453a54e | 335 | archive_format: "gnu".to_string(), |
c1a9b12d | 336 | custom_unwind_resume: false, |
e9174d1e SL |
337 | lib_allocation_crate: "alloc_system".to_string(), |
338 | exe_allocation_crate: "alloc_system".to_string(), | |
339 | allow_asm: true, | |
9cc50fc6 | 340 | has_elf_tls: false, |
7453a54e | 341 | obj_is_bitcode: false, |
1a4d82fc JJ |
342 | } |
343 | } | |
344 | } | |
345 | ||
346 | impl Target { | |
347 | /// Given a function ABI, turn "System" into the correct ABI for this target. | |
7453a54e | 348 | pub fn adjust_abi(&self, abi: Abi) -> Abi { |
1a4d82fc | 349 | match abi { |
7453a54e | 350 | Abi::System => { |
1a4d82fc | 351 | if self.options.is_like_windows && self.arch == "x86" { |
7453a54e | 352 | Abi::Stdcall |
1a4d82fc | 353 | } else { |
7453a54e | 354 | Abi::C |
1a4d82fc JJ |
355 | } |
356 | }, | |
357 | abi => abi | |
358 | } | |
359 | } | |
360 | ||
361 | /// Load a target descriptor from a JSON object. | |
362 | pub fn from_json(obj: Json) -> Target { | |
363 | // this is 1. ugly, 2. error prone. | |
364 | ||
85aaf69f | 365 | let get_req_field = |name: &str| { |
1a4d82fc JJ |
366 | match obj.find(name) |
367 | .map(|s| s.as_string()) | |
368 | .and_then(|os| os.map(|s| s.to_string())) { | |
369 | Some(val) => val, | |
92a42be0 | 370 | None => { |
9cc50fc6 | 371 | panic!("Field {} in target specification is required", name) |
92a42be0 | 372 | } |
1a4d82fc JJ |
373 | } |
374 | }; | |
375 | ||
b039eaaf SL |
376 | let get_opt_field = |name: &str, default: &str| { |
377 | obj.find(name).and_then(|s| s.as_string()) | |
378 | .map(|s| s.to_string()) | |
379 | .unwrap_or(default.to_string()) | |
380 | }; | |
381 | ||
1a4d82fc | 382 | let mut base = Target { |
1a4d82fc JJ |
383 | llvm_target: get_req_field("llvm-target"), |
384 | target_endian: get_req_field("target-endian"), | |
85aaf69f | 385 | target_pointer_width: get_req_field("target-pointer-width"), |
54a0048b | 386 | data_layout: get_req_field("data-layout"), |
1a4d82fc JJ |
387 | arch: get_req_field("arch"), |
388 | target_os: get_req_field("os"), | |
b039eaaf SL |
389 | target_env: get_opt_field("env", ""), |
390 | target_vendor: get_opt_field("vendor", "unknown"), | |
1a4d82fc JJ |
391 | options: Default::default(), |
392 | }; | |
393 | ||
394 | macro_rules! key { | |
395 | ($key_name:ident) => ( { | |
396 | let name = (stringify!($key_name)).replace("_", "-"); | |
85aaf69f | 397 | obj.find(&name[..]).map(|o| o.as_string() |
1a4d82fc JJ |
398 | .map(|s| base.options.$key_name = s.to_string())); |
399 | } ); | |
400 | ($key_name:ident, bool) => ( { | |
401 | let name = (stringify!($key_name)).replace("_", "-"); | |
85aaf69f | 402 | obj.find(&name[..]) |
1a4d82fc JJ |
403 | .map(|o| o.as_boolean() |
404 | .map(|s| base.options.$key_name = s)); | |
405 | } ); | |
406 | ($key_name:ident, list) => ( { | |
407 | let name = (stringify!($key_name)).replace("_", "-"); | |
85aaf69f | 408 | obj.find(&name[..]).map(|o| o.as_array() |
1a4d82fc JJ |
409 | .map(|v| base.options.$key_name = v.iter() |
410 | .map(|a| a.as_string().unwrap().to_string()).collect() | |
411 | ) | |
412 | ); | |
413 | } ); | |
e9174d1e SL |
414 | ($key_name:ident, optional) => ( { |
415 | let name = (stringify!($key_name)).replace("_", "-"); | |
416 | if let Some(o) = obj.find(&name[..]) { | |
417 | base.options.$key_name = o | |
418 | .as_string() | |
419 | .map(|s| s.to_string() ); | |
420 | } | |
421 | } ); | |
1a4d82fc JJ |
422 | } |
423 | ||
424 | key!(cpu); | |
62682a34 | 425 | key!(ar); |
1a4d82fc JJ |
426 | key!(linker); |
427 | key!(relocation_model); | |
428 | key!(code_model); | |
429 | key!(dll_prefix); | |
430 | key!(dll_suffix); | |
431 | key!(exe_suffix); | |
432 | key!(staticlib_prefix); | |
433 | key!(staticlib_suffix); | |
434 | key!(features); | |
435 | key!(dynamic_linking, bool); | |
436 | key!(executables, bool); | |
1a4d82fc JJ |
437 | key!(disable_redzone, bool); |
438 | key!(eliminate_frame_pointer, bool); | |
439 | key!(function_sections, bool); | |
92a42be0 | 440 | key!(target_family, optional); |
1a4d82fc JJ |
441 | key!(is_like_osx, bool); |
442 | key!(is_like_windows, bool); | |
443 | key!(linker_is_gnu, bool); | |
444 | key!(has_rpath, bool); | |
445 | key!(no_compiler_rt, bool); | |
b039eaaf | 446 | key!(no_default_libraries, bool); |
1a4d82fc JJ |
447 | key!(pre_link_args, list); |
448 | key!(post_link_args, list); | |
92a42be0 | 449 | key!(archive_format); |
e9174d1e | 450 | key!(allow_asm, bool); |
92a42be0 | 451 | key!(custom_unwind_resume, bool); |
1a4d82fc JJ |
452 | |
453 | base | |
454 | } | |
455 | ||
c34b1796 AL |
456 | /// Search RUST_TARGET_PATH for a JSON file specifying the given target |
457 | /// triple. Note that it could also just be a bare filename already, so also | |
458 | /// check for that. If one of the hardcoded targets we know about, just | |
459 | /// return it directly. | |
1a4d82fc | 460 | /// |
c34b1796 AL |
461 | /// The error string could come from any of the APIs called, including |
462 | /// filesystem access and JSON decoding. | |
1a4d82fc | 463 | pub fn search(target: &str) -> Result<Target, String> { |
85aaf69f SL |
464 | use std::env; |
465 | use std::ffi::OsString; | |
c34b1796 AL |
466 | use std::fs::File; |
467 | use std::path::{Path, PathBuf}; | |
1a4d82fc JJ |
468 | use serialize::json; |
469 | ||
470 | fn load_file(path: &Path) -> Result<Target, String> { | |
54a0048b | 471 | let mut f = File::open(path).map_err(|e| e.to_string())?; |
c34b1796 | 472 | let mut contents = Vec::new(); |
54a0048b SL |
473 | f.read_to_end(&mut contents).map_err(|e| e.to_string())?; |
474 | let obj = json::from_reader(&mut &contents[..]) | |
475 | .map_err(|e| e.to_string())?; | |
1a4d82fc JJ |
476 | Ok(Target::from_json(obj)) |
477 | } | |
478 | ||
7453a54e SL |
479 | if let Some(t) = load_specific(target) { |
480 | return Ok(t) | |
1a4d82fc JJ |
481 | } |
482 | ||
1a4d82fc JJ |
483 | let path = Path::new(target); |
484 | ||
485 | if path.is_file() { | |
486 | return load_file(&path); | |
487 | } | |
488 | ||
489 | let path = { | |
490 | let mut target = target.to_string(); | |
491 | target.push_str(".json"); | |
c34b1796 | 492 | PathBuf::from(target) |
1a4d82fc JJ |
493 | }; |
494 | ||
c34b1796 AL |
495 | let target_path = env::var_os("RUST_TARGET_PATH") |
496 | .unwrap_or(OsString::new()); | |
1a4d82fc | 497 | |
1a4d82fc JJ |
498 | // FIXME 16351: add a sane default search path? |
499 | ||
85aaf69f | 500 | for dir in env::split_paths(&target_path) { |
c34b1796 | 501 | let p = dir.join(&path); |
1a4d82fc JJ |
502 | if p.is_file() { |
503 | return load_file(&p); | |
504 | } | |
505 | } | |
506 | ||
507 | Err(format!("Could not find specification for target {:?}", target)) | |
508 | } | |
509 | } | |
e9174d1e | 510 | |
b039eaaf | 511 | fn maybe_jemalloc() -> String { |
7453a54e | 512 | if cfg!(feature = "jemalloc") { |
e9174d1e | 513 | "alloc_jemalloc".to_string() |
7453a54e SL |
514 | } else { |
515 | "alloc_system".to_string() | |
e9174d1e SL |
516 | } |
517 | } |