aboutsummaryrefslogtreecommitdiff
path: root/gcc/src/lib.rs
blob: 7d19e135e808e4d4d19b3e468cd8498ab7d84c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
//! A library for build scripts to compile custom C code
//!
//! This library is intended to be used as a `build-dependencies` entry in
//! `Cargo.toml`:
//!
//! ```toml
//! [build-dependencies]
//! gcc = "0.3"
//! ```
//!
//! The purpose of this crate is to provide the utility functions necessary to
//! compile C code into a static archive which is then linked into a Rust crate.
//! The top-level `compile_library` function serves as a convenience and more
//! advanced configuration is available through the `Config` builder.
//!
//! This crate will automatically detect situations such as cross compilation or
//! other environment variables set by Cargo and will build code appropriately.
//!
//! # Examples
//!
//! Use the default configuration:
//!
//! ```no_run
//! extern crate gcc;
//!
//! fn main() {
//!     gcc::compile_library("libfoo.a", &["src/foo.c"]);
//! }
//! ```
//!
//! Use more advanced configuration:
//!
//! ```no_run
//! extern crate gcc;
//!
//! fn main() {
//!     gcc::Config::new()
//!                 .file("src/foo.c")
//!                 .define("FOO", Some("bar"))
//!                 .include("src")
//!                 .compile("libfoo.a");
//! }
//! ```

#![doc(html_root_url = "https://docs.rs/gcc/0.3")]
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]

#[cfg(feature = "parallel")]
extern crate rayon;

use std::env;
use std::ffi::{OsString, OsStr};
use std::fs;
use std::path::{PathBuf, Path};
use std::process::{Command, Stdio, Child};
use std::io::{self, BufReader, BufRead, Read, Write};
use std::thread::{self, JoinHandle};

// These modules are all glue to support reading the MSVC version from
// the registry and from COM interfaces
#[cfg(windows)]
mod registry;
#[cfg(windows)]
#[macro_use]
mod winapi;
#[cfg(windows)]
mod com;
#[cfg(windows)]
mod setup_config;

pub mod windows_registry;

/// Extra configuration to pass to gcc.
pub struct Config {
    include_directories: Vec<PathBuf>,
    definitions: Vec<(String, Option<String>)>,
    objects: Vec<PathBuf>,
    flags: Vec<String>,
    files: Vec<PathBuf>,
    cpp: bool,
    cpp_link_stdlib: Option<Option<String>>,
    cpp_set_stdlib: Option<String>,
    target: Option<String>,
    host: Option<String>,
    out_dir: Option<PathBuf>,
    opt_level: Option<String>,
    debug: Option<bool>,
    env: Vec<(OsString, OsString)>,
    compiler: Option<PathBuf>,
    archiver: Option<PathBuf>,
    cargo_metadata: bool,
    pic: Option<bool>,
    static_crt: Option<bool>,
}

/// Configuration used to represent an invocation of a C compiler.
///
/// This can be used to figure out what compiler is in use, what the arguments
/// to it are, and what the environment variables look like for the compiler.
/// This can be used to further configure other build systems (e.g. forward
/// along CC and/or CFLAGS) or the `to_command` method can be used to run the
/// compiler itself.
pub struct Tool {
    path: PathBuf,
    args: Vec<OsString>,
    env: Vec<(OsString, OsString)>,
    family: ToolFamily
}

/// Represents the family of tools this tool belongs to.
///
/// Each family of tools differs in how and what arguments they accept.
///
/// Detection of a family is done on best-effort basis and may not accurately reflect the tool.
#[derive(Copy, Clone, Debug)]
enum ToolFamily {
    /// Tool is GNU Compiler Collection-like.
    Gnu,
    /// Tool is Clang-like. It differs from the GCC in a sense that it accepts superset of flags
    /// and its cross-compilation approach is different.
    Clang,
    /// Tool is the MSVC cl.exe.
    Msvc,
}

impl ToolFamily {
    /// What the flag to request debug info for this family of tools look like
    fn debug_flag(&self) -> &'static str {
        match *self {
            ToolFamily::Msvc => "/Z7",
            ToolFamily::Gnu |
            ToolFamily::Clang => "-g",
        }
    }

    /// What the flag to include directories into header search path looks like
    fn include_flag(&self) -> &'static str {
        match *self {
            ToolFamily::Msvc => "/I",
            ToolFamily::Gnu |
            ToolFamily::Clang => "-I",
        }
    }

    /// What the flag to request macro-expanded source output looks like
    fn expand_flag(&self) -> &'static str {
        match *self {
            ToolFamily::Msvc => "/E",
            ToolFamily::Gnu |
            ToolFamily::Clang => "-E",
        }
    }
}

/// Compile a library from the given set of input C files.
///
/// This will simply compile all files into object files and then assemble them
/// into the output. This will read the standard environment variables to detect
/// cross compilations and such.
///
/// This function will also print all metadata on standard output for Cargo.
///
/// # Example
///
/// ```no_run
/// gcc::compile_library("libfoo.a", &["foo.c", "bar.c"]);
/// ```
pub fn compile_library(output: &str, files: &[&str]) {
    let mut c = Config::new();
    for f in files.iter() {
        c.file(*f);
    }
    c.compile(output);
}

impl Config {
    /// Construct a new instance of a blank set of configuration.
    ///
    /// This builder is finished with the `compile` function.
    pub fn new() -> Config {
        Config {
            include_directories: Vec::new(),
            definitions: Vec::new(),
            objects: Vec::new(),
            flags: Vec::new(),
            files: Vec::new(),
            cpp: false,
            cpp_link_stdlib: None,
            cpp_set_stdlib: None,
            target: None,
            host: None,
            out_dir: None,
            opt_level: None,
            debug: None,
            env: Vec::new(),
            compiler: None,
            archiver: None,
            cargo_metadata: true,
            pic: None,
            static_crt: None,
        }
    }

    /// Add a directory to the `-I` or include path for headers
    pub fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut Config {
        self.include_directories.push(dir.as_ref().to_path_buf());
        self
    }

    /// Specify a `-D` variable with an optional value.
    pub fn define(&mut self, var: &str, val: Option<&str>) -> &mut Config {
        self.definitions.push((var.to_string(), val.map(|s| s.to_string())));
        self
    }

    /// Add an arbitrary object file to link in
    pub fn object<P: AsRef<Path>>(&mut self, obj: P) -> &mut Config {
        self.objects.push(obj.as_ref().to_path_buf());
        self
    }

    /// Add an arbitrary flag to the invocation of the compiler
    pub fn flag(&mut self, flag: &str) -> &mut Config {
        self.flags.push(flag.to_string());
        self
    }

    /// Add a file which will be compiled
    pub fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut Config {
        self.files.push(p.as_ref().to_path_buf());
        self
    }

    /// Set C++ support.
    ///
    /// The other `cpp_*` options will only become active if this is set to
    /// `true`.
    pub fn cpp(&mut self, cpp: bool) -> &mut Config {
        self.cpp = cpp;
        self
    }

    /// Set the standard library to link against when compiling with C++
    /// support.
    ///
    /// The default value of this property depends on the current target: On
    /// OS X `Some("c++")` is used, when compiling for a Visual Studio based
    /// target `None` is used and for other targets `Some("stdc++")` is used.
    ///
    /// A value of `None` indicates that no automatic linking should happen,
    /// otherwise cargo will link against the specified library.
    ///
    /// The given library name must not contain the `lib` prefix.
    pub fn cpp_link_stdlib(&mut self, cpp_link_stdlib: Option<&str>) -> &mut Config {
        self.cpp_link_stdlib = Some(cpp_link_stdlib.map(|s| s.into()));
        self
    }

    /// Force the C++ compiler to use the specified standard library.
    ///
    /// Setting this option will automatically set `cpp_link_stdlib` to the same
    /// value.
    ///
    /// The default value of this option is always `None`.
    ///
    /// This option has no effect when compiling for a Visual Studio based
    /// target.
    ///
    /// This option sets the `-stdlib` flag, which is only supported by some
    /// compilers (clang, icc) but not by others (gcc). The library will not
    /// detect which compiler is used, as such it is the responsibility of the
    /// caller to ensure that this option is only used in conjuction with a
    /// compiler which supports the `-stdlib` flag.
    ///
    /// A value of `None` indicates that no specific C++ standard library should
    /// be used, otherwise `-stdlib` is added to the compile invocation.
    ///
    /// The given library name must not contain the `lib` prefix.
    pub fn cpp_set_stdlib(&mut self, cpp_set_stdlib: Option<&str>) -> &mut Config {
        self.cpp_set_stdlib = cpp_set_stdlib.map(|s| s.into());
        self.cpp_link_stdlib(cpp_set_stdlib);
        self
    }

    /// Configures the target this configuration will be compiling for.
    ///
    /// This option is automatically scraped from the `TARGET` environment
    /// variable by build scripts, so it's not required to call this function.
    pub fn target(&mut self, target: &str) -> &mut Config {
        self.target = Some(target.to_string());
        self
    }

    /// Configures the host assumed by this configuration.
    ///
    /// This option is automatically scraped from the `HOST` environment
    /// variable by build scripts, so it's not required to call this function.
    pub fn host(&mut self, host: &str) -> &mut Config {
        self.host = Some(host.to_string());
        self
    }

    /// Configures the optimization level of the generated object files.
    ///
    /// This option is automatically scraped from the `OPT_LEVEL` environment
    /// variable by build scripts, so it's not required to call this function.
    pub fn opt_level(&mut self, opt_level: u32) -> &mut Config {
        self.opt_level = Some(opt_level.to_string());
        self
    }

    /// Configures the optimization level of the generated object files.
    ///
    /// This option is automatically scraped from the `OPT_LEVEL` environment
    /// variable by build scripts, so it's not required to call this function.
    pub fn opt_level_str(&mut self, opt_level: &str) -> &mut Config {
        self.opt_level = Some(opt_level.to_string());
        self
    }

    /// Configures whether the compiler will emit debug information when
    /// generating object files.
    ///
    /// This option is automatically scraped from the `PROFILE` environment
    /// variable by build scripts (only enabled when the profile is "debug"), so
    /// it's not required to call this function.
    pub fn debug(&mut self, debug: bool) -> &mut Config {
        self.debug = Some(debug);
        self
    }

    /// Configures the output directory where all object files and static
    /// libraries will be located.
    ///
    /// This option is automatically scraped from the `OUT_DIR` environment
    /// variable by build scripts, so it's not required to call this function.
    pub fn out_dir<P: AsRef<Path>>(&mut self, out_dir: P) -> &mut Config {
        self.out_dir = Some(out_dir.as_ref().to_owned());
        self
    }

    /// Configures the compiler to be used to produce output.
    ///
    /// This option is automatically determined from the target platform or a
    /// number of environment variables, so it's not required to call this
    /// function.
    pub fn compiler<P: AsRef<Path>>(&mut self, compiler: P) -> &mut Config {
        self.compiler = Some(compiler.as_ref().to_owned());
        self
    }

    /// Configures the tool used to assemble archives.
    ///
    /// This option is automatically determined from the target platform or a
    /// number of environment variables, so it's not required to call this
    /// function.
    pub fn archiver<P: AsRef<Path>>(&mut self, archiver: P) -> &mut Config {
        self.archiver = Some(archiver.as_ref().to_owned());
        self
    }
    /// Define whether metadata should be emitted for cargo allowing it to
    /// automatically link the binary. Defaults to `true`.
    pub fn cargo_metadata(&mut self, cargo_metadata: bool) -> &mut Config {
        self.cargo_metadata = cargo_metadata;
        self
    }

    /// Configures whether the compiler will emit position independent code.
    ///
    /// This option defaults to `false` for `windows-gnu` targets and
    /// to `true` for all other targets.
    pub fn pic(&mut self, pic: bool) -> &mut Config {
        self.pic = Some(pic);
        self
    }

    /// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
    ///
    /// This option defaults to `false`, and affect only msvc targets.
    pub fn static_crt(&mut self, static_crt: bool) -> &mut Config {
        self.static_crt = Some(static_crt);
        self
    }


    #[doc(hidden)]
    pub fn __set_env<A, B>(&mut self, a: A, b: B) -> &mut Config
        where A: AsRef<OsStr>,
              B: AsRef<OsStr>
    {
        self.env.push((a.as_ref().to_owned(), b.as_ref().to_owned()));
        self
    }

    /// Run the compiler, generating the file `output`
    ///
    /// The name `output` must begin with `lib` and end with `.a`
    pub fn compile(&self, output: &str) {
        assert!(output.starts_with("lib"));
        assert!(output.ends_with(".a"));
        let lib_name = &output[3..output.len() - 2];
        let dst = self.get_out_dir();

        let mut objects = Vec::new();
        let mut src_dst = Vec::new();
        for file in self.files.iter() {
            let obj = dst.join(file).with_extension("o");
            let obj = if !obj.starts_with(&dst) {
                dst.join(obj.file_name().unwrap())
            } else {
                obj
            };
            fs::create_dir_all(&obj.parent().unwrap()).unwrap();
            src_dst.push((file.to_path_buf(), obj.clone()));
            objects.push(obj);
        }
        self.compile_objects(&src_dst);
        self.assemble(lib_name, &dst.join(output), &objects);

        if self.get_target().contains("msvc") {
            let compiler = self.get_base_compiler();
            let atlmfc_lib = compiler.env()
                .iter()
                .find(|&&(ref var, _)| var.as_os_str() == OsStr::new("LIB"))
                .and_then(|&(_, ref lib_paths)| {
                    env::split_paths(lib_paths).find(|path| {
                        let sub = Path::new("atlmfc/lib");
                        path.ends_with(sub) || path.parent().map_or(false, |p| p.ends_with(sub))
                    })
                });

            if let Some(atlmfc_lib) = atlmfc_lib {
                self.print(&format!("cargo:rustc-link-search=native={}", atlmfc_lib.display()));
            }
        }

        self.print(&format!("cargo:rustc-link-lib=static={}",
                            &output[3..output.len() - 2]));
        self.print(&format!("cargo:rustc-link-search=native={}", dst.display()));

        // Add specific C++ libraries, if enabled.
        if self.cpp {
            if let Some(stdlib) = self.get_cpp_link_stdlib() {
                self.print(&format!("cargo:rustc-link-lib={}", stdlib));
            }
        }
    }

    #[cfg(feature = "parallel")]
    fn compile_objects(&self, objs: &[(PathBuf, PathBuf)]) {
        use self::rayon::prelude::*;

        let mut cfg = rayon::Configuration::new();
        if let Ok(amt) = env::var("NUM_JOBS") {
            if let Ok(amt) = amt.parse() {
                cfg = cfg.num_threads(amt);
            }
        }
        drop(rayon::initialize(cfg));

        objs.par_iter().with_max_len(1)
            .for_each(|&(ref src, ref dst)| self.compile_object(src, dst));
    }

    #[cfg(not(feature = "parallel"))]
    fn compile_objects(&self, objs: &[(PathBuf, PathBuf)]) {
        for &(ref src, ref dst) in objs {
            self.compile_object(src, dst);
        }
    }

    fn compile_object(&self, file: &Path, dst: &Path) {
        let is_asm = file.extension().and_then(|s| s.to_str()) == Some("asm");
        let msvc = self.get_target().contains("msvc");
        let (mut cmd, name) = if msvc && is_asm {
            self.msvc_macro_assembler()
        } else {
            let compiler = self.get_compiler();
            let mut cmd = compiler.to_command();
            for &(ref a, ref b) in self.env.iter() {
                cmd.env(a, b);
            }
            (cmd,
             compiler.path
                 .file_name()
                 .unwrap()
                 .to_string_lossy()
                 .into_owned())
        };
        if msvc && is_asm {
            cmd.arg("/Fo").arg(dst);
        } else if msvc {
            let mut s = OsString::from("/Fo");
            s.push(&dst);
            cmd.arg(s);
        } else {
            cmd.arg("-o").arg(&dst);
        }
        cmd.arg(if msvc { "/c" } else { "-c" });
        cmd.arg(file);

        run(&mut cmd, &name);
    }

    /// Run the compiler, returning the macro-expanded version of the input files.
    ///
    /// This is only relevant for C and C++ files.
    pub fn expand(&self) -> Vec<u8> {
        let compiler = self.get_compiler();
        let mut cmd = compiler.to_command();
        for &(ref a, ref b) in self.env.iter() {
            cmd.env(a, b);
        }
        cmd.arg(compiler.family.expand_flag());
        for file in self.files.iter() {
            cmd.arg(file);
        }

        let name = compiler.path
            .file_name()
            .unwrap()
            .to_string_lossy()
            .into_owned();

        run_output(&mut cmd, &name)
    }

    /// Get the compiler that's in use for this configuration.
    ///
    /// This function will return a `Tool` which represents the culmination
    /// of this configuration at a snapshot in time. The returned compiler can
    /// be inspected (e.g. the path, arguments, environment) to forward along to
    /// other tools, or the `to_command` method can be used to invoke the
    /// compiler itself.
    ///
    /// This method will take into account all configuration such as debug
    /// information, optimization level, include directories, defines, etc.
    /// Additionally, the compiler binary in use follows the standard
    /// conventions for this path, e.g. looking at the explicitly set compiler,
    /// environment variables (a number of which are inspected here), and then
    /// falling back to the default configuration.
    pub fn get_compiler(&self) -> Tool {
        let opt_level = self.get_opt_level();
        let target = self.get_target();

        let mut cmd = self.get_base_compiler();
        let nvcc = cmd.path.file_name()
            .and_then(|p| p.to_str()).map(|p| p.contains("nvcc"))
            .unwrap_or(false);

        // Non-target flags
        // If the flag is not conditioned on target variable, it belongs here :)
        match cmd.family {
            ToolFamily::Msvc => {
                cmd.args.push("/nologo".into());

                let crt_flag = match self.static_crt {
                    Some(true) => "/MT",
                    Some(false) => "/MD",
                    None => {
                        let features = env::var("CARGO_CFG_TARGET_FEATURE")
                                  .unwrap_or(String::new());
                        if features.contains("crt-static") {
                            "/MT"
                        } else {
                            "/MD"
                        }
                    },
                };
                cmd.args.push(crt_flag.into());

                match &opt_level[..] {
                    "z" | "s" => cmd.args.push("/Os".into()),
                    "1" => cmd.args.push("/O1".into()),
                    // -O3 is a valid value for gcc and clang compilers, but not msvc. Cap to /O2.
                    "2" | "3" => cmd.args.push("/O2".into()),
                    _ => {}
                }
            }
            ToolFamily::Gnu |
            ToolFamily::Clang => {
                cmd.args.push(format!("-O{}", opt_level).into());
                if !nvcc {
                    cmd.args.push("-ffunction-sections".into());
                    cmd.args.push("-fdata-sections".into());
                    if self.pic.unwrap_or(!target.contains("windows-gnu")) {
                        cmd.args.push("-fPIC".into());
                    }
                } else if self.pic.unwrap_or(false) {
                    cmd.args.push("-Xcompiler".into());
                    cmd.args.push("\'-fPIC\'".into());
                }
            }
        }
        for arg in self.envflags(if self.cpp {"CXXFLAGS"} else {"CFLAGS"}) {
            cmd.args.push(arg.into());
        }

        if self.get_debug() {
            cmd.args.push(cmd.family.debug_flag().into());
        }

        // Target flags
        match cmd.family {
            ToolFamily::Clang => {
                cmd.args.push(format!("--target={}", target).into());
            }
            ToolFamily::Msvc => {
                if target.contains("i586") {
                    cmd.args.push("/ARCH:IA32".into());
                }
            }
            ToolFamily::Gnu => {
                if target.contains("i686") || target.contains("i586") {
                    cmd.args.push("-m32".into());
                } else if target.contains("x86_64") || target.contains("powerpc64") {
                    cmd.args.push("-m64".into());
                }

                if target.contains("musl") {
                    cmd.args.push("-static".into());
                }

                // armv7 targets get to use armv7 instructions
                if target.starts_with("armv7-") && target.contains("-linux-") {
                    cmd.args.push("-march=armv7-a".into());
                }

                // On android we can guarantee some extra float instructions
                // (specified in the android spec online)
                if target.starts_with("armv7-linux-androideabi") {
                    cmd.args.push("-march=armv7-a".into());
                    cmd.args.push("-mfpu=vfpv3-d16".into());
                    cmd.args.push("-mfloat-abi=softfp".into());
                }

                // For us arm == armv6 by default
                if target.starts_with("arm-unknown-linux-") {
                    cmd.args.push("-march=armv6".into());
                    cmd.args.push("-marm".into());
                }

                // We can guarantee some settings for FRC
                if target.starts_with("arm-frc-") {
                    cmd.args.push("-march=armv7-a".into());
                    cmd.args.push("-mcpu=cortex-a9".into());
                    cmd.args.push("-mfpu=vfpv3".into());
                    cmd.args.push("-mfloat-abi=softfp".into());
                    cmd.args.push("-marm".into());
                }

                // Turn codegen down on i586 to avoid some instructions.
                if target.starts_with("i586-unknown-linux-") {
                    cmd.args.push("-march=pentium".into());
                }

                // Set codegen level for i686 correctly
                if target.starts_with("i686-unknown-linux-") {
                    cmd.args.push("-march=i686".into());
                }

                // Looks like `musl-gcc` makes is hard for `-m32` to make its way
                // all the way to the linker, so we need to actually instruct the
                // linker that we're generating 32-bit executables as well. This'll
                // typically only be used for build scripts which transitively use
                // these flags that try to compile executables.
                if target == "i686-unknown-linux-musl" {
                    cmd.args.push("-Wl,-melf_i386".into());
                }

                if target.starts_with("thumb") {
                    cmd.args.push("-mthumb".into());

                    if target.ends_with("eabihf") {
                        cmd.args.push("-mfloat-abi=hard".into())
                    }
                }
                if target.starts_with("thumbv6m") {
                    cmd.args.push("-march=armv6s-m".into());
                }
                if target.starts_with("thumbv7em") {
                    cmd.args.push("-march=armv7e-m".into());

                    if target.ends_with("eabihf") {
                        cmd.args.push("-mfpu=fpv4-sp-d16".into())
                    }
                }
                if target.starts_with("thumbv7m") {
                    cmd.args.push("-march=armv7-m".into());
                }
            }
        }

        if target.contains("-ios") {
            // FIXME: potential bug. iOS is always compiled with Clang, but Gcc compiler may be
            // detected instead.
            self.ios_flags(&mut cmd);
        }

        if self.cpp {
            match (self.cpp_set_stdlib.as_ref(), cmd.family) {
                (None, _) => { }
                (Some(stdlib), ToolFamily::Gnu) |
                (Some(stdlib), ToolFamily::Clang) => {
                    cmd.args.push(format!("-stdlib=lib{}", stdlib).into());
                }
                _ => {
                    println!("cargo:warning=cpp_set_stdlib is specified, but the {:?} compiler \
                              does not support this option, ignored", cmd.family);
                }
            }
        }

        for directory in self.include_directories.iter() {
            cmd.args.push(cmd.family.include_flag().into());
            cmd.args.push(directory.into());
        }

        for flag in self.flags.iter() {
            cmd.args.push(flag.into());
        }

        for &(ref key, ref value) in self.definitions.iter() {
            let lead = if let ToolFamily::Msvc = cmd.family {"/"} else {"-"};
            if let Some(ref value) = *value {
                cmd.args.push(format!("{}D{}={}", lead, key, value).into());
            } else {
                cmd.args.push(format!("{}D{}", lead, key).into());
            }
        }
        cmd
    }

    fn msvc_macro_assembler(&self) -> (Command, String) {
        let target = self.get_target();
        let tool = if target.contains("x86_64") {
            "ml64.exe"
        } else {
            "ml.exe"
        };
        let mut cmd = windows_registry::find(&target, tool).unwrap_or_else(|| self.cmd(tool));
        for directory in self.include_directories.iter() {
            cmd.arg("/I").arg(directory);
        }
        for &(ref key, ref value) in self.definitions.iter() {
            if let Some(ref value) = *value {
                cmd.arg(&format!("/D{}={}", key, value));
            } else {
                cmd.arg(&format!("/D{}", key));
            }
        }

        if target.contains("i686") || target.contains("i586") {
            cmd.arg("/safeseh");
        }
        for flag in self.flags.iter() {
            cmd.arg(flag);
        }

        (cmd, tool.to_string())
    }

    fn assemble(&self, lib_name: &str, dst: &Path, objects: &[PathBuf]) {
        // Delete the destination if it exists as the `ar` tool at least on Unix
        // appends to it, which we don't want.
        let _ = fs::remove_file(&dst);

        let target = self.get_target();
        if target.contains("msvc") {
            let mut cmd = match self.archiver {
                Some(ref s) => self.cmd(s),
                None => windows_registry::find(&target, "lib.exe").unwrap_or_else(|| self.cmd("lib.exe")),
            };
            let mut out = OsString::from("/OUT:");
            out.push(dst);
            run(cmd.arg(out)
                    .arg("/nologo")
                    .args(objects)
                    .args(&self.objects),
                "lib.exe");

            // The Rust compiler will look for libfoo.a and foo.lib, but the
            // MSVC linker will also be passed foo.lib, so be sure that both
            // exist for now.
            let lib_dst = dst.with_file_name(format!("{}.lib", lib_name));
            let _ = fs::remove_file(&lib_dst);
            fs::hard_link(&dst, &lib_dst)
                .or_else(|_| {
                    // if hard-link fails, just copy (ignoring the number of bytes written)
                    fs::copy(&dst, &lib_dst).map(|_| ())
                })
                .expect("Copying from {:?} to {:?} failed.");;
        } else {
            let ar = self.get_ar();
            let cmd = ar.file_name().unwrap().to_string_lossy();
            run(self.cmd(&ar)
                    .arg("crs")
                    .arg(dst)
                    .args(objects)
                    .args(&self.objects),
                &cmd);
        }
    }

    fn ios_flags(&self, cmd: &mut Tool) {
        enum ArchSpec {
            Device(&'static str),
            Simulator(&'static str),
        }

        let target = self.get_target();
        let arch = target.split('-').nth(0).unwrap();
        let arch = match arch {
            "arm" | "armv7" | "thumbv7" => ArchSpec::Device("armv7"),
            "armv7s" | "thumbv7s" => ArchSpec::Device("armv7s"),
            "arm64" | "aarch64" => ArchSpec::Device("arm64"),
            "i386" | "i686" => ArchSpec::Simulator("-m32"),
            "x86_64" => ArchSpec::Simulator("-m64"),
            _ => fail("Unknown arch for iOS target"),
        };

        let sdk = match arch {
            ArchSpec::Device(arch) => {
                cmd.args.push("-arch".into());
                cmd.args.push(arch.into());
                cmd.args.push("-miphoneos-version-min=7.0".into());
                "iphoneos"
            }
            ArchSpec::Simulator(arch) => {
                cmd.args.push(arch.into());
                cmd.args.push("-mios-simulator-version-min=7.0".into());
                "iphonesimulator"
            }
        };

        self.print(&format!("Detecting iOS SDK path for {}", sdk));
        let sdk_path = self.cmd("xcrun")
            .arg("--show-sdk-path")
            .arg("--sdk")
            .arg(sdk)
            .stderr(Stdio::inherit())
            .output()
            .unwrap()
            .stdout;

        let sdk_path = String::from_utf8(sdk_path).unwrap();

        cmd.args.push("-isysroot".into());
        cmd.args.push(sdk_path.trim().into());
    }

    fn cmd<P: AsRef<OsStr>>(&self, prog: P) -> Command {
        let mut cmd = Command::new(prog);
        for &(ref a, ref b) in self.env.iter() {
            cmd.env(a, b);
        }
        cmd
    }

    fn get_base_compiler(&self) -> Tool {
        if let Some(ref c) = self.compiler {
            return Tool::new(c.clone());
        }
        let host = self.get_host();
        let target = self.get_target();
        let (env, msvc, gnu) = if self.cpp {
            ("CXX", "cl.exe", "g++")
        } else {
            ("CC", "cl.exe", "gcc")
        };

        let default = if host.contains("solaris") {
            // In this case, c++/cc unlikely to exist or be correct.
            gnu
        } else if self.cpp {
            "c++"
        } else {
            "cc"
        };

        self.env_tool(env)
            .map(|(tool, args)| {
                let mut t = Tool::new(PathBuf::from(tool));
                for arg in args {
                    t.args.push(arg.into());
                }
                t
            })
            .or_else(|| {
                if target.contains("emscripten") {
                    //Windows uses bat file so we have to be a bit more specific
                    let tool = if self.cpp {
                        if cfg!(windows) {
                            "em++.bat"
                        } else {
                            "em++"
                        }
                    } else {
                        if cfg!(windows) {
                            "emcc.bat"
                        } else {
                            "emcc"
                        }
                    };

                    Some(Tool::new(PathBuf::from(tool)))
                } else {
                    None
                }
            })
            .or_else(|| windows_registry::find_tool(&target, "cl.exe"))
            .unwrap_or_else(|| {
                let compiler = if host.contains("windows") && target.contains("windows") {
                    if target.contains("msvc") {
                        msvc.to_string()
                    } else {
                        format!("{}.exe", gnu)
                    }
                } else if target.contains("android") {
                    format!("{}-{}", target.replace("armv7", "arm"), gnu)
                } else if self.get_host() != target {
                    // CROSS_COMPILE is of the form: "arm-linux-gnueabi-"
                    let cc_env = self.getenv("CROSS_COMPILE");
                    let cross_compile = cc_env.as_ref().map(|s| s.trim_right_matches('-'));
                    let prefix = cross_compile.or(match &target[..] {
                        "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu"),
                        "arm-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"),
                        "arm-frc-linux-gnueabi" => Some("arm-frc-linux-gnueabi"),
                        "arm-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"),
                        "arm-unknown-linux-musleabi" => Some("arm-linux-musleabi"),
                        "arm-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"),
                        "arm-unknown-netbsdelf-eabi" => Some("arm--netbsdelf-eabi"),
                        "armv6-unknown-netbsdelf-eabihf" => Some("armv6--netbsdelf-eabihf"),
                        "armv7-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"),
                        "armv7-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"),
                        "armv7-unknown-netbsdelf-eabihf" => Some("armv7--netbsdelf-eabihf"),
                        "i686-pc-windows-gnu" => Some("i686-w64-mingw32"),
                        "i686-unknown-linux-musl" => Some("musl"),
                        "i686-unknown-netbsdelf" => Some("i486--netbsdelf"),
                        "mips-unknown-linux-gnu" => Some("mips-linux-gnu"),
                        "mipsel-unknown-linux-gnu" => Some("mipsel-linux-gnu"),
                        "mips64-unknown-linux-gnuabi64" => Some("mips64-linux-gnuabi64"),
                        "mips64el-unknown-linux-gnuabi64" => Some("mips64el-linux-gnuabi64"),
                        "powerpc-unknown-linux-gnu" => Some("powerpc-linux-gnu"),
                        "powerpc-unknown-netbsd" => Some("powerpc--netbsd"),
                        "powerpc64-unknown-linux-gnu" => Some("powerpc-linux-gnu"),
                        "powerpc64le-unknown-linux-gnu" => Some("powerpc64le-linux-gnu"),
                        "s390x-unknown-linux-gnu" => Some("s390x-linux-gnu"),
                        "sparc64-unknown-netbsd" => Some("sparc64--netbsd"),
                        "sparcv9-sun-solaris" => Some("sparcv9-sun-solaris"),
                        "thumbv6m-none-eabi" => Some("arm-none-eabi"),
                        "thumbv7em-none-eabi" => Some("arm-none-eabi"),
                        "thumbv7em-none-eabihf" => Some("arm-none-eabi"),
                        "thumbv7m-none-eabi" => Some("arm-none-eabi"),
                        "x86_64-pc-windows-gnu" => Some("x86_64-w64-mingw32"),
                        "x86_64-rumprun-netbsd" => Some("x86_64-rumprun-netbsd"),
                        "x86_64-unknown-linux-musl" => Some("musl"),
                        "x86_64-unknown-netbsd" => Some("x86_64--netbsd"),
                        _ => None,
                    });
                    match prefix {
                        Some(prefix) => format!("{}-{}", prefix, gnu),
                        None => default.to_string(),
                    }
                } else {
                    default.to_string()
                };
                Tool::new(PathBuf::from(compiler))
            })
    }

    fn get_var(&self, var_base: &str) -> Result<String, String> {
        let target = self.get_target();
        let host = self.get_host();
        let kind = if host == target { "HOST" } else { "TARGET" };
        let target_u = target.replace("-", "_");
        let res = self.getenv(&format!("{}_{}", var_base, target))
            .or_else(|| self.getenv(&format!("{}_{}", var_base, target_u)))
            .or_else(|| self.getenv(&format!("{}_{}", kind, var_base)))
            .or_else(|| self.getenv(var_base));

        match res {
            Some(res) => Ok(res),
            None => Err("could not get environment variable".to_string()),
        }
    }

    fn envflags(&self, name: &str) -> Vec<String> {
        self.get_var(name)
            .unwrap_or(String::new())
            .split(|c: char| c.is_whitespace())
            .filter(|s| !s.is_empty())
            .map(|s| s.to_string())
            .collect()
    }

    fn env_tool(&self, name: &str) -> Option<(String, Vec<String>)> {
        self.get_var(name).ok().map(|tool| {
            let whitelist = ["ccache", "distcc"];
            for t in whitelist.iter() {
                if tool.starts_with(t) && tool[t.len()..].starts_with(' ') {
                    return (t.to_string(), vec![tool[t.len()..].trim_left().to_string()]);
                }
            }
            (tool, Vec::new())
        })
    }

    /// Returns the default C++ standard library for the current target: `libc++`
    /// for OS X and `libstdc++` for anything else.
    fn get_cpp_link_stdlib(&self) -> Option<String> {
        self.cpp_link_stdlib.clone().unwrap_or_else(|| {
            let target = self.get_target();
            if target.contains("msvc") {
                None
            } else if target.contains("darwin") {
                Some("c++".to_string())
            } else if target.contains("freebsd") {
                Some("c++".to_string())
            } else {
                Some("stdc++".to_string())
            }
        })
    }

    fn get_ar(&self) -> PathBuf {
        self.archiver
            .clone()
            .or_else(|| self.get_var("AR").map(PathBuf::from).ok())
            .unwrap_or_else(|| {
                if self.get_target().contains("android") {
                    PathBuf::from(format!("{}-ar", self.get_target().replace("armv7", "arm")))
                } else if self.get_target().contains("emscripten") {
                    //Windows use bat files so we have to be a bit more specific
                    let tool = if cfg!(windows) {
                        "emar.bat"
                    } else {
                        "emar"
                    };

                    PathBuf::from(tool)
                } else {
                    PathBuf::from("ar")
                }
            })
    }

    fn get_target(&self) -> String {
        self.target.clone().unwrap_or_else(|| self.getenv_unwrap("TARGET"))
    }

    fn get_host(&self) -> String {
        self.host.clone().unwrap_or_else(|| self.getenv_unwrap("HOST"))
    }

    fn get_opt_level(&self) -> String {
        self.opt_level.as_ref().cloned().unwrap_or_else(|| self.getenv_unwrap("OPT_LEVEL"))
    }

    fn get_debug(&self) -> bool {
        self.debug.unwrap_or_else(|| self.getenv_unwrap("PROFILE") == "debug")
    }

    fn get_out_dir(&self) -> PathBuf {
        self.out_dir.clone().unwrap_or_else(|| env::var_os("OUT_DIR").map(PathBuf::from).unwrap())
    }

    fn getenv(&self, v: &str) -> Option<String> {
        let r = env::var(v).ok();
        self.print(&format!("{} = {:?}", v, r));
        r
    }

    fn getenv_unwrap(&self, v: &str) -> String {
        match self.getenv(v) {
            Some(s) => s,
            None => fail(&format!("environment variable `{}` not defined", v)),
        }
    }

    fn print(&self, s: &str) {
        if self.cargo_metadata {
            println!("{}", s);
        }
    }
}

impl Tool {
    fn new(path: PathBuf) -> Tool {
        // Try to detect family of the tool from its name, falling back to Gnu.
        let family = if let Some(fname) = path.file_name().and_then(|p| p.to_str()) {
            if fname.contains("clang") {
                ToolFamily::Clang
            } else if fname.contains("cl") && !fname.contains("uclibc") {
                ToolFamily::Msvc
            } else {
                ToolFamily::Gnu
            }
        } else {
            ToolFamily::Gnu
        };
        Tool {
            path: path,
            args: Vec::new(),
            env: Vec::new(),
            family: family
        }
    }

    /// Converts this compiler into a `Command` that's ready to be run.
    ///
    /// This is useful for when the compiler needs to be executed and the
    /// command returned will already have the initial arguments and environment
    /// variables configured.
    pub fn to_command(&self) -> Command {
        let mut cmd = Command::new(&self.path);
        cmd.args(&self.args);
        for &(ref k, ref v) in self.env.iter() {
            cmd.env(k, v);
        }
        cmd
    }

    /// Returns the path for this compiler.
    ///
    /// Note that this may not be a path to a file on the filesystem, e.g. "cc",
    /// but rather something which will be resolved when a process is spawned.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the default set of arguments to the compiler needed to produce
    /// executables for the target this compiler generates.
    pub fn args(&self) -> &[OsString] {
        &self.args
    }

    /// Returns the set of environment variables needed for this compiler to
    /// operate.
    ///
    /// This is typically only used for MSVC compilers currently.
    pub fn env(&self) -> &[(OsString, OsString)] {
        &self.env
    }
}

fn run(cmd: &mut Command, program: &str) {
    let (mut child, print) = spawn(cmd, program);
    let status = child.wait().expect("failed to wait on child process");
    print.join().unwrap();
    println!("{}", status);
    if !status.success() {
        fail(&format!("command did not execute successfully, got: {}", status));
    }
}

fn run_output(cmd: &mut Command, program: &str) -> Vec<u8> {
    cmd.stdout(Stdio::piped());
    let (mut child, print) = spawn(cmd, program);
    let mut stdout = vec![];
    child.stdout.take().unwrap().read_to_end(&mut stdout).unwrap();
    let status = child.wait().expect("failed to wait on child process");
    print.join().unwrap();
    println!("{}", status);
    if !status.success() {
        fail(&format!("command did not execute successfully, got: {}", status));
    }
    stdout
}

fn spawn(cmd: &mut Command, program: &str) -> (Child, JoinHandle<()>) {
    println!("running: {:?}", cmd);

    // Capture the standard error coming from these programs, and write it out
    // with cargo:warning= prefixes. Note that this is a bit wonky to avoid
    // requiring the output to be UTF-8, we instead just ship bytes from one
    // location to another.
    match cmd.stderr(Stdio::piped()).spawn() {
        Ok(mut child) => {
            let stderr = BufReader::new(child.stderr.take().unwrap());
            let print = thread::spawn(move || {
                for line in stderr.split(b'\n').filter_map(|l| l.ok()) {
                    print!("cargo:warning=");
                    std::io::stdout().write_all(&line).unwrap();
                    println!("");
                }
            });
            (child, print)
        }
        Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
            let extra = if cfg!(windows) {
                " (see https://github.com/alexcrichton/gcc-rs#compile-time-requirements \
                   for help)"
            } else {
                ""
            };
            fail(&format!("failed to execute command: {}\nIs `{}` \
                           not installed?{}",
                          e,
                          program,
                          extra));
        }
        Err(e) => fail(&format!("failed to execute command: {}", e)),
    }
}

fn fail(s: &str) -> ! {
    println!("\n\n{}\n\n", s);
    panic!()
}