+#
+# Simple makefile that will forward all commands to src/xxx
+# > NAME: main program (for 'man' and 'run')
+# > NAMES: list of all the programs to compile
+# > TESTS: list of all test programs to compile and run
+#
NAME = cbook
NAMES = $(NAME) cutils
-TEST = tests-cbook
-TESTS = $(TEST)
+TESTS = tests-cbook
+# You may override these when calling make
PREFIX = /usr/local
-
dstdir = bin
-
-.PHONY: all run clean mrpropre mrpropre love debug doc man \
- test tests run-test run-tests run-test-more run-tests-more \
+.PHONY: all build run clean mrpropre mrpropre love debug doc man \
+ test run-test run-test-more \
mess-build mess-run mess-clean mess-propre mess-doc mess-man \
mess-test mess-run-test mess-run-test-more \
$(NAMES) $(TESTS)
test: mess-test $(TESTS)
# Main buildables
-M_OPTS=$(MAKECMDGOALS) --no-print-directory PREFIX=$(PREFIX) DEBUG=$(DEBUG)
+M_OPTS=$(MAKECMDGOALS) --no-print-directory \
+ PREFIX=$(PREFIX) DEBUG=$(DEBUG) dstdir=$(abspath $(dstdir))
$(NAMES) $(TESTS):
- $(MAKE) -C src/$@ $(M_OPTS)
+ $(MAKE) -C src/$@ $(M_OPTS)
# Manual
man: mess-man
- @$(MAKE) -f man.d $(M_OPTS) NAME=$(NAME)
+ @$(MAKE) -f man.d $(MAKECMDGOALS) NAME=$(NAME)
# Run
-run: mess-run
- $(dstdir)/$(NAME)
+run: mess-run $(NAME)
# Run main test
-run-test: mess-run-test $(TEST)
-run-test-more: mess-run-test-more $(TEST)
+run-test: mess-run-test $(TESTS)
+run-test-more: mess-run-test-more $(TESTS)
# Doc/man/misc
doc: mess-doc
love:
@echo " ...not war."
debug:
- $(MAKE) DEBUG=1
+ $(MAKE) $(MAKECMDGOALS) PREFIX=$(PREFIX) NAME=$(NAME) DEBUG=1
# Clean
-clean: mess-clean $(NAMES) $(TESTS)
+clean: mess-clean $(TESTS) $(NAMES)
mrproper: mrpropre
-mrpropre: mess-propre $(NAMES) $(TESTS) man
+mrpropre: mess-propre $(TESTS) $(NAMES) man
rm -rf doc/html doc/latex doc/man
rmdir doc 2>/dev/null || true
# Install/uninstall
-install: mess-install $(NAME) man
-uninstall: mess-uninstall $(NAME) man
+install: mess-install $(NAMES) man
+uninstall: mess-uninstall $(NAMES) man
# Messages
mess-build:
@echo
- @echo ">>>>>>>>>> Running $(NAME)..."
+ @echo ">>>>>>>>>> Building $(NAMES) in $(dstdir)..."
mess-run:
@echo
@echo ">>>>>>>>>> Running $(NAME)..."
mess-clean:
@echo
- @echo ">>>>>>>>>> Cleaning $(NAME)..."
+ @echo ">>>>>>>>>> Cleaning $(NAMES) $(TESTS)..."
mess-propre:
@echo
@echo ">>>>>>>>>> Calling Mr Propre..."
@echo ">>>>>>>>>> Generating documentation for $(NAME)..."
mess-man:
@echo
- @echo ">>>>>>>>>> Generating the manual of $(NAME)..."
+ @echo ">>>>>>>>>> Manual of $(NAME): $(MAKECMDGOALS)..."
mess-test:
@echo
- @echo ">>>>>>>>>> Building tests for $(NAME)..."
+ @echo ">>>>>>>>>> Building all tests in $(dstdir): $(TESTS)..."
mess-run-test:
@echo
- @echo ">>>>>>>>>> Running tests on $(NAME)..."
+ @echo ">>>>>>>>>> Running tests: $(TESTS)..."
mess-run-test-more:
@echo
- @echo ">>>>>>>>>> Running more tests on $(NAME)..."
+ @echo ">>>>>>>>>> Running more tests: $(TESTS)..."
mess-install:
@echo
@echo ">>>>>>>>>> Installing $(NAME) into $(PREFIX)..."
NAME = cbook
NAMES = $(NAME)
srcdir = $(NAME)
-dstdir = ../bin
+
+ifeq ($(dstdir),)
+dstdir = $(srcdir)/bin
+endif
CFLAGS += -Wall -pedantic -I./ -std=c99
CXXFLAGS += -Wall -pedantic -I./
endif
.PHONY: all build install uninstall clean mrpropre mrpropre \
- $(NAMES) deps run
+ $(NAMES) deps test run run-test run-test-more
SOURCES=$(wildcard $(srcdir)/*.c)
OBJECTS=$(SOURCES:%.c=%.o)
$(NAME): deps $(dstdir)/$(NAME)
+# Program, so no test
+run:
+ @echo
+ $(dstdir)/$(NAME) --help
+test run-test run-test-more:
+ @echo you are in the sources of the program, look at the tests instead
+
################
# Dependencies #
################
$(dstdir)/$(NAME): $(DEPS) $(OBJECTS)
mkdir -p $(dstdir)
+ # note: -r = --relocatable, but former also works with Clang
$(CC) $(CFLAGS) $(LDFLAGS) $(DEPS) $(OBJECTS) -o $@
-run:
- @echo
- $(dstdir)/$(NAME)
+debug:
+ $(MAKE) -f $(srcdir)/makefile.d DEBUG=1
clean:
- rm -f $(OBJECTS) $(DEPENDS)
+ rm -f $(OBJECTS)
+ rm -f $(DEPENDS)
+ rm -f $(DEPS)
mrproper: mrpropre
mrpropre: clean
rm -f $(dstdir)/$(NAME)
rmdir $(dstdir) 2>/dev/null || true
-install:
+install: build
mkdir -p "$(PREFIX)/bin"
- cp $(dstdir)/$(NAME) "$(PREFIX)/bin/"
+ cp "$(dstdir)/$(NAME)" "$(PREFIX)/bin/"
-uninstall:
+uninstall: build
rm "$(PREFIX)/bin/$(NAME)"
rmdir "$(PREFIX)/bin" 2>/dev/null
NAME = tests-cbook
NAMES = $(NAME)
srcdir = $(NAME)
-dstdir = ../bin
+
+ifeq ($(dstdir),)
+dstdir = $(srcdir)/bin
+endif
CFLAGS += -Wall -pedantic -I./ -std=c99
CXXFLAGS += -Wall -pedantic -I./
$(NAME): deps $(dstdir)/$(NAME)
+# Test program, so running = running tests
+run: run-test
+test: build
+run-test: test
+ @echo
+ $(dstdir)/$(NAME)
+run-test-more: test
+ @echo
+ $(dstdir)/$(NAME) --more
+
################
# Dependencies #
################
## Libs (if needed)
deps:
- $(MAKE) --no-print-directory -C cutils/ check
-DEPS=$(dstdir)/libcutils-check.o
+ $(MAKE) dstdir=$(dstdir) --no-print-directory -C cutils/ cutils
+ $(MAKE) dstdir=$(dstdir) --no-print-directory -C cutils/ check
+DEPS=$(dstdir)/libcutils.o $(dstdir)/libcutils-check.o
## Headers
DEPENDS=$(SOURCES:%.c=%.d)
mkdir -p $(dstdir)
$(CC) $(CFLAGS) $(LDFLAGS) $(DEPS) $(OBJECTS) -o $@
-# Test program, so running = running tests
-run: run-test
-test: build
-
-run-test: test
- @echo
- $(dstdir)/$(NAME)
-run-test-more: test
- @echo
- $(dstdir)/$(NAME) --more
-
clean:
- rm -f $(OBJECTS) $(DEPENDS)
+ rm -f $(OBJECTS)
+ rm -f $(DEPENDS)
+ rm -f $(DEPS)
mrproper: mrpropre
mrpropre: clean
rm -f $(dstdir)/$(NAME)
rmdir $(dstdir) 2>/dev/null || true
+install uninstall:
+ @echo Those are tests, nothing to install/uninstall
+