changeset 260:0ae8f6f2a27d draft

Move to a BSD style Makefile and supply mk stubs that work with gmake and pmake.
author Roy Marples <roy@marples.name>
date Mon, 07 Jan 2008 23:45:48 +0000
parents b5103bcaa6ad
children 6ebf880b635e
files Makefile mk/cc.mk mk/depend.mk mk/man.mk mk/os.mk mk/prog.mk
diffstat 6 files changed, 175 insertions(+), 143 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon Jan 07 20:52:49 2008 +0000
+++ b/Makefile	Mon Jan 07 23:45:48 2008 +0000
@@ -1,153 +1,35 @@
-# Should work for both GNU make and BSD make
-# NOTE: I really don't like autotools, but we do need to work a few things out
-# such as the need to link to libresolv and/or librt so please forgive the
-# embedded code :)
-
-NAME = dhcpcd
-VERSION = 3.1.8
-PKG = $(NAME)-$(VERSION)
-
-CFLAGS ?= -O2 -pipe
-
-INSTALL ?= install
-DESTDIR =
-MANPREFIX ?= /usr/share
-SBINDIR = $(DESTDIR)$(PREFIX)/sbin
-MANDIR = $(DESTDIR)$(MANPREFIX)/man
-INFODIR = /var/lib/dhcpcd
-
-SBIN_TARGETS = dhcpcd
-MAN_TARGETS = dhcpcd.8
-TARGET = $(SBIN_TARGETS) $(MAN_TARGETS)
+# Makefile based on BSD's pmake.
+# Our mk stubs also work with GNU make.
+# Copyright 2008 Roy Marples
 
-# Work out if we need -lresolv or not
-_LIBRESOLV_SH = printf '\#include <netinet/in.h>\n\#include <resolv.h>\nint main (void) { return (res_init ()); }\n' > .res_init.c; \
-	if $(CC) .res_init.c -o .res_init >/dev/null 2>&1; then \
-		echo ""; \
-	elif $(CC) .res_init.c -lresolv -o .res_init >/dev/null 2>&1; then \
-		echo "-lresolv"; \
-	else \
-		echo "Cannot work out how to get res_init to link" >&2; \
-		rm -f .res_init.c .res_init; \
-		exit 1; \
-	fi; \
-	rm -f .res_init.c .res_init
-_LIBRESOLV != $(_LIBRESOLV_SH)
-LIBRESOLV = $(_LIBRESOLV)$(shell $(_LIBRESOLV_SH))
+PROG=		dhcpcd
+SRCS=		arp.c client.c common.c configure.c dhcp.c dhcpcd.c duid.c \
+		info.c interface.c ipv4ll.c logger.c signal.c socket.c
+MAN=		dhcpcd.8
 
-# Work out if we need -lrt or not
-_LIBRT_SH = printf '\#include <time.h>\n\#include <unistd.h>\n\nint main (void) { struct timespec ts;\n\#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)\nreturn (clock_gettime (CLOCK_MONOTONIC, &ts));\n\#else\nreturn -1;\n\#endif\n}\n' > .clock_gettime.c; \
-	if $(CC) .clock_gettime.c -o .clock_gettime >/dev/null 2>&1; then \
-		echo ""; \
-	elif $(CC) .clock_gettime.c -lrt -o .clock_gettime >/dev/null 2>&1; then \
-		echo "-lrt"; \
-	else \
-		echo ""; \
-	fi; \
-	rm -f .clock_gettime.c .clock_gettime
-_LIBRT != $(_LIBRT_SH)
-LIBRT = $(_LIBRT)$(shell $(_LIBRT_SH))
+VERSION=	3.1.8
+CLEANFILES=	version.h dhcpcd.8
+
+BINDIR=		/sbin
 
-# Work out if our fork() works or not
-_HAVE_FORK_SH = if [ "$(HAVE_FORK)" = "yes" ]; then \
-		echo ""; \
-	elif [ -n "$(HAVE_FORK)" ]; then \
-		echo "-DTHERE_IS_NO_FORK"; \
-	else \
-		printf '\#include <stdlib.h>\n\#include <unistd.h>\nint main (void) { pid_t pid = fork(); if (pid == -1) exit (-1); exit (0); }\n' > .fork.c; \
-		$(CC) .fork.c -o .fork >/dev/null 2>&1; \
-		if ./.fork; then \
-			echo ""; \
-		else \
-			echo "-DTHERE_IS_NO_FORK"; \
-		fi; \
-		rm -f .fork.c .fork; \
-	fi;
-_HAVE_FORK != $(_HAVE_FORK_SH)
-FORK = $(_HAVE_FORK)$(shell $(_HAVE_FORK_SH))
+# Needed for crappy Linux headers :/
+CSTD=		gnu99
 
-# Work out how to restart services 
-_RC_SH = if [ -n "$(HAVE_INIT)" ]; then \
-		 [ "$(HAVE_INIT)" = "no" ] || echo "-DENABLE_$(HAVE_INIT)"; \
-		 elif [ -x /sbin/runscript ]; then echo "-DENABLE_OPENRC"; \
-		 elif [ -x /sbin/service ]; then echo "-DENABLE_SERVICE"; \
-		 elif [ -x /etc/rc.d/rc.S -a -x /etc/rc.d/rc.M ]; then echo "-DENABLE_SLACKRC"; \
-		 elif [ -d /etc/rc.d ]; then echo "-DENABLE_BSDRC"; \
-		 elif [ -d /etc/init.d ]; then echo "-DENABLE_SYSV"; \
-		 fi
-_RC != $(_RC_SH)
-RC = $(_RC)$(shell $(_RC_SH))
+MK=		mk
+include ${MK}/os.mk
+include ${MK}/cc.mk
+include ${MK}/prog.mk
 
-# pmake check for extra cflags 
-WEXTRA != for x in -Wdeclaration-after-statement -Wsequence-point -Wextra; do \
-	if $(CC) -Wdeclaration-after-statement -S -o /dev/null -xc /dev/null >/dev/null 2>&1; \
-	then echo -n "$$x "; fi \
-	done
-
-# gmake function for similar, but called below
-check_gcc=$(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null >/dev/null 2>&1; \
-		  then echo "$(1)"; else echo "$(2)"; fi)
+# os.mk should define this, but heh
+INFOD?=		/var/db
 
-# Loads of nice flags to ensure our code is good
-# IMPORTANT: We should be using c99 instead of gnu99 but for some reason
-# generic linux headers as of 2.6.19 don't allow this in asm/types.h
-CFLAGS += -pedantic -std=gnu99 \
-		  -Wall -Wunused -Wimplicit -Wshadow -Wformat=2 \
-		  -Wmissing-declarations -Wno-missing-prototypes -Wwrite-strings \
-		  -Wbad-function-cast -Wnested-externs -Wcomment -Winline \
-		  -Wchar-subscripts -Wcast-align -Wno-format-nonliteral \
-		  $(call check_gcc, -Wdeclaration-after-statement) \
-		  $(call check_gcc, -Wsequence-point) \
-		  $(call check_gcc, -Wextra) $(WEXTRA)
+LDADD+=		${LIBRESOLV} ${LIBRT}
+CFLAGS+=	-DINFORDIR=\"${INFOD}\" ${FORK} ${RC}
 
-# -Werrror is a good flag to use for development, but some platforms may
-#  have buggy headers from time to time, so you may need to comment this out
-#CFLAGS += -Werror
-
-all: $(TARGET)
-
-dhcpcd_H = version.h
-dhcpcd_OBJS = arp.o client.o common.o configure.o dhcp.o dhcpcd.o duid.o \
-			  info.o interface.o ipv4ll.o logger.o signal.o socket.o
-
-$(dhcpcd_OBJS): 
-	$(CC) $(FORK) $(RC) -DINFODIR=\"$(INFODIR)\" $(CFLAGS) -c $*.c
-
-dhcpcd: $(dhcpcd_H) .depend $(dhcpcd_OBJS)
-	$(CC) $(LDFLAGS) $(dhcpcd_OBJS) $(LIBRESOLV) $(LIBRT) -o dhcpcd
-
+# As version.h is generated by us, hardcode the depend correctly.
+${SRCS}:	version.h
 version.h:
-	echo '#define VERSION "$(VERSION)"' > version.h
+	echo "#define VERSION \"${VERSION}\""> version.h
 
 dhcpcd.8: dhcpcd.8.in
-	sed 's:%%INFODIR%%:$(INFODIR):g' dhcpcd.8.in > dhcpcd.8
-
-man: $(MAN_TARGETS) 
-
-.PHONY: clean install dist
-
-# We always need to have a .depend file as not all make implentations can work
-# with each others way of optionally including a file
-clean:
-	rm -f $(TARGET) $(dhcpcd_H) *.o *~ *.core *.bz2 .depend
-
-install: $(TARGET)
-	$(INSTALL) -m 0755 -d $(SBINDIR)
-	$(INSTALL) -m 0755 $(SBIN_TARGETS) $(SBINDIR)
-	$(INSTALL) -m 0755 -d $(MANDIR)/man8
-	$(INSTALL) -m 0644 $(MAN_TARGETS) $(MANDIR)/man8
-
-dist:
-	$(INSTALL) -d /tmp/$(PKG)
-	cp -RPp . /tmp/$(PKG)
-	(cd /tmp/$(PKG); $(MAKE) clean)
-	rm -rf /tmp/$(PKG)/*.bz2 /tmp/$(PKG)/.git /tmp/$(PKG)/test
-	tar cvjpf $(PKG).tar.bz2 -C /tmp $(PKG) 
-	rm -rf /tmp/$(PKG) 
-	ls -l $(PKG).tar.bz2
-
-# Sucky, but I cannot find a way of optional including the .depend file
-# that works for all make implementations :/
-_DEPS != ls *.c *.h
-.depend: $(dhcpcd_H) $(_DEPS)$(wildcard *.c *.h)
-	$(CC) $(CPPFLAGS) -MM *.c > .depend
+	sed 's:%%INFODIR%%:${INFOD}:g' dhcpcd.8.in > dhcpcd.8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mk/cc.mk	Mon Jan 07 23:45:48 2008 +0000
@@ -0,0 +1,27 @@
+# Copyright 2008 Roy Marples
+
+# Setup some good default CFLAGS
+
+CFLAGS?=	-O2 -pipe
+CSTD?=		c99
+
+# GNU Make way of detecting gcc flags we can use
+check_gcc=$(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null >/dev/null 2>&1; \
+	then echo "$(1)"; else echo "$(2)"; fi)
+
+# pmake check for extra cflags 
+WEXTRA!= for x in -Wdeclaration-after-statement -Wsequence-point -Wextra; do \
+	if $(CC) $$x -S -o /dev/null -xc /dev/null >/dev/null 2>&1; \
+	then echo -n "$$x "; fi \
+	done
+
+# Loads of nice flags to ensure our code is good
+CFLAGS+=	-pedantic -std=${CSTD} \
+		-Wall -Wunused -Wimplicit -Wshadow -Wformat=2 \
+		-Wmissing-declarations -Wno-missing-prototypes -Wwrite-strings \
+		-Wbad-function-cast -Wnested-externs -Wcomment -Winline \
+		-Wchar-subscripts -Wcast-align -Wno-format-nonliteral \
+		$(call check_gcc, -Wdeclaration-after-statement) \
+		$(call check_gcc, -Wsequence-point) \
+		$(call check_gcc, -Wextra) $(WEXTRA)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mk/depend.mk	Mon Jan 07 23:45:48 2008 +0000
@@ -0,0 +1,12 @@
+# This only works for make implementations that always include a .depend if
+# it exists. Only GNU make does not do this.
+
+# Copyright 2008 Roy Marples
+
+.depend: ${SCRIPTS} ${SRCS}
+	$(CC) $(CFLAGS) -MM ${SRCS} > .depend
+
+depend: .depend
+
+_dependclean:
+	rm -f .depend
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mk/man.mk	Mon Jan 07 23:45:48 2008 +0000
@@ -0,0 +1,11 @@
+# rules to install manpages
+# Copyright 2008 Roy Marples
+
+MANPREFIX?=	/usr/share
+MANMODE?=	0444
+MINSTALL?=	${INSTALL} -m ${MANMODE}
+
+# We cheat as all our pages go into section 8
+maninstall: ${MAN}
+	${INSTALL} -d ${DESTDIR}${MANPREFIX}/man/man8
+	for man in ${MAN}; do ${MINSTALL} $$man ${DESTDIR}${MANPREFIX}/man/man8; done
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mk/os.mk	Mon Jan 07 23:45:48 2008 +0000
@@ -0,0 +1,72 @@
+# Setup OS specific variables
+# Copyright 2008 Roy Marples
+
+# Work out if we need -lresolv or not
+_LIBRESOLV_SH= printf '\#include <netinet/in.h>\n\#include <resolv.h>\nint main (void) { return (res_init ()); }\n' > .res_init.c; \
+	if $(CC) .res_init.c -o .res_init >/dev/null 2>&1; then \
+		echo ""; \
+	elif $(CC) .res_init.c -lresolv -o .res_init >/dev/null 2>&1; then \
+		echo "-lresolv"; \
+	else \
+		echo "Cannot work out how to get res_init to link" >&2; \
+		rm -f .res_init.c .res_init; \
+		exit 1; \
+	fi; \
+	rm -f .res_init.c .res_init
+_LIBRESOLV!= $(_LIBRESOLV_SH)
+LIBRESOLV= $(_LIBRESOLV)$(shell $(_LIBRESOLV_SH))
+
+# Work out if we need -lrt or not
+_LIBRT_SH= printf '\#include <time.h>\n\#include <unistd.h>\n\nint main (void) { struct timespec ts;\n\#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)\nreturn (clock_gettime (CLOCK_MONOTONIC, &ts));\n\#else\nreturn -1;\n\#endif\n}\n' > .clock_gettime.c; \
+	if $(CC) .clock_gettime.c -o .clock_gettime >/dev/null 2>&1; then \
+		echo ""; \
+	elif $(CC) .clock_gettime.c -lrt -o .clock_gettime >/dev/null 2>&1; then \
+		echo "-lrt"; \
+	else \
+		echo ""; \
+	fi; \
+	rm -f .clock_gettime.c .clock_gettime
+_LIBRT!= $(_LIBRT_SH)
+LIBRT= $(_LIBRT)$(shell $(_LIBRT_SH))
+
+# Work out if our fork() works or not
+_HAVE_FORK_SH= if test "$(HAVE_FORK)" = "yes"; then \
+		echo ""; \
+	elif test -n "$(HAVE_FORK)"; then \
+		echo "-DTHERE_IS_NO_FORK"; \
+	else \
+		printf '\#include <stdlib.h>\n\#include <unistd.h>\nint main (void) { pid_t pid = fork(); if (pid == -1) exit (-1); exit (0); }\n' > .fork.c; \
+		$(CC) .fork.c -o .fork >/dev/null 2>&1; \
+		if ./.fork; then \
+			echo ""; \
+		else \
+			echo "-DTHERE_IS_NO_FORK"; \
+		fi; \
+		rm -f .fork.c .fork; \
+	fi;
+_HAVE_FORK!= $(_HAVE_FORK_SH)
+FORK= $(_HAVE_FORK)$(shell $(_HAVE_FORK_SH))
+
+# info dir defaults to /var/lib/dhcpcd on Linux and /var/db elsewhere
+_INFODIR_SH= if test -n "${INFODIR}"; then \
+			  echo "${INFODIR}"; \
+			  else \
+			  case `uname -s` in \
+			  Linux) echo "/var/lib/dhcpcd";; \
+			  *) echo "/var/db";; \
+			  esac \
+			  fi
+_INFODIR!= ${_INFODIR_SH}
+INFOD?= ${_INFODIR}$(shell $(_INFODIR_SH))
+
+# Work out how to restart services 
+_RC_SH= if test -n "$(HAVE_INIT)"; then \
+		 test "$(HAVE_INIT)" = "no" || echo "-DENABLE_$(HAVE_INIT)"; \
+		 elif test -x /sbin/runscript; then echo "-DENABLE_OPENRC"; \
+		 elif test -x /sbin/service; then echo "-DENABLE_SERVICE"; \
+		 elif test -x /etc/rc.d/rc.S -a -x /etc/rc.d/rc.M; then echo "-DENABLE_SLACKRC"; \
+		 elif test -d /etc/rc.d; then echo "-DENABLE_BSDRC"; \
+		 elif test -d /etc/init.d; then echo "-DENABLE_SYSV"; \
+		 fi
+_RC!= $(_RC_SH)
+RC= $(_RC)$(shell $(_RC_SH))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mk/prog.mk	Mon Jan 07 23:45:48 2008 +0000
@@ -0,0 +1,28 @@
+# rules to build a program 
+# based on FreeBSD's bsd.prog.mk
+
+# Copyright 2008 Roy Marples
+
+BINDIR?=	/sbin
+OBJS+=		${SRCS:.c=.o}
+
+INSTALL?=	install
+
+all: ${PROG} ${MAN}
+
+${PROG}: ${SCRIPTS} ${OBJS}
+	${CC} ${CFLAGS} ${LDFLAGS} ${PROGLDFLAGS} -o $@ ${OBJS} ${LDADD}
+
+_proginstall: ${PROG}
+	${INSTALL} -d ${DESTDIR}${BINDIR}
+	${INSTALL} ${PROG} ${DESTDIR}${BINDIR}
+
+_progclean:
+	rm -f ${OBJS} ${PROG} ${CLEANFILES}
+
+include ${MK}/depend.mk
+include ${MK}/man.mk
+
+install: _proginstall maninstall
+
+clean: _progclean _dependclean