-
Notifications
You must be signed in to change notification settings - Fork 422
/
Copy pathbuild.rs
258 lines (239 loc) · 9.4 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
extern crate curl;
extern crate flate2;
extern crate pkg_config;
extern crate semver;
extern crate tar;
use std::error::Error;
use std::fs::File;
use std::io::BufWriter;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process;
use std::process::Command;
use std::{env, fs};
use curl::easy::Easy;
use flate2::read::GzDecoder;
use semver::Version;
use tar::Archive;
const LIBRARY: &'static str = "tensorflow";
const REPOSITORY: &'static str = "https://github.com/tensorflow/tensorflow.git";
const TARGET: &'static str = "tensorflow:libtensorflow.so";
// `VERSION` and `TAG` are separate because the tag is not always `'v' + VERSION`.
const VERSION: &'static str = "1.3.0";
const TAG: &'static str = "v1.3.0";
const MIN_BAZEL: &'static str = "0.3.2";
macro_rules! get(($name:expr) => (ok!(env::var($name))));
macro_rules! ok(($expression:expr) => ($expression.unwrap()));
macro_rules! log {
($fmt:expr) => (println!(concat!("libtensorflow-sys/build.rs:{}: ", $fmt), line!()));
($fmt:expr, $($arg:tt)*) => (println!(concat!("libtensorflow-sys/build.rs:{}: ", $fmt),
line!(), $($arg)*));
}
macro_rules! log_var(($var:ident) => (log!(concat!(stringify!($var), " = {:?}"), $var)));
fn main() {
if pkg_config::find_library(LIBRARY).is_ok() {
log!("Returning early because {} was already found", LIBRARY);
return;
}
let force_src = match env::var("TF_RUST_BUILD_FROM_SRC") {
Ok(s) => s == "true",
Err(_) => false,
};
if !force_src && env::consts::ARCH == "x86_64" && (env::consts::OS == "linux" || env::consts::OS == "macos") {
install_prebuilt();
} else {
build_from_src();
}
}
fn remove_suffix(value: &mut String, suffix: &str) {
if value.ends_with(suffix) {
let n = value.len();
value.truncate(n - suffix.len());
}
}
fn extract<P: AsRef<Path>, P2: AsRef<Path>>(archive_path: P, extract_to: P2) {
let file = File::open(archive_path).unwrap();
let unzipped = GzDecoder::new(file).unwrap();
let mut a = Archive::new(unzipped);
a.unpack(extract_to).unwrap();
}
// Downloads and unpacks a prebuilt binary. Only works for certain platforms.
fn install_prebuilt() {
// Figure out the file names.
let os = match env::consts::OS {
"macos" => "darwin",
x => x,
};
let proc_type = if cfg!(feature = "tensorflow_gpu") {"gpu"} else {"cpu"};
let binary_url = format!(
"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-{}-{}-{}-{}.tar.gz",
proc_type, os, env::consts::ARCH, VERSION);
log_var!(binary_url);
let short_file_name = binary_url.split("/").last().unwrap();
let mut base_name = short_file_name.to_string();
remove_suffix(&mut base_name, ".tar.gz");
log_var!(base_name);
let download_dir = match env::var("TF_RUST_DOWNLOAD_DIR") {
Ok(s) => PathBuf::from(s),
Err(_) => PathBuf::from(&get!("CARGO_MANIFEST_DIR")).join("target"),
};
if !download_dir.exists() {
fs::create_dir(&download_dir).unwrap();
}
let file_name = download_dir.join(short_file_name);
log_var!(file_name);
// Download the tarball.
if !file_name.exists() {
let f = File::create(&file_name).unwrap();
let mut writer = BufWriter::new(f);
let mut easy = Easy::new();
easy.url(&binary_url).unwrap();
easy.write_function(move |data| {
Ok(writer.write(data).unwrap())
}).unwrap();
easy.perform().unwrap();
let response_code = easy.response_code().unwrap();
if response_code != 200 {
panic!("Unexpected response code {} for {}", response_code, binary_url);
}
}
// Extract the tarball.
let unpacked_dir = download_dir.join(base_name);
let lib_dir = unpacked_dir.join("lib");
let library_file = format!("lib{}.so", LIBRARY);
let library_full_path = lib_dir.join(&library_file);
if !library_full_path.exists() {
extract(file_name, &unpacked_dir);
}
//run("find", |command| command); // TODO: remove
run("ls", |command| {
command.arg("-l").arg(lib_dir.to_str().unwrap())
}); // TODO: remove
println!("cargo:rustc-link-lib=dylib={}", LIBRARY);
let output = PathBuf::from(&get!("OUT_DIR"));
let new_library_full_path = output.join(&library_file);
if new_library_full_path.exists() {
log!("File {} already exists, deleting.", new_library_full_path.display());
std::fs::remove_file(&new_library_full_path).unwrap();
}
log!("Copying {} to {}...", library_full_path.display(), new_library_full_path.display());
std::fs::copy(&library_full_path, &new_library_full_path).unwrap();
println!("cargo:rustc-link-search={}", output.display());
}
fn build_from_src() {
let output = PathBuf::from(&get!("OUT_DIR"));
log_var!(output);
let source = PathBuf::from(&get!("CARGO_MANIFEST_DIR")).join(format!("target/source-{}", TAG));
log_var!(source);
let lib_dir = output.join(format!("lib-{}", TAG));
log_var!(lib_dir);
if lib_dir.exists() {
log!("Directory {:?} already exists", lib_dir);
} else {
log!("Creating directory {:?}", lib_dir);
fs::create_dir(lib_dir.clone()).unwrap();
}
let library_path = lib_dir.join(format!("lib{}.so", LIBRARY));
log_var!(library_path);
if library_path.exists() {
log!("{:?} already exists, not building", library_path);
} else {
if let Err(e) = check_bazel() {
println!("cargo:error=Bazel must be installed at version {} or greater. (Error: {})",
MIN_BAZEL,
e);
process::exit(1);
}
let target_path = &TARGET.replace(":", "/");
log_var!(target_path);
if !Path::new(&source.join(".git")).exists() {
run("git", |command| {
command.arg("clone")
.arg(format!("--branch={}", TAG))
.arg("--recursive")
.arg(REPOSITORY)
.arg(&source)
});
}
// Only configure if not previously configured. Configuring runs a
// `bazel clean`, which we don't want, because we want to be able to
// continue from a cancelled build.
let configure_hint_file_pb = source.join(".rust-configured");
let configure_hint_file = Path::new(&configure_hint_file_pb);
if !configure_hint_file.exists() {
run("bash",
|command| command.current_dir(&source)
.env("TF_NEED_CUDA", if cfg!(feature = "tensorflow_gpu") {"1"} else {"0"})
.arg("-c")
.arg("yes ''|./configure"));
File::create(configure_hint_file).unwrap();
}
run("bazel", |command| {
command.current_dir(&source)
.arg("build")
.arg(format!("--jobs={}", get!("NUM_JOBS")))
.arg("--compilation_mode=opt")
.arg("--copt=-march=native")
.arg(TARGET)
});
let target_bazel_bin = source.join("bazel-bin").join(target_path);
log!("Copying {:?} to {:?}", target_bazel_bin, library_path);
fs::copy(target_bazel_bin, library_path).unwrap();
}
println!("cargo:rustc-link-lib=dylib={}", LIBRARY);
println!("cargo:rustc-link-search={}", lib_dir.display());
}
fn run<F>(name: &str, mut configure: F)
where F: FnMut(&mut Command) -> &mut Command
{
let mut command = Command::new(name);
let configured = configure(&mut command);
log!("Executing {:?}", configured);
if !ok!(configured.status()).success() {
panic!("failed to execute {:?}", configured);
}
log!("Command {:?} finished successfully", configured);
}
// Building TF 0.11.0rc1 with Bazel 0.3.0 gives this error when running `configure`:
// expected ConfigurationTransition or NoneType for 'cfg' while calling label_list but got
// string instead: data.
// ERROR: com.google.devtools.build.lib.packages.BuildFileContainsErrorsException: error
// loading package '': Extension file 'tensorflow/tensorflow.bzl' has errors.
// And the simple solution is to require Bazel 0.3.1 or higher.
fn check_bazel() -> Result<(), Box<Error>> {
let mut command = Command::new("bazel");
command.arg("version");
log!("Executing {:?}", command);
let out = command.output()?;
log!("Command {:?} finished successfully", command);
let stdout = String::from_utf8(out.stdout)?;
let mut found_version = false;
for line in stdout.lines() {
if line.starts_with("Build label:") {
found_version = true;
let mut version_str = line.split(":")
.nth(1)
.unwrap()
.split(" ")
.nth(1)
.unwrap()
.trim();
if version_str.ends_with('-') {
// hyphen is 1 byte long, so it's safe
version_str = &version_str[..version_str.len() - 1];
}
let version = Version::parse(version_str)?;
let want = Version::parse(MIN_BAZEL)?;
if version < want {
return Err(format!("Installed version {} is less than required version {}",
version_str,
MIN_BAZEL)
.into());
}
}
}
if !found_version {
return Err("Did not find version number in `bazel version` output.".into());
}
Ok(())
}