diff Makefile @ 24:eb5ce870eb16

Build compatibility functions as a library that can be reused
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Wed, 20 Sep 2017 23:49:31 +0200
parents 5c6155c8e9b6
children 616385fa1fd9
line wrap: on
line diff
--- a/Makefile	Sun Sep 17 18:45:05 2017 +0200
+++ b/Makefile	Wed Sep 20 23:49:31 2017 +0200
@@ -36,6 +36,8 @@
 #MAKEDEPEND.c =	makedepend -f- -Y -- $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS) --
 LINK.c =	$(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS) $(LDFLAGS) $(XLDFLAGS) $(TARGET_ARCH)
 LINK.o =	$(CC) $(LDFLAGS) $(XLDFLAGS) $(TARGET_ARCH)
+AR :=		ar
+RANLIB :=	ranlib
 CP :=		cp
 INSTALL :=	install
 INSTALL.exec :=	$(INSTALL) -D -m 0755
@@ -139,15 +141,22 @@
   HAVE_SYS_TREE_H ?=	0
 endif
 
-OBJS =	cmd.o \
-	io.o \
-	pager.o \
-	proc.o \
-	pw.o \
-	pwfile.o \
-	pwm.o \
-	tok.o \
-	util.o
+LIBCOMPAT_OBJS =	compat/compat.o
+LIBCOMPAT_LIB =		compat/libcompat.a
+LIBCOMPAT_LIB_MEMBERS =	$(LIBCOMPAT_OBJS:%.o=$(LIBCOMPAT_LIB)(%.o))
+
+PWM_OBJS =		cmd.o \
+			io.o \
+			pager.o \
+			proc.o \
+			pw.o \
+			pwfile.o \
+			pwm.o \
+			tok.o \
+			util.o
+PWM_BIN =		$(PACKAGE)
+
+OBJS = $(LIBCOMPAT_OBJS) $(PWM_OBJS)
 
 MANPAGES = pwm.1
 
@@ -155,59 +164,61 @@
 
 .PHONY: all clean clobber dist install
 
-all: $(PACKAGE) $(MANPAGES)
+all: $(PWM_BIN) $(MANPAGES)
 
-XCPPFLAGS =	-DPACKAGE=\"$(PACKAGE)\" \
-		-DVERSION=\"$(VERSION)\"
-LDLIBS =	-ltecla -lcurses -lpws -lnettle
+$(PWM_BIN): XCPPFLAGS += -DPACKAGE=\"$(PACKAGE)\" \
+			-DVERSION=\"$(VERSION)\"
+$(PWM_BIN): LDLIBS =	-ltecla -lcurses -lpws -lnettle
 ifeq ($(HAVE_ASPRINTF),1)
-  XCPPFLAGS +=	-DHAVE_ASPRINTF
+  XCPPFLAGS +=		-DHAVE_ASPRINTF
 else
-  OBJS +=	compat/asprintf.o
+  LIBCOMPAT_OBJS +=	compat/asprintf.o
 endif
 ifeq ($(HAVE_ARC4RANDOM),1)
-  XCPPFLAGS +=	-DHAVE_ARC4RANDOM
-  OBJS +=	rand-arc4random.o
+  XCPPFLAGS +=		-DHAVE_ARC4RANDOM
+  PWM_OBJS +=		rand-arc4random.o
 else ifeq ($(HAVE_GETRANDOM),1)
-  XCPPFLAGS +=	-DHAVE_GETRANDOM
-  OBJS +=	rand-getrandom.o
+  XCPPFLAGS +=		-DHAVE_GETRANDOM
+  PWM_OBJS +=		rand-getrandom.o
 else
-  OBJS +=	rand-dev-random.o
+  PWM_OBJS +=		rand-dev-random.o
 endif
 ifeq ($(HAVE_CLOSEFROM),1)
-  XCPPFLAGS +=	-DHAVE_CLOSEFROM
+  XCPPFLAGS +=		-DHAVE_CLOSEFROM
 else
-  OBJS +=	compat/closefrom.o
+  LIBCOMPAT_OBJS +=	compat/closefrom.o
 endif
 ifeq ($(HAVE_ERR_H),1)
-  XCPPFLAGS +=	-DHAVE_ERR_H
+  XCPPFLAGS +=		-DHAVE_ERR_H
 else
-  OBJS +=	compat/err.o
+  LIBCOMPAT_OBJS +=	compat/err.o
 endif
 ifeq ($(HAVE_SETPROGNAME),1)
-  XCPPFLAGS +=	-DHAVE_SETPROGNAME
+  XCPPFLAGS +=		-DHAVE_SETPROGNAME
 else
-  OBJS +=	compat/setprogname.o
+  LIBCOMPAT_OBJS +=	compat/setprogname.o
 endif
 ifeq ($(HAVE_SYS_TREE_H),1)
-  XCPPFLAGS +=	-DHAVE_SYS_TREE_H
+  XCPPFLAGS +=		-DHAVE_SYS_TREE_H
 endif
 ifneq ($(findstring $(OS_NAME),FreeBSD DragonFly OpenBSD),)
-  XCPPFLAGS +=	-I/usr/local/include
-  XLDFLAGS +=	-L/usr/local/lib
+  XCPPFLAGS +=		-I/usr/local/include
+  XLDFLAGS +=		-L/usr/local/lib
 else ifeq ($(OS_NAME),NetBSD)
-  XCPPFLAGS +=	-I/usr/pkg/include
-  XLDFLAGS +=	-L/usr/pkg/lib
+  XCPPFLAGS +=		-I/usr/pkg/include
+  XLDFLAGS +=		-L/usr/pkg/lib
 endif
 ifeq ($(findstring $(OS_NAME),FreeBSD DragonFly NetBSD OpenBSD),)
-  XCPPFLAGS +=	-D_XOPEN_SOURCE=600
+  XCPPFLAGS +=		-D_XOPEN_SOURCE=600
 endif
 ifeq ($(OS_NAME),SunOS)
-  XCPPFLAGS +=	-I/usr/xpg4/include -D__EXTENSIONS__
-  XLDFLAGS +=	-L/usr/xpg4/lib -R/usr/xpg4/lib
+  XCPPFLAGS +=		-I/usr/xpg4/include -D__EXTENSIONS__
+  XLDFLAGS +=		-L/usr/xpg4/lib -R/usr/xpg4/lib
 endif
 
-$(PACKAGE): $(OBJS)
+$(LIBCOMPAT_LIB): $(LIBCOMPAT_LIB_MEMBERS)
+
+$(PWM_BIN): $(PWM_OBJS) $(LIBCOMPAT_LIB)
 	$(LINK.o) $^ $(LDLIBS) -o $@
 
 $(eval $(call generate-manpage-rule,$(MANPAGES)))
@@ -216,8 +227,12 @@
 	$(MAKEDEPEND.c) $< | $(SED) -f deps.sed >$*.d
 	$(COMPILE.c) -o $@ $<
 
+(%): %
+	$(AR) $(ARFLAGS) $@ $<
+	$(RANLIB) $@
+
 install: all
-	$(INSTALL.exec) $(PACKAGE) "$(DESTDIR)$(bindir)/$(PACKAGE)"
+	$(INSTALL.exec) $(PWM_BIN) "$(DESTDIR)$(bindir)/$(PWM_BIN)"
 	for manpage in $(MANPAGES); do \
 	    if [ -L $${manpage} ]; then \
 	        $(INSTALL.link) $${manpage} \
@@ -229,7 +244,7 @@
 	done
 
 clean:
-	rm -f $(PACKAGE) $(OBJS) $(MANPAGES)
+	rm -f $(LIBCOMPAT_LIB) $(PWM_BIN) $(OBJS) $(MANPAGES)
 
 clobber: clean
 	rm -f $(patsubst %.o,%.d,$(OBJS))