-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbuild.rs
229 lines (206 loc) · 7.86 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2023 Pants project contributors.
// Licensed under the Apache License, Version 2.0 (see LICENSE).
use std::cell::Cell;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::{Context, Result};
use log::info;
use sha2::{Digest, Sha256};
use termcolor::WriteColor;
use crate::utils::exe::{binary_full_name, execute, prepare_exe};
use crate::utils::fs::{copy, ensure_directory, path_as_str, rename};
use crate::utils::os::PATHSEP;
use crate::{build_step, BINARY, SCIENCE_TAG};
const CARGO: &str = env!("CARGO");
const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
// N.B.: OUT_DIR and TARGET are not normally available under compilation, but our custom build
// script forwards them.
const OUT_DIR: &str = env!("OUT_DIR");
const TARGET: &str = env!("TARGET");
pub(crate) fn fingerprint(path: &Path) -> Result<String> {
let mut reader = std::fs::File::open(path)
.with_context(|| format!("Failed to open {path} for hashing.", path = path.display()))?;
let mut hasher = Sha256::new();
std::io::copy(&mut reader, &mut hasher).context("Failed to digest stream")?;
Ok(format!("{digest:x}", digest = hasher.finalize()))
}
pub(crate) fn check_sha256(path: &Path) -> Result<()> {
let sha256_file = PathBuf::from(format!("{path}.sha256", path = path.display()));
let contents = std::fs::read_to_string(&sha256_file).with_context(|| {
format!(
"Failed to read {sha256_file}",
sha256_file = sha256_file.display()
)
})?;
let expected_sha256 = contents.split(' ').next().with_context(|| {
format!(
"Expected {sha256_file} to have a leading hash",
sha256_file = sha256_file.display()
)
})?;
assert_eq!(expected_sha256, fingerprint(path)?.as_str());
Ok(())
}
fn fetch_file(url: &str, dest_file: &Path) -> Result<()> {
let mut file = File::create(dest_file)?;
std::io::copy(&mut ureq::get(url).call()?.into_reader(), &mut file)?;
Ok(())
}
fn fetch_and_check_trusted_sha256(url: &str, dest_file: &Path) -> Result<()> {
fetch_file(url, dest_file)?;
let mut sha256_dest_file = dest_file.to_owned();
// Add the additional .sha256 extension, to whatever the base file
// had, _without_ replacing the existing extension:
sha256_dest_file.as_mut_os_string().push(".sha256");
let sha256_url = format!("{url}.sha256");
fetch_file(&sha256_url, &sha256_dest_file)?;
info!("Checking downloaded {url} has sha256 reported in {sha256_url}");
check_sha256(dest_file)
}
pub(crate) struct BuildContext {
pub(crate) workspace_root: PathBuf,
pub(crate) package_crate_root: PathBuf,
pub(crate) cargo_output_root: PathBuf,
target: String,
target_prepared: Cell<bool>,
science_repo: Option<PathBuf>,
cargo_output_bin_dir: PathBuf,
}
impl BuildContext {
pub(crate) fn new(target: Option<&str>, science_repo: Option<&Path>) -> Result<Self> {
let target = target.unwrap_or(TARGET).to_string();
let package_crate_root = PathBuf::from(CARGO_MANIFEST_DIR);
let workspace_root = package_crate_root
.join("..")
.canonicalize()
.context("Failed to canonicalize workspace root")?;
let output_root = PathBuf::from(OUT_DIR).join("dist");
let output_bin_dir = output_root.join("bin");
Ok(Self {
workspace_root,
package_crate_root,
cargo_output_root: output_root,
target,
target_prepared: Cell::new(false),
science_repo: science_repo.map(Path::to_path_buf),
cargo_output_bin_dir: output_bin_dir,
})
}
fn ensure_target(&self) -> Result<()> {
if !self.target_prepared.get() {
build_step!(
"Ensuring --target {target} is available",
target = self.target
);
execute(Command::new("rustup").args(["target", "add", &self.target]))?;
self.target_prepared.set(true);
}
Ok(())
}
pub(crate) fn obtain_science(&self, dest_dir: &Path) -> Result<PathBuf> {
if let Some(ref science_from) = self.science_repo {
self.ensure_target()?;
build_step!(
"Building the `science` binary from the source at {science_from}",
science_from = science_from.display()
);
execute(
Command::new("nox")
.args(["-e", "package"])
.env(
"SCIENCE_LIFT_BUILD_DEST_DIR",
format!("{dest_dir}", dest_dir = dest_dir.display()),
)
.current_dir(science_from),
)?;
} else {
fetch_a_scie_project("lift", SCIENCE_TAG, "science", dest_dir)?;
}
let science_exe_path = dest_dir.join(binary_full_name("science"));
prepare_exe(&science_exe_path)?;
let science_exe = dest_dir.join("science");
rename(&science_exe_path, &science_exe)?;
Ok(science_exe)
}
pub(crate) fn build_scie_pants(&self) -> Result<PathBuf> {
build_step!("Building the scie-pants Rust binary.");
execute(
Command::new(CARGO)
.args([
"install",
"--path",
path_as_str(&self.workspace_root)?,
"--target",
&self.target,
"--root",
path_as_str(&self.cargo_output_root)?,
])
// N.B.: This just suppresses a warning about adding this bin dir to your PATH.
.env(
"PATH",
[self.cargo_output_bin_dir.to_str().unwrap(), env!("PATH")].join(PATHSEP),
),
)?;
Ok(self
.cargo_output_bin_dir
.join(BINARY)
.with_extension(env::consts::EXE_EXTENSION))
}
}
fn fetch_a_scie_project(
project_name: &str,
tag: &str,
binary_name: &str,
dest_dir: &Path,
) -> Result<()> {
let file_name = binary_full_name(binary_name);
let cache_dir = crate::utils::fs::dev_cache_dir()?
.join("downloads")
.join(project_name);
ensure_directory(&cache_dir, false)?;
// We proceed with single-checked locking, contention is not a concern in this build process!
// We only care about correctness.
let target_dir = cache_dir.join(tag);
let lock_file = cache_dir.join(format!("{tag}.lck"));
let lock_fd = std::fs::File::create(&lock_file).with_context(|| {
format!(
"Failed to open {path} for locking",
path = lock_file.display()
)
})?;
let mut lock = fd_lock::RwLock::new(lock_fd);
let _write_lock = lock.write();
if !target_dir.exists() {
build_step!(format!("Fetching the `{project_name}` {tag} binary"));
let work_dir = cache_dir.join(format!("{tag}.work"));
ensure_directory(&work_dir, true)?;
fetch_and_check_trusted_sha256(
format!(
"https://github.com/a-scie/{project_name}/releases/download/{tag}/{file_name}",
)
.as_str(),
&work_dir.join(&file_name),
)?;
rename(&work_dir, &target_dir)?;
} else {
build_step!(format!(
"Loading the `{project_name}` {tag} binary from the cache"
));
}
copy(&target_dir.join(&file_name), &dest_dir.join(file_name))
}
pub(crate) struct Science(PathBuf);
impl Science {
pub(crate) fn command(&self) -> Command {
Command::new(&self.0)
}
}
pub(crate) fn fetch_science(build_context: &BuildContext) -> Result<Science> {
let dest_dir = build_context.cargo_output_root.join("science");
ensure_directory(&dest_dir, true)?;
let science_exe = build_context.obtain_science(&dest_dir)?;
Ok(Science(science_exe))
}