Add scaffolding for a Python build script.

Hook up a Cargo build script that runs a Python script in the meta directory.
This commit is contained in:
Jakob Stoklund Olesen
2016-04-05 13:21:46 -07:00
parent 807b718358
commit 0b8db43bbe
3 changed files with 61 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
authors = ["The cwCretonneRust Project Developers"] authors = ["The cwCretonneRust Project Developers"]
name = "cretonne" name = "cretonne"
version = "0.0.0" version = "0.0.0"
build = "build.rs"
[lib] [lib]
name = "cretonne" name = "cretonne"

View File

@@ -0,0 +1,49 @@
// Build script.
//
// This program is run by Cargo when building libcretonne. It is used to generate Rust code from
// the language definitions in the meta directory.
//
// Environment:
//
// OUT_DIR
// Directory where generated files should be placed.
//
// The build script expects to be run from the directory where this build.rs file lives. The
// current directory is used to find the sources.
use std::env;
use std::process;
fn main() {
let out_dir = env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set");
println!("Build script generating files in {}", out_dir);
let mut cur_dir = env::current_dir().expect("Can't access current working directory");
// We're in src/libcretonne. Find the top-level directory.
assert!(cur_dir.pop(), "No parent 'src' directory");
assert!(cur_dir.pop(), "No top-level directory");
let top_dir = cur_dir.as_path();
// Scripts are in $top_dir/meta.
let meta_dir = top_dir.join("meta");
let build_script = meta_dir.join("build.py");
// Let Cargo known that this script should be rerun if anything changes in the meta directory.
println!("cargo:rerun-if-changed={}", meta_dir.display());
// Launch build script with Python. We'll just find python in the path.
let status = process::Command::new("python")
.current_dir(top_dir)
.arg(build_script)
.arg("--out-dir")
.arg(out_dir)
.status()
.expect("Failed to launch second-level build script");
if !status.success() {
process::exit(status.code().unwrap());
}
}

11
meta/build.py Normal file
View File

@@ -0,0 +1,11 @@
# Second-level build script.
#
# This script is run from src/libcretonne/build.rs to generate Rust files.
import argparse
parser = argparse.ArgumentParser(description='Generate sources for Cretonne.')
parser.add_argument('--out-dir', help='set output directory')
args = parser.parse_args()
out_dir = args.out_dir