view Makefile @ 25:616385fa1fd9

Build common functions as a library that can be reused
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Wed, 20 Sep 2017 23:57:51 +0200
parents eb5ce870eb16
children 5bdea77d0c1d
line wrap: on
line source

#
# Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

PACKAGE =	pwm
VERSION =	1
DISTNAME :=	$(PACKAGE)-$(VERSION)

# gcc, clang, icc, Sun/Solaris Studio
CC :=		$(CC) -std=c99
COMPILE.c =	$(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS) $(TARGET_ARCH) -c
# gcc, clang, icc
MAKEDEPEND.c =	$(CC) -MM $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS)
# Sun/Solaris Studio
#MAKEDEPEND.c =	$(CC) -xM1 $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS)
# X makedepend
#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
INSTALL.data :=	$(INSTALL) -D -m 0644
INSTALL.link :=	$(CP) -f -P
PAX :=		pax
GZIP :=		gzip
SED :=		sed
XSLTPROC :=	xsltproc
DOCBOOK5_MANPAGES_STYLESHEET =	http://docbook.sourceforge.net/release/xsl-ns/current/manpages/docbook.xsl
DOCBOOK5_MANPAGES_FLAGS =	--stringparam man.authors.section.enabled 0 \
				--stringparam man.copyright.section.enabled 0

define generate-manpage-rule =
$(addsuffix .%,$(basename $1)): $(addsuffix .%.xml,$(basename $(firstword $1))) docbook-update-source-data.xsl
	$$(XSLTPROC) \
	    --xinclude \
	    --stringparam package $$(PACKAGE) \
	    --stringparam version $$(VERSION) \
	    docbook-update-source-data.xsl $$< | \
	    $$(XSLTPROC) \
	    --xinclude \
	    --output $(firstword $1) \
	    $$(DOCBOOK5_MANPAGES_FLAGS) \
	    $$(DOCBOOK5_MANPAGES_STYLESHEET) \
	    -
	for alias in $(wordlist 2,$(words $1),$1); do \
	    ln -sf $(notdir $(firstword $1)) $$$$alias; \
	done
endef

DESTDIR ?=
prefix ?=	/usr/local
bindir ?=	$(prefix)/bin
datadir ?=	$(prefix)/share
mandir ?=	$(datadir)/man

OS_NAME :=	$(shell uname -s)
OS_RELEASE :=	$(shell uname -r)

ifeq ($(OS_NAME),Linux)
  HAVE_ARC4RANDOM ?=	0
  HAVE_ASPRINTF ?=	1
  HAVE_CLOSEFROM ?=	0
  HAVE_ERR_H ?=		1
  HAVE_GETRANDOM ?=	0
  HAVE_SYS_QUEUE_H ?=	0
  HAVE_SETPROGNAME ?=	0
  HAVE_SYS_TREE_H ?=	0
else ifneq ($(findstring $(OS_NAME),FreeBSD DragonFly),)
  HAVE_ARC4RANDOM ?=	1
  HAVE_ASPRINTF ?=	1
  HAVE_CLOSEFROM ?=	1
  HAVE_ERR_H ?=		1
  HAVE_GETRANDOM ?=	0
  HAVE_SYS_QUEUE_H ?=	1
  HAVE_SETPROGNAME ?=	1
  HAVE_SYS_TREE_H ?=	1
else ifeq ($(OS_NAME),NetBSD)
  HAVE_ARC4RANDOM ?=	1
  HAVE_ASPRINTF ?=	1
  HAVE_CLOSEFROM ?=	1
  HAVE_ERR_H ?=		1
  HAVE_GETRANDOM ?=	0
  HAVE_SYS_QUEUE_H ?=	1
  HAVE_SYS_TREE_H ?=	1
  HAVE_SETPROGNAME ?=	1
else ifeq ($(OS_NAME),OpenBSD)
  HAVE_ARC4RANDOM ?=	1
  HAVE_ASPRINTF ?=	1
  HAVE_CLOSEFROM ?=	1
  HAVE_ERR_H ?=		1
  HAVE_GETRANDOM ?=	0
  HAVE_SYS_QUEUE_H ?=	1
  HAVE_SYS_TREE_H ?=	1
  HAVE_SETPROGNAME ?=	1
else ifeq ($(OS_NAME),SunOS)
  ifeq ($(OS_RELEASE),5.10)
    HAVE_ARC4RANDOM ?=	0
    HAVE_ASPRINTF ?=	0
    HAVE_ERR_H ?=	0
    HAVE_GETRANDOM ?=	0
  else
    HAVE_ARC4RANDOM ?=	1
    HAVE_ASPRINTF ?=	1
    HAVE_ERR_H ?=	1
    HAVE_GETRANDOM ?=	1
  endif
  HAVE_CLOSEFROM ?=	1
  HAVE_SYS_QUEUE_H ?=	0
  HAVE_SYS_TREE_H ?=	0
  HAVE_SETPROGNAME ?=	0
else
  HAVE_ARC4RANDOM ?=	0
  HAVE_ASPRINTF ?=	0
  HAVE_CLOSEFROM ?=	0
  HAVE_ERR_H ?=		0
  HAVE_GETRANDOM ?=	0
  HAVE_SYS_QUEUE_H ?=	0
  HAVE_SETPROGNAME ?=	0
  HAVE_SYS_TREE_H ?=	0
endif

LIBCOMPAT_OBJS =	compat/compat.o
LIBCOMPAT_LIB =		compat/libcompat.a
LIBCOMPAT_LIB_MEMBERS =	$(LIBCOMPAT_OBJS:%.o=$(LIBCOMPAT_LIB)(%.o))

LIBPWM_OBJS =		util.o
LIBPWM_LIB =		libpwm.a
LIBPWM_LIB_MEMBERS =	$(LIBPWM_OBJS:%.o=$(LIBPWM_LIB)(%.o))

PWM_OBJS =		cmd.o \
			io.o \
			pager.o \
			proc.o \
			pw.o \
			pwfile.o \
			pwm.o \
			tok.o
PWM_BIN =		$(PACKAGE)

OBJS = $(LIBCOMPAT_OBJS) $(LIBPWM_OBJS) $(PWM_OBJS)

MANPAGES = pwm.1

.DEFAULT_TARGET = all

.PHONY: all clean clobber dist install

all: $(PWM_BIN) $(MANPAGES)

$(PWM_BIN): XCPPFLAGS += -DPACKAGE=\"$(PACKAGE)\" \
			-DVERSION=\"$(VERSION)\"
$(PWM_BIN): LDLIBS =	-ltecla -lcurses -lpws -lnettle
ifeq ($(HAVE_ASPRINTF),1)
  XCPPFLAGS +=		-DHAVE_ASPRINTF
else
  LIBCOMPAT_OBJS +=	compat/asprintf.o
endif
ifeq ($(HAVE_ARC4RANDOM),1)
  XCPPFLAGS +=		-DHAVE_ARC4RANDOM
  LIBPWM_OBJS +=	rand-arc4random.o
else ifeq ($(HAVE_GETRANDOM),1)
  XCPPFLAGS +=		-DHAVE_GETRANDOM
  LIBPWM_OBJS +=	rand-getrandom.o
else
  LIBPWM_OBJS +=	rand-dev-random.o
endif
ifeq ($(HAVE_CLOSEFROM),1)
  XCPPFLAGS +=		-DHAVE_CLOSEFROM
else
  LIBCOMPAT_OBJS +=	compat/closefrom.o
endif
ifeq ($(HAVE_ERR_H),1)
  XCPPFLAGS +=		-DHAVE_ERR_H
else
  LIBCOMPAT_OBJS +=	compat/err.o
endif
ifeq ($(HAVE_SETPROGNAME),1)
  XCPPFLAGS +=		-DHAVE_SETPROGNAME
else
  LIBCOMPAT_OBJS +=	compat/setprogname.o
endif
ifeq ($(HAVE_SYS_TREE_H),1)
  XCPPFLAGS +=		-DHAVE_SYS_TREE_H
endif
ifneq ($(findstring $(OS_NAME),FreeBSD DragonFly OpenBSD),)
  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
endif
ifeq ($(findstring $(OS_NAME),FreeBSD DragonFly NetBSD OpenBSD),)
  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
endif

$(LIBCOMPAT_LIB): $(LIBCOMPAT_LIB_MEMBERS)

$(LIBPWM_LIB): $(LIBPWM_LIB_MEMBERS)

$(PWM_BIN): $(PWM_OBJS) $(LIBPWM_LIB) $(LIBCOMPAT_LIB)
	$(LINK.o) $^ $(LDLIBS) -o $@

$(eval $(call generate-manpage-rule,$(MANPAGES)))

%.o: %.c
	$(MAKEDEPEND.c) $< | $(SED) -f deps.sed >$*.d
	$(COMPILE.c) -o $@ $<

(%): %
	$(AR) $(ARFLAGS) $@ $<
	$(RANLIB) $@

install: all
	$(INSTALL.exec) $(PWM_BIN) "$(DESTDIR)$(bindir)/$(PWM_BIN)"
	for manpage in $(MANPAGES); do \
	    if [ -L $${manpage} ]; then \
	        $(INSTALL.link) $${manpage} \
	            "$(DESTDIR)$(mandir)/man$${manpage##*.}/$${manpage##*/}"; \
	    else \
	        $(INSTALL.data) $${manpage} \
	            "$(DESTDIR)$(mandir)/man$${manpage##*.}/$${manpage##*/}"; \
	    fi \
	done

clean:
	rm -f $(LIBCOMPAT_LIB) $(LIBPWM_LIB) $(PWM_BIN) $(OBJS) $(MANPAGES)

clobber: clean
	rm -f $(patsubst %.o,%.d,$(OBJS))

dist: clobber
	$(PAX) -w -x ustar -s ',.*/\..*,,' -s ',./[^/]*\.tar\.gz,,' \
	    -s ',^\.$$,,' -s ',\./,$(DISTNAME)/,' . | \
	    $(GZIP) > $(DISTNAME).tar.gz

-include $(patsubst %.o,%.d,$(OBJS))