| @@ -49,12 +49,17 @@ pub struct CcOptions { |
| 49 | 49 | /// Native dependencies to build and link (from the packages' |
| 50 | 50 | /// manifests, ADR 0005). |
| 51 | 51 | pub natives: Vec<NativeDep>, |
| 52 | + /// The runtime archive to link (default: `$LOUISE_RUNTIME`, else by |
| 53 | + /// profile; see [`runtime_archive`]). |
| 54 | + pub runtime: Option<Runtime>, |
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | #[derive(Debug)] |
| 55 | 58 | pub enum BuildError { |
| 56 | | − /// `liblouise_rt.a` was not found. |
| 57 | | − NoRuntime(String), |
| 59 | + /// The runtime archive (its file name) was not found in these places. |
| 60 | + NoRuntime(String, String), |
| 61 | + /// `$LOUISE_RUNTIME` names no runtime archive (its value). |
| 62 | + BadRuntime(String), |
| 58 | 63 | Io(std::io::Error), |
| 59 | 64 | /// The C compiler failed (its output). |
| 60 | 65 | Cc(String), |
| @@ -63,10 +68,13 @@ pub enum BuildError { |
| 63 | 68 | impl std::fmt::Display for BuildError { |
| 64 | 69 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 65 | 70 | match self { |
| 66 | | − BuildError::NoRuntime(searched) => write!( |
| 71 | + BuildError::NoRuntime(file, searched) => write!( |
| 67 | 72 | f, |
| 68 | | − "cannot find the runtime library liblouise_rt.a (searched {searched}); build it with `cargo build -p louise_rt` or set LOUISE_LIB_DIR" |
| 73 | + "cannot find the runtime library {file} (searched {searched}); build it with `cargo build -p louise_rt` (scripts/runtime.sh for liblouise_rt-fast.a) or set LOUISE_LIB_DIR" |
| 69 | 74 | ), |
| 75 | + BuildError::BadRuntime(v) => { |
| 76 | + write!(f, "LOUISE_RUNTIME is `{v}`: it may be `checked` or `fast`") |
| 77 | + } |
| 70 | 78 | BuildError::Io(e) => write!(f, "{e}"), |
| 71 | 79 | BuildError::Cc(out) => write!(f, "the C compiler failed:\n{out}"), |
| 72 | 80 | } |
| @@ -79,7 +87,68 @@ impl From<std::io::Error> for BuildError { |
| 79 | 87 | } |
| 80 | 88 | } |
| 81 | 89 | |
| 82 | | −fn runtime_dir(opts: &CcOptions) -> Result<PathBuf, BuildError> { |
| 90 | +/// Which runtime archive a program links (ADR 0031). |
| 91 | +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 92 | +pub enum Runtime { |
| 93 | + /// `liblouise_rt.a`: with the leak oracle (`LOUISE_LEAKS`), for debug |
| 94 | + /// builds, `run` and `test --native`, and every test that counts leaks. |
| 95 | + Checked, |
| 96 | + /// `liblouise_rt-fast.a`: whole-program LTO, one codegen unit, aborts |
| 97 | + /// on a runtime panic, no leak oracle; for release builds. |
| 98 | + Fast, |
| 99 | +} |
| 100 | + |
| 101 | +impl Runtime { |
| 102 | + fn file(self) -> &'static str { |
| 103 | + match self { |
| 104 | + Runtime::Checked => "liblouise_rt.a", |
| 105 | + Runtime::Fast => "liblouise_rt-fast.a", |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// `$LOUISE_RUNTIME` (`checked` or `fast`), if it is set. |
| 110 | + fn from_env() -> Result<Option<Runtime>, BuildError> { |
| 111 | + match std::env::var("LOUISE_RUNTIME").ok().as_deref() { |
| 112 | + None | Some("") => Ok(None), |
| 113 | + Some("checked") => Ok(Some(Runtime::Checked)), |
| 114 | + Some("fast") => Ok(Some(Runtime::Fast)), |
| 115 | + Some(other) => Err(BuildError::BadRuntime(other.to_owned())), |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/// The runtime archive `opts` link: the one asked for (`opts.runtime`, |
| 121 | +/// else `$LOUISE_RUNTIME`), else `fast` for a release build and `checked` |
| 122 | +/// otherwise. A release build falls back to `checked`, with a warning, |
| 123 | +/// when `fast` was not built (`scripts/runtime.sh`). |
| 124 | +pub fn runtime_archive(opts: &CcOptions) -> Result<PathBuf, BuildError> { |
| 125 | + let asked = match opts.runtime { |
| 126 | + Some(r) => Some(r), |
| 127 | + None => Runtime::from_env()?, |
| 128 | + }; |
| 129 | + let wanted = asked.unwrap_or(if opts.release { |
| 130 | + Runtime::Fast |
| 131 | + } else { |
| 132 | + Runtime::Checked |
| 133 | + }); |
| 134 | + match runtime_dir(opts, wanted.file()) { |
| 135 | + Ok(dir) => Ok(dir.join(wanted.file())), |
| 136 | + Err(_) if asked.is_none() && wanted == Runtime::Fast => { |
| 137 | + static WARNED: std::sync::Once = std::sync::Once::new(); |
| 138 | + WARNED.call_once(|| { |
| 139 | + eprintln!( |
| 140 | + "warning: no liblouise_rt-fast.a (build it with scripts/runtime.sh); linking the checked runtime" |
| 141 | + ); |
| 142 | + }); |
| 143 | + runtime_dir(opts, Runtime::Checked.file()).map(|d| d.join(Runtime::Checked.file())) |
| 144 | + } |
| 145 | + Err(e) => Err(e), |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/// The first directory holding `file`: `opts.lib_dir`, else |
| 150 | +/// `$LOUISE_LIB_DIR`, else next to the running executable or its parent. |
| 151 | +fn runtime_dir(opts: &CcOptions, file: &str) -> Result<PathBuf, BuildError> { |
| 83 | 152 | let mut candidates = Vec::new(); |
| 84 | 153 | if let Some(d) = &opts.lib_dir { |
| 85 | 154 | candidates.push(d.clone()); |
| @@ -93,10 +162,11 @@ fn runtime_dir(opts: &CcOptions) -> Result<PathBuf, BuildError> { |
| 93 | 162 | } |
| 94 | 163 | candidates |
| 95 | 164 | .iter() |
| 96 | | − .find(|d| d.join("liblouise_rt.a").is_file()) |
| 165 | + .find(|d| d.join(file).is_file()) |
| 97 | 166 | .cloned() |
| 98 | 167 | .ok_or_else(|| { |
| 99 | 168 | BuildError::NoRuntime( |
| 169 | + file.to_owned(), |
| 100 | 170 | candidates |
| 101 | 171 | .iter() |
| 102 | 172 | .map(|d| d.display().to_string()) |
| @@ -333,7 +403,7 @@ pub fn build_in( |
| 333 | 403 | timings: &mut Timings, |
| 334 | 404 | ) -> Result<Built, BuildError> { |
| 335 | 405 | let build = build.path(); |
| 336 | | − let lib_dir = runtime_dir(opts)?; |
| 406 | + let runtime = runtime_archive(opts)?; |
| 337 | 407 | let cache = build.join("cache"); |
| 338 | 408 | if let Some(dir) = output.parent().filter(|d| !d.as_os_str().is_empty()) { |
| 339 | 409 | std::fs::create_dir_all(dir)?; |
| @@ -414,7 +484,7 @@ pub fn build_in( |
| 414 | 484 | built.inputs.push(lib.clone()); |
| 415 | 485 | } |
| 416 | 486 | } |
| 417 | | − built.inputs.push(lib_dir.join("liblouise_rt.a")); |
| 487 | + built.inputs.push(runtime.clone()); |
| 418 | 488 | let mut args: Vec<std::ffi::OsString> = Vec::new(); |
| 419 | 489 | // A release program's own C has no debug information; what a link |
| 420 | 490 | // would copy is the runtime's (Rust's std, about 5 MB). Kept when the |
| @@ -427,7 +497,7 @@ pub fn build_in( |
| 427 | 497 | args.push("-Wl,--gc-sections".into()); |
| 428 | 498 | args.extend(objects.into_iter().map(Into::into)); |
| 429 | 499 | args.extend(natives.into_iter().map(Into::into)); |
| 430 | | − args.push(lib_dir.join("liblouise_rt.a").into()); |
| 500 | + args.push(runtime.into()); |
| 431 | 501 | args.extend(SYSTEM_LIBS.iter().map(Into::into)); |
| 432 | 502 | // Linked beside the output, then renamed over it. |
| 433 | 503 | let linked = output.with_file_name(format!(".{name}.{}.tmp", std::process::id())); |
| @@ -876,4 +946,44 @@ mod tests { |
| 876 | 946 | ); |
| 877 | 947 | let _ = std::fs::remove_dir_all(&dir); |
| 878 | 948 | } |
| 949 | + |
| 950 | + #[test] |
| 951 | + fn the_runtime_archive_follows_the_profile_and_falls_back_to_checked() { |
| 952 | + if std::env::var_os("LOUISE_RUNTIME").is_some() { |
| 953 | + eprintln!("skipping: LOUISE_RUNTIME is set"); |
| 954 | + return; |
| 955 | + } |
| 956 | + let dir = std::env::temp_dir().join(format!("louise-runtime-test-{}", std::process::id())); |
| 957 | + std::fs::create_dir_all(&dir).unwrap(); |
| 958 | + std::fs::write(dir.join("liblouise_rt.a"), "").unwrap(); |
| 959 | + let opts = |release, runtime| CcOptions { |
| 960 | + lib_dir: Some(dir.clone()), |
| 961 | + release, |
| 962 | + runtime, |
| 963 | + ..CcOptions::default() |
| 964 | + }; |
| 965 | + let checked = dir.join("liblouise_rt.a"); |
| 966 | + let fast = dir.join("liblouise_rt-fast.a"); |
| 967 | + // No fast archive: a release build links the checked one; asking |
| 968 | + // for fast is an error naming the file. |
| 969 | + assert_eq!(runtime_archive(&opts(true, None)).unwrap(), checked); |
| 970 | + match runtime_archive(&opts(false, Some(Runtime::Fast))) { |
| 971 | + Err(e @ BuildError::NoRuntime(..)) => { |
| 972 | + assert!(e.to_string().contains("liblouise_rt-fast.a"), "{e}") |
| 973 | + } |
| 974 | + other => panic!("expected NoRuntime, got {other:?}"), |
| 975 | + } |
| 976 | + std::fs::write(&fast, "").unwrap(); |
| 977 | + assert_eq!(runtime_archive(&opts(true, None)).unwrap(), fast); |
| 978 | + assert_eq!(runtime_archive(&opts(false, None)).unwrap(), checked); |
| 979 | + assert_eq!( |
| 980 | + runtime_archive(&opts(true, Some(Runtime::Checked))).unwrap(), |
| 981 | + checked |
| 982 | + ); |
| 983 | + assert_eq!( |
| 984 | + runtime_archive(&opts(false, Some(Runtime::Fast))).unwrap(), |
| 985 | + fast |
| 986 | + ); |
| 987 | + let _ = std::fs::remove_dir_all(&dir); |
| 988 | + } |
| 879 | 989 | } |