From 18e1931b7433e4ccda0cc39b95f88d3f91ecf74f Mon Sep 17 00:00:00 2001 From: Mark Simulacrum Date: Sat, 20 May 2017 08:43:44 -0600 Subject: [PATCH] Utilize cfg_attr instead of a build script. This also updates serde to a more recent version (1.0). --- Cargo.toml | 10 +++------- build.rs | 51 -------------------------------------------------- src/capture.rs | 27 ++++++-------------------- src/lib.rs | 8 +++++--- tests/smoke.rs | 2 +- 5 files changed, 15 insertions(+), 83 deletions(-) delete mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 799d90ef5..c72cff2eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,15 +11,14 @@ documentation = "http://alexcrichton.com/backtrace-rs" description = """ A library to acquire a stack trace (backtrace) at runtime in a Rust program. """ -build = "build.rs" - [dependencies] libc = "0.2" cfg-if = "0.1" rustc-demangle = "0.1.4" # Optionally enable the ability to serialize a `Backtrace` -serde = { version = "0.8", optional = true } +serde = { version = "1.0", optional = true } +serde_derive = { version = "1.0", optional = true } rustc-serialize = { version = "0.3", optional = true } # Optionally demangle C++ frames' symbols in backtraces. @@ -33,9 +32,6 @@ winapi = { version = "0.2.5", optional = true } [target.'cfg(all(unix, not(target_os = "emscripten"), not(target_os = "macos"), not(target_os = "ios")))'.dependencies] backtrace-sys = { path = "backtrace-sys", version = "0.1.3", optional = true } -[build-dependencies] -serde_codegen = { version = "0.8", optional = true } - # Each feature controls the two phases of finding a backtrace: getting a # backtrace and then resolving instruction pointers to symbols. The default # feature enables all the necessary features for each platform this library @@ -88,4 +84,4 @@ default = ["libunwind", "libbacktrace", "coresymbolication", "dladdr", "dbghelp" # # Various features used for enabling rustc-serialize or syntex codegen. serialize-rustc = ["rustc-serialize"] - serialize-serde = ["serde", "serde_codegen"] + serialize-serde = ["serde", "serde_derive"] diff --git a/build.rs b/build.rs deleted file mode 100644 index a7c587068..000000000 --- a/build.rs +++ /dev/null @@ -1,51 +0,0 @@ -#[cfg(feature = "serialize-serde")] -extern crate serde_codegen; - -use std::env; -use std::fs::{self, File}; -use std::io::prelude::*; -use std::path::Path; - -// See src/capture.rs for what in the world this build script is doing. - -fn main() { - let out_dir = env::var_os("OUT_DIR").unwrap(); - let out_dir = Path::new(&out_dir); - - if cfg!(feature = "serialize-rustc") { - let mut s = String::new(); - File::open("src/capture.rs").unwrap() - .read_to_string(&mut s).unwrap(); - let s = s.replace("//~ HACK1 ", ""); - File::create(out_dir.join("capture.rs")).unwrap() - .write_all(s.as_bytes()).unwrap(); - } else { - fs::copy("src/capture.rs", out_dir.join("capture.rs")).unwrap(); - } - - expand_serde(out_dir); - println!("cargo:rerun-if-changed=src/capture.rs"); -} - -#[cfg(not(feature = "serialize-serde"))] -fn expand_serde(_out_dir: &Path) {} - -#[cfg(feature = "serialize-serde")] -fn expand_serde(out_dir: &Path) { - use std::thread; - - let dst = out_dir.join("capture.rs"); - - let mut input = File::open(&dst).unwrap(); - let mut tmp = File::create(out_dir.join("tmp.rs")).unwrap(); - let mut s = String::new(); - input.read_to_string(&mut s).unwrap(); - tmp.write_all(s.replace("//~ HACK2 ", "").as_bytes()).unwrap(); - - // This has been seen to overflow the stack on travis, so just use a - // dedicated big-stack thread. - let out_dir = out_dir.to_path_buf(); - thread::Builder::new().stack_size(16 * 1024 * 1024).spawn(move || { - serde_codegen::expand(&out_dir.join("tmp.rs"), &dst).unwrap(); - }).unwrap().join().unwrap(); -} diff --git a/src/capture.rs b/src/capture.rs index 58ae0aad4..8846ea47e 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -5,28 +5,13 @@ use std::path::{Path, PathBuf}; use {trace, resolve, SymbolName}; -// Ok so the `//~ HACK` directives here are, well, hacks. Right now we want to -// compile on stable for serde support, but we also want to use -// #[derive(Serialize, Deserialize)] macros *along* with the -// `#[derive(RustcEncodable, RustcDecodable)]` macros. In theory both of these -// can be behind a #[cfg_attr], but that unfortunately doesn't work for two -// reasons: -// -// 1. rust-lang/rust#32957 - means the include! of this module doesn't expand -// the RustcDecodable/RustcEncodable blocks. -// 2. serde-rs/serde#148 - means that Serialize/Deserialize won't get expanded. -// -// We just hack around it by doing #[cfg_attr] manually essentially. Our build -// script will just strip the `//~ HACKn` prefixes here if the corresponding -// feature is enabled. - /// Representation of an owned and self-contained backtrace. /// /// This structure can be used to capture a backtrace at various points in a /// program and later used to inspect what the backtrace was at that time. #[derive(Clone)] -//~ HACK1 #[derive(RustcDecodable, RustcEncodable)] -//~ HACK2 #[derive(Deserialize, Serialize)] +#[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] +#[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))] pub struct Backtrace { frames: Vec, } @@ -36,8 +21,8 @@ pub struct Backtrace { /// This type is returned as a list from `Backtrace::frames` and represents one /// stack frame in a captured backtrace. #[derive(Clone)] -//~ HACK1 #[derive(RustcDecodable, RustcEncodable)] -//~ HACK2 #[derive(Deserialize, Serialize)] +#[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] +#[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))] pub struct BacktraceFrame { ip: usize, symbol_address: usize, @@ -49,8 +34,8 @@ pub struct BacktraceFrame { /// This type is returned as a list from `BacktraceFrame::symbols` and /// represents the metadata for a symbol in a backtrace. #[derive(Clone)] -//~ HACK1 #[derive(RustcDecodable, RustcEncodable)] -//~ HACK2 #[derive(Deserialize, Serialize)] +#[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] +#[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))] pub struct BacktraceSymbol { name: Option>, addr: Option, diff --git a/src/lib.rs b/src/lib.rs index da92b8949..c9b9c4eb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,6 +77,10 @@ extern crate libc; #[cfg(feature = "serde")] extern crate serde; +#[cfg(feature = "serde_derive")] +#[cfg_attr(feature = "serde_derive", macro_use)] +extern crate serde_derive; + #[cfg(feature = "rustc-serialize")] extern crate rustc_serialize; @@ -100,9 +104,7 @@ pub use symbolize::{resolve, Symbol, SymbolName}; mod symbolize; pub use capture::{Backtrace, BacktraceFrame, BacktraceSymbol}; -mod capture { - include!(concat!(env!("OUT_DIR"), "/capture.rs")); -} +mod capture; #[allow(dead_code)] struct Bomb { diff --git a/tests/smoke.rs b/tests/smoke.rs index 174ffecb7..bef71af37 100644 --- a/tests/smoke.rs +++ b/tests/smoke.rs @@ -160,7 +160,7 @@ fn is_serde() { extern crate serde; fn is_serialize() {} - fn is_deserialize() {} + fn is_deserialize() {} is_serialize::(); is_deserialize::();