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
230
231
232
233
234
235
236
use std::env;
use std::fs::remove_file;
use std::io;
use std::path::PathBuf;
use std::process::Command;
static CARGO_LINK_SEARCH: &'static str = "cargo:rustc-link-search=native=";
static CARGO_LINK_LIB: &'static str = "cargo:rustc-link-lib=";
pub fn link_lib(path: Option<&str>, name: &str) {
if let Some(path) = path {
println!("{}{}", CARGO_LINK_SEARCH, path);
}
println!("{}{}", CARGO_LINK_LIB, name);
}
pub fn link<P: AsRef<std::path::Path>>(filename: P) {
let mut filename = filename.as_ref().to_path_buf();
let name = filename.file_stem().expect("Invalid filename");
let s = String::from(name.to_str().expect("Invalid filename"));
let mut tmp: &str = &s;
if s.starts_with("lib") {
tmp = &s[3..]
}
filename.pop();
link_lib(filename.to_str(), tmp);
}
pub fn compile_shared_library(
compiler: Option<&str>,
output: &str,
args: &[&str],
) -> Result<bool, std::io::Error> {
let cxx = std::env::var("CXX").unwrap_or("c++".to_owned());
let mut cmd = Command::new(compiler.unwrap_or(&cxx));
let res = cmd
.arg("-shared")
.arg("-o")
.arg(output)
.args(args)
.status()?;
Ok(res.success())
}
#[derive(Debug)]
pub struct Build<'a> {
pub halide_path: PathBuf,
pub src: Vec<PathBuf>,
pub output: PathBuf,
pub cxx: Option<&'a str>,
pub cxxflags: Option<&'a str>,
pub ldflags: Option<&'a str>,
pub build_args: Vec<&'a str>,
pub run_args: Vec<&'a str>,
pub keep: bool,
pub generator: bool,
}
impl<'a> Build<'a> {
pub fn new<P: AsRef<std::path::Path>, Q: AsRef<std::path::PathBuf>>(
halide_path: P,
output: Q,
) -> Build<'a> {
Build {
halide_path: halide_path.as_ref().to_path_buf(),
src: vec![],
output: output.as_ref().to_path_buf(),
cxx: None,
cxxflags: None,
ldflags: None,
build_args: vec![],
run_args: vec![],
keep: false,
generator: false,
}
}
pub fn build(&self) -> io::Result<bool> {
let cxx_default = env::var("CXX").unwrap_or("c++".to_string());
let mut cmd = Command::new(self.cxx.clone().unwrap_or(cxx_default.as_str()));
cmd.args(&["-I", &self.halide_path.join("include").to_string_lossy()])
.args(&["-I", &self.halide_path.join("tools").to_string_lossy()]);
if let Some(flags) = &self.cxxflags {
cmd.args(flags.split(" "));
}
if self.generator {
cmd.arg(
&self
.halide_path
.join("tools")
.join("GenGen.cpp")
.to_string_lossy()
.as_ref(),
);
cmd.args(&self.build_args);
}
cmd.args(&self.src)
.args(&["-o", &self.output.to_string_lossy()])
.args(&[
"-L",
&self.halide_path.join("lib").to_string_lossy(),
"-lHalide",
"-lpng",
"-ljpeg",
"-lpthread",
"-ltinfo",
"-ldl",
"-lz",
]);
if let Some(flags) = &self.ldflags {
cmd.args(flags.split(" "));
}
cmd.status().map(|status| status.success())
}
pub fn run(&self) -> io::Result<bool> {
if !self.output.exists() {
return Ok(false);
}
let res = Command::new(&self.output)
.args(&self.run_args)
.env("LD_LIBRARY_PATH", self.halide_path.join("lib"))
.status()
.map(|status| status.success());
if !self.keep {
let _ = remove_file(&self.output);
}
res
}
}
pub fn run<P: AsRef<std::path::Path>, Q: AsRef<std::path::Path>>(
halide_path: P,
path: Q,
run_args: Vec<&str>,
generator: bool,
) -> io::Result<bool> {
let build = Build {
halide_path: halide_path.as_ref().to_path_buf(),
src: vec![path.as_ref().to_path_buf()],
output: path.as_ref().to_path_buf().with_extension("exe"),
cxx: None,
cxxflags: None,
ldflags: None,
keep: false,
generator,
run_args,
build_args: vec![],
};
build.build()?;
build.run()
}
pub struct Source {
pub halide_path: PathBuf,
pub repo: String,
pub branch: String,
pub make: String,
pub make_flags: Vec<String>,
}
impl Source {
pub fn download(&self) -> io::Result<bool> {
Command::new("git")
.arg("clone")
.args(&["-b", self.branch.as_str()])
.arg(&self.repo)
.arg(&self.halide_path)
.status()
.map(|status| status.success())
}
pub fn update(&self) -> io::Result<bool> {
Command::new("git")
.current_dir(&self.halide_path)
.arg("pull")
.arg("origin")
.arg(&self.branch)
.status()
.map(|status| status.success())
}
pub fn build(&self) -> io::Result<bool> {
Command::new(&self.make)
.current_dir(&self.halide_path)
.args(&self.make_flags)
.status()
.map(|status| status.success())
}
}