]> git.proxmox.com Git - cargo.git/blame - build.rs
Auto merge of #11257 - arlosi:build-deadlock-1.65.0, r=weihanglo
[cargo.git] / build.rs
CommitLineData
0e26eae5
EH
1use flate2::{Compression, GzBuilder};
2use std::ffi::OsStr;
3use std::fs;
4use std::path::Path;
295ea6d9 5use std::process::Command;
0e26eae5
EH
6
7fn main() {
295ea6d9 8 commit_info();
0e26eae5 9 compress_man();
c5318a17
EH
10 println!(
11 "cargo:rustc-env=RUST_HOST_TARGET={}",
12 std::env::var("TARGET").unwrap()
13 );
0e26eae5
EH
14}
15
16fn compress_man() {
17 let out_path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("man.tgz");
18 let dst = fs::File::create(out_path).unwrap();
19 let encoder = GzBuilder::new()
20 .filename("man.tar")
21 .write(dst, Compression::best());
22 let mut ar = tar::Builder::new(encoder);
624acc8e 23 ar.mode(tar::HeaderMode::Deterministic);
0e26eae5
EH
24
25 let mut add_files = |dir, extension| {
624acc8e
AC
26 let mut files = fs::read_dir(dir)
27 .unwrap()
28 .map(|e| e.unwrap().path())
29 .collect::<Vec<_>>();
30 files.sort();
31 for path in files {
0e26eae5
EH
32 if path.extension() != Some(extension) {
33 continue;
34 }
35 println!("cargo:rerun-if-changed={}", path.display());
36 ar.append_path_with_name(&path, path.file_name().unwrap())
37 .unwrap();
38 }
39 };
40
41 add_files(Path::new("src/etc/man"), OsStr::new("1"));
42 add_files(Path::new("src/doc/man/generated_txt"), OsStr::new("txt"));
43 let encoder = ar.into_inner().unwrap();
44 encoder.finish().unwrap();
45}
295ea6d9
EH
46
47fn commit_info() {
48 if !Path::new(".git").exists() {
49 return;
50 }
51 let output = match Command::new("git")
52 .arg("log")
53 .arg("-1")
54 .arg("--date=short")
55 .arg("--format=%H %h %cd")
50ce6419 56 .arg("--abbrev=9")
295ea6d9
EH
57 .output()
58 {
59 Ok(output) if output.status.success() => output,
60 _ => return,
61 };
62 let stdout = String::from_utf8(output.stdout).unwrap();
63 let mut parts = stdout.split_whitespace();
64 let mut next = || parts.next().unwrap();
65 println!("cargo:rustc-env=CARGO_COMMIT_HASH={}", next());
66 println!("cargo:rustc-env=CARGO_COMMIT_SHORT_HASH={}", next());
67 println!("cargo:rustc-env=CARGO_COMMIT_DATE={}", next())
68}