From d32c35244ae5484ff643d60a84577554e03ebdc6 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 20 May 2022 11:48:26 -0400 Subject: Makefile: Add a BUILDDIR variable for out-of-tree builds bfs can now be built from a read-only source tree. --- Makefile | 106 ++++++++++++++++++++++++++++++--------------------------- tests/tests.sh | 7 +++- 2 files changed, 61 insertions(+), 52 deletions(-) diff --git a/Makefile b/Makefile index e2d258e..3440575 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,14 @@ INSTALL ?= install MKDIR ?= mkdir -p RM ?= rm -f +export BUILDDIR ?= . +DESTDIR ?= +PREFIX ?= /usr +MANDIR ?= $(PREFIX)/share/man + +BIN := $(BUILDDIR)/bin +OBJ := $(BUILDDIR)/obj + DEFAULT_CFLAGS := \ -g \ -Wall \ @@ -48,10 +56,6 @@ CFLAGS ?= $(DEFAULT_CFLAGS) LDFLAGS ?= DEPFLAGS ?= -MD -MP -MF $(@:.o=.d) -DESTDIR ?= -PREFIX ?= /usr -MANDIR ?= $(PREFIX)/share/man - LOCAL_CPPFLAGS := \ -D__EXTENSIONS__ \ -D_ATFILE_SOURCE \ @@ -189,58 +193,58 @@ CHECKS := $(STRATEGY_CHECKS) check-trie check-xtimegm # Custom test flags for distcheck DISTCHECK_FLAGS := -s TEST_FLAGS="--sudo --verbose=skipped" -bfs: bin/bfs +bfs: $(BIN)/bfs .PHONY: bfs -all: bin/bfs bin/tests/mksock bin/tests/trie bin/tests/xtimegm +all: $(BIN)/bfs $(BIN)/tests/mksock $(BIN)/tests/trie $(BIN)/tests/xtimegm .PHONY: all -bin/bfs: \ - obj/src/bar.o \ - obj/src/bftw.o \ - obj/src/color.o \ - obj/src/ctx.o \ - obj/src/darray.o \ - obj/src/diag.o \ - obj/src/dir.o \ - obj/src/dstring.o \ - obj/src/eval.o \ - obj/src/exec.o \ - obj/src/fsade.o \ - obj/src/main.o \ - obj/src/mtab.o \ - obj/src/opt.o \ - obj/src/parse.o \ - obj/src/printf.o \ - obj/src/pwcache.o \ - obj/src/stat.o \ - obj/src/trie.o \ - obj/src/typo.o \ - obj/src/util.o \ - obj/src/xregex.o \ - obj/src/xspawn.o \ - obj/src/xtime.o - -bin/tests/mksock: obj/tests/mksock.o -bin/tests/trie: obj/src/trie.o obj/tests/trie.o -bin/tests/xtimegm: obj/src/xtime.o obj/tests/xtimegm.o - -bin/%: +$(BIN)/bfs: \ + $(OBJ)/src/bar.o \ + $(OBJ)/src/bftw.o \ + $(OBJ)/src/color.o \ + $(OBJ)/src/ctx.o \ + $(OBJ)/src/darray.o \ + $(OBJ)/src/diag.o \ + $(OBJ)/src/dir.o \ + $(OBJ)/src/dstring.o \ + $(OBJ)/src/eval.o \ + $(OBJ)/src/exec.o \ + $(OBJ)/src/fsade.o \ + $(OBJ)/src/main.o \ + $(OBJ)/src/mtab.o \ + $(OBJ)/src/opt.o \ + $(OBJ)/src/parse.o \ + $(OBJ)/src/printf.o \ + $(OBJ)/src/pwcache.o \ + $(OBJ)/src/stat.o \ + $(OBJ)/src/trie.o \ + $(OBJ)/src/typo.o \ + $(OBJ)/src/util.o \ + $(OBJ)/src/xregex.o \ + $(OBJ)/src/xspawn.o \ + $(OBJ)/src/xtime.o + +$(BIN)/tests/mksock: $(OBJ)/tests/mksock.o +$(BIN)/tests/trie: $(OBJ)/src/trie.o $(OBJ)/tests/trie.o +$(BIN)/tests/xtimegm: $(OBJ)/src/xtime.o $(OBJ)/tests/xtimegm.o + +$(BIN)/%: @$(MKDIR) $(@D) +$(CC) $(ALL_LDFLAGS) $^ $(ALL_LDLIBS) -o $@ -obj/%.o: %.c obj/FLAGS +$(OBJ)/%.o: %.c $(OBJ)/FLAGS @$(MKDIR) $(@D) $(CC) $(ALL_CFLAGS) -c $< -o $@ # Save the full set of flags to rebuild everything when they change -obj/FLAGS.new: +$(OBJ)/FLAGS.new: @$(MKDIR) $(@D) @echo $(CC) : $(ALL_CFLAGS) : $(ALL_LDFLAGS) : $(ALL_LDLIBS) >$@ -.PHONY: obj/FLAGS.new +.PHONY: $(OBJ)/FLAGS.new # Only update obj/FLAGS if obj/FLAGS.new is different -obj/FLAGS: obj/FLAGS.new +$(OBJ)/FLAGS: $(OBJ)/FLAGS.new @test -e $@ && cmp -s $@ $< && rm $< || mv $< $@ # Make sure that "make release" builds everything, but "make release obj/src/main.o" doesn't @@ -251,10 +255,10 @@ $(FLAG_GOALS): $(FLAG_PREREQS) check: $(CHECKS) .PHONY: check $(CHECKS) -$(STRATEGY_CHECKS): check-%: bin/bfs bin/tests/mksock - ./tests/tests.sh --bfs="./bin/bfs -S $*" $(TEST_FLAGS) +$(STRATEGY_CHECKS): check-%: $(BIN)/bfs $(BIN)/tests/mksock + ./tests/tests.sh --bfs="$(BIN)/bfs -S $*" $(TEST_FLAGS) -check-trie check-xtimegm: check-%: bin/tests/% +check-trie check-xtimegm: check-%: $(BIN)/tests/% $< distcheck: @@ -271,12 +275,12 @@ endif .PHONY: distcheck clean: - $(RM) -r bin obj + $(RM) -r $(BIN) $(OBJ) .PHONY: clean install: $(MKDIR) $(DESTDIR)$(PREFIX)/bin - $(INSTALL) -m755 bin/bfs $(DESTDIR)$(PREFIX)/bin/bfs + $(INSTALL) -m755 $(BIN)/bfs $(DESTDIR)$(PREFIX)/bin/bfs $(MKDIR) $(DESTDIR)$(MANDIR)/man1 $(INSTALL) -m644 docs/bfs.1 $(DESTDIR)$(MANDIR)/man1/bfs.1 $(MKDIR) $(DESTDIR)$(PREFIX)/share/bash-completion/completions @@ -293,12 +297,12 @@ uninstall: .PHONY: uninstall check-install: - +$(MAKE) install DESTDIR=pkg - +$(MAKE) uninstall DESTDIR=pkg - ./bin/bfs pkg -not -type d -print -exit 1 - $(RM) -r pkg + +$(MAKE) install DESTDIR=$(BUILDDIR)/pkg + +$(MAKE) uninstall DESTDIR=$(BUILDDIR)/pkg + $(BIN)/bfs $(BUILDDIR)/pkg -not -type d -print -exit 1 + $(RM) -r $(BUILDDIR)/pkg .PHONY: check-install .SUFFIXES: --include $(wildcard obj/*/*.d) +-include $(wildcard $(OBJ)/*/*.d) diff --git a/tests/tests.sh b/tests/tests.sh index 434e058..eaf5add 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -897,7 +897,12 @@ function _realpath() { } TESTS=$(_realpath "$(dirname -- "${BASH_SOURCE[0]}")") -BIN=$(_realpath "$TESTS/../bin") + +if [ "$BUILDDIR" ]; then + BIN=$(_realpath "$BUILDDIR/bin") +else + BIN=$(_realpath "$TESTS/../bin") +fi # Try to resolve the path to $BFS before we cd, while also supporting # --bfs="./bin/bfs -S ids" -- cgit v1.2.3