From: Niki Roo Date: Tue, 2 Jul 2024 14:58:08 +0000 (+0200) Subject: makefile X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=e16bf65fb712e3a8d077bc4f8bdabf7402b0aa7d;p=cutils.git makefile --- diff --git a/src/cutils b/src/cutils index 780c8d0..8cf0ab8 160000 --- a/src/cutils +++ b/src/cutils @@ -1 +1 @@ -Subproject commit 780c8d0590a2f34e68ff896058b3b1a89539dcd8 +Subproject commit 8cf0ab8f25623889760fef03414c476c1cc19613 diff --git a/src/tests-cutils/Makefile b/src/tests-cutils/Makefile old mode 100644 new mode 100755 index 46c73e5..4b2abfa --- a/src/tests-cutils/Makefile +++ b/src/tests-cutils/Makefile @@ -1,10 +1,10 @@ # Simply pass everything to makefile.d, but calling from "../" -.PHONY: all build install uninstall clean mrpropre mrpropre \ - run test run-test run-test-more default \ - tests-cutils +.PHONY: default $(MAKECMDGOALS) default $(MAKECMDGOALS): - @$(MAKE) --no-print-directory -C ../ -f $(CURDIR)/makefile.d \ - $(MAKECMDGOALS) + @for mk in makefile.d; do \ + $(MAKE) --no-print-directory -C ../ -f "$(CURDIR)/$$mk" \ + $(MAKECMDGOALS); \ + done; diff --git a/src/tests-cutils/makefile.d b/src/tests-cutils/makefile.d index 77d6cdc..3633bc1 100644 --- a/src/tests-cutils/makefile.d +++ b/src/tests-cutils/makefile.d @@ -1,34 +1,74 @@ -# Note: 99+ required for for-loop initial declaration (CentOS 6) +# +# Makefile for C test projets +# > NAME : the name of the main program (if programs, make a single .d file +# per program, link them up in Makfile and use a $ssrcdir) +# > srcdir : the source directory +# > ssrcdir: the sub-sources directory (defaults to $srcdir) +# > dstdir: the destination directory (defaults to $srcdir/bin) +# +# Environment variables: +# > PREFIX: where to (un)install (defaults to /usr/local) +# > DEBUG: define it to build with all debug symbols +# +NAME = tests-cutils +srcdir = $(NAME) +ssrcdir = $(srcdir) + +# Note: c99+ required for for-loop initial declaration (not default in CentOS 6) +# Note: gnu99 can be required for some projects (i.e.: libcutils-net) +CFLAGS += -Wall -pedantic -I./ -std=c99 +CXXFLAGS += -Wall -pedantic -I./ +PREFIX = /usr/local + +# Required libraries if any: +LDFLAGS += -lcheck -lcutils-check -NAME = tests-cutils -NAMES = $(NAME) -srcdir = $(NAME) +# Required *locally compiled* libraries if any: +LIBS = cutils + +################################################################################ ifeq ($(dstdir),) dstdir = $(srcdir)/bin endif -CFLAGS += -Wall -pedantic -I./ -std=c99 -CXXFLAGS += -Wall -pedantic -I./ -LDFLAGS += -lcheck -PREFIX = /usr/local - ifdef DEBUG CFLAGS += -ggdb -O0 CXXFLAGS += -ggdb -O0 endif -.PHONY: all build install uninstall clean mrpropre mrpropre \ - $(NAME) deps test run run-test run-test-more +# Default target +.PHONY: all deps +all: + +# locally compiled libs: +ifneq ($(LIBS),) +LDFLAGS += -L$(dstdir) +LDFLAGS += $(foreach lib,$(LIBS),-l$(lib)) +endif +deps: + $(foreach lib,$(LIBS),$(MAKE) --no-print-directory \ + -C $(lib)/ dstdir=$(dstdir)) + +.PHONY: build rebuild install uninstall clean mrpropre mrpropre \ + $(NAME) test run run-test run-test-more -SOURCES=$(wildcard $(srcdir)/*.c) +SOURCES=$(wildcard $(ssrcdir)/*.c) OBJECTS=$(SOURCES:%.c=%.o) +DEPENDS =$(SOURCES:%.c=%.d) -# Main target +# Autogenerate dependencies from code +-include $(DEPENDS) +%.o: %.c + $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ + +# Main targets all: build -build: $(NAMES) +build: $(NAME) + +rebuild: clean build $(NAME): deps $(dstdir)/$(NAME) @@ -42,36 +82,29 @@ run-test-more: test @echo $(dstdir)/$(NAME) --more -################ -# Dependencies # -################ -## Libs (if needed) -deps: - $(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) --include $(DEPENDS) -%.o: %.c - $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ -################ - -$(dstdir)/$(NAME): $(DEPS) $(OBJECTS) +$(dstdir)/$(NAME): $(OBJECTS) mkdir -p $(dstdir) - $(CC) $(CFLAGS) $(LDFLAGS) $(DEPS) $(OBJECTS) -o $@ + # note: LDFLAGS *must* be near the end + $(CC) $(CFLAGS) $(OBJECTS) -o $@ $(LDFLAGS) clean: - rm -f $(OBJECTS) + $(foreach lib,$(LIBS),$(MAKE) --no-print-directory \ + -C $(lib)/ $@ dstdir=$(dstdir)) + rm -f $(OBJECTS) rm -f $(DEPENDS) - rm -f $(DEPS) mrproper: mrpropre mrpropre: clean + $(foreach lib,$(LIBS),$(MAKE) --no-print-directory \ + -C $(lib)/ $@ dstdir=$(dstdir)) rm -f $(dstdir)/$(NAME) rmdir $(dstdir) 2>/dev/null || true -install uninstall: - @echo Those are tests, nothing to install/uninstall +install: build + mkdir -p "$(PREFIX)/bin" + cp "$(dstdir)/$(NAME)" "$(PREFIX)/bin/" + +uninstall: + rm "$(PREFIX)/bin/$(NAME)" + rmdir "$(PREFIX)/bin" 2>/dev/null