Browse Source

Select module version based on rustc version

Jethro Beekman 8 years ago
parent
commit
6380905235
3 changed files with 60 additions and 2 deletions
  1. 3 1
      Cargo.toml
  2. 56 0
      build.rs
  3. 1 1
      src/lib.rs

+ 3 - 1
Cargo.toml

@@ -2,5 +2,7 @@
 name = "core_io"
 version = "0.0.20160707"
 authors = ["Jethro Beekman <jethro@jbeekman.nl>"]
+build = "build.rs"
 
-[dependencies]
+[build-dependencies]
+rustc_version = "0.1.7"

+ 56 - 0
build.rs

@@ -0,0 +1,56 @@
+extern crate rustc_version;
+
+use std::env;
+use std::fs::File;
+use std::io::Write;
+use std::path::PathBuf;
+use std::ops::{Neg,Sub};
+
+/*
+ * Let me explain this hack. For the sync shell script it's easiest if every 
+ * line in mapping.rs looks exactly the same. This means that specifying an 
+ * array literal is not possible. include!() can only expand to expressions, so 
+ * just specifying the contents of an array is also not possible.
+ *
+ * This leaves us with trying to find an expression in which every line looks 
+ * the same. This can be done using the `-` operator. This can be a unary 
+ * operator (first thing on the first line), or a binary operator (later 
+ * lines). That is exactly what's going on here, and Neg and Sub simply build a 
+ * vector of the operangs.
+ */
+struct Mapping(&'static str,&'static str);
+
+impl Neg for Mapping {
+	type Output = Vec<Mapping>;
+    fn neg(self) -> Vec<Mapping> {
+		vec![self.into()]
+	}
+}
+
+impl Sub<Mapping> for Vec<Mapping> {
+    type Output=Vec<Mapping>;
+    fn sub(mut self, rhs: Mapping) -> Vec<Mapping> {
+		self.push(rhs.into());
+		self
+	}
+}
+
+fn main() {
+	let mappings=include!("mapping.rs");
+	
+	let compiler=rustc_version::version_meta().commit_hash.expect("Couldn't determine compiler version");
+	let io_commit=mappings.iter().find(|&&Mapping(elem,_)|elem==compiler).expect("Unknown compiler version, upgrade core_io?").1;
+	
+	let mut dest_path=PathBuf::from(env::var_os("OUT_DIR").unwrap());
+	dest_path.push("io.rs");
+	let mut f=File::create(&dest_path).unwrap();
+	
+	let mut target_path=PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
+	target_path.push("src");
+	target_path.push(io_commit);
+	target_path.push("mod.rs");
+
+	f.write_all(br#"#[path=""#).unwrap();
+	f.write_all(target_path.into_os_string().into_string().unwrap().as_bytes()).unwrap();
+	f.write_all(br#""] mod io;"#).unwrap();
+}

+ 1 - 1
src/lib.rs

@@ -7,5 +7,5 @@ extern crate collections;
 extern crate alloc;
 extern crate rustc_unicode;
 
-mod io;
+include!(concat!(env!("OUT_DIR"), "/io.rs"));
 pub use io::*;