|
3 | 3 | extern crate cc;
|
4 | 4 | extern crate ctest2 as ctest;
|
5 | 5 |
|
6 |
| -use std::env; |
| 6 | +use std::fs::File; |
| 7 | +use std::io::{BufRead, BufReader, BufWriter, Write}; |
| 8 | +use std::path::{Path, PathBuf}; |
| 9 | +use std::{env, io}; |
7 | 10 |
|
8 | 11 | fn do_cc() {
|
9 | 12 | let target = env::var("TARGET").unwrap();
|
@@ -63,9 +66,79 @@ fn ctest_cfg() -> ctest::TestGenerator {
|
63 | 66 | cfg
|
64 | 67 | }
|
65 | 68 |
|
| 69 | +fn do_semver() { |
| 70 | + let mut out = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 71 | + out.push("semver.rs"); |
| 72 | + let mut output = BufWriter::new(File::create(&out).unwrap()); |
| 73 | + |
| 74 | + let family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap(); |
| 75 | + let vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap(); |
| 76 | + let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); |
| 77 | + let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); |
| 78 | + let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); |
| 79 | + |
| 80 | + // `libc-test/semver` dir. |
| 81 | + let mut semver_root = |
| 82 | + PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); |
| 83 | + semver_root.push("semver"); |
| 84 | + |
| 85 | + // NOTE: Windows has the same `family` as `os`, no point in including it |
| 86 | + // twice. |
| 87 | + if family != os { |
| 88 | + process_semver_file(&mut output, &mut semver_root, &family); |
| 89 | + } |
| 90 | + process_semver_file(&mut output, &mut semver_root, &vendor); |
| 91 | + process_semver_file(&mut output, &mut semver_root, &os); |
| 92 | + let os_arch = format!("{}-{}", os, arch); |
| 93 | + process_semver_file(&mut output, &mut semver_root, &os_arch); |
| 94 | + if target_env != "" { |
| 95 | + let os_env = format!("{}-{}", os, target_env); |
| 96 | + process_semver_file(&mut output, &mut semver_root, &os_env); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +fn process_semver_file<W: Write, P: AsRef<Path>>( |
| 101 | + output: &mut W, |
| 102 | + path: &mut PathBuf, |
| 103 | + file: P, |
| 104 | +) { |
| 105 | + // NOTE: `path` is reused between calls, so always remove the file again. |
| 106 | + path.push(file); |
| 107 | + path.set_extension("txt"); |
| 108 | + |
| 109 | + println!("cargo:rerun-if-changed={}", path.display()); |
| 110 | + let input_file = match File::open(&*path) { |
| 111 | + Ok(file) => file, |
| 112 | + Err(ref err) if err.kind() == io::ErrorKind::NotFound => { |
| 113 | + path.pop(); |
| 114 | + return; |
| 115 | + } |
| 116 | + Err(err) => panic!("unexpected error opening file: {}", err), |
| 117 | + }; |
| 118 | + let input = BufReader::new(input_file); |
| 119 | + |
| 120 | + write!(output, "// Source: {}.\n", path.display()).unwrap(); |
| 121 | + output.write(b"use libc::{\n").unwrap(); |
| 122 | + for line in input.lines() { |
| 123 | + let line = line.unwrap().into_bytes(); |
| 124 | + match line.first() { |
| 125 | + // Ignore comments and empty lines. |
| 126 | + Some(b'#') | None => continue, |
| 127 | + _ => { |
| 128 | + output.write(b" ").unwrap(); |
| 129 | + output.write(&line).unwrap(); |
| 130 | + output.write(b",\n").unwrap(); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + output.write(b"};\n\n").unwrap(); |
| 135 | + path.pop(); |
| 136 | +} |
| 137 | + |
66 | 138 | fn main() {
|
67 | 139 | do_cc();
|
68 | 140 | do_ctest();
|
| 141 | + do_semver(); |
69 | 142 | }
|
70 | 143 |
|
71 | 144 | macro_rules! headers {
|
|
0 commit comments