forked from erdos-project/erdos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
171 lines (148 loc) · 5.11 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
use std::{
env,
fs::File,
path::Path,
process::{Command, Stdio},
sync::Mutex,
};
use slog::{Drain, Logger};
static DEFAULT_BUNDLE_MAX_READ_STREAMS: usize = 20;
static DEAFUALT_BUNDLE_MAX_WRITE_STREAMS: usize = 10;
/// Parses an environment variable and falls back to the default if it is not set.
fn parse_env_variable<T: std::str::FromStr + std::fmt::Display>(
key: &str,
default: T,
logger: &Logger,
) -> T
where
<T as std::str::FromStr>::Err: std::fmt::Debug,
{
match env::var(key) {
Ok(s) => s
.parse()
.expect(&format!("Error parsing environment variable {}.", key)),
Err(env::VarError::NotPresent) => {
slog::info!(logger, "{} not set. Defaulting to {}", key, default,);
default
}
Err(env::VarError::NotUnicode(_)) => panic!("Error decoding {}.", key),
}
}
fn make_callback_builder(max_read_streams: usize, max_write_streams: usize) -> Result<(), String> {
let out_dir = env::var("OUT_DIR").unwrap();
let out_file_path = Path::new(&out_dir).join("callback_builder_generated.rs");
let mut script = env::current_dir().unwrap();
script.push("scripts");
script.push("make_callback_builder.py");
let file = File::create(out_file_path.to_str().unwrap()).map_err(|e| {
format!(
"Error creating file {}: {}",
out_file_path.to_str().unwrap(),
e.to_string()
)
})?;
let child = Command::new("python3")
.arg(script.to_str().unwrap())
.args(&[max_read_streams.to_string(), max_write_streams.to_string()])
.stdout(Stdio::from(file))
.spawn()
.map_err(|e| {
format!(
"Error running {}: {}",
script.to_str().unwrap(),
e.to_string()
)
})?;
let output = child.wait_with_output().map_err(|e| {
format!(
"Error running {}: {}",
script.to_str().unwrap(),
e.to_string()
)
})?;
if !output.status.success() {
return Err(format!(
"Error running {}: {}",
script.to_str().unwrap(),
String::from_utf8(output.stderr).unwrap_or("".to_string())
));
}
Ok(())
}
fn make_add_watermark_callback(
max_read_streams: usize,
max_write_streams: usize,
) -> Result<(), String> {
let out_dir = env::var("OUT_DIR").unwrap();
let out_file_path = Path::new(&out_dir).join("add_watermark_callback_vec_generated.rs");
let mut script = env::current_dir().unwrap();
script.push("scripts");
script.push("make_add_watermark_callback_vec.py");
let file = File::create(out_file_path.to_str().unwrap()).map_err(|e| {
format!(
"Error creating file {}: {}",
out_file_path.to_str().unwrap(),
e.to_string()
)
})?;
let child = Command::new("python3")
.arg(script.to_str().unwrap())
.args(&[max_read_streams.to_string(), max_write_streams.to_string()])
.stdout(Stdio::from(file))
.spawn()
.map_err(|e| {
format!(
"Error running {}: {}",
script.to_str().unwrap(),
e.to_string()
)
})?;
let output = child.wait_with_output().map_err(|e| {
format!(
"Error running {}: {}",
script.to_str().unwrap(),
e.to_string()
)
})?;
if !output.status.success() {
return Err(format!(
"Error running {}: {}",
script.to_str().unwrap(),
String::from_utf8(output.stderr).unwrap_or("".to_string())
));
}
Ok(())
}
fn main() -> Result<(), String> {
let logger = Logger::root(Mutex::new(slog_term::term_full()).fuse(), slog::o!());
let bundle_max_read_streams: usize = parse_env_variable(
"ERDOS_BUNDLE_MAX_READ_STREAMS",
DEFAULT_BUNDLE_MAX_READ_STREAMS,
&logger,
);
let bundle_max_write_streams: usize = parse_env_variable(
"ERDOS_BUNDLE_MAX_WRITE_STREAMS",
DEAFUALT_BUNDLE_MAX_WRITE_STREAMS,
&logger,
);
slog::info!(logger, "Generating code for stream bundles.");
make_callback_builder(bundle_max_read_streams, bundle_max_write_streams)?;
slog::info!(logger, "Done generating code for stream bundles.");
slog::info!(
logger,
"Generating code for adding callbacks over vectors of streams."
);
make_add_watermark_callback(bundle_max_read_streams, bundle_max_write_streams)?;
slog::info!(
logger,
"Done generating code for adding callbacks over vectors of streams."
);
// Re-run build.rs if the following files are changed.
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=scripts/make_callback_builder.py");
println!("cargo:rerun-if-changed=scripts/make_add_watermark_callback_vec.py");
// Re-run build.rs if the following environment variables are changed.
println!("cargo:rerun-if-env-changed=ERDOS_BUNDLE_MAX_READ_STREAMS");
println!("cargo:rerun-if-env-changed=ERDOS_BUNDLE_MAX_WRITE_STREAMS");
Ok(())
}