2019-09-26 19:31:27 -04:00
|
|
|
use std::env;
|
2019-09-30 17:41:09 -04:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{BufRead, BufReader, Error, ErrorKind, Result};
|
|
|
|
use std::ops::Bound;
|
|
|
|
use std::path::{Path, PathBuf};
|
2019-09-26 19:31:27 -04:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
trait CommandExt {
|
2019-09-30 17:41:09 -04:00
|
|
|
fn execute(&mut self) -> Result<()>;
|
2019-09-26 19:31:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandExt for Command {
|
|
|
|
/// Execute the command and return an error if it exited with a failure status.
|
2019-09-30 17:41:09 -04:00
|
|
|
fn execute(&mut self) -> Result<()> {
|
|
|
|
self.status()
|
|
|
|
.and_then(|status| {
|
|
|
|
if status.success() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::new(ErrorKind::Other, "non-zero exit code"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("The command {:?} did not run successfully.", self),
|
|
|
|
)
|
|
|
|
})
|
2019-09-26 19:31:27 -04:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 08:51:51 -04:00
|
|
|
|
2019-09-30 17:41:09 -04:00
|
|
|
fn use_custom_lua<S: AsRef<str>>(include_dir: &S, lib_dir: &S, lua_lib: &S) -> Result<String> {
|
|
|
|
let mut version_found = String::new();
|
|
|
|
|
|
|
|
// Find LUA_VERSION_NUM
|
|
|
|
let mut lua_h_path = PathBuf::from(include_dir.as_ref());
|
|
|
|
lua_h_path.push("lua.h");
|
|
|
|
let f = File::open(lua_h_path)?;
|
|
|
|
let reader = BufReader::new(f);
|
|
|
|
for line in reader.lines() {
|
|
|
|
let line = line?;
|
|
|
|
let parts = line.split_whitespace().collect::<Vec<_>>();
|
|
|
|
if parts.len() == 3 && parts[1] == "LUA_VERSION_NUM" {
|
|
|
|
version_found = parts[2].to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("cargo:rustc-link-search=native={}", lib_dir.as_ref());
|
2019-10-08 11:05:56 -04:00
|
|
|
println!("cargo:rustc-link-lib=static={}", lua_lib.as_ref());
|
2019-09-26 19:31:27 -04:00
|
|
|
|
2019-09-30 17:41:09 -04:00
|
|
|
Ok(version_found)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_glue<P: AsRef<Path> + std::fmt::Debug>(include_paths: &[P]) {
|
2019-09-26 19:31:27 -04:00
|
|
|
let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
|
|
|
|
|
|
// Ensure the presence of glue.rs
|
2019-09-30 17:41:09 -04:00
|
|
|
// if build_dir.join("glue.rs").exists() {
|
|
|
|
// return;
|
|
|
|
// }
|
2019-09-26 19:31:27 -04:00
|
|
|
|
2019-09-30 17:41:09 -04:00
|
|
|
let mut config = cc::Build::new();
|
2019-09-26 19:31:27 -04:00
|
|
|
|
2019-09-30 17:41:09 -04:00
|
|
|
for path in include_paths {
|
|
|
|
config.include(path);
|
2019-09-26 19:31:27 -04:00
|
|
|
}
|
2019-09-30 17:41:09 -04:00
|
|
|
|
|
|
|
// Compile and run glue.c
|
|
|
|
let glue = build_dir.join("glue");
|
|
|
|
|
|
|
|
config
|
|
|
|
.get_compiler()
|
|
|
|
.to_command()
|
|
|
|
.arg("src/ffi/glue/glue.c")
|
|
|
|
.arg("-o")
|
|
|
|
.arg(&glue)
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Command::new(glue)
|
|
|
|
.arg(build_dir.join("glue.rs"))
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2019-10-08 11:05:56 -04:00
|
|
|
let include_dir = env::var("LUA_INC").unwrap_or(String::new());
|
|
|
|
let lib_dir = env::var("LUA_LIB").unwrap_or(String::new());
|
2019-09-30 17:41:09 -04:00
|
|
|
let lua_lib = env::var("LUA_LIB_NAME").unwrap_or(String::new());
|
|
|
|
|
2019-10-08 11:05:56 -04:00
|
|
|
println!("cargo:rerun-if-env-changed=LUA_INC");
|
|
|
|
println!("cargo:rerun-if-env-changed=LUA_LIB");
|
2019-09-30 17:41:09 -04:00
|
|
|
println!("cargo:rerun-if-env-changed=LUA_LIB_NAME");
|
2019-10-01 11:11:36 -04:00
|
|
|
println!("cargo:rerun-if-changed=src/ffi/glue/glue.c");
|
2019-09-30 17:41:09 -04:00
|
|
|
|
|
|
|
if include_dir != "" && lib_dir != "" && lua_lib != "" {
|
|
|
|
let _version = use_custom_lua(&include_dir, &lib_dir, &lua_lib).unwrap();
|
|
|
|
build_glue(&[include_dir]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find lua via pkg-config
|
|
|
|
|
|
|
|
let lua = pkg_config::Config::new()
|
|
|
|
.range_version((Bound::Included("5.3"), Bound::Excluded("5.4")))
|
|
|
|
.probe("lua")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
build_glue(&lua.include_paths);
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|