| 1 |
use crate::util::{self, ArrayDisplay}; |
| 2 |
use crate::{fmt_option_str, write_str_variable, write_variable}; |
| 3 |
use std::{cell, collections, env, ffi, fmt, fs, io, process}; |
| 4 |
|
| 5 |
const BUILT_OVERRIDE_PREFIX: &str = "BUILT_OVERRIDE_"; |
| 6 |
|
| 7 |
#[derive(Debug, Default)] |
| 8 |
enum EnvironmentValue { |
| 9 |
#[default] |
| 10 |
Unused, |
| 11 |
Queried, |
| 12 |
Used, |
| 13 |
} |
| 14 |
|
| 15 |
impl EnvironmentValue { |
| 16 |
fn upgrade_to_queried(&mut self) { |
| 17 |
if matches!(self, EnvironmentValue::Unused) { |
| 18 |
*self = EnvironmentValue::Queried; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
fn upgrade_to_used(&mut self) { |
| 23 |
*self = EnvironmentValue::Used; |
| 24 |
} |
| 25 |
|
| 26 |
pub fn is_unused(&self) -> bool { |
| 27 |
matches!(self, EnvironmentValue::Unused) |
| 28 |
} |
| 29 |
|
| 30 |
pub fn is_used(&self) -> bool { |
| 31 |
matches!(self, EnvironmentValue::Used) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
pub struct EnvironmentMap { |
| 36 |
map: collections::HashMap<String, (String, cell::RefCell<EnvironmentValue>)>, |
| 37 |
override_prefix: String, |
| 38 |
} |
| 39 |
|
| 40 |
fn get_version_from_cmd(executable: &ffi::OsStr) -> io::Result<String> { |
| 41 |
let output = process::Command::new(executable).arg("-V").output()?; |
| 42 |
let mut v = String::from_utf8(output.stdout).unwrap(); |
| 43 |
v.pop(); // remove newline |
| 44 |
Ok(v) |
| 45 |
} |
| 46 |
|
| 47 |
impl EnvironmentMap { |
| 48 |
pub fn new() -> Self { |
| 49 |
let map = env::vars_os() |
| 50 |
.filter_map(|(k, v)| match (k.into_string(), v.into_string()) { |
| 51 |
(Ok(k), Ok(v)) => Some((k, (v, cell::RefCell::default()))), |
| 52 |
_ => None, |
| 53 |
}) |
| 54 |
.collect::<collections::HashMap<_, _>>(); |
| 55 |
let override_prefix = format!("{}{}_", BUILT_OVERRIDE_PREFIX, map["CARGO_PKG_NAME"].0); |
| 56 |
Self { |
| 57 |
map, |
| 58 |
override_prefix, |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
fn override_key(&self, key: &str) -> String { |
| 63 |
let mut prefixed_key = self.override_prefix.clone(); |
| 64 |
prefixed_key.push_str(key); |
| 65 |
prefixed_key |
| 66 |
} |
| 67 |
|
| 68 |
pub fn get(&self, key: &str) -> Option<&str> { |
| 69 |
self.map.get(key).map(|v| { |
| 70 |
v.1.borrow_mut().upgrade_to_queried(); |
| 71 |
v.0.as_str() |
| 72 |
}) |
| 73 |
} |
| 74 |
|
| 75 |
pub fn contains_key(&self, key: &str) -> bool { |
| 76 |
self.get(key).is_some() |
| 77 |
} |
| 78 |
|
| 79 |
pub fn filter_map_keys<F>(&self, mut f: F) -> impl Iterator<Item = &str> |
| 80 |
where |
| 81 |
F: FnMut(&str) -> Option<&str>, |
| 82 |
{ |
| 83 |
self.map.iter().filter_map(move |v| match f(v.0.as_str()) { |
| 84 |
Some(w) => { |
| 85 |
v.1 .1.borrow_mut().upgrade_to_queried(); |
| 86 |
Some(w) |
| 87 |
} |
| 88 |
None => None, |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
pub fn get_override_var<'a, T>(&'a self, key: &str) -> Option<T> |
| 93 |
where |
| 94 |
T: util::ParseFromEnv<'a>, |
| 95 |
{ |
| 96 |
self.map.get(&self.override_key(key)).map(|v| { |
| 97 |
v.1.borrow_mut().upgrade_to_queried(); |
| 98 |
match T::parse_from_env(v.0.as_str()) { |
| 99 |
Ok(t) => { |
| 100 |
v.1.borrow_mut().upgrade_to_used(); |
| 101 |
t |
| 102 |
} |
| 103 |
Err(e) => { |
| 104 |
panic!("Failed to parse `{key}`=`{0}`: {e:?}", v.0); |
| 105 |
} |
| 106 |
} |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
pub fn unused_override_vars(&self) -> impl Iterator<Item = &str> { |
| 111 |
self.map.iter().filter_map(|(k, v)| { |
| 112 |
if k.starts_with(&self.override_prefix) && v.1.borrow().is_unused() { |
| 113 |
Some(k.as_str()) |
| 114 |
} else { |
| 115 |
None |
| 116 |
} |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
pub fn used_override_vars(&self) -> impl Iterator<Item = &str> { |
| 121 |
self.map.iter().filter_map(|(k, v)| { |
| 122 |
if v.1.borrow().is_used() { |
| 123 |
Some(k.strip_prefix(&self.override_prefix).unwrap()) |
| 124 |
} else { |
| 125 |
None |
| 126 |
} |
| 127 |
}) |
| 128 |
} |
| 129 |
|
| 130 |
pub fn write_ci(&self, mut w: &fs::File) -> io::Result<()> { |
| 131 |
use io::Write; |
| 132 |
|
| 133 |
let ci = match self.get_override_var("CI_PLATFORM") { |
| 134 |
Some(v) => v, |
| 135 |
None => self.detect_ci().map(|ci| ci.to_string()), |
| 136 |
}; |
| 137 |
write_variable!( |
| 138 |
w, |
| 139 |
"CI_PLATFORM", |
| 140 |
"Option<&str>", |
| 141 |
fmt_option_str(ci), |
| 142 |
"The Continuous Integration platform detected during compilation." |
| 143 |
); |
| 144 |
Ok(()) |
| 145 |
} |
| 146 |
|
| 147 |
pub fn write_env(&self, mut w: &fs::File) -> io::Result<()> { |
| 148 |
use io::Write; |
| 149 |
macro_rules! write_env_str { |
| 150 |
($(($name:ident, $env_name:expr, $doc:expr)),*) => {$( |
| 151 |
let v = match self.get_override_var(stringify!($name)) { |
| 152 |
Some(v) => v, |
| 153 |
None => self.get($env_name).expect(stringify!(Missing expected environment variable $env_name)), |
| 154 |
}; |
| 155 |
write_str_variable!( |
| 156 |
w, |
| 157 |
stringify!($name), |
| 158 |
v, |
| 159 |
$doc |
| 160 |
); |
| 161 |
)*} |
| 162 |
} |
| 163 |
|
| 164 |
write_env_str!( |
| 165 |
(PKG_VERSION, "CARGO_PKG_VERSION", "The full version."), |
| 166 |
( |
| 167 |
PKG_VERSION_MAJOR, |
| 168 |
"CARGO_PKG_VERSION_MAJOR", |
| 169 |
"The major version." |
| 170 |
), |
| 171 |
( |
| 172 |
PKG_VERSION_MINOR, |
| 173 |
"CARGO_PKG_VERSION_MINOR", |
| 174 |
"The minor version." |
| 175 |
), |
| 176 |
( |
| 177 |
PKG_VERSION_PATCH, |
| 178 |
"CARGO_PKG_VERSION_PATCH", |
| 179 |
"The patch version." |
| 180 |
), |
| 181 |
( |
| 182 |
PKG_VERSION_PRE, |
| 183 |
"CARGO_PKG_VERSION_PRE", |
| 184 |
"The pre-release version." |
| 185 |
), |
| 186 |
( |
| 187 |
PKG_AUTHORS, |
| 188 |
"CARGO_PKG_AUTHORS", |
| 189 |
"A colon-separated list of authors." |
| 190 |
), |
| 191 |
(PKG_NAME, "CARGO_PKG_NAME", "The name of the package."), |
| 192 |
(PKG_DESCRIPTION, "CARGO_PKG_DESCRIPTION", "The description."), |
| 193 |
(PKG_HOMEPAGE, "CARGO_PKG_HOMEPAGE", "The homepage."), |
| 194 |
(PKG_LICENSE, "CARGO_PKG_LICENSE", "The license."), |
| 195 |
( |
| 196 |
PKG_REPOSITORY, |
| 197 |
"CARGO_PKG_REPOSITORY", |
| 198 |
"The source repository as advertised in Cargo.toml." |
| 199 |
), |
| 200 |
( |
| 201 |
TARGET, |
| 202 |
"TARGET", |
| 203 |
"The target triple that was being compiled for." |
| 204 |
), |
| 205 |
(HOST, "HOST", "The host triple of the rust compiler."), |
| 206 |
( |
| 207 |
PROFILE, |
| 208 |
"PROFILE", |
| 209 |
"`release` for release builds, `debug` for other builds." |
| 210 |
), |
| 211 |
(RUSTC, "RUSTC", "The compiler that cargo resolved to use."), |
| 212 |
( |
| 213 |
RUSTDOC, |
| 214 |
"RUSTDOC", |
| 215 |
"The documentation generator that cargo resolved to use." |
| 216 |
) |
| 217 |
); |
| 218 |
|
| 219 |
write_str_variable!( |
| 220 |
w, |
| 221 |
"OPT_LEVEL", |
| 222 |
self.get_override_var("OPT_LEVEL") |
| 223 |
.unwrap_or_else(|| env::var("OPT_LEVEL").unwrap()), |
| 224 |
"Value of OPT_LEVEL for the profile used during compilation." |
| 225 |
); |
| 226 |
|
| 227 |
write_variable!( |
| 228 |
w, |
| 229 |
"NUM_JOBS", |
| 230 |
"u32", |
| 231 |
self.get_override_var("NUM_JOBS").unwrap_or_else(|| { |
| 232 |
if env::var(crate::SOURCE_DATE_EPOCH).is_ok() { |
| 233 |
1u32 |
| 234 |
} else { |
| 235 |
env::var("NUM_JOBS").unwrap().parse().unwrap() |
| 236 |
} |
| 237 |
}), |
| 238 |
"The parallelism that was specified during compilation." |
| 239 |
); |
| 240 |
|
| 241 |
write_variable!( |
| 242 |
w, |
| 243 |
"DEBUG", |
| 244 |
"bool", |
| 245 |
self.get_override_var("DEBUG") |
| 246 |
.unwrap_or_else(|| env::var("DEBUG").unwrap() == "true"), |
| 247 |
"Value of DEBUG for the profile used during compilation." |
| 248 |
); |
| 249 |
Ok(()) |
| 250 |
} |
| 251 |
|
| 252 |
pub fn write_features(&self, mut w: &fs::File) -> io::Result<()> { |
| 253 |
use io::Write; |
| 254 |
|
| 255 |
let mut features = self.get_override_var("FEATURES").unwrap_or_else(|| { |
| 256 |
self.filter_map_keys(|k| k.strip_prefix("CARGO_FEATURE_")) |
| 257 |
.map(|f| f.to_owned()) |
| 258 |
.collect::<Vec<_>>() |
| 259 |
}); |
| 260 |
features.sort_unstable(); |
| 261 |
|
| 262 |
write_variable!( |
| 263 |
w, |
| 264 |
"FEATURES", |
| 265 |
format_args!("[&str; {}]", features.len()), |
| 266 |
ArrayDisplay(&features, |t, f| write!(f, "\"{}\"", t.escape_default())), |
| 267 |
"The features that were enabled during compilation." |
| 268 |
); |
| 269 |
let features_str = features.join(", "); |
| 270 |
write_str_variable!( |
| 271 |
w, |
| 272 |
"FEATURES_STR", |
| 273 |
features_str, |
| 274 |
"The features as a comma-separated string." |
| 275 |
); |
| 276 |
|
| 277 |
let mut lowercase_features = features |
| 278 |
.iter() |
| 279 |
.map(|name| name.to_lowercase()) |
| 280 |
.collect::<Vec<_>>(); |
| 281 |
lowercase_features.sort_unstable(); |
| 282 |
|
| 283 |
write_variable!( |
| 284 |
w, |
| 285 |
"FEATURES_LOWERCASE", |
| 286 |
format_args!("[&str; {}]", lowercase_features.len()), |
| 287 |
ArrayDisplay(&lowercase_features, |val, fmt| write!( |
| 288 |
fmt, |
| 289 |
"\"{}\"", |
| 290 |
val.escape_default() |
| 291 |
)), |
| 292 |
"The features as above, as lowercase strings." |
| 293 |
); |
| 294 |
let lowercase_features_str = lowercase_features.join(", "); |
| 295 |
write_str_variable!( |
| 296 |
w, |
| 297 |
"FEATURES_LOWERCASE_STR", |
| 298 |
lowercase_features_str, |
| 299 |
"The feature-string as above, from lowercase strings." |
| 300 |
); |
| 301 |
|
| 302 |
Ok(()) |
| 303 |
} |
| 304 |
|
| 305 |
pub fn write_cfg(&self, mut w: &fs::File) -> io::Result<()> { |
| 306 |
use io::Write; |
| 307 |
|
| 308 |
write_str_variable!( |
| 309 |
w, |
| 310 |
"CFG_TARGET_ARCH", |
| 311 |
self.get_override_var("CFG_TARGET_ARCH") |
| 312 |
.unwrap_or_else(|| self.get("CARGO_CFG_TARGET_ARCH").unwrap()), |
| 313 |
"The target architecture, given by `CARGO_CFG_TARGET_ARCH`." |
| 314 |
); |
| 315 |
|
| 316 |
write_str_variable!( |
| 317 |
w, |
| 318 |
"CFG_ENDIAN", |
| 319 |
self.get_override_var("CFG_ENDIAN") |
| 320 |
.unwrap_or_else(|| self.get("CARGO_CFG_TARGET_ENDIAN").unwrap()), |
| 321 |
"The endianness, given by `CARGO_CFG_TARGET_ENDIAN`." |
| 322 |
); |
| 323 |
|
| 324 |
write_str_variable!( |
| 325 |
w, |
| 326 |
"CFG_ENV", |
| 327 |
self.get_override_var("CFG_ENV") |
| 328 |
.unwrap_or_else(|| self.get("CARGO_CFG_TARGET_ENV").unwrap()), |
| 329 |
"The toolchain-environment, given by `CARGO_CFG_TARGET_ENV`." |
| 330 |
); |
| 331 |
|
| 332 |
write_str_variable!( |
| 333 |
w, |
| 334 |
"CFG_FAMILY", |
| 335 |
self.get_override_var("CFG_FAMILY") |
| 336 |
.unwrap_or_else(|| self.get("CARGO_CFG_TARGET_FAMILY").unwrap_or_default()), |
| 337 |
"The OS-family, given by `CARGO_CFG_TARGET_FAMILY`." |
| 338 |
); |
| 339 |
|
| 340 |
write_str_variable!( |
| 341 |
w, |
| 342 |
"CFG_OS", |
| 343 |
self.get_override_var("CFG_OS") |
| 344 |
.unwrap_or_else(|| self.get("CARGO_CFG_TARGET_OS").unwrap()), |
| 345 |
"The operating system, given by `CARGO_CFG_TARGET_OS`." |
| 346 |
); |
| 347 |
|
| 348 |
write_str_variable!( |
| 349 |
w, |
| 350 |
"CFG_POINTER_WIDTH", |
| 351 |
self.get_override_var("CFG_POINTER_WIDTH") |
| 352 |
.unwrap_or_else(|| self.get("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap()), |
| 353 |
"The pointer width, given by `CARGO_CFG_TARGET_POINTER_WIDTH`." |
| 354 |
); |
| 355 |
|
| 356 |
Ok(()) |
| 357 |
} |
| 358 |
|
| 359 |
pub fn write_compiler_version(&self, mut w: &fs::File) -> io::Result<()> { |
| 360 |
use std::io::Write; |
| 361 |
|
| 362 |
let rustc; |
| 363 |
let rustc_version; |
| 364 |
match self.get_override_var("RUSTC") { |
| 365 |
Some(v) => { |
| 366 |
rustc = v; |
| 367 |
rustc_version = self |
| 368 |
.get_override_var("RUSTC_VERSION") |
| 369 |
.expect("RUSTC_VERSION must be overridden if RUSTC is") |
| 370 |
} |
| 371 |
None => { |
| 372 |
rustc = self.get("RUSTC").unwrap(); |
| 373 |
rustc_version = get_version_from_cmd(rustc.as_ref())?; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
let rustdoc; |
| 378 |
let rustdoc_version; |
| 379 |
match self.get_override_var("RUSTDOC") { |
| 380 |
Some(v) => { |
| 381 |
rustdoc = v; |
| 382 |
rustdoc_version = self.get_override_var("RUSTDOC_VERSION").unwrap_or_default(); |
| 383 |
} |
| 384 |
None => { |
| 385 |
rustdoc = self.get("RUSTDOC").unwrap(); |
| 386 |
rustdoc_version = get_version_from_cmd(rustdoc.as_ref()).unwrap_or_default(); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
write_str_variable!( |
| 391 |
w, |
| 392 |
"RUSTC_VERSION", |
| 393 |
rustc_version, |
| 394 |
format_args!("The output of `{rustc} -V`") |
| 395 |
); |
| 396 |
|
| 397 |
write_str_variable!( |
| 398 |
w, |
| 399 |
"RUSTDOC_VERSION", |
| 400 |
rustdoc_version, |
| 401 |
format_args!( |
| 402 |
"The output of `{rustdoc} -V`; empty string if `{rustdoc} -V` failed to execute" |
| 403 |
) |
| 404 |
); |
| 405 |
Ok(()) |
| 406 |
} |
| 407 |
|
| 408 |
pub fn detect_ci(&self) -> Option<CIPlatform> { |
| 409 |
macro_rules! detect { |
| 410 |
($(($k:expr, $v:expr, $i:ident)),*) => {$( |
| 411 |
if self.get($k).map_or(false, |v| v == $v) { |
| 412 |
return Some(CIPlatform::$i); |
| 413 |
} |
| 414 |
)*}; |
| 415 |
($(($k:expr, $i:ident)),*) => {$( |
| 416 |
if self.contains_key($k) { |
| 417 |
return Some(CIPlatform::$i); |
| 418 |
} |
| 419 |
)*}; |
| 420 |
($($k:expr),*) => {$( |
| 421 |
if self.contains_key($k) { |
| 422 |
return Some(CIPlatform::Generic); |
| 423 |
} |
| 424 |
)*}; |
| 425 |
} |
| 426 |
// Variable names collected by watson/ci-info |
| 427 |
detect!( |
| 428 |
("TRAVIS", Travis), |
| 429 |
("CIRCLECI", Circle), |
| 430 |
("GITLAB_CI", GitLab), |
| 431 |
("APPVEYOR", AppVeyor), |
| 432 |
("DRONE", Drone), |
| 433 |
("MAGNUM", Magnum), |
| 434 |
("SEMAPHORE", Semaphore), |
| 435 |
("JENKINS_URL", Jenkins), |
| 436 |
("bamboo_planKey", Bamboo), |
| 437 |
("TF_BUILD", TFS), |
| 438 |
("TEAMCITY_VERSION", TeamCity), |
| 439 |
("BUILDKITE", Buildkite), |
| 440 |
("HUDSON_URL", Hudson), |
| 441 |
("GO_PIPELINE_LABEL", GoCD), |
| 442 |
("BITBUCKET_COMMIT", BitBucket), |
| 443 |
("GITHUB_ACTIONS", GitHubActions) |
| 444 |
); |
| 445 |
|
| 446 |
if self.contains_key("TASK_ID") && self.contains_key("RUN_ID") { |
| 447 |
return Some(CIPlatform::TaskCluster); |
| 448 |
} |
| 449 |
|
| 450 |
detect!(("CI_NAME", "codeship", Codeship)); |
| 451 |
|
| 452 |
detect!( |
| 453 |
"CI", // Could be Travis, Circle, GitLab, AppVeyor or CodeShip |
| 454 |
"CONTINUOUS_INTEGRATION", // Probably Travis |
| 455 |
"BUILD_NUMBER" // Jenkins, TeamCity |
| 456 |
); |
| 457 |
None |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
/// Various Continuous Integration platforms whose presence can be detected. |
| 462 |
pub enum CIPlatform { |
| 463 |
/// <https://travis-ci.org> |
| 464 |
Travis, |
| 465 |
/// <https://circleci.com> |
| 466 |
Circle, |
| 467 |
/// <https://about.gitlab.com/gitlab-ci> |
| 468 |
GitLab, |
| 469 |
/// <https://www.appveyor.com> |
| 470 |
AppVeyor, |
| 471 |
/// <https://codeship.com> |
| 472 |
Codeship, |
| 473 |
/// <https://github.com/drone/drone> |
| 474 |
Drone, |
| 475 |
/// <https://magnum-ci.com> |
| 476 |
Magnum, |
| 477 |
/// <https://semaphoreci.com> |
| 478 |
Semaphore, |
| 479 |
/// <https://jenkins.io> |
| 480 |
Jenkins, |
| 481 |
/// <https://www.atlassian.com/software/bamboo> |
| 482 |
Bamboo, |
| 483 |
/// <https://www.visualstudio.com/de/tfs> |
| 484 |
TFS, |
| 485 |
/// <https://www.jetbrains.com/teamcity> |
| 486 |
TeamCity, |
| 487 |
/// <https://buildkite.com> |
| 488 |
Buildkite, |
| 489 |
/// <http://hudson-ci.org> |
| 490 |
Hudson, |
| 491 |
/// <https://github.com/taskcluster> |
| 492 |
TaskCluster, |
| 493 |
/// <https://www.gocd.io> |
| 494 |
GoCD, |
| 495 |
/// <https://bitbucket.org> |
| 496 |
BitBucket, |
| 497 |
/// <https://github.com/features/actions> |
| 498 |
GitHubActions, |
| 499 |
/// Unspecific |
| 500 |
Generic, |
| 501 |
} |
| 502 |
|
| 503 |
impl fmt::Display for CIPlatform { |
| 504 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 505 |
f.write_str(match *self { |
| 506 |
CIPlatform::Travis => "Travis CI", |
| 507 |
CIPlatform::Circle => "CircleCI", |
| 508 |
CIPlatform::GitLab => "GitLab", |
| 509 |
CIPlatform::AppVeyor => "AppVeyor", |
| 510 |
CIPlatform::Codeship => "CodeShip", |
| 511 |
CIPlatform::Drone => "Drone", |
| 512 |
CIPlatform::Magnum => "Magnum", |
| 513 |
CIPlatform::Semaphore => "Semaphore", |
| 514 |
CIPlatform::Jenkins => "Jenkins", |
| 515 |
CIPlatform::Bamboo => "Bamboo", |
| 516 |
CIPlatform::TFS => "Team Foundation Server", |
| 517 |
CIPlatform::TeamCity => "TeamCity", |
| 518 |
CIPlatform::Buildkite => "Buildkite", |
| 519 |
CIPlatform::Hudson => "Hudson", |
| 520 |
CIPlatform::TaskCluster => "TaskCluster", |
| 521 |
CIPlatform::GoCD => "GoCD", |
| 522 |
CIPlatform::BitBucket => "BitBucket", |
| 523 |
CIPlatform::GitHubActions => "GitHub Actions", |
| 524 |
CIPlatform::Generic => "Generic CI", |
| 525 |
}) |
| 526 |
} |
| 527 |
} |