|
| 1 | +use std::env; |
| 2 | +use std::path::Path; |
| 3 | +use std::process::{Command, Output}; |
| 4 | + |
| 5 | +use crate::{bin_name, cygpath_windows, handle_failed_output, is_msvc, is_windows, tmp_dir, uname}; |
| 6 | + |
| 7 | +/// Construct a new platform-specific C compiler invocation. |
| 8 | +/// |
| 9 | +/// WARNING: This means that what flags are accepted by the underlying C compiler is |
| 10 | +/// platform- AND compiler-specific. Consult the relevant docs for `gcc`, `clang` and `mvsc`. |
| 11 | +pub fn cc() -> Cc { |
| 12 | + Cc::new() |
| 13 | +} |
| 14 | + |
| 15 | +/// A platform-specific C compiler invocation builder. The specific C compiler used is |
| 16 | +/// passed down from compiletest. |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct Cc { |
| 19 | + cmd: Command, |
| 20 | +} |
| 21 | + |
| 22 | +impl Cc { |
| 23 | + /// Construct a new platform-specific C compiler invocation. |
| 24 | + /// |
| 25 | + /// WARNING: This means that what flags are accepted by the underlying C compile is |
| 26 | + /// platform- AND compiler-specific. Consult the relevant docs for `gcc`, `clang` and `mvsc`. |
| 27 | + pub fn new() -> Self { |
| 28 | + let compiler = env::var("CC").unwrap(); |
| 29 | + |
| 30 | + let mut cmd = Command::new(compiler); |
| 31 | + |
| 32 | + let default_cflags = env::var("CC_DEFAULT_FLAGS").unwrap(); |
| 33 | + for flag in default_cflags.split(char::is_whitespace) { |
| 34 | + cmd.arg(flag); |
| 35 | + } |
| 36 | + |
| 37 | + Self { cmd } |
| 38 | + } |
| 39 | + |
| 40 | + /// Specify path of the input file. |
| 41 | + pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { |
| 42 | + self.cmd.arg(path.as_ref()); |
| 43 | + self |
| 44 | + } |
| 45 | + |
| 46 | + /// Add a *platform-and-compiler-specific* argument. Please consult the docs for the various |
| 47 | + /// possible C compilers on the various platforms to check which arguments are legal for |
| 48 | + /// which compiler. |
| 49 | + pub fn arg(&mut self, flag: &str) -> &mut Self { |
| 50 | + self.cmd.arg(flag); |
| 51 | + self |
| 52 | + } |
| 53 | + |
| 54 | + /// Add multiple *platform-and-compiler-specific* arguments. Please consult the docs for the |
| 55 | + /// various possible C compilers on the various platforms to check which arguments are legal |
| 56 | + /// for which compiler. |
| 57 | + pub fn args(&mut self, args: &[&str]) -> &mut Self { |
| 58 | + self.cmd.args(args); |
| 59 | + self |
| 60 | + } |
| 61 | + |
| 62 | + /// Specify `-o` or `-Fe`/`-Fo` depending on platform/compiler. This assumes that the executable |
| 63 | + /// is under `$TMPDIR`. |
| 64 | + pub fn out_exe(&mut self, name: &str) -> &mut Self { |
| 65 | + // Ref: tools.mk (irrelevant lines omitted): |
| 66 | + // |
| 67 | + // ```makefile |
| 68 | + // ifdef IS_MSVC |
| 69 | + // OUT_EXE=-Fe:`cygpath -w $(TMPDIR)/$(call BIN,$(1))` \ |
| 70 | + // -Fo:`cygpath -w $(TMPDIR)/$(1).obj` |
| 71 | + // else |
| 72 | + // OUT_EXE=-o $(TMPDIR)/$(1) |
| 73 | + // endif |
| 74 | + // ``` |
| 75 | + |
| 76 | + if is_msvc() { |
| 77 | + let fe_path = cygpath_windows(tmp_dir().join(bin_name(name))); |
| 78 | + let fo_path = cygpath_windows(tmp_dir().join(format!("{name}.obj"))); |
| 79 | + self.cmd.arg(format!("-Fe:{fe_path}")); |
| 80 | + self.cmd.arg(format!("-Fo:{fo_path}")); |
| 81 | + } else { |
| 82 | + self.cmd.arg("-o"); |
| 83 | + self.cmd.arg(tmp_dir().join(name)); |
| 84 | + } |
| 85 | + |
| 86 | + self |
| 87 | + } |
| 88 | + |
| 89 | + /// Run the constructed C invocation command and assert that it is successfully run. |
| 90 | + #[track_caller] |
| 91 | + pub fn run(&mut self) -> Output { |
| 92 | + let caller_location = std::panic::Location::caller(); |
| 93 | + let caller_line_number = caller_location.line(); |
| 94 | + |
| 95 | + let output = self.cmd.output().unwrap(); |
| 96 | + if !output.status.success() { |
| 97 | + handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number); |
| 98 | + } |
| 99 | + output |
| 100 | + } |
| 101 | + |
| 102 | + /// Inspect what the underlying [`Command`] is up to the current construction. |
| 103 | + pub fn inspect(&mut self, f: impl FnOnce(&Command)) -> &mut Self { |
| 104 | + f(&self.cmd); |
| 105 | + self |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// `EXTRACFLAGS` |
| 110 | +pub fn extra_c_flags() -> Vec<&'static str> { |
| 111 | + // Adapted from tools.mk (trimmed): |
| 112 | + // |
| 113 | + // ```makefile |
| 114 | + // ifdef IS_WINDOWS |
| 115 | + // ifdef IS_MSVC |
| 116 | + // EXTRACFLAGS := ws2_32.lib userenv.lib advapi32.lib bcrypt.lib ntdll.lib synchronization.lib |
| 117 | + // else |
| 118 | + // EXTRACFLAGS := -lws2_32 -luserenv -lbcrypt -lntdll -lsynchronization |
| 119 | + // endif |
| 120 | + // else |
| 121 | + // ifeq ($(UNAME),Darwin) |
| 122 | + // EXTRACFLAGS := -lresolv |
| 123 | + // else |
| 124 | + // ifeq ($(UNAME),FreeBSD) |
| 125 | + // EXTRACFLAGS := -lm -lpthread -lgcc_s |
| 126 | + // else |
| 127 | + // ifeq ($(UNAME),SunOS) |
| 128 | + // EXTRACFLAGS := -lm -lpthread -lposix4 -lsocket -lresolv |
| 129 | + // else |
| 130 | + // ifeq ($(UNAME),OpenBSD) |
| 131 | + // EXTRACFLAGS := -lm -lpthread -lc++abi |
| 132 | + // else |
| 133 | + // EXTRACFLAGS := -lm -lrt -ldl -lpthread |
| 134 | + // endif |
| 135 | + // endif |
| 136 | + // endif |
| 137 | + // endif |
| 138 | + // endif |
| 139 | + // ``` |
| 140 | + |
| 141 | + if is_windows() { |
| 142 | + if is_msvc() { |
| 143 | + vec![ |
| 144 | + "ws2_32.lib", |
| 145 | + "userenv.lib", |
| 146 | + "advapi32.lib", |
| 147 | + "bcrypt.lib", |
| 148 | + "ntdll.lib", |
| 149 | + "synchronization.lib", |
| 150 | + ] |
| 151 | + } else { |
| 152 | + vec!["-lws2_32", "-luserenv", "-lbcrypt", "-lntdll", "-lsynchronization"] |
| 153 | + } |
| 154 | + } else { |
| 155 | + match uname() { |
| 156 | + n if n.contains("Darwin") => vec!["-lresolv"], |
| 157 | + n if n.contains("FreeBSD") => vec!["-lm", "-lpthread", "-lgcc_s"], |
| 158 | + n if n.contains("SunOS") => { |
| 159 | + vec!["-lm", "-lpthread", "-lposix4", "-lsocket", "-lresolv"] |
| 160 | + } |
| 161 | + n if n.contains("OpenBSD") => vec!["-lm", "-lpthread", "-lc++abi"], |
| 162 | + _ => vec!["-lm", "-lrt", "-ldl", "-lpthread"], |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +/// `EXTRACXXFLAGS` |
| 168 | +pub fn extra_cxx_flags() -> Vec<&'static str> { |
| 169 | + // Adapted from tools.mk (trimmed): |
| 170 | + // |
| 171 | + // ```makefile |
| 172 | + // ifdef IS_WINDOWS |
| 173 | + // ifdef IS_MSVC |
| 174 | + // else |
| 175 | + // EXTRACXXFLAGS := -lstdc++ |
| 176 | + // endif |
| 177 | + // else |
| 178 | + // ifeq ($(UNAME),Darwin) |
| 179 | + // EXTRACXXFLAGS := -lc++ |
| 180 | + // else |
| 181 | + // ifeq ($(UNAME),FreeBSD) |
| 182 | + // else |
| 183 | + // ifeq ($(UNAME),SunOS) |
| 184 | + // else |
| 185 | + // ifeq ($(UNAME),OpenBSD) |
| 186 | + // else |
| 187 | + // EXTRACXXFLAGS := -lstdc++ |
| 188 | + // endif |
| 189 | + // endif |
| 190 | + // endif |
| 191 | + // endif |
| 192 | + // endif |
| 193 | + // ``` |
| 194 | + if is_windows() { |
| 195 | + if is_msvc() { vec![] } else { vec!["-lstdc++"] } |
| 196 | + } else { |
| 197 | + match uname() { |
| 198 | + n if n.contains("Darwin") => vec!["-lc++"], |
| 199 | + _ => vec!["-lstdc++"], |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments