build: Activate .DELETE_ON_ERROR.
[lopsub.git] / Makefile
1 # Implicit rules are implemented in make as suffix rules. The following rule
2 # empties the suffix list to disable the predefined implicit rules. This
3 # increases performance and avoids hard-to-debug behaviour.
4 .SUFFIXES:
5 MAKEFLAGS += -Rr
6 ifeq ("$(origin CC)", "default")
7         CC := cc
8 endif
9 .ONESHELL:
10 .SHELLFLAGS := -ec
11
12 # Recipes which redirect stdout to the target of the rule (i.e., constructs
13 # like cmd > $@) create empty or incomplete output files if the command fails,
14 # for example when cmd was not found. Since the target exists and is uptodate
15 # in this case, this may lead to all sorts of problems. The following target
16 # makes sure that such files are removed.
17 .DELETE_ON_ERROR:
18
19 PREFIX ?= /usr/local
20 M4 := m4
21 LN := ln -f
22 LEX := lex
23 RM := rm -f
24 AR := ar
25 GROFF := groff
26 CP := cp
27 INSTALL := install
28 GZIP := gzip -fn9
29 ZCAT := zcat
30
31 dummy != $(M4) /dev/null || printf 'failed'
32 ifeq ($(dummy), failed)
33 $(error m4 is required to build this package)
34 endif
35 dummy != printf '%%%%\n' | $(LEX) -o /dev/null || printf 'failed'
36 ifeq ($(dummy), failed)
37 $(error (f)lex is required to build this package)
38 endif
39
40 DATE_FMT := +%B %Y
41 # To get a reproducible build, we use $(SOURCE_DATE_EPOCH) instead of the
42 # current time if this variable is set.
43 ifdef SOURCE_DATE_EPOCH
44         DATE := $(shell LC_ALL=C date -u -d '@$(SOURCE_DATE_EPOCH)' \
45                 '$(DATE_FMT)' 2>/dev/null || LC_ALL=C date -u '$(DATE_FMT)')
46 else
47         DATE := $(shell date '$(DATE_FMT)')
48 endif
49 GIT_VERSION := $(shell ./version-gen.sh)
50 PLAIN_VERSION := $(firstword $(subst -, , $(GIT_VERSION)))
51 MAJOR_VERSION := $(firstword $(subst ., , $(PLAIN_VERSION)))
52 SONAME := liblopsub.so.$(MAJOR_VERSION)
53 REALNAME := liblopsub.so.$(PLAIN_VERSION)
54 LINKERNAME:=liblopsub.so
55
56 m4_man_pages := lopsub-suite.5.gz lopsub.7.gz
57 all := $(m4_man_pages) $(REALNAME) lopsubgen lopsubgen.1.gz \
58         lopsubex lopsubex.1.gz
59 all: $(all)
60
61 # deps
62 lopsubgen.o: lsg.h
63 lopsub.o lsg.o: lopsub.h lopsub-internal.h
64 lsg.o: lopsubgen.lsg.h lopsub-internal.h
65 lopsubex.o: lopsubex.lsg.h lopsub.h
66 config_file.c: lopsub-internal.h lopsub.h
67 version.o: version.c
68
69 # m4 stuff
70 gendoc := gendoc/gendoc.m4
71 %.h: %.h.m4 $(gendoc)
72         $(M4) -DOUTPUT_MODE=C $(gendoc) $< > $@
73 $(m4_man_pages): %.gz: %.m4 version.c
74         $(M4) -DGIT_VERSION=$(GIT_VERSION) -DDATE="$(DATE)" $< | $(GZIP) > $@
75
76 # flex
77 %.c: %.l
78         $(LEX) -o $@ $<
79
80 # lopsubgen
81 lopsubgen.lsg.c lopsubgen.lsg.h: lopsubgen.suite lopsubgen-stage1 \
82                 lopsub-internal.h
83         ./lopsubgen-stage1 < $<
84 %.lsg.c: %.suite lopsubgen
85         ./lopsubgen --gen-c < $<
86 %.lsg.h: %.suite lopsubgen
87         ./lopsubgen --gen-header < $<
88 %.1.gz: %.suite lopsubgen
89         ./lopsubgen --gen-man=${@:.gz=} --version-string $(GIT_VERSION) < $<
90         $(GZIP) ${@:.gz=}
91
92 # compiling
93 lsg1_objs := lopsubgen.o lsg1.o version.o
94 lsg_objs := lopsubgen.o lsg.o lopsubgen.lsg.o lopsub.o version.o
95 liblopsub_objs := config_file.o lopsub.o version.o
96 lopsubex_objs := lopsubex.o lopsubex.lsg.o $(liblopsub_objs)
97
98 $(lsg_objs) $(liblopsub_objs) $(lopsubex_objs): %.o: %.c
99 lopsubgen.o config_file.o:
100         $(CC) -g -c -fPIC -o $@ ${@:.o=.c}
101 lsg1.o: lsg.c lsg.h
102         $(CC) -g -DSTAGE1 -Wall -c $< -o $@
103 %.o: %.c
104         $(CC) -Wall -I. -g -c -fPIC -o $@ $<
105
106 # linking
107 lopsubgen-stage1: $(lsg1_objs)
108         $(CC) -Wall -g $(lsg1_objs) -o $@
109 lopsubgen: $(lsg_objs)
110         $(CC) -Wall -g -o $@ $(lsg_objs)
111 $(REALNAME): $(liblopsub_objs)
112         $(CC) --shared -Wl,-soname,liblopsub.so.$(MAJOR_VERSION) -o $@ $^
113 lopsubex: $(lopsubex_objs) $(REALNAME)
114         $(CC) -Wall -g -o $@ $(lopsubex_objs)
115
116 # web
117 html := $(addprefix web/, $(addsuffix .html, \
118         index lopsub-api lopsubgen.1 lopsubex.1 $(m4_man_pages:.gz=)))
119 www: $(html)
120 web/lopsub-api.html: lopsub.h.m4 web/header.html web/footer.html
121         $(M4) -DOUTPUT_MODE=HTML web/header.html $(gendoc) \
122                 $< web/footer.html > $@
123 web/index.html: web/lopsub.7.html
124         $(LN) -s $(notdir $<) $@
125 web/%.html: %.gz web/header.html
126         $(CP) web/header.html $@
127         $(ZCAT) $< | $(GROFF) -m man -Thtml | sed -e '1,/^<body>/d' >> $@
128
129 install: $(all)
130         $(INSTALL) -d $(DESTDIR)$(PREFIX)/lib $(DESTDIR)$(PREFIX)/include \
131                 $(DESTDIR)$(PREFIX)/share/man/man1 $(DESTDIR)$(PREFIX)/share/man/man5 \
132                 $(DESTDIR)$(PREFIX)/share/man/man7 $(DESTDIR)$(PREFIX)/bin
133         $(INSTALL) -m 644 $(REALNAME) $(DESTDIR)$(PREFIX)/lib
134         $(LN) -s $(REALNAME) $(DESTDIR)$(PREFIX)/lib/$(SONAME)
135         $(LN) -s $(SONAME) $(DESTDIR)$(PREFIX)/lib/$(LINKERNAME)
136         $(INSTALL) -m 755 lopsubgen $(DESTDIR)$(PREFIX)/bin
137         $(INSTALL) -m 644 lopsub.h $(DESTDIR)$(PREFIX)/include
138         $(INSTALL) -m 644 lopsub-internal.h $(DESTDIR)$(PREFIX)/include
139         $(INSTALL) -m 644 lopsubgen.1.gz $(DESTDIR)$(PREFIX)/share/man/man1
140         $(INSTALL) -m 644 lopsub-suite.5.gz $(DESTDIR)$(PREFIX)/share/man/man5
141         $(INSTALL) -m 644 lopsub.7.gz $(DESTDIR)$(PREFIX)/share/man/man7
142
143 clean:
144         $(RM) $(all) $(html) *.o *.man
145 distclean: clean
146         $(RM) *.lsg.c *.lsg.h lopsubgen.c config_file.c lopsubgen-stage1 \
147                 lopsub.h lopsub.7 lopsub-suite.5 version.c
148