-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
147 lines (122 loc) · 4.62 KB
/
build.rs
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
137
138
139
140
141
142
143
144
145
146
147
/*
* SPDX-FileCopyrightText: © 2023 Jinwoo Park ([email protected])
*
* SPDX-License-Identifier: MIT OR Apache-2.0
*/
use std::process::Command;
use card_terminal_adapter::CardTerminalConst;
use cargo_metadata::{Error, MetadataCommand};
use git2::Repository;
use mp_fingerprint_type::{FirmwareFingerprint, MpFingerprint};
const IGNORE_PATH_DEP_INJ: &str = ".cargo/config.toml";
fn main() -> Result<(), Error> {
println!("cargo:rustc-link-arg-bins=--nmagic");
println!("cargo:rustc-link-arg-bins=-Tlink.x");
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
// Get project name and version
let metadata = MetadataCommand::new().no_deps().exec()?;
if let Some(package) = metadata.packages.first() {
let project_name = &package.name;
let project_version = package.version.to_string();
println!("cargo:rustc-env=PROJECT_NAME={}", project_name);
println!(
"cargo:rustc-env=PROJECT_VERSION={}",
hex::encode(project_version),
);
} else {
println!("cargo:rustc-env=PROJECT_NAME=unkown");
println!("cargo:rustc-env=PROJECT_VERSION={}", hex::encode(b"?.?.?"),);
}
// Get the Git commit hash
let repo = Repository::open(".").expect("Failed to open repository");
let head = repo.head().expect("Failed to get HEAD");
let commit = head.peel_to_commit().expect("Failed to peel commit");
let commit_hash = commit.id().to_string();
let commit_short_hash = String::from_utf8(
commit
.as_object()
.short_id()
.expect("Filed to get short_id")
.to_ascii_lowercase(),
)
.expect("Failed to convert short_id to UTF-8");
let statuses = match repo.statuses(None) {
Ok(statuses) => statuses,
Err(_) => {
return Err(Error::CargoMetadata {
stderr: "Failed to open git repository".to_owned(),
})
} // Failed to get repository status
};
let is_dirty = statuses.iter().any(|status| {
let s = status.status();
let p = status.path();
// ignore config.toml for dependency injection
(p != Some(IGNORE_PATH_DEP_INJ))
& !((s == git2::Status::CURRENT) | (s == git2::Status::IGNORED))
});
let (dirty_str, short_dirty_str) = if is_dirty {
("-dirty".to_owned(), "-d".to_owned())
} else {
("".to_owned(), " ".to_owned())
};
let output = Command::new("git")
.args(["log", "-1", "--format=%ai", &commit_hash])
.output()
.expect("Failed to execute command");
let commit_datetime = String::from_utf8_lossy(&output.stdout);
// Output the version and commit hash to a file
// This is u8 array
println!(
"cargo:rustc-env=GIT_COMMIT_HASH={}{}",
commit_hash, dirty_str
);
println!(
"cargo:rustc-env=GIT_COMMIT_SHORT_HASH={}",
hex::encode(format!("{}{}", commit_short_hash, short_dirty_str))
);
println!("cargo:rustc-env=GIT_COMMIT_DATETIME={}", commit_datetime);
// Generate elf header fingerprint
let metadata = MetadataCommand::new().no_deps().exec()?;
let main_package = metadata
.packages
.first()
.expect("Cargo.toml doesn't have metadata");
let hw_feature: Vec<(String, String)> = std::env::vars()
.filter(|(key, value)| key.starts_with("CARGO_FEATURE_HW_") && value == "1")
.collect();
if hw_feature.is_empty() {
panic!("There's no specified hardware target");
} else if hw_feature.len() > 1 {
panic!("Cannot specify multiple hardware");
}
let feature_based_model_ver = hw_feature[0]
.0
.strip_prefix("CARGO_FEATURE_HW_")
.unwrap()
.replace('_', "-");
let fingerprint = MpFingerprint {
firmware_fingerprint: FirmwareFingerprint {
model_name: "BillMock-HW".to_owned(), // this is const value
model_ver: feature_based_model_ver,
firmware_ver: main_package.version.to_string(),
firmware_git_hash: format!("{}{}", commit_hash, dirty_str),
is_nda: billmock_plug_card::KiccEd785Plug::is_nda(),
},
};
if !billmock_plug_card::KiccEd785Plug::is_nda() {
println!("cargo:warning=This is not NDA serial device build.",);
}
println!(
"cargo:rustc-env=MP_FINGERPRINT_TOML_HEX={}",
fingerprint.to_hex_string(),
);
// DEFMT_LOG level configuration
let profile = std::env::var("PROFILE").unwrap();
let log_level = match profile.as_str() {
"release" => "error",
_ => "trace",
};
println!("cargo:rustc-env=DEFMT_LOG={}", log_level);
Ok(())
}