projects/pwm

view Makefile @ 25:616385fa1fd9

Build common functions as a library that can be reused
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Wed Sep 20 23:57:51 2017 +0200 (2017-09-20)
parents eb5ce870eb16
children 5bdea77d0c1d
line source
1 #
2 # Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #
24 PACKAGE = pwm
25 VERSION = 1
26 DISTNAME := $(PACKAGE)-$(VERSION)
28 # gcc, clang, icc, Sun/Solaris Studio
29 CC := $(CC) -std=c99
30 COMPILE.c = $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS) $(TARGET_ARCH) -c
31 # gcc, clang, icc
32 MAKEDEPEND.c = $(CC) -MM $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS)
33 # Sun/Solaris Studio
34 #MAKEDEPEND.c = $(CC) -xM1 $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS)
35 # X makedepend
36 #MAKEDEPEND.c = makedepend -f- -Y -- $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS) --
37 LINK.c = $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS) $(LDFLAGS) $(XLDFLAGS) $(TARGET_ARCH)
38 LINK.o = $(CC) $(LDFLAGS) $(XLDFLAGS) $(TARGET_ARCH)
39 AR := ar
40 RANLIB := ranlib
41 CP := cp
42 INSTALL := install
43 INSTALL.exec := $(INSTALL) -D -m 0755
44 INSTALL.data := $(INSTALL) -D -m 0644
45 INSTALL.link := $(CP) -f -P
46 PAX := pax
47 GZIP := gzip
48 SED := sed
49 XSLTPROC := xsltproc
50 DOCBOOK5_MANPAGES_STYLESHEET = http://docbook.sourceforge.net/release/xsl-ns/current/manpages/docbook.xsl
51 DOCBOOK5_MANPAGES_FLAGS = --stringparam man.authors.section.enabled 0 \
52 --stringparam man.copyright.section.enabled 0
54 define generate-manpage-rule =
55 $(addsuffix .%,$(basename $1)): $(addsuffix .%.xml,$(basename $(firstword $1))) docbook-update-source-data.xsl
56 $$(XSLTPROC) \
57 --xinclude \
58 --stringparam package $$(PACKAGE) \
59 --stringparam version $$(VERSION) \
60 docbook-update-source-data.xsl $$< | \
61 $$(XSLTPROC) \
62 --xinclude \
63 --output $(firstword $1) \
64 $$(DOCBOOK5_MANPAGES_FLAGS) \
65 $$(DOCBOOK5_MANPAGES_STYLESHEET) \
66 -
67 for alias in $(wordlist 2,$(words $1),$1); do \
68 ln -sf $(notdir $(firstword $1)) $$$$alias; \
69 done
70 endef
72 DESTDIR ?=
73 prefix ?= /usr/local
74 bindir ?= $(prefix)/bin
75 datadir ?= $(prefix)/share
76 mandir ?= $(datadir)/man
78 OS_NAME := $(shell uname -s)
79 OS_RELEASE := $(shell uname -r)
81 ifeq ($(OS_NAME),Linux)
82 HAVE_ARC4RANDOM ?= 0
83 HAVE_ASPRINTF ?= 1
84 HAVE_CLOSEFROM ?= 0
85 HAVE_ERR_H ?= 1
86 HAVE_GETRANDOM ?= 0
87 HAVE_SYS_QUEUE_H ?= 0
88 HAVE_SETPROGNAME ?= 0
89 HAVE_SYS_TREE_H ?= 0
90 else ifneq ($(findstring $(OS_NAME),FreeBSD DragonFly),)
91 HAVE_ARC4RANDOM ?= 1
92 HAVE_ASPRINTF ?= 1
93 HAVE_CLOSEFROM ?= 1
94 HAVE_ERR_H ?= 1
95 HAVE_GETRANDOM ?= 0
96 HAVE_SYS_QUEUE_H ?= 1
97 HAVE_SETPROGNAME ?= 1
98 HAVE_SYS_TREE_H ?= 1
99 else ifeq ($(OS_NAME),NetBSD)
100 HAVE_ARC4RANDOM ?= 1
101 HAVE_ASPRINTF ?= 1
102 HAVE_CLOSEFROM ?= 1
103 HAVE_ERR_H ?= 1
104 HAVE_GETRANDOM ?= 0
105 HAVE_SYS_QUEUE_H ?= 1
106 HAVE_SYS_TREE_H ?= 1
107 HAVE_SETPROGNAME ?= 1
108 else ifeq ($(OS_NAME),OpenBSD)
109 HAVE_ARC4RANDOM ?= 1
110 HAVE_ASPRINTF ?= 1
111 HAVE_CLOSEFROM ?= 1
112 HAVE_ERR_H ?= 1
113 HAVE_GETRANDOM ?= 0
114 HAVE_SYS_QUEUE_H ?= 1
115 HAVE_SYS_TREE_H ?= 1
116 HAVE_SETPROGNAME ?= 1
117 else ifeq ($(OS_NAME),SunOS)
118 ifeq ($(OS_RELEASE),5.10)
119 HAVE_ARC4RANDOM ?= 0
120 HAVE_ASPRINTF ?= 0
121 HAVE_ERR_H ?= 0
122 HAVE_GETRANDOM ?= 0
123 else
124 HAVE_ARC4RANDOM ?= 1
125 HAVE_ASPRINTF ?= 1
126 HAVE_ERR_H ?= 1
127 HAVE_GETRANDOM ?= 1
128 endif
129 HAVE_CLOSEFROM ?= 1
130 HAVE_SYS_QUEUE_H ?= 0
131 HAVE_SYS_TREE_H ?= 0
132 HAVE_SETPROGNAME ?= 0
133 else
134 HAVE_ARC4RANDOM ?= 0
135 HAVE_ASPRINTF ?= 0
136 HAVE_CLOSEFROM ?= 0
137 HAVE_ERR_H ?= 0
138 HAVE_GETRANDOM ?= 0
139 HAVE_SYS_QUEUE_H ?= 0
140 HAVE_SETPROGNAME ?= 0
141 HAVE_SYS_TREE_H ?= 0
142 endif
144 LIBCOMPAT_OBJS = compat/compat.o
145 LIBCOMPAT_LIB = compat/libcompat.a
146 LIBCOMPAT_LIB_MEMBERS = $(LIBCOMPAT_OBJS:%.o=$(LIBCOMPAT_LIB)(%.o))
148 LIBPWM_OBJS = util.o
149 LIBPWM_LIB = libpwm.a
150 LIBPWM_LIB_MEMBERS = $(LIBPWM_OBJS:%.o=$(LIBPWM_LIB)(%.o))
152 PWM_OBJS = cmd.o \
153 io.o \
154 pager.o \
155 proc.o \
156 pw.o \
157 pwfile.o \
158 pwm.o \
159 tok.o
160 PWM_BIN = $(PACKAGE)
162 OBJS = $(LIBCOMPAT_OBJS) $(LIBPWM_OBJS) $(PWM_OBJS)
164 MANPAGES = pwm.1
166 .DEFAULT_TARGET = all
168 .PHONY: all clean clobber dist install
170 all: $(PWM_BIN) $(MANPAGES)
172 $(PWM_BIN): XCPPFLAGS += -DPACKAGE=\"$(PACKAGE)\" \
173 -DVERSION=\"$(VERSION)\"
174 $(PWM_BIN): LDLIBS = -ltecla -lcurses -lpws -lnettle
175 ifeq ($(HAVE_ASPRINTF),1)
176 XCPPFLAGS += -DHAVE_ASPRINTF
177 else
178 LIBCOMPAT_OBJS += compat/asprintf.o
179 endif
180 ifeq ($(HAVE_ARC4RANDOM),1)
181 XCPPFLAGS += -DHAVE_ARC4RANDOM
182 LIBPWM_OBJS += rand-arc4random.o
183 else ifeq ($(HAVE_GETRANDOM),1)
184 XCPPFLAGS += -DHAVE_GETRANDOM
185 LIBPWM_OBJS += rand-getrandom.o
186 else
187 LIBPWM_OBJS += rand-dev-random.o
188 endif
189 ifeq ($(HAVE_CLOSEFROM),1)
190 XCPPFLAGS += -DHAVE_CLOSEFROM
191 else
192 LIBCOMPAT_OBJS += compat/closefrom.o
193 endif
194 ifeq ($(HAVE_ERR_H),1)
195 XCPPFLAGS += -DHAVE_ERR_H
196 else
197 LIBCOMPAT_OBJS += compat/err.o
198 endif
199 ifeq ($(HAVE_SETPROGNAME),1)
200 XCPPFLAGS += -DHAVE_SETPROGNAME
201 else
202 LIBCOMPAT_OBJS += compat/setprogname.o
203 endif
204 ifeq ($(HAVE_SYS_TREE_H),1)
205 XCPPFLAGS += -DHAVE_SYS_TREE_H
206 endif
207 ifneq ($(findstring $(OS_NAME),FreeBSD DragonFly OpenBSD),)
208 XCPPFLAGS += -I/usr/local/include
209 XLDFLAGS += -L/usr/local/lib
210 else ifeq ($(OS_NAME),NetBSD)
211 XCPPFLAGS += -I/usr/pkg/include
212 XLDFLAGS += -L/usr/pkg/lib
213 endif
214 ifeq ($(findstring $(OS_NAME),FreeBSD DragonFly NetBSD OpenBSD),)
215 XCPPFLAGS += -D_XOPEN_SOURCE=600
216 endif
217 ifeq ($(OS_NAME),SunOS)
218 XCPPFLAGS += -I/usr/xpg4/include -D__EXTENSIONS__
219 XLDFLAGS += -L/usr/xpg4/lib -R/usr/xpg4/lib
220 endif
222 $(LIBCOMPAT_LIB): $(LIBCOMPAT_LIB_MEMBERS)
224 $(LIBPWM_LIB): $(LIBPWM_LIB_MEMBERS)
226 $(PWM_BIN): $(PWM_OBJS) $(LIBPWM_LIB) $(LIBCOMPAT_LIB)
227 $(LINK.o) $^ $(LDLIBS) -o $@
229 $(eval $(call generate-manpage-rule,$(MANPAGES)))
231 %.o: %.c
232 $(MAKEDEPEND.c) $< | $(SED) -f deps.sed >$*.d
233 $(COMPILE.c) -o $@ $<
235 (%): %
236 $(AR) $(ARFLAGS) $@ $<
237 $(RANLIB) $@
239 install: all
240 $(INSTALL.exec) $(PWM_BIN) "$(DESTDIR)$(bindir)/$(PWM_BIN)"
241 for manpage in $(MANPAGES); do \
242 if [ -L $${manpage} ]; then \
243 $(INSTALL.link) $${manpage} \
244 "$(DESTDIR)$(mandir)/man$${manpage##*.}/$${manpage##*/}"; \
245 else \
246 $(INSTALL.data) $${manpage} \
247 "$(DESTDIR)$(mandir)/man$${manpage##*.}/$${manpage##*/}"; \
248 fi \
249 done
251 clean:
252 rm -f $(LIBCOMPAT_LIB) $(LIBPWM_LIB) $(PWM_BIN) $(OBJS) $(MANPAGES)
254 clobber: clean
255 rm -f $(patsubst %.o,%.d,$(OBJS))
257 dist: clobber
258 $(PAX) -w -x ustar -s ',.*/\..*,,' -s ',./[^/]*\.tar\.gz,,' \
259 -s ',^\.$$,,' -s ',\./,$(DISTNAME)/,' . | \
260 $(GZIP) > $(DISTNAME).tar.gz
262 -include $(patsubst %.o,%.d,$(OBJS))