-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathbuild.rs
More file actions
91 lines (75 loc) · 2.56 KB
/
Copy pathbuild.rs
File metadata and controls
91 lines (75 loc) · 2.56 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is dual-licensed under either the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree or the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree. You may select, at your option, one of the
* above-listed licenses.
*/
//! Generate source file containing buck2/prelude tree with contents.
use std::io;
use std::path::Path;
fn main() {
imp().unwrap();
}
fn imp() -> io::Result<()> {
let out_path = std::env::var_os("OUT_DIR").unwrap();
let include_file = Path::new(&out_path).join("include.rs");
let manifest_path = std::env::var_os("CARGO_MANIFEST_DIR").unwrap();
let prelude_path = Path::new(&manifest_path)
.parent()
.unwrap()
.parent()
.unwrap()
.join("prelude");
// Self-check.
assert!(prelude_path.join("prelude.bzl").exists());
println!("cargo:rerun-if-changed={}", prelude_path.display());
write_include_file(&prelude_path, std::fs::File::create(&include_file)?)?;
Ok(())
}
fn as_unix_like(path: &Path) -> String {
path.to_str().unwrap().replace('\\', "/")
}
fn write_include_file(prelude: &Path, mut include_file: impl io::Write) -> io::Result<()> {
#[allow(clippy::write_literal)]
writeln!(include_file, "// {}generated by crate build.rs", "@")?;
writeln!(
include_file,
"pub(crate) const DATA: &[crate::BundledFile] = &["
)?;
for res in walkdir::WalkDir::new(prelude) {
let entry = res.map_err(|e| e.into_io_error().unwrap())?;
// Watch all files and directories for changes
println!("cargo:rerun-if-changed={}", entry.path().display());
if !entry.file_type().is_file() {
continue;
}
writeln!(include_file, "crate::BundledFile {{")?;
writeln!(
include_file,
" path: r\"{}\",",
as_unix_like(entry.path().strip_prefix(prelude).unwrap())
)?;
writeln!(
include_file,
" contents: include_bytes!(r\"{}\"),",
entry.path().display()
)?;
let exec_bit;
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
exec_bit = entry.metadata()?.mode() & 0o111 != 0;
}
#[cfg(not(unix))]
{
exec_bit = false;
}
writeln!(include_file, " is_executable: {exec_bit},")?;
writeln!(include_file, "}},")?;
}
writeln!(include_file, "];")?;
Ok(())
}