From 2b4a685286dc88ef131452b1d2919fd63ca64eab Mon Sep 17 00:00:00 2001 From: Alex Lyon Date: Fri, 8 Dec 2017 18:50:18 -0800 Subject: [PATCH] stdbuf: stop using GNU make as part of the build process --- src/stdbuf/Cargo.toml | 2 ++ src/stdbuf/Makefile | 31 ----------------------- src/stdbuf/build.rs | 58 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 32 deletions(-) delete mode 100644 src/stdbuf/Makefile diff --git a/src/stdbuf/Cargo.toml b/src/stdbuf/Cargo.toml index c4aebd1c1..205406298 100644 --- a/src/stdbuf/Cargo.toml +++ b/src/stdbuf/Cargo.toml @@ -15,6 +15,8 @@ libstdbuf = { path="libstdbuf" } [build-dependencies] cc = "1.0" +glob = "0.2" +libstdbuf = { path = "libstdbuf" } [[bin]] name = "stdbuf" diff --git a/src/stdbuf/Makefile b/src/stdbuf/Makefile deleted file mode 100644 index c9c0350b2..000000000 --- a/src/stdbuf/Makefile +++ /dev/null @@ -1,31 +0,0 @@ -BUILDDIR ?= $(OUT_DIR) - -CC ?= gcc -RUSTC ?= rustc - -# Shared library extension -SYSTEM := $(shell uname) -DYLIB_EXT := -ifeq ($(SYSTEM),Linux) - DYLIB_EXT := so - DYLIB_FLAGS := -shared - DYLIB_LINK_START := -Wl,--whole-archive - DYLIB_LINK_END := -Wl,--no-whole-archive -endif -ifeq ($(SYSTEM),Darwin) - DYLIB_EXT := dylib - DYLIB_FLAGS := -dynamiclib -undefined dynamic_lookup - DYLIB_LINK_START := - DYLIB_LINK_END := -endif - -all: $(BUILDDIR)/libstdbuf.$(DYLIB_EXT) - -$(BUILDDIR): - mkdir -p $@ - -$(BUILDDIR)/libstdbuf.$(DYLIB_EXT): build.rs libstdbuf/libstdbuf.rs libstdbuf.c libstdbuf.h | $(BUILDDIR) - cp $(BUILDDIR)/../../../deps/liblibstdbuf-*.a $(BUILDDIR)/liblibstdbuf.a && \ - $(CC) -c -Wall -Werror -fPIC libstdbuf.c -o $(BUILDDIR)/libstdbuf.o && \ - $(CC) $(DYLIB_FLAGS) -o $@ $(BUILDDIR)/libstdbuf.o $(DYLIB_LINK_START) $(BUILDDIR)/liblibstdbuf.a $(DYLIB_LINK_END) - diff --git a/src/stdbuf/build.rs b/src/stdbuf/build.rs index c53bd59c0..c0697c557 100644 --- a/src/stdbuf/build.rs +++ b/src/stdbuf/build.rs @@ -1,10 +1,66 @@ +extern crate cc; +extern crate glob; + +use std::env; use std::process::Command; +use glob::glob; + #[path = "../../mkmain.rs"] mod mkmain; +#[cfg(target_os = "macos")] +mod platform { + pub const DYLIB_EXT: &'static str = "dylib"; + pub const DYLIB_FLAGS: [&'static str; 3] = ["-dynamiclib", "-undefined", "dynamic_lookup"]; + pub const DYLIB_LINK_START: &'static str = ""; + pub const DYLIB_LINK_END: &'static str = ""; +} + +#[cfg(target_os = "linux")] +mod platform { + pub const DYLIB_EXT: &'static str = "so"; + pub const DYLIB_FLAGS: [&'static str; 1] = ["-shared"]; + pub const DYLIB_LINK_START: &'static str = "-Wl,--whole-archive"; + pub const DYLIB_LINK_END: &'static str = "-Wl,--no-whole-archive"; +} + +// FIXME: this entire thing is pretty fragile fn main() { mkmain::main(); - Command::new("make").output().expect("failed to execute make"); + let cc = env::var("CC").unwrap_or("gcc".to_string()); + + let out_dir = env::var("OUT_DIR").unwrap(); + + let entry = glob(&format!("{}/../../../deps/liblibstdbuf-*.a", out_dir)).unwrap() + .next() + .unwrap() + .unwrap(); + + cc::Build::new() + .flag("-Wall") + .flag("-Werror") + .pic(true) + .file("libstdbuf.c") + .compile("libstdbuf.a"); + + // XXX: we have to link manually because apparently cc-rs does not support shared libraries + let mut link = Command::new(cc); + for flag in platform::DYLIB_FLAGS.iter() { + link.arg(flag); + } + link.arg("-o") + .arg(format!("{}/libstdbuf.{}", out_dir, platform::DYLIB_EXT)) + .arg(format!("{}/libstdbuf.a", out_dir)); + if platform::DYLIB_LINK_START.len() > 0 { + link.arg(platform::DYLIB_LINK_START); + } + link.arg(entry); + if platform::DYLIB_LINK_END.len() > 0 { + link.arg(platform::DYLIB_LINK_END); + } + if !link.spawn().unwrap().wait().unwrap().success() { + panic!("linking failed"); + } }