-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rs
188 lines (149 loc) · 5.5 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (C) 2013 - 2021 Tim Düsterhus
// Copyright (C) 2021 Maximilian Mader
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use base64::{prelude::BASE64_STANDARD as BASE64, Engine};
use cargo_license::GetDependenciesOpt;
use sha2::{Digest, Sha384};
use std::{
collections::HashMap,
io::Write,
path::{Path, PathBuf},
};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn write_license_info_file(
dependencies: Vec<cargo_license::DependencyDetails>,
) -> std::io::Result<()> {
let dst = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("licence_info.rs");
let mut file = std::fs::File::create(dst)?;
writeln!(file, "pub type LicenseInfo = &'static [(&'static str, &'static str, &'static str, &'static [&'static str])];")?;
writeln!(file, "pub const LICENSE_INFO: LicenseInfo = &[")?;
for dependency in dependencies {
let name = dependency.name.clone();
let version = dependency.version.clone();
let license = dependency.license.unwrap_or_else(|| "N/A".to_owned());
let authors = dependency
.authors
.unwrap_or_else(|| "N/A".to_owned())
.split('|')
.map(|a| format!(r#""{}""#, a.replace(" <>", "")))
.collect::<Vec<_>>()
.join(", ");
writeln!(
file,
r#" ("{name}", "{version}", "{license}", &[{authors}]),"#
)?;
}
writeln!(file, "];")?;
Ok(())
}
fn bundle_source_code() -> crate::Result<()> {
use ignore::{overrides::OverrideBuilder, WalkBuilder};
use std::io::BufWriter;
fn const_name(path: &Path, const_names: &mut HashMap<PathBuf, String>) -> String {
if let Some(name) = const_names.get(path) {
return name.clone();
}
let name = format!("FILE_{}", const_names.len());
const_names.insert(path.to_path_buf(), name.clone());
name
}
let mut builder = phf_codegen::OrderedMap::new();
let overrides = OverrideBuilder::new("./")
.add("!/.git/")
.unwrap()
.add("!/target/")
.unwrap()
.build()
.unwrap();
let walker = WalkBuilder::new("./")
.hidden(false)
.follow_links(true)
.require_git(false)
.overrides(overrides)
.sort_by_file_name(|a, b| a.cmp(b))
.build();
let dst = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("source_files.rs");
let mut out = BufWriter::new(std::fs::File::create(dst)?);
let mut const_names = HashMap::new();
let mut combined_hasher = Sha384::new();
for result in walker {
let entry = result?;
if let Some(file_type) = entry.file_type() {
if !file_type.is_file() {
continue;
}
println!("cargo:rerun-if-changed={}", entry.path().display());
let file = entry.path().strip_prefix("./").unwrap();
let name = file.to_str().unwrap().to_owned();
let const_name = const_name(file, &mut const_names);
let hash = {
let mut hasher = Sha384::new();
let mut file = std::fs::File::open(entry.path())?;
let _ = std::io::copy(&mut file, &mut hasher)?;
hasher.finalize()
};
combined_hasher.update(hash);
let src = std::path::Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join(entry.path());
writeln!(
out,
"const {}: &[u8] = include_bytes!(\"{}\");",
const_name,
src.display()
)?;
writeln!(
out,
"const {}_SHA384_DIGEST: &str = \"sha384-{}\";",
const_name,
BASE64.encode(hash)
)?;
builder.entry(
name,
&format!(
"SourceFile {{ contents: {}, sha384_digest: {}_SHA384_DIGEST }}",
&const_name, &const_name
),
);
}
}
writeln!(
out,
"pub const SOURCE_FILES: ::phf::OrderedMap<&'static str, SourceFile> = \n{};\n",
builder.build()
)?;
writeln!(
out,
"pub const SOURCE_FILES_COMBINED_HASH: &str = \"{}\";\n",
BASE64.encode(combined_hasher.finalize())
)?;
out.flush().unwrap();
Ok(())
}
fn main() -> crate::Result<()> {
built::write_built_file().expect("Failed to acquire build-time information");
let cmd = Default::default();
let ops = GetDependenciesOpt {
avoid_dev_deps: false,
avoid_build_deps: false,
direct_deps_only: false,
root_only: false,
};
let dependencies = cargo_license::get_dependencies_from_cargo_lock(cmd, ops)?;
write_license_info_file(dependencies)?;
bundle_source_code()?;
Ok(())
}