Build cleanup (#1)

* Move build script to build.rs
* Add homepage
This commit is contained in:
Jake Shadle 2019-06-25 16:37:46 +02:00 committed by GitHub
parent dd57ac272f
commit 74914007cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 152 additions and 53 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target /target
**/*.rs.bk **/*.rs.bk
Cargo.lock Cargo.lock
MoltenVK

View file

@ -7,6 +7,7 @@ license = "MIT/Apache-2.0"
readme = "README.md" readme = "README.md"
keywords = ["vulkan", "metal"] keywords = ["vulkan", "metal"]
repository = "https://github.com/EmbarkStudios/ash-molten" repository = "https://github.com/EmbarkStudios/ash-molten"
homepage = "https://github.com/EmbarkStudios/ash-molten"
documentation = "https://docs.rs/ash-molten" documentation = "https://docs.rs/ash-molten"
[dependencies] [dependencies]

196
build.rs
View file

@ -1,58 +1,160 @@
use std::path::PathBuf; #[cfg(any(target_os = "macos", target_os = "ios"))]
// Iterator over target_os cfg flags mod mac {
fn target_os() -> impl Iterator<Item = String> { use std::path::{Path, PathBuf};
std::env::vars().filter_map(|(flag, val)| {
const NAME: &'static str = "CARGO_CFG_TARGET_OS"; // Features are not used inside build scripts, so we have to explicitly query them from the
if flag.starts_with(NAME) { // enviroment
Some(val) pub(crate) fn is_external_enabled() -> bool {
} else { std::env::vars()
None .filter_map(|(flag, _)| {
} const NAME: &'static str = "CARGO_FEATURE_";
}) if flag.starts_with(NAME) {
} let feature = flag.split(NAME).nth(1).expect("").to_string();
// Features are not used inside build scripts, so we have to explicitly query them from the println!("{:?}", feature);
// enviroment return Some(feature);
fn is_external_enabled() -> bool { }
std::env::vars() None
.filter_map(|(flag, _)| { })
const NAME: &'static str = "CARGO_FEATURE_"; .find(|f| f == "EXTERNAL")
if flag.starts_with(NAME) { .is_some()
let feature = flag.split(NAME).nth(1).expect("").to_string(); }
println!("{:?}", feature);
return Some(feature); pub(crate) fn build_molten<P: AsRef<Path>>(target_dir: &P) -> &'static str {
use std::{
process::Command,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
let exit = Arc::new(AtomicBool::new(false));
let wants_exit = exit.clone();
// Periodically emit log messages so that Travis doesn't make a sad
let handle = std::thread::spawn(move || {
let mut counter = 0;
while !wants_exit.load(Ordering::Acquire) {
std::thread::sleep(std::time::Duration::from_millis(100));
counter += 100;
if counter >= 30 * 1000 {
counter = 0;
println!("still building MoltenVK");
}
} }
None });
})
.find(|f| f == "EXTERNAL") let git_status = if Path::new("MoltenVK").exists() {
.is_some() Command::new("git")
} .arg("pull")
fn main() { .spawn()
let supported_platforms = ["macos", "ios"]; .expect("failed to spawn git")
let is_mac_or_ios = target_os() .wait()
.find(|target| supported_platforms.contains(&target.as_str())) .expect("failed to pull MoltenVK")
.is_some(); } else {
if !is_mac_or_ios { Command::new("git")
panic!("ash-molten can only be built on macOS or of iOS"); .arg("clone")
} .arg("https://github.com/KhronosGroup/MoltenVK.git")
// The 'external' feature was not enabled. Molten will be built automaticaly. .spawn()
if !is_external_enabled() { .expect("failed to spawn git")
let mut build = std::process::Command::new("bash") .wait()
.arg("build_molten.sh") .expect("failed to clone MoltenVK")
};
assert!(git_status.success(), "failed to clone MoltenVK");
let status = Command::new("sh")
.current_dir("MoltenVK")
.arg("fetchDependencies")
.spawn() .spawn()
.expect("Unable to build molten"); .expect("failed to spawn fetchDependencies")
while build.try_wait().unwrap().is_none() { .wait()
println!("Still building MoltenVK"); .expect("failed to fetchDependencies");
std::thread::sleep(std::time::Duration::from_secs(30));
assert!(status.success(), "failed to fetchDependencies");
// These (currently) match the identifiers used by moltenvk
let (target_name, dir) = match std::env::var("CARGO_CFG_TARGET_OS") {
Ok(target) => match target.as_ref() {
"macos" => ("macos", "macOS"),
"ios" => ("ios", "iOS"),
target => panic!("unknown target '{}'", target),
},
Err(e) => panic!("failed to determinte target os '{}'", e),
};
let status = Command::new("make")
.current_dir("MoltenVK")
.arg(target_name)
.spawn()
.expect("failed to spawn fetchDependencies")
.wait()
.expect("failed to fetchDependencies");
assert!(status.success(), "failed to fetchDependencies");
let src = {
let mut pb = PathBuf::new();
pb.push("MoltenVK");
pb.push("Package/Release/MoltenVK");
pb.push(dir);
pb.push("static/libMoltenVK.a");
pb
};
let target = {
let mut pb = PathBuf::new();
pb.push(target_dir);
pb.push(format!("lib{}MoltenVK.a", target_name));
pb
};
if let Err(e) = std::fs::copy(&src, &target) {
panic!("failed to copy {:?} to {:?}: {}", src, target, e);
} }
let project_dir =
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("native"); exit.store(true, Ordering::Release);
println!("cargo:rustc-link-search=native={}", project_dir.display()); handle.join().unwrap();
target_name
} }
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
fn main() {
use crate::mac::*;
use std::path::{Path, PathBuf};
// The 'external' feature was not enabled. Molten will be built automaticaly.
let target_name = if !is_external_enabled() {
let target_dir = Path::new("native");
let target_name = build_molten(&target_dir);
let project_dir = PathBuf::from(
std::env::var("CARGO_MANIFEST_DIR").expect("unable to find env:CARGO_MANIFEST_DIR"),
)
.join(target_dir);
println!("cargo:rustc-link-search=native={}", project_dir.display());
target_name
} else if cfg!(target_os = "macos") {
"macos"
} else if cfg!(target_os = "ios") {
"ios"
} else {
unreachable!();
};
println!("cargo:rustc-link-lib=framework=Metal"); println!("cargo:rustc-link-lib=framework=Metal");
println!("cargo:rustc-link-lib=framework=AppKit"); println!("cargo:rustc-link-lib=framework=AppKit");
println!("cargo:rustc-link-lib=framework=QuartzCore"); println!("cargo:rustc-link-lib=framework=QuartzCore");
println!("cargo:rustc-link-lib=framework=IOKit"); println!("cargo:rustc-link-lib=framework=IOKit");
println!("cargo:rustc-link-lib=framework=IOSurface"); println!("cargo:rustc-link-lib=framework=IOSurface");
println!("cargo:rustc-link-lib=dylib=c++"); println!("cargo:rustc-link-lib=dylib=c++");
println!("cargo:rustc-link-lib=static=MoltenVK"); println!("cargo:rustc-link-lib=static={}MoltenVK", target_name);
}
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
fn main() {
eprintln!("ash-molten requires either 'macos' or 'ios' target");
} }

View file

@ -1,5 +0,0 @@
git clone https://github.com/KhronosGroup/MoltenVK.git
cd MoltenVK
sh fetchDependencies
make macos
cp Package/Release/MoltenVK/macOS/static/libMoltenVK.a ../native