mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
use cargo idioms to manage dependency resolution and compilation
This commit is contained in:
parent
a21c54e2cd
commit
502957dc3e
178 changed files with 765 additions and 616 deletions
353
Makefile
353
Makefile
|
@ -1,43 +1,30 @@
|
|||
# Config options
|
||||
ENABLE_LTO ?= n
|
||||
ENABLE_STRIP ?= n
|
||||
ENABLE_RELEASE ?= n
|
||||
PROFILE ?= debug
|
||||
MULTICALL ?= n
|
||||
|
||||
PROFILE_CMD :=
|
||||
ifeq (${PROFILE},release)
|
||||
PROFILE_CMD = --release
|
||||
endif
|
||||
|
||||
# Binaries
|
||||
RUSTC ?= rustc
|
||||
CARGO ?= cargo
|
||||
CC ?= gcc
|
||||
RM := rm
|
||||
|
||||
# Install directories
|
||||
PREFIX ?= /usr/local
|
||||
BINDIR ?= /bin
|
||||
LIBDIR ?= /lib
|
||||
|
||||
INSTALLDIR=$(DESTDIR)$(PREFIX)
|
||||
|
||||
# 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.
|
||||
BASEDIR ?= $(shell pwd)
|
||||
SRCDIR := $(BASEDIR)/src
|
||||
BUILDDIR := $(BASEDIR)/build
|
||||
TESTDIR := $(BASEDIR)/tests
|
||||
TEMPDIR := $(BASEDIR)/tmp
|
||||
BUILDDIR := $(BASEDIR)/target/${PROFILE}/
|
||||
PKG_BUILDDIR := $(BUILDDIR)/deps/
|
||||
|
||||
# Flags
|
||||
RUSTCFLAGS := -O
|
||||
RMFLAGS :=
|
||||
|
||||
RUSTCLIBFLAGS := $(RUSTCFLAGS) -L $(BUILDDIR)/
|
||||
RUSTCTESTFLAGS := $(RUSTCFLAGS)
|
||||
|
||||
# Handle config setup
|
||||
ifeq ($(ENABLE_LTO),y)
|
||||
RUSTCBINFLAGS := $(RUSTCLIBFLAGS) -C lto
|
||||
else
|
||||
RUSTCBINFLAGS := $(RUSTCLIBFLAGS)
|
||||
endif
|
||||
|
||||
ifneq ($(ENABLE_STRIP),y)
|
||||
ENABLE_STRIP :=
|
||||
endif
|
||||
|
||||
# Possible programs
|
||||
PROGS := \
|
||||
|
@ -83,7 +70,7 @@ PROGS := \
|
|||
sync \
|
||||
tac \
|
||||
tee \
|
||||
test \
|
||||
test_uu \
|
||||
tr \
|
||||
true \
|
||||
truncate \
|
||||
|
@ -132,17 +119,20 @@ ALIASES := \
|
|||
|
||||
BUILD ?= $(PROGS)
|
||||
|
||||
TESTS := \
|
||||
$(filter $(PROGS),$(filter-out $(DONT_TEST),$(filter $(BUILD),$(filter-out $(DONT_BUILD),$(TEST_PROGS)))))
|
||||
|
||||
TEST ?= $(TEST_PROGS)
|
||||
|
||||
# Output names
|
||||
EXES := \
|
||||
$(sort $(filter $(BUILD),$(filter-out $(DONT_BUILD),$(PROGS))))
|
||||
|
||||
CRATE_RLIBS :=
|
||||
|
||||
INSTALL ?= $(EXES)
|
||||
|
||||
INSTALLEES := \
|
||||
$(filter $(INSTALL),$(filter-out $(DONT_INSTALL),$(EXES) uutils))
|
||||
|
||||
INSTALL ?= $(EXES)
|
||||
|
||||
# Shared library extension
|
||||
SYSTEM := $(shell uname)
|
||||
DYLIB_EXT :=
|
||||
|
@ -161,282 +151,57 @@ ifneq (,$(findstring stdbuf, $(INSTALLEES)))
|
|||
LIBS += libstdbuf.$(DYLIB_EXT)
|
||||
endif
|
||||
|
||||
# Programs with usable tests
|
||||
TEST_PROGS := \
|
||||
base64 \
|
||||
basename \
|
||||
cat \
|
||||
cksum \
|
||||
cp \
|
||||
cut \
|
||||
env \
|
||||
dirname \
|
||||
echo \
|
||||
factor \
|
||||
false \
|
||||
fold \
|
||||
hashsum \
|
||||
head \
|
||||
link \
|
||||
ln \
|
||||
mkdir \
|
||||
mv \
|
||||
nl \
|
||||
paste \
|
||||
ptx \
|
||||
pwd \
|
||||
readlink \
|
||||
realpath \
|
||||
rm \
|
||||
rmdir \
|
||||
seq \
|
||||
sort \
|
||||
split \
|
||||
stdbuf \
|
||||
sum \
|
||||
tac \
|
||||
test \
|
||||
touch \
|
||||
tr \
|
||||
true \
|
||||
truncate \
|
||||
tsort \
|
||||
unlink \
|
||||
unexpand \
|
||||
wc
|
||||
|
||||
TEST ?= $(TEST_PROGS)
|
||||
|
||||
TESTS := \
|
||||
$(filter $(TEST),$(filter-out $(DONT_TEST),$(filter $(BUILD),$(filter-out $(DONT_BUILD),$(TEST_PROGS)))))
|
||||
|
||||
# figure out what dependencies we need based on which programs we're building
|
||||
define DEP_INCLUDE
|
||||
-include $(SRCDIR)/$(1)/deps.mk
|
||||
endef
|
||||
# we always depend on libc because common/util does
|
||||
# we also depend on getopts since all utilities support command-line arguments
|
||||
DEPLIBS := libc getopts
|
||||
DEPPLUGS :=
|
||||
# now, add in deps in src/utilname/deps.mk
|
||||
# if we're testing, only consider the TESTS variable,
|
||||
# otherwise consider the EXES variable
|
||||
ifeq ($(MAKECMDGOALS),test)
|
||||
$(foreach build,$(TESTS),$(eval $(call DEP_INCLUDE,$(build))))
|
||||
else
|
||||
$(foreach build,$(sort $(TESTS) $(EXES)),$(eval $(call DEP_INCLUDE,$(build))))
|
||||
endif
|
||||
# uniqify deps
|
||||
DEPLIBS := $(sort $(DEPLIBS))
|
||||
DEPPLUGS := $(sort $(DEPPLUGS))
|
||||
# build --extern commandline for rustc
|
||||
DEP_EXTERN := $(foreach lib,$(subst -,_,$(DEPLIBS)),--extern $(lib)=$(BUILDDIR)/lib$(lib).rlib)
|
||||
DEP_EXTERN += $(foreach plug,$(subst -,_,$(DEPPLUGS)),--extern $(plug)=$(BUILDDIR)/lib$(plug).$(DYLIB_EXT))
|
||||
|
||||
# Setup for building crates
|
||||
define BUILD_SETUP
|
||||
X := $(shell $(RUSTC) --print file-names --crate-type rlib $(SRCDIR)/$(1)/$(1).rs)
|
||||
$(1)_RLIB := $$(X)
|
||||
CRATE_RLIBS += $$(X)
|
||||
endef
|
||||
$(foreach crate,$(EXES),$(eval $(call BUILD_SETUP,$(crate))))
|
||||
|
||||
# Utils stuff
|
||||
EXES_PATHS := $(addprefix $(BUILDDIR)/,$(EXES))
|
||||
RLIB_PATHS := $(addprefix $(BUILDDIR)/,$(CRATE_RLIBS))
|
||||
command = sh -c '$(1)'
|
||||
RESERVED_EXTERNS := --extern uufalse=$(BUILDDIR)/libfalse.rlib --extern uutrue=$(BUILDDIR)/libtrue.rlib --extern uutest=$(BUILDDIR)/libtest.rlib
|
||||
|
||||
# Main exe build rule
|
||||
define EXE_BUILD
|
||||
$(BUILDDIR)/gen/$(1).rs: $(BUILDDIR)/mkmain
|
||||
$(BUILDDIR)/mkmain $(1) $$@
|
||||
|
||||
$(BUILDDIR)/$(1): $(BUILDDIR)/gen/$(1).rs $(BUILDDIR)/$($(1)_RLIB) | $(BUILDDIR) deps
|
||||
$(RUSTC) $(RUSTCBINFLAGS) $(RESERVED_EXTERNS) -o $$@ $$<
|
||||
$(if $(ENABLE_STRIP),strip $$@,)
|
||||
endef
|
||||
|
||||
# GRRR rust-crypto makes a crate called "crypto".
|
||||
# This should NOT be allowed by crates.io. GRRRR.
|
||||
define DEP_BUILD
|
||||
DEP_$(1):
|
||||
ifeq ($(1),crypto)
|
||||
cd $(BASEDIR)/deps && $(CARGO) build --package rust-crypto --release
|
||||
else ifeq ($(1),kernel32)
|
||||
cd $(BASEDIR)/deps && $(CARGO) build --package kernel32-sys --release
|
||||
else ifeq ($(1),advapi32)
|
||||
cd $(BASEDIR)/deps && $(CARGO) build --package advapi32-sys --release
|
||||
else
|
||||
cd $(BASEDIR)/deps && $(CARGO) build --package $(1) --release
|
||||
endif
|
||||
endef
|
||||
|
||||
define CRATE_BUILD
|
||||
-include $(BUILDDIR)/$(1).d
|
||||
|
||||
$(BUILDDIR)/$($(1)_RLIB): $(SRCDIR)/$(1)/$(1).rs | $(BUILDDIR) deps
|
||||
$(RUSTC) $(RUSTCLIBFLAGS) $(DEP_EXTERN) --crate-type rlib --emit link,dep-info $$< --out-dir $(BUILDDIR)
|
||||
endef
|
||||
|
||||
# Aliases build rule
|
||||
ALIAS_SOURCE = $(firstword $(subst :, ,$(1)))
|
||||
ALIAS_TARGET = $(word 2,$(subst :, ,$(1)))
|
||||
define MAKE_ALIAS
|
||||
|
||||
ifneq ($(ALIAS_TARGET,$(1)),)
|
||||
all: $(BUILDDIR)/$(call ALIAS_TARGET,$(1))
|
||||
$(BUILDDIR)/$(call ALIAS_TARGET,$(1)): $(BUILDDIR)/$(call ALIAS_SOURCE,$(1))
|
||||
$(call command,install $$@ $$<)
|
||||
endif
|
||||
|
||||
endef
|
||||
|
||||
# Test exe built rules
|
||||
define TEST_BUILD
|
||||
test_$(1): $(BUILDDIR)/$(1) $(TEMPDIR)/$(1)/$(1)_test
|
||||
$(call command,cp $(BUILDDIR)/$(1) $(TEMPDIR)/$(1) && cd $(TEMPDIR)/$(1) && $(TEMPDIR)/$(1)/$(1)_test)
|
||||
|
||||
$(TEMPDIR)/$(1)/$(1)_test: $(TESTDIR)/$(1).rs | $(TEMPDIR)/$(1)
|
||||
$(call command,$(RUSTC) $(RUSTCTESTFLAGS) $(DEP_EXTERN) --test -o $$@ $$<)
|
||||
|
||||
$(TEMPDIR)/$(1): | $(TEMPDIR)
|
||||
$(call command,cp -r $(TESTDIR)/fixtures/$(1) $$@ || mkdir $$@)
|
||||
endef
|
||||
|
||||
# Main rules
|
||||
all: $(EXES_PATHS) $(BUILDDIR)/uutils
|
||||
|
||||
# Creating necessary rules for each targets
|
||||
$(foreach crate,$(EXES),$(eval $(call CRATE_BUILD,$(crate))))
|
||||
$(foreach exe,$(EXES),$(eval $(call EXE_BUILD,$(exe))))
|
||||
$(foreach alias,$(ALIASES),$(eval $(call MAKE_ALIAS,$(alias))))
|
||||
$(foreach test,$(TESTS),$(eval $(call TEST_BUILD,$(test))))
|
||||
$(foreach dep,$(sort $(DEPLIBS) $(DEPPLUGS)),$(eval $(call DEP_BUILD,$(dep))))
|
||||
|
||||
-include $(BUILDDIR)/uutils.d
|
||||
$(BUILDDIR)/uutils: $(SRCDIR)/uutils/uutils.rs $(BUILDDIR)/mkuutils $(RLIB_PATHS)
|
||||
$(BUILDDIR)/mkuutils $(BUILDDIR)/gen/uutils.rs $(EXES)
|
||||
$(RUSTC) $(RUSTCBINFLAGS) $(RESERVED_EXTERNS) --emit link,dep-info $(BUILDDIR)/gen/uutils.rs --out-dir $(BUILDDIR)
|
||||
$(if $(ENABLE_STRIP),strip $@)
|
||||
|
||||
# Library for stdbuf
|
||||
$(BUILDDIR)/libstdbuf.$(DYLIB_EXT): $(SRCDIR)/stdbuf/libstdbuf.rs $(SRCDIR)/stdbuf/libstdbuf.c $(SRCDIR)/stdbuf/libstdbuf.h | $(BUILDDIR)
|
||||
cd $(SRCDIR)/stdbuf && \
|
||||
$(RUSTC) libstdbuf.rs --extern libc=$(BUILDDIR)/liblibc.rlib && \
|
||||
$(CC) -c -Wall -Werror -fPIC libstdbuf.c && \
|
||||
$(CC) $(DYLIB_FLAGS) -o libstdbuf.$(DYLIB_EXT) liblibstdbuf.a libstdbuf.o && \
|
||||
mv *.$(DYLIB_EXT) $(BUILDDIR) && $(RM) *.o && $(RM) *.a
|
||||
|
||||
$(BUILDDIR)/stdbuf: $(BUILDDIR)/libstdbuf.$(DYLIB_EXT)
|
||||
|
||||
deps: $(BUILDDIR) $(SRCDIR)/cksum/crc_table.rs $(addprefix DEP_,$(DEPLIBS) $(DEPPLUGS))
|
||||
$(foreach lib,$(subst -,_,$(DEPLIBS)),$(shell cp $(BASEDIR)/deps/target/release/deps/lib$(lib)-*.rlib $(BUILDDIR)/lib$(lib).rlib))
|
||||
$(foreach plug,$(subst -,_,$(DEPPLUGS)),$(shell cp $(BASEDIR)/deps/target/release/deps/lib$(plug)-*.$(DYLIB_EXT) $(BUILDDIR)/lib$(plug).$(DYLIB_EXT)))
|
||||
|
||||
$(BUILDDIR)/mkmain: mkmain.rs | $(BUILDDIR)
|
||||
$(RUSTC) $(RUSTCFLAGS) $< -o $@
|
||||
|
||||
$(BUILDDIR)/mkuutils: mkuutils.rs | $(BUILDDIR)
|
||||
$(RUSTC) $(RUSTCFLAGS) $< -o $@
|
||||
|
||||
$(SRCDIR)/cksum/crc_table.rs: $(SRCDIR)/cksum/gen_table.rs
|
||||
cd $(SRCDIR)/cksum && $(RUSTC) $(RUSTCBINFLAGS) gen_table.rs && ./gen_table && $(RM) gen_table
|
||||
|
||||
$(SRCDIR)/factor/prime_table.rs: $(SRCDIR)/factor/gen_table.rs
|
||||
cd $(SRCDIR)/factor && $(RUSTC) $(RUSTCBINFLAGS) gen_table.rs && ./gen_table > $@ && $(RM) gen_table
|
||||
all: build
|
||||
|
||||
crates:
|
||||
echo $(EXES)
|
||||
echo "okay" $(EXES)
|
||||
|
||||
test: $(TEMPDIR) $(addprefix test_,$(TESTS))
|
||||
$(RM) -rf $(TEMPDIR)
|
||||
build_uutils = ${CARGO} build --features "${1}" ${PROFILE_CMD} --no-default-features
|
||||
build_pkg = ${CARGO} build ${PROFILE_CMD} -p "${1}"
|
||||
run_integration_tests = ${CARGO} test --test "${1}"
|
||||
run_unit_tests = ${CARGO} test -p "${l}"
|
||||
do_install = install ${1}
|
||||
use_default := 1
|
||||
|
||||
test:
|
||||
$(call build_uutils, ${TESTS})
|
||||
$(foreach util, ${TESTS}, $(call run_integration_tests, ${util});)
|
||||
$(foreach util, ${TESTS}, $(call run_unit_tests, ${util});)
|
||||
|
||||
build:
|
||||
$(call build_uutils,${EXES})
|
||||
$(foreach util, ${EXES}, $(call build_pkg,${util});)
|
||||
|
||||
clean:
|
||||
$(RM) -rf $(BUILDDIR) $(TEMPDIR)
|
||||
$(RM) -rf $(BUILDDIR)
|
||||
|
||||
distclean: clean
|
||||
cd $(BASEDIR)/deps && $(CARGO) clean && $(CARGO) update
|
||||
$(CARGO) clean && $(CARGO) update
|
||||
|
||||
$(BUILDDIR):
|
||||
mkdir -p $(BUILDDIR)/gen
|
||||
|
||||
$(TEMPDIR):
|
||||
$(RM) -rf $(TEMPDIR)
|
||||
mkdir $(TEMPDIR)
|
||||
|
||||
install: $(addprefix $(BUILDDIR)/,$(INSTALLEES))
|
||||
mkdir -p $(DESTDIR)$(PREFIX)$(BINDIR)
|
||||
for prog in $(INSTALLEES); do \
|
||||
install $(BUILDDIR)/$$prog $(DESTDIR)$(PREFIX)$(BINDIR)/$(PROG_PREFIX)$$prog; \
|
||||
done
|
||||
mkdir -p $(DESTDIR)$(PREFIX)$(LIBDIR)
|
||||
for lib in $(LIBS); do \
|
||||
install $(BUILDDIR)/$$lib $(DESTDIR)$(PREFIX)$(LIBDIR)/$$lib; \
|
||||
done
|
||||
build-check: build clean
|
||||
test-check: test clean
|
||||
|
||||
# TODO: figure out if there is way for prefixes to work with the symlinks
|
||||
install-multicall: $(BUILDDIR)/uutils
|
||||
mkdir -p $(DESTDIR)$(PREFIX)$(BINDIR)
|
||||
install $(BUILDDIR)/uutils $(DESTDIR)$(PREFIX)$(BINDIR)/$(PROG_PREFIX)uutils
|
||||
cd $(DESTDIR)$(PREFIX)$(BINDIR)
|
||||
for prog in $(INSTALLEES); do \
|
||||
ln -s $(PROG_PREFIX)uutils $$prog; \
|
||||
done
|
||||
mkdir -p $(DESTDIR)$(PREFIX)$(LIBDIR)
|
||||
for lib in $(LIBS); do \
|
||||
install $(BUILDDIR)/$$lib $(DESTDIR)$(PREFIX)$(LIBDIR)/$$lib; \
|
||||
done
|
||||
install: build
|
||||
PROFILE_CMD=--release
|
||||
mkdir -p $(INSTALLDIR)$(BINDIR)
|
||||
ifeq (${MULTICALL}, y)
|
||||
install $(BUILDDIR)/uutils $(INSTALLDIR)$(BINDIR)/$(PROG_PREFIX)uutils
|
||||
cd $(INSTALLDIR)$(BINDIR)
|
||||
$(foreach prog, $(INSTALLEES), ln -s $(PROG_PREFIX)uutils $$prog;)
|
||||
else
|
||||
$(foreach prog, $(INSTALLEES); \
|
||||
install $(PKG_BUILDDIR)/$$prog $(INSTALLDIR)$(BINDIR)/$(PROG_PREFIX)$$prog;)
|
||||
endif
|
||||
mkdir -p $(INSTALLDIR)$(LIBDIR)
|
||||
$(foreach lib, $(LIBS), install $(BUILDDIR)/$$lib $(INSTALLDIR)$(LIBDIR)/$$lib;)
|
||||
|
||||
uninstall:
|
||||
rm -f $(addprefix $(DESTDIR)$(PREFIX)$(BINDIR)/$(PROG_PREFIX),$(PROGS))
|
||||
rm -f $(addprefix $(DESTDIR)$(PREFIX)$(LIBDIR)/,$(LIBS))
|
||||
rm -f $(addprefix $(INSTALLDIR)$(BINDIR)/$(PROG_PREFIX),$(PROGS))
|
||||
rm -f $(addprefix $(INSTALLDIR)$(LIBDIR)/,$(LIBS))
|
||||
|
||||
uninstall-multicall:
|
||||
rm -f $(addprefix $(DESTDIR)$(PREFIX)$(BINDIR)/,$(PROGS) $(PROG_PREFIX)uutils)
|
||||
rm -f $(addprefix $(DESTDIR)$(PREFIX)$(LIBDIR)/,$(LIBS))
|
||||
rm -f $(addprefix $(INSTALLDIR)$(BINDIR)/,$(PROGS) $(PROG_PREFIX)uutils)
|
||||
rm -f $(addprefix $(INSTALLDIR)$(LIBDIR)/,$(LIBS))
|
||||
|
||||
# Test under the busybox testsuite
|
||||
$(BUILDDIR)/busybox: $(BUILDDIR)/uutils
|
||||
rm -f $(BUILDDIR)/busybox
|
||||
ln -s $(BUILDDIR)/uutils $(BUILDDIR)/busybox
|
||||
|
||||
# This is a busybox-specific config file their test suite wants to parse.
|
||||
$(BUILDDIR)/.config: $(BASEDIR)/.busybox-config $(BUILDDIR)/uutils
|
||||
cp $< $@
|
||||
|
||||
ifeq ($(BUSYBOX_SRC),)
|
||||
busytest:
|
||||
@echo
|
||||
@echo "To run \`busytest\` set BUSYBOX_SRC to the directory of the compiled busybox source code."
|
||||
@echo "Optionally set RUNTEST_ARGS to arguments to pass to the busybox \`runtest\` program."
|
||||
@echo
|
||||
@false
|
||||
else
|
||||
busytest: $(BUILDDIR)/busybox $(BUILDDIR)/.config
|
||||
(cd $(BUSYBOX_SRC)/testsuite && bindir=$(BUILDDIR) ./runtest $(RUNTEST_ARGS))
|
||||
endif
|
||||
|
||||
# This rule will build each program, ignore all output, and return pass
|
||||
# or fail depending on whether the build has errors.
|
||||
build-check:
|
||||
@for prog in $(sort $(PROGS)); do \
|
||||
make BUILD="$$prog" >/dev/null 2>&1; status=$$?; \
|
||||
if [ $$status -eq 0 ]; \
|
||||
then printf "%-10s\t\033[1;32mpass\033[00;m\n" $$prog; \
|
||||
else printf "%-10s\t\033[1;31mfail\033[00;m\n" $$prog; \
|
||||
fi; \
|
||||
done
|
||||
|
||||
# This rule will test each program, ignore all output, and return pass
|
||||
# or fail depending on whether the test has errors.
|
||||
test-check:
|
||||
@for prog in $(sort $(TEST_PROGS)); do \
|
||||
make TEST="$$prog" test >/dev/null 2>&1; status=$$?; \
|
||||
if [ $$status -eq 0 ]; \
|
||||
then printf "%-10s\t\033[1;32mpass\033[00;m\n" $$prog; \
|
||||
else printf "%-10s\t\033[1;31mfail\033[00;m\n" $$prog; \
|
||||
fi; \
|
||||
done
|
||||
|
||||
.PHONY: $(TEMPDIR) all deps test distclean clean busytest install uninstall
|
||||
.PHONY: $(TEMPDIR) all build test distclean clean busytest install uninstall
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue