1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

prepare_libs in Makefile

This commit is contained in:
dokaptur 2014-12-30 20:11:36 +01:00
parent 563c9ab34e
commit b71df2fd78
4 changed files with 78 additions and 39 deletions

View file

@ -3,13 +3,15 @@ ENABLE_LTO ?= n
ENABLE_STRIP ?= n ENABLE_STRIP ?= n
# Binaries # Binaries
RUSTC ?= rustc RUSTC ?= rustc
CARGO ?= cargo CARGO ?= cargo
RM := rm CC ?= gcc
RM := rm
# Install directories # Install directories
PREFIX ?= /usr/local PREFIX ?= /usr/local
BINDIR ?= /bin BINDIR ?= /bin
LIBDIR ?= /lib
# This won't support any directory with spaces in its name, but you can just # This won't support any directory with spaces in its name, but you can just
# make a symlink without spaces that points to the directory. # make a symlink without spaces that points to the directory.
@ -75,7 +77,6 @@ PROGS := \
seq \ seq \
shuf \ shuf \
sort \ sort \
stdbuf \
sum \ sum \
sync \ sync \
tac \ tac \
@ -106,6 +107,7 @@ UNIX_PROGS := \
mkfifo \ mkfifo \
nice \ nice \
nohup \ nohup \
stdbuf \
timeout \ timeout \
tty \ tty \
uname \ uname \
@ -137,6 +139,22 @@ INSTALL ?= $(EXES)
INSTALLEES := \ INSTALLEES := \
$(filter $(INSTALL),$(filter-out $(DONT_INSTALL),$(EXES) uutils)) $(filter $(INSTALL),$(filter-out $(DONT_INSTALL),$(EXES) uutils))
# Shared library extension
SYSTEM := $(shell uname)
DYLIB_EXT :=
ifeq ($(SYSTEM),Linux)
DYLIB_EXT := so
endif
ifeq ($(SYSTEM),Darwin)
DYLIB_EXT := dylib
endif
# Libaries to install
LIBS :=
ifneq (,$(findstring stdbuf, $(INSTALLEES)))
LIBS += libstdbuf.$(DYLIB_EXT)
endif
# Programs with usable tests # Programs with usable tests
TEST_PROGS := \ TEST_PROGS := \
cat \ cat \
@ -225,6 +243,16 @@ $(BUILDDIR)/uutils: $(SRCDIR)/uutils/uutils.rs $(BUILDDIR)/mkuutils $(RLIB_PATHS
$(BUILDDIR)/mkuutils $(BUILDDIR)/gen/uutils.rs $(EXES) $(BUILDDIR)/mkuutils $(BUILDDIR)/gen/uutils.rs $(EXES)
$(RUSTC) $(RUSTCBINFLAGS) --extern test=$(BUILDDIR)/libtest.rlib --emit link,dep-info $(BUILDDIR)/gen/uutils.rs --out-dir $(BUILDDIR) $(RUSTC) $(RUSTCBINFLAGS) --extern test=$(BUILDDIR)/libtest.rlib --emit link,dep-info $(BUILDDIR)/gen/uutils.rs --out-dir $(BUILDDIR)
$(if $(ENABLE_STRIP),strip $@) $(if $(ENABLE_STRIP),strip $@)
# Library for stdbuf
$(BUILDIR)/libstdbuf.$(DYLIB_EXT): $(SRCDIR)/stdbuf/libstdbuf.rs $(SRCDIR)/stdbuf/libstdbuf.c $(SRCDIR)/stdbuf/libstdbuf.h | $(BUILDDIR)
cd $(SRCDIR)/stdbuf && \
$(RUSTC) libstdbuf.rs && \
$(CC) -c -Wall -Werror -fpic libstdbuf.c -L. -llibstdbuf.a && \
$(CC) -shared -o libstdbuf.$(DYLIB_EXT) -Wl,--whole-archive liblibstdbuf.a -Wl,--no-whole-archive libstdbuf.o -lpthread && \
mv *.so $(BUILDDIR) && $(RM) *.o && $(RM) *.a
$(BUILDDIR)/stdbuf: $(BUILDIR)/libstdbuf.$(DYLIB_EXT)
# Dependencies # Dependencies
$(BUILDDIR)/.rust-crypto: | $(BUILDDIR) $(BUILDDIR)/.rust-crypto: | $(BUILDDIR)
@ -278,6 +306,10 @@ install: $(addprefix $(BUILDDIR)/,$(INSTALLEES))
for prog in $(INSTALLEES); do \ for prog in $(INSTALLEES); do \
install $(BUILDDIR)/$$prog $(DESTDIR)$(PREFIX)$(BINDIR)/$(PROG_PREFIX)$$prog; \ install $(BUILDDIR)/$$prog $(DESTDIR)$(PREFIX)$(BINDIR)/$(PROG_PREFIX)$$prog; \
done done
mkdir -p $(DESTDIR)$(PREFIX)$(LIBDIR)
for lib in $(LIBS); do \
install $(BUILDDIR)/$$lib $(DESTDIR)$(PREFIX)$(LIBDIR)/$$lib; \
done
# TODO: figure out if there is way for prefixes to work with the symlinks # TODO: figure out if there is way for prefixes to work with the symlinks
install-multicall: $(BUILDDIR)/uutils install-multicall: $(BUILDDIR)/uutils
@ -290,6 +322,7 @@ install-multicall: $(BUILDDIR)/uutils
uninstall: uninstall:
rm -f $(addprefix $(DESTDIR)$(PREFIX)$(BINDIR)/$(PROG_PREFIX),$(PROGS)) rm -f $(addprefix $(DESTDIR)$(PREFIX)$(BINDIR)/$(PROG_PREFIX),$(PROGS))
rm -f $(addprefix $(DESTDIR)$(PREFIX)$(LIBDIR)/,$(LIBS))
uninstall-multicall: uninstall-multicall:
rm -f $(addprefix $(DESTDIR)$(PREFIX)$(BINDIR)/,$(PROGS) $(PROG_PREFIX)uutils) rm -f $(addprefix $(DESTDIR)$(PREFIX)$(BINDIR)/,$(PROGS) $(PROG_PREFIX)uutils)

View file

@ -1,6 +1,6 @@
#![crate_name = "libstdbuf"] #![crate_name = "libstdbuf"]
#![crate_type = "staticlib"] #![crate_type = "staticlib"]
#![feature(macro_rules)] #![allow(unstable)]
extern crate libc; extern crate libc;
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, setvbuf};
@ -8,6 +8,7 @@ use std::ptr;
use std::os; use std::os;
#[path = "../common/util.rs"] #[path = "../common/util.rs"]
#[macro_use]
mod util; mod util;
extern { extern {
@ -23,7 +24,7 @@ fn set_buffer(stream: *mut FILE, value: &str) {
"0" => (_IONBF, 0 as size_t), "0" => (_IONBF, 0 as size_t),
"L" => (_IOLBF, 0 as size_t), "L" => (_IOLBF, 0 as size_t),
input => { input => {
let buff_size: uint = match from_str(input) { let buff_size: usize = match input.parse() {
Some(num) => num, Some(num) => num,
None => crash!(1, "incorrect size of buffer!") None => crash!(1, "incorrect size of buffer!")
}; };

View file

@ -1,9 +0,0 @@
#!/bin/bash
cd $(dirname $0)
rustc libstdbuf.rs
gcc -c -Wall -Werror -fpic libstdbuf.c -L. -llibstdbuf.a
gcc -shared -o libstdbuf.so -Wl,--whole-archive liblibstdbuf.a -Wl,--no-whole-archive libstdbuf.o -lpthread
mv *.so ../../build/
rm *.o
rm *.a

View file

@ -1,4 +1,5 @@
#![crate_name = "stdbuf"] #![crate_name = "stdbuf"]
#![allow(unstable)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -8,22 +9,23 @@
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
#![feature(macro_rules)]
extern crate getopts; extern crate getopts;
extern crate libc; extern crate libc;
use getopts::{optopt, optflag, getopts, usage, Matches, OptGroup}; use getopts::{optopt, optflag, getopts, usage, Matches, OptGroup};
use std::io::process::{Command, StdioContainer}; use std::io::process::{Command, StdioContainer};
use std::io::fs::PathExtensions;
use std::iter::range_inclusive; use std::iter::range_inclusive;
use std::num::Int; use std::num::Int;
use std::os; use std::os;
#[path = "../common/util.rs"] #[path = "../common/util.rs"]
#[macro_use]
mod util; mod util;
static NAME: &'static str = "stdbuf"; static NAME: &'static str = "stdbuf";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
static LIBSTDBUF: &'static str = "libstdbuf.so"; static LIBSTDBUF: &'static str = "libstdbuf";
enum BufferType { enum BufferType {
Default, Default,
@ -49,17 +51,17 @@ enum OkMsg {
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn preload_env() -> &'static str { fn preload_strings() -> (&'static str, &'static str) {
"LD_PRELOAD" ("LD_PRELOAD", ".so")
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
fn preload_env() -> &'static str { fn preload_strings() -> (&'static str, &'static str) {
"DYLD_INSERT_LIBRARIES" ("DYLD_INSERT_LIBRARIES", ".dyl")
} }
#[cfg(not(any(target_os = "linux", target_os = "macos")))] #[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn preload_env() -> &'static str { fn preload_strings() -> (&'static str, &'static str) {
crash!(1, "Command not supported for this operating system!") crash!(1, "Command not supported for this operating system!")
} }
@ -88,18 +90,18 @@ fn print_usage(opts: &[OptGroup]) {
} }
fn parse_size(size: &str) -> Option<u64> { fn parse_size(size: &str) -> Option<u64> {
let ext = size.trim_left_chars(|c: char| c.is_digit(10)); let ext = size.trim_left_matches(|&: c: char| c.is_digit(10));
let num = size.trim_right_chars(|c: char| c.is_alphabetic()); let num = size.trim_right_matches(|&: c: char| c.is_alphabetic());
let mut recovered = num.to_string(); let mut recovered = num.to_string();
recovered.push_str(ext); recovered.push_str(ext);
if recovered.as_slice() != size { if recovered.as_slice() != size {
return None; return None;
} }
let buf_size: u64 = match from_str(num) { let buf_size: u64 = match num.parse() {
Some(m) => m, Some(m) => m,
None => return None, None => return None,
}; };
let (power, base): (uint, u64) = match ext { let (power, base): (usize, u64) = match ext {
"" => (0, 0), "" => (0, 0),
"KB" => (1, 1024), "KB" => (1, 1024),
"K" => (1, 1000), "K" => (1, 1000),
@ -182,8 +184,28 @@ fn set_command_env(command: &mut Command, buffer_name: &str, buffer_type: Buffer
} }
} }
fn get_preload_env() -> (String, String) {
let (preload, extension) = preload_strings();
let mut libstdbuf = LIBSTDBUF.to_string();
libstdbuf.push_str(extension);
// First search for library in directory of executable.
let mut path = match os::self_exe_path() {
Some(exe_path) => exe_path,
None => crash!(1, "Impossible to fetch the path of this executable.")
};
path.push(libstdbuf.as_slice());
if path.exists() {
match path.as_str() {
Some(s) => { return (preload.to_string(), s.to_string()); },
None => crash!(1, "Error while converting path.")
};
}
// We assume library is in LD_LIBRARY_PATH/ DYLD_LIBRARY_PATH.
(preload.to_string(), libstdbuf)
}
pub fn uumain(args: Vec<String>) -> int {
pub fn uumain(args: Vec<String>) -> isize {
let optgrps = [ let optgrps = [
optopt("i", "input", "adjust standard input stream buffering", "MODE"), optopt("i", "input", "adjust standard input stream buffering", "MODE"),
optopt("o", "output", "adjust standard output stream buffering", "MODE"), optopt("o", "output", "adjust standard output stream buffering", "MODE"),
@ -216,16 +238,8 @@ pub fn uumain(args: Vec<String>) -> int {
} }
let ref command_name = args[command_idx]; let ref command_name = args[command_idx];
let mut command = Command::new(command_name); let mut command = Command::new(command_name);
let mut path = match os::self_exe_path() { let (preload_env, libstdbuf) = get_preload_env();
Some(exe_path) => exe_path, command.args(args.slice_from(command_idx+1)).env(preload_env.as_slice(), libstdbuf.as_slice());
None => crash!(1, "Impossible to fetch the path of this executable.")
};
path.push(LIBSTDBUF);
let libstdbuf = match path.as_str() {
Some(s) => s,
None => crash!(1, "Error while converting path.")
};
command.args(args.slice_from(command_idx+1)).env(preload_env(), libstdbuf);
command.stdin(StdioContainer::InheritFd(0)).stdout(StdioContainer::InheritFd(1)).stderr(StdioContainer::InheritFd(2)); command.stdin(StdioContainer::InheritFd(0)).stdout(StdioContainer::InheritFd(1)).stderr(StdioContainer::InheritFd(2));
set_command_env(&mut command, "_STDBUF_I", options.stdin); set_command_env(&mut command, "_STDBUF_I", options.stdin);
set_command_env(&mut command, "_STDBUF_O", options.stdout); set_command_env(&mut command, "_STDBUF_O", options.stdout);