summaryrefslogtreecommitdiffstats
path: root/testing/geckodriver/build.rs
blob: eb590476b785ce274379945896684e84d3069fe2 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Writes build information to ${OUT_DIR}/build-info.rs which is included in
// the program during compilation:
//
// ```no_run
// const COMMIT_HASH: Option<&'static str> = Some("c31a366");
// const COMMIT_DATE: Option<&'static str> = Some("1988-05-10");
// ```
//
// The values are `None` if running hg failed, e.g. if it is not installed or
// if we are not in an hg repo.

use std::env;
use std::ffi::OsStr;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;

fn main() -> io::Result<()> {
    let cur_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    let build_info = get_build_info(&cur_dir);

    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    let mut fh = File::create(out_dir.join("build-info.rs"))?;
    writeln!(
        fh,
        "const COMMIT_HASH: Option<&'static str> = {:?};",
        build_info.hash()
    )?;
    writeln!(
        fh,
        "const COMMIT_DATE: Option<&'static str> = {:?};",
        build_info.date()
    )?;

    Ok(())
}

fn get_build_info(dir: &Path) -> Box<dyn BuildInfo> {
    if Path::exists(&dir.join(".hg")) {
        Box::new(Hg {})
    } else if Path::exists(&dir.join(".git")) {
        Box::new(Git {})
    } else if let Some(parent) = dir.parent() {
        get_build_info(parent)
    } else {
        eprintln!("unable to detect vcs");
        Box::new(Noop {})
    }
}

trait BuildInfo {
    fn hash(&self) -> Option<String>;
    fn date(&self) -> Option<String>;
}

struct Hg;

impl Hg {
    fn exec<I, S>(&self, args: I) -> Option<String>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        Command::new("hg")
            .env("HGPLAIN", "1")
            .args(args)
            .output()
            .ok()
            .and_then(|r| String::from_utf8(r.stdout).ok())
            .map(|s| s.trim_end().into())
    }
}

impl BuildInfo for Hg {
    fn hash(&self) -> Option<String> {
        self.exec(["log", "-r.", "-T{node|short}"])
    }

    fn date(&self) -> Option<String> {
        self.exec(["log", "-r.", "-T{date|isodate}"])
    }
}

struct Git;

impl Git {
    fn exec<I, S>(&self, args: I) -> Option<String>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        Command::new("git")
            .env("GIT_CONFIG_NOSYSTEM", "1")
            .args(args)
            .output()
            .ok()
            .and_then(|r| String::from_utf8(r.stdout).ok())
            .map(|s| s.trim_end().into())
    }

    fn to_hg_sha(&self, git_sha: String) -> Option<String> {
        self.exec(["cinnabar", "git2hg", &git_sha])
    }
}

impl BuildInfo for Git {
    fn hash(&self) -> Option<String> {
        self.exec(["rev-parse", "HEAD"])
            .and_then(|sha| self.to_hg_sha(sha))
            .map(|mut s| {
                s.truncate(12);
                s
            })
    }

    fn date(&self) -> Option<String> {
        self.exec(["log", "-1", "--date=short", "--pretty=format:%cd"])
    }
}

struct Noop;

impl BuildInfo for Noop {
    fn hash(&self) -> Option<String> {
        None
    }
    fn date(&self) -> Option<String> {
        None
    }
}