stdbuf: build the shared library again and fix the Makefile

This commit is contained in:
Alex Lyon 2017-10-09 15:09:24 -07:00
parent 4e034b02bd
commit de07c6218b
No known key found for this signature in database
GPG key ID: B517F04B325131B1
8 changed files with 367 additions and 264 deletions

517
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -306,10 +306,10 @@ ifeq (${MULTICALL}, y)
cd $(INSTALLDIR_BIN) && ln -fs $(PROG_PREFIX)uutils $(PROG_PREFIX)$(prog);)
else
$(foreach prog, $(INSTALLEES), \
$(INSTALL) $(PKG_BUILDDIR)/$(prog) $(INSTALLDIR_BIN)/$(PROG_PREFIX)$(prog);)
$(INSTALL) $(BUILDDIR)/$(prog) $(INSTALLDIR_BIN)/$(PROG_PREFIX)$(prog);)
endif
mkdir -p $(INSTALLDIR_LIB)
$(foreach lib, $(LIBS), $(INSTALL) $(BUILDDIR)/$(lib) $(INSTALLDIR_LIB)/$(lib);)
$(foreach lib, $(LIBS), $(INSTALL) $(BUILDDIR)/build/*/out/$(lib) $(INSTALLDIR_LIB)/$(lib);)
uninstall:
ifeq (${MULTICALL}, y)

View file

@ -2,6 +2,7 @@
name = "stdbuf"
version = "0.0.1"
authors = []
build = "build.rs"
[lib]
name = "uu_stdbuf"
@ -10,6 +11,10 @@ path = "stdbuf.rs"
[dependencies]
getopts = "0.2.14"
uucore = { path="../uucore" }
libstdbuf = { path="libstdbuf" }
[build-dependencies]
cc = "1.0"
[[bin]]
name = "stdbuf"

27
src/stdbuf/Makefile Normal file
View file

@ -0,0 +1,27 @@
BUILDDIR ?= $(OUT_DIR)
CC ?= gcc
RUSTC ?= rustc
# Shared library extension
SYSTEM := $(shell uname)
DYLIB_EXT :=
ifeq ($(SYSTEM),Linux)
DYLIB_EXT := so
DYLIB_FLAGS := -shared
endif
ifeq ($(SYSTEM),Darwin)
DYLIB_EXT := dylib
DYLIB_FLAGS := -dynamiclib -undefined dynamic_lookup
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 -Wl,--whole-archive $(BUILDDIR)/liblibstdbuf.a -Wl,--no-whole-archive

5
src/stdbuf/build.rs Normal file
View file

@ -0,0 +1,5 @@
use std::process::Command;
fn main() {
Command::new("make").output().expect("failed to execute make");
}

View file

@ -1,12 +1,9 @@
#![crate_name = "libstdbuf"]
#![crate_type = "staticlib"]
extern crate libc;
#[macro_use]
extern crate uucore;
use libc::{c_int, size_t, c_char, FILE, _IOFBF, _IONBF, _IOLBF, setvbuf};
use libc::{c_int, size_t, c_char, FILE, _IOFBF, _IONBF, _IOLBF};
use std::env;
use std::io::Write;
use std::ptr;
@ -17,8 +14,6 @@ extern {
static stderr: *mut FILE;
}
static NAME: &'static str = "libstdbuf";
fn set_buffer(stream: *mut FILE, value: &str) {
let (mode, size): (c_int, size_t) = match value {
"0" => (_IONBF, 0 as size_t),
@ -43,7 +38,7 @@ fn set_buffer(stream: *mut FILE, value: &str) {
}
#[no_mangle]
pub extern fn stdbuf() {
pub unsafe extern "C" fn stdbuf() {
if let Ok(val) = env::var("_STDBUF_E") {
set_buffer(stderr, &val);
}

View file

@ -0,0 +1,13 @@
[package]
name = "libstdbuf"
version = "0.0.1"
authors = []
[lib]
name = "libstdbuf"
path = "libstdbuf.rs"
crate-type = ["staticlib"]
[dependencies]
uucore = { path="../../uucore" }
libc = "0.2"

View file

@ -0,0 +1,51 @@
extern crate libc;
#[macro_use]
extern crate uucore;
use libc::{c_int, size_t, c_char, FILE, _IOFBF, _IONBF, _IOLBF};
use std::env;
use std::io::Write;
use std::ptr;
extern {
static stdin: *mut FILE;
static stdout: *mut FILE;
static stderr: *mut FILE;
}
fn set_buffer(stream: *mut FILE, value: &str) {
let (mode, size): (c_int, size_t) = match value {
"0" => (_IONBF, 0 as size_t),
"L" => (_IOLBF, 0 as size_t),
input => {
let buff_size: usize = match input.parse() {
Ok(num) => num,
Err(e) => crash!(1, "incorrect size of buffer!: {}", e)
};
(_IOFBF, buff_size as size_t)
}
};
let res: c_int;
unsafe {
let buffer: *mut c_char = ptr::null_mut();
assert!(buffer.is_null());
res = libc::setvbuf(stream, buffer, mode, size);
}
if res != 0 {
crash!(res, "error while calling setvbuf!");
}
}
#[no_mangle]
pub unsafe extern fn stdbuf() {
if let Ok(val) = env::var("_STDBUF_E") {
set_buffer(stderr, &val);
}
if let Ok(val) = env::var("_STDBUF_I") {
set_buffer(stdin, &val);
}
if let Ok(val) = env::var("_STDBUF_O") {
set_buffer(stdout, &val);
}
}