| 1 |
# spell-checker:ignore (misc) testsuite runtest findstring (targets) busytest toybox distclean pkgs nextest ; (vars/env) BINDIR BUILDDIR CARGOFLAGS CFLAGS DESTDIR DOCSDIR INSTALLDIR INSTALLEES MULTICALL DATAROOTDIR TESTDIR manpages |
| 2 |
|
| 3 |
# Config options |
| 4 |
ifneq (,$(filter install, $(MAKECMDGOALS))) |
| 5 |
PROFILE?=release |
| 6 |
endif |
| 7 |
PROFILE ?= debug |
| 8 |
MULTICALL ?= n |
| 9 |
COMPLETIONS ?= y |
| 10 |
MANPAGES ?= y |
| 11 |
LOCALES ?= y |
| 12 |
# allow bootstrapping by our install. mkdir, chmod and cp should be avoided |
| 13 |
INSTALL ?= install |
| 14 |
|
| 15 |
# Needed for the foreach loops to split each loop into a separate command |
| 16 |
define newline |
| 17 |
|
| 18 |
|
| 19 |
endef |
| 20 |
|
| 21 |
PROFILE_CMD := --profile=${PROFILE} |
| 22 |
ifeq ($(PROFILE),debug) |
| 23 |
PROFILE_CMD = |
| 24 |
endif |
| 25 |
|
| 26 |
# Binaries |
| 27 |
CARGO ?= cargo |
| 28 |
CARGOFLAGS ?= |
| 29 |
|
| 30 |
#prefix prepended to all binaries and library dir |
| 31 |
PROG_PREFIX ?= rust- |
| 32 |
|
| 33 |
# Install directories |
| 34 |
PREFIX ?= /usr |
| 35 |
DESTDIR ?= |
| 36 |
BINDIR ?= $(PREFIX)/bin |
| 37 |
DATAROOTDIR ?= $(PREFIX)/share |
| 38 |
LIBSTDBUF_DIR ?= $(PREFIX)/libexec/$(PROG_PREFIX)coreutils |
| 39 |
# Export variable so that it is used during the build |
| 40 |
export LIBSTDBUF_DIR |
| 41 |
|
| 42 |
INSTALLDIR_BIN=$(DESTDIR)$(BINDIR) |
| 43 |
|
| 44 |
# This won't support any directory with spaces in its name, but you can just |
| 45 |
# make a symlink without spaces that points to the directory. |
| 46 |
BASEDIR ?= $(shell pwd) |
| 47 |
ifdef CARGO_TARGET_DIR |
| 48 |
BUILDDIR := $(CARGO_TARGET_DIR)/${PROFILE} |
| 49 |
BUILDDIR_UUDOC := $(CARGO_TARGET_DIR)/${PROFILE} |
| 50 |
else |
| 51 |
BUILDDIR := $(BASEDIR)/target/$(CARGO_BUILD_TARGET)/${PROFILE} |
| 52 |
# uudoc should not be cross build |
| 53 |
BUILDDIR_UUDOC := $(BASEDIR)/target/$(PROFILE) |
| 54 |
endif |
| 55 |
PKG_BUILDDIR := $(BUILDDIR)/deps |
| 56 |
DOCSDIR := $(BASEDIR)/docs |
| 57 |
|
| 58 |
BUSYBOX_ROOT := $(BASEDIR)/tmp |
| 59 |
BUSYBOX_VER := 1.36.1 |
| 60 |
BUSYBOX_SRC := $(BUSYBOX_ROOT)/busybox-$(BUSYBOX_VER) |
| 61 |
|
| 62 |
TOYBOX_ROOT := $(BASEDIR)/tmp |
| 63 |
TOYBOX_VER := 0.8.12 |
| 64 |
TOYBOX_SRC := $(TOYBOX_ROOT)/toybox-$(TOYBOX_VER) |
| 65 |
|
| 66 |
# Detect the target system |
| 67 |
# See https://doc.rust-lang.org/beta/rustc/platform-support.html |
| 68 |
# todo: support building wasm |
| 69 |
OS := $(or $(CARGO_BUILD_TARGET),$(shell rustc --print host-tuple)) |
| 70 |
|
| 71 |
# hardlinks are better default since |
| 72 |
# - Windows(cygwin) does not allow symlink by default |
| 73 |
# - std::env:current_exe resolves symlink |
| 74 |
LN ?= ln -f |
| 75 |
|
| 76 |
SELINUX_PROGS := \ |
| 77 |
chcon \ |
| 78 |
runcon |
| 79 |
|
| 80 |
$(info Detected OS = $(OS)) |
| 81 |
|
| 82 |
ifeq (,$(findstring windows,$(OS))) |
| 83 |
FEATURE_EXTRACT_UTILS := feat_os_unix |
| 84 |
else |
| 85 |
FEATURE_EXTRACT_UTILS := windows |
| 86 |
endif |
| 87 |
PROGS := $(shell cargo tree --depth 1 --features $(FEATURE_EXTRACT_UTILS) --format "{p}" --prefix none | sed -E -n 's/^uu_([^ ]+).*/\1/p') |
| 88 |
|
| 89 |
ifeq ($(SELINUX_ENABLED),1) |
| 90 |
PROGS += $(SELINUX_PROGS) |
| 91 |
endif |
| 92 |
|
| 93 |
UTILS ?= $(filter-out $(SKIP_UTILS),$(PROGS)) |
| 94 |
|
| 95 |
ifneq ($(findstring stdbuf,$(UTILS)),) |
| 96 |
# Use external libstdbuf per default. It is more robust than embedding libstdbuf. |
| 97 |
CARGOFLAGS += --features feat_external_libstdbuf |
| 98 |
endif |
| 99 |
|
| 100 |
# Programs with usable tests |
| 101 |
|
| 102 |
TESTS := \ |
| 103 |
$(sort $(filter $(UTILS),$(PROGS) $(SELINUX_PROGS))) |
| 104 |
|
| 105 |
TEST_NO_FAIL_FAST := |
| 106 |
TEST_SPEC_FEATURE := |
| 107 |
ifneq ($(SPEC),) |
| 108 |
TEST_NO_FAIL_FAST :=--no-fail-fast |
| 109 |
TEST_SPEC_FEATURE := test_unimplemented |
| 110 |
else ifeq ($(SELINUX_ENABLED),1) |
| 111 |
TEST_NO_FAIL_FAST := |
| 112 |
TEST_SPEC_FEATURE := selinux |
| 113 |
BUILD_SPEC_FEATURE := selinux |
| 114 |
endif |
| 115 |
|
| 116 |
define TEST_BUSYBOX |
| 117 |
test_busybox_$(1): |
| 118 |
-(cd $(BUSYBOX_SRC)/testsuite && bindir=$(BUILDDIR) ./runtest $(RUNTEST_ARGS) $(1)) |
| 119 |
endef |
| 120 |
|
| 121 |
# Output names |
| 122 |
EXES := \ |
| 123 |
$(sort $(UTILS)) |
| 124 |
|
| 125 |
INSTALLEES := ${EXES} |
| 126 |
ifeq (${MULTICALL}, y) |
| 127 |
INSTALLEES := ${INSTALLEES} coreutils |
| 128 |
endif |
| 129 |
|
| 130 |
all: build |
| 131 |
|
| 132 |
build-pkgs: |
| 133 |
ifneq (${MULTICALL}, y) |
| 134 |
ifdef BUILD_SPEC_FEATURE |
| 135 |
${CARGO} build ${CARGOFLAGS} --features "$(BUILD_SPEC_FEATURE)" ${PROFILE_CMD} $(foreach pkg,$(EXES),-p uu_$(pkg)) |
| 136 |
else |
| 137 |
${CARGO} build ${CARGOFLAGS} ${PROFILE_CMD} $(foreach pkg,$(EXES),-p uu_$(pkg)) |
| 138 |
endif |
| 139 |
endif |
| 140 |
|
| 141 |
build-coreutils: |
| 142 |
${CARGO} build ${CARGOFLAGS} --features "${EXES} $(BUILD_SPEC_FEATURE)" ${PROFILE_CMD} --no-default-features |
| 143 |
|
| 144 |
ifeq (${MULTICALL}, y) |
| 145 |
build: build-coreutils locales |
| 146 |
else |
| 147 |
build: build-pkgs locales |
| 148 |
endif |
| 149 |
|
| 150 |
$(foreach test,$(UTILS),$(eval $(call TEST_BUSYBOX,$(test)))) |
| 151 |
|
| 152 |
test: |
| 153 |
${CARGO} test ${CARGOFLAGS} --features "$(TESTS) $(TEST_SPEC_FEATURE)" $(PROFILE_CMD) --no-default-features $(TEST_NO_FAIL_FAST) |
| 154 |
|
| 155 |
nextest: |
| 156 |
${CARGO} nextest run ${CARGOFLAGS} --features "$(TESTS) $(TEST_SPEC_FEATURE)" $(PROFILE_CMD) --no-default-features $(TEST_NO_FAIL_FAST) |
| 157 |
|
| 158 |
test_toybox: |
| 159 |
-(cd $(TOYBOX_SRC)/ && CFLAGS="-pipe -O0 -s" make tests) |
| 160 |
|
| 161 |
toybox-src: |
| 162 |
if [ ! -e "$(TOYBOX_SRC)" ] ; then \ |
| 163 |
$(INSTALL) -d "$(TOYBOX_ROOT)" ; \ |
| 164 |
curl -Ls "https://github.com/landley/toybox/archive/refs/tags/$(TOYBOX_VER).tar.gz" -o "$(TOYBOX_ROOT)/$(TOYBOX_VER).tar.gz" ; \ |
| 165 |
tar -C "$(TOYBOX_ROOT)" -xf "$(TOYBOX_ROOT)/$(TOYBOX_VER).tar.gz" ; \ |
| 166 |
sed -i -e "s|TESTDIR=\".*\"|TESTDIR=\"$(BUILDDIR)\"|g" $(TOYBOX_SRC)/scripts/test.sh; \ |
| 167 |
sed -i -e "s/ || exit 1//g" $(TOYBOX_SRC)/scripts/test.sh; \ |
| 168 |
fi ; |
| 169 |
|
| 170 |
busybox-src: |
| 171 |
if [ ! -e "$(BUSYBOX_SRC)" ] ; then \ |
| 172 |
$(INSTALL) -d "$(BUSYBOX_ROOT)" ; \ |
| 173 |
curl -Ls "https://github.com/mirror/busybox/archive/refs/tags/$(subst .,_,$(BUSYBOX_VER)).tar.gz" -o "$(BUSYBOX_ROOT)/busybox-$(BUSYBOX_VER).tar.gz" ; \ |
| 174 |
tar -C "$(BUSYBOX_ROOT)" -xf "$(BUSYBOX_ROOT)/busybox-$(BUSYBOX_VER).tar.gz" ; \ |
| 175 |
fi ; |
| 176 |
|
| 177 |
# This is a busybox-specific config file their test suite wants to parse. |
| 178 |
$(BUILDDIR)/.config: $(BASEDIR)/.busybox-config |
| 179 |
$(INSTALL) -m 644 $< $@ |
| 180 |
|
| 181 |
# Test under the busybox test suite |
| 182 |
$(BUILDDIR)/busybox: busybox-src build-coreutils $(BUILDDIR)/.config |
| 183 |
$(INSTALL) -m 755 "$(BUILDDIR)/coreutils" "$(BUILDDIR)/busybox" |
| 184 |
|
| 185 |
prepare-busytest: $(BUILDDIR)/busybox |
| 186 |
# disable inapplicable tests |
| 187 |
-( cd "$(BUSYBOX_SRC)/testsuite" ; if [ -e "busybox.tests" ] ; then mv busybox.tests busybox.tests- ; fi ; ) |
| 188 |
|
| 189 |
ifeq ($(EXES),) |
| 190 |
busytest: |
| 191 |
else |
| 192 |
busytest: $(BUILDDIR)/busybox $(addprefix test_busybox_,$(filter-out $(SKIP_UTILS),$(EXES))) |
| 193 |
endif |
| 194 |
|
| 195 |
clean: |
| 196 |
cargo clean |
| 197 |
cd $(DOCSDIR) && $(MAKE) clean |
| 198 |
|
| 199 |
distclean: clean |
| 200 |
$(CARGO) clean $(CARGOFLAGS) && $(CARGO) update $(CARGOFLAGS) |
| 201 |
|
| 202 |
ifeq ($(MANPAGES),y) |
| 203 |
# Do not cross-build uudoc |
| 204 |
build-uudoc: |
| 205 |
@unset CARGO_BUILD_TARGET && ${CARGO} build ${CARGOFLAGS} --bin uudoc --features "uudoc ${EXES}" ${PROFILE_CMD} --no-default-features |
| 206 |
|
| 207 |
install-manpages: build-uudoc |
| 208 |
$(INSTALL) -d $(DESTDIR)$(DATAROOTDIR)/man/man1 |
| 209 |
$(foreach prog, $(INSTALLEES), \ |
| 210 |
$(BUILDDIR_UUDOC)/uudoc manpage $(prog) > $(DESTDIR)$(DATAROOTDIR)/man/man1/$(PROG_PREFIX)$(prog).1 $(newline) \ |
| 211 |
) |
| 212 |
else |
| 213 |
install-manpages: |
| 214 |
endif |
| 215 |
|
| 216 |
ifeq ($(COMPLETIONS),y) |
| 217 |
|
| 218 |
install-completions: build-uudoc |
| 219 |
$(INSTALL) -d $(DESTDIR)$(DATAROOTDIR)/zsh/site-functions |
| 220 |
$(INSTALL) -d $(DESTDIR)$(DATAROOTDIR)/bash-completion/completions |
| 221 |
$(INSTALL) -d $(DESTDIR)$(DATAROOTDIR)/fish/vendor_completions.d |
| 222 |
$(foreach prog, $(INSTALLEES), \ |
| 223 |
$(BUILDDIR_UUDOC)/uudoc completion $(prog) zsh > $(DESTDIR)$(DATAROOTDIR)/zsh/site-functions/_$(PROG_PREFIX)$(prog) $(newline) \ |
| 224 |
$(BUILDDIR_UUDOC)/uudoc completion $(prog) bash > $(DESTDIR)$(DATAROOTDIR)/bash-completion/completions/$(PROG_PREFIX)$(prog).bash $(newline) \ |
| 225 |
$(BUILDDIR_UUDOC)/uudoc completion $(prog) fish > $(DESTDIR)$(DATAROOTDIR)/fish/vendor_completions.d/$(PROG_PREFIX)$(prog).fish $(newline) \ |
| 226 |
) |
| 227 |
else |
| 228 |
install-completions: |
| 229 |
endif |
| 230 |
|
| 231 |
ifeq ($(LOCALES),y) |
| 232 |
locales: |
| 233 |
@# Copy uucore common locales |
| 234 |
@if [ -d "$(BASEDIR)/src/uucore/locales" ]; then \ |
| 235 |
$(INSTALL) -d "$(BUILDDIR)/locales/uucore"; \ |
| 236 |
for locale_file in "$(BASEDIR)"/src/uucore/locales/*.ftl; do \ |
| 237 |
$(INSTALL) -m 644 "$$locale_file" "$(BUILDDIR)/locales/uucore/"; \ |
| 238 |
done; \ |
| 239 |
fi; \ |
| 240 |
# Copy utility-specific locales |
| 241 |
@for prog in $(INSTALLEES); do \ |
| 242 |
if [ -d "$(BASEDIR)/src/uu/$$prog/locales" ]; then \ |
| 243 |
$(INSTALL) -d "$(BUILDDIR)/locales/$$prog"; \ |
| 244 |
for locale_file in "$(BASEDIR)"/src/uu/$$prog/locales/*.ftl; do \ |
| 245 |
if [ "$$(basename "$$locale_file")" != "en-US.ftl" ]; then \ |
| 246 |
$(INSTALL) -m 644 "$$locale_file" "$(BUILDDIR)/locales/$$prog/"; \ |
| 247 |
fi; \ |
| 248 |
done; \ |
| 249 |
fi; \ |
| 250 |
done |
| 251 |
|
| 252 |
# Some utils require extra locale files outside of their package: |
| 253 |
# - *sum binaries need the files from checksum_common |
| 254 |
INSTALLEES_WITH_EXTRA_LOCALE = \ |
| 255 |
$(INSTALLEES) \ |
| 256 |
$(if $(findstring sum, $(INSTALLEES)),checksum_common, ) |
| 257 |
install-locales: |
| 258 |
@for prog in $(INSTALLEES_WITH_EXTRA_LOCALE); do \ |
| 259 |
if [ -d "$(BASEDIR)/l10n/src/uu/$$prog/locales" ]; then \ |
| 260 |
$(INSTALL) -d "$(DESTDIR)$(DATAROOTDIR)/locales/$$prog"; \ |
| 261 |
for locale_file in "$(BASEDIR)"/l10n/src/uu/$$prog/locales/*.ftl; do \ |
| 262 |
if [ "$$(basename "$$locale_file")" != "en-US.ftl" ]; then \ |
| 263 |
$(INSTALL) -m 644 "$$locale_file" "$(DESTDIR)$(DATAROOTDIR)/locales/$$prog/"; \ |
| 264 |
fi; \ |
| 265 |
done; \ |
| 266 |
fi; \ |
| 267 |
done |
| 268 |
if [ -d "$(BASEDIR)/l10n/src/uucore/locales" ]; then \ |
| 269 |
mkdir -p "$(DESTDIR)$(DATAROOTDIR)/locales/uucore"; \ |
| 270 |
$(INSTALL) -m 644 "$(BASEDIR)"/l10n/src/uucore/locales/* "$(DESTDIR)$(DATAROOTDIR)/locales/uucore/"; \ |
| 271 |
fi; |
| 272 |
else |
| 273 |
locales: |
| 274 |
install-locales: |
| 275 |
endif |
| 276 |
|
| 277 |
install: build install-manpages install-completions install-locales |
| 278 |
$(INSTALL) -d $(INSTALLDIR_BIN) |
| 279 |
ifneq (,$(and $(findstring stdbuf,$(UTILS)),$(findstring feat_external_libstdbuf,$(CARGOFLAGS)))) |
| 280 |
ifneq (,$(findstring cygwin,$(OS))) |
| 281 |
$(INSTALL) -d $(DESTDIR)$(LIBSTDBUF_DIR) |
| 282 |
$(INSTALL) -m 755 $(BUILDDIR)/deps/stdbuf.dll $(DESTDIR)$(LIBSTDBUF_DIR)/libstdbuf.dll |
| 283 |
else ifneq (,$(findstring windows,$(OS))) |
| 284 |
@echo "NOTE: Windows stdbuf depends on Cygwin's libstdbuf.dll" |
| 285 |
else |
| 286 |
$(INSTALL) -d $(DESTDIR)$(LIBSTDBUF_DIR) |
| 287 |
$(INSTALL) -m 755 $(BUILDDIR)/deps/libstdbuf.* $(DESTDIR)$(LIBSTDBUF_DIR)/ |
| 288 |
endif |
| 289 |
endif |
| 290 |
ifeq (${MULTICALL}, y) |
| 291 |
$(INSTALL) -m 755 $(BUILDDIR)/coreutils $(INSTALLDIR_BIN)/$(PROG_PREFIX)coreutils |
| 292 |
$(foreach prog, $(filter-out coreutils, $(INSTALLEES)), \ |
| 293 |
cd $(INSTALLDIR_BIN) && $(LN) $(PROG_PREFIX)coreutils $(PROG_PREFIX)$(prog) $(newline) \ |
| 294 |
) |
| 295 |
$(if $(findstring test,$(INSTALLEES)), cd $(INSTALLDIR_BIN) && $(LN) $(PROG_PREFIX)coreutils $(PROG_PREFIX)[) |
| 296 |
else |
| 297 |
$(foreach prog, $(INSTALLEES), \ |
| 298 |
$(INSTALL) -m 755 $(BUILDDIR)/$(prog) $(INSTALLDIR_BIN)/$(PROG_PREFIX)$(prog) $(newline) \ |
| 299 |
) |
| 300 |
$(if $(findstring test,$(INSTALLEES)), $(INSTALL) -m 755 $(BUILDDIR)/test $(INSTALLDIR_BIN)/$(PROG_PREFIX)[) |
| 301 |
endif |
| 302 |
|
| 303 |
uninstall: |
| 304 |
ifeq (,$(findstring windows,$(OS))) |
| 305 |
rm -f $(DESTDIR)$(LIBSTDBUF_DIR)/libstdbuf.* |
| 306 |
-rm -d $(DESTDIR)$(LIBSTDBUF_DIR) 2>/dev/null || true |
| 307 |
endif |
| 308 |
ifeq (${MULTICALL}, y) |
| 309 |
rm -f $(addprefix $(INSTALLDIR_BIN)/,$(PROG_PREFIX)coreutils) |
| 310 |
endif |
| 311 |
rm -f $(addprefix $(INSTALLDIR_BIN)/$(PROG_PREFIX),$(PROGS)) |
| 312 |
rm -f $(INSTALLDIR_BIN)/$(PROG_PREFIX)[ |
| 313 |
rm -f $(addprefix $(DESTDIR)$(DATAROOTDIR)/zsh/site-functions/_$(PROG_PREFIX),$(PROGS)) |
| 314 |
rm -f $(addprefix $(DESTDIR)$(DATAROOTDIR)/bash-completion/completions/$(PROG_PREFIX),$(PROGS).bash) |
| 315 |
rm -f $(addprefix $(DESTDIR)$(DATAROOTDIR)/fish/vendor_completions.d/$(PROG_PREFIX),$(addsuffix .fish,$(PROGS))) |
| 316 |
rm -f $(addprefix $(DESTDIR)$(DATAROOTDIR)/man/man1/$(PROG_PREFIX),$(addsuffix .1,$(PROGS))) |
| 317 |
|
| 318 |
.PHONY: all build build-coreutils build-pkgs build-uudoc test distclean clean busytest install uninstall |