From d32d78d16c51fc172b19426896738b7177603247 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Tue, 5 Apr 2016 13:21:46 -0700 Subject: [PATCH] Add scaffolding for a Python build script. Hook up a Cargo build script that runs a Python script in the meta directory. --- meta/build.py | 11 +++++++++ src/libcretonne/Cargo.toml | 1 + src/libcretonne/build.rs | 49 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 meta/build.py create mode 100644 src/libcretonne/build.rs diff --git a/meta/build.py b/meta/build.py new file mode 100644 index 0000000000..2cdda53403 --- /dev/null +++ b/meta/build.py @@ -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 diff --git a/src/libcretonne/Cargo.toml b/src/libcretonne/Cargo.toml index 58a67fe569..38edc811d1 100644 --- a/src/libcretonne/Cargo.toml +++ b/src/libcretonne/Cargo.toml @@ -2,6 +2,7 @@ authors = ["The cwCretonneRust Project Developers"] name = "cretonne" version = "0.0.0" +build = "build.rs" [lib] name = "cretonne" diff --git a/src/libcretonne/build.rs b/src/libcretonne/build.rs new file mode 100644 index 0000000000..523ddb3548 --- /dev/null +++ b/src/libcretonne/build.rs @@ -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()); + } +}