makefile
authorNiki Roo <niki@nikiroo.be>
Sun, 30 Jun 2024 11:37:43 +0000 (13:37 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 30 Jun 2024 11:37:43 +0000 (13:37 +0200)
Makefile
src/cutils
src/nsub/makefile.d

index c542c0778e5e51494ec96d9bc3e57b480ede5933..6f6ce65c9768d1d9744a4e709d15596f73c6b84f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,6 +8,7 @@ NAME   = nsub
 NAMES  = $(NAME) cutils
 TESTS  = 
 
+# You may override these when calling make
 PREFIX = /usr/local
 dstdir = bin
 
@@ -24,7 +25,8 @@ build: mess-build $(NAMES)
 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) 
 
@@ -61,7 +63,7 @@ uninstall: mess-uninstall $(NAMES) man
 # Messages
 mess-build:
        @echo
-       @echo ">>>>>>>>>> Building $(NAMES)..."
+       @echo ">>>>>>>>>> Building $(NAMES) in $(dstdir)..."
 mess-run:
        @echo
        @echo ">>>>>>>>>> Running $(NAME)..."
@@ -79,7 +81,7 @@ mess-man:
        @echo ">>>>>>>>>> Manual of $(NAME): $(MAKECMDGOALS)..."
 mess-test:
        @echo
-       @echo ">>>>>>>>>> Building all tests: $(TESTS)..."
+       @echo ">>>>>>>>>> Building all tests in $(dstdir): $(TESTS)..."
 mess-run-test:
        @echo
        @echo ">>>>>>>>>> Running tests: $(TESTS)..."
index 9e29e2f55d36a2e05a3420825b8ecc33006a45eb..780c8d0590a2f34e68ff896058b3b1a89539dcd8 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 9e29e2f55d36a2e05a3420825b8ecc33006a45eb
+Subproject commit 780c8d0590a2f34e68ff896058b3b1a89539dcd8
index 52181645279da6db20526e0fb0ae4bb462349ac2..5ea5c9534970d671037f4296527ce8a87fb373af 100644 (file)
@@ -1,12 +1,14 @@
 # Note: 99+ required for for-loop initial declaration (CentOS 6)
-# Note: gnu99 (instead of c99) required for libcutils-net
 
 NAME   = nsub
 NAMES  = $(NAME)
 srcdir = $(NAME)
-dstdir = ../bin
 
-CFLAGS   += -Wall -pedantic -I./ -std=gnu99
+ifeq ($(dstdir),)
+dstdir = $(srcdir)/bin
+endif
+
+CFLAGS   += -Wall -pedantic -I./ -std=c99
 CXXFLAGS += -Wall -pedantic -I./
 # LDFLAGS  += -lcheck
 PREFIX   =  /usr/local
@@ -17,10 +19,9 @@ CXXFLAGS += -ggdb -O0
 endif
 
 .PHONY: all build install uninstall clean mrpropre mrpropre \
-       $(NAMES)
+       $(NAMES) deps test run run-test run-test-more
 
 SOURCES=$(wildcard $(srcdir)/*.c)
-HEADERS=$(wildcard $(srcdir)/*.h)
 OBJECTS=$(SOURCES:%.c=%.o)
 
 # Main targets
@@ -29,40 +30,53 @@ all: build
 
 build: $(NAMES)
 
-nsub: $(dstdir)/nsub
+$(NAME): deps $(dstdir)/$(NAME)
 
+# Program, so no test
 run:
-       $(dstdir)/nsub --help
+       @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 #
 ################
-OBJECTS+=$(dstdir)/libcutils.o
-$(dstdir)/libcutils.o:
+## Libs (if needed)
+deps:
        $(MAKE) --no-print-directory -C cutils/ cutils
+DEPS=$(dstdir)/libcutils.o
+
+## Headers
+DEPENDS=$(SOURCES:%.c=%.d)
+-include $(DEPENDS)
+%.o: %.c
+       $(CC) $(CFLAGS) -MMD -MP -c $< -o $@
 ################
 
-$(dstdir)/nsub: $(OBJECTS)
+$(dstdir)/$(NAME): $(DEPS) $(OBJECTS)
        mkdir -p $(dstdir)
-       $(CC) $(OBJECTS) -o $@
+       # note: -r = --relocatable, but former also works with Clang
+       $(CC) $(CFLAGS) $(LDFLAGS) $(DEPS) $(OBJECTS) -o $@
 
 debug: 
        $(MAKE) -f $(srcdir)/makefile.d DEBUG=1
 
 clean:
-       rm -f $(srcdir)/*.o
+       rm -f $(OBJECTS)
+       rm -f $(DEPENDS)
+       rm -f $(DEPS)
 
 mrproper: mrpropre
-
 mrpropre: clean
-       rm -f $(dstdir)/nsub
+       rm -f $(dstdir)/$(NAME)
        rmdir $(dstdir) 2>/dev/null || true
 
-install: 
+install: build
        mkdir -p "$(PREFIX)/bin"
-       cp $(dstdir)/nsub "$(PREFIX)/bin/"
+       cp "$(dstdir)/$(NAME)" "$(PREFIX)/bin/"
 
-uninstall:
-       rm "$(PREFIX)/bin/nsub"
+uninstall: build
+       rm "$(PREFIX)/bin/$(NAME)"
        rmdir "$(PREFIX)/bin" 2>/dev/null