Merge branch 't/build-improvements'
[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 LLS_CFLAGS := -g -fPIC
99 STRICT_CFLAGS := -Wall
100 STRICT_CFLAGS += -Werror-implicit-function-declaration
101
102 $(lsg_objs) $(liblopsub_objs) $(lopsubex_objs): %.o: %.c
103 lopsubgen.o config_file.o:
104         $(CC) $(CPPFLAGS) $(LLS_CFLAGS) $(CFLAGS) -c -o $@ ${@:.o=.c}
105 lsg1.o: lsg.c lsg.h
106         $(CC) $(CPPFLAGS) $(LLS_CFLAGS) $(STRICT_CFLAGS) $(CFLAGS) -DSTAGE1 -c -o $@ $<
107 %.o: %.c
108         $(CC) -I. $(CPPFLAGS) $(LLS_CFLAGS) $(STRICT_CFLAGS) $(CFLAGS) -c -o $@ $<
109
110 # linking
111 lopsubgen-stage1: $(lsg1_objs)
112         $(CC) -Wall -g $(lsg1_objs) -o $@
113 lopsubgen: $(lsg_objs)
114         $(CC) -Wall -g -o $@ $(lsg_objs)
115 $(REALNAME): $(liblopsub_objs)
116         $(CC) --shared -Wl,-soname,liblopsub.so.$(MAJOR_VERSION) -o $@ $^
117 lopsubex: $(lopsubex_objs) $(REALNAME)
118         $(CC) -Wall -g -o $@ $(lopsubex_objs)
119
120 # web
121 html := $(addprefix web/, $(addsuffix .html, \
122         index lopsub-api lopsubgen.1 lopsubex.1 $(m4_man_pages:.gz=)))
123 www: $(html)
124 web/lopsub-api.html: lopsub.h.m4 web/header.html web/footer.html
125         $(M4) -DOUTPUT_MODE=HTML web/header.html $(gendoc) \
126                 $< web/footer.html > $@
127 web/index.html: web/lopsub.7.html
128         $(LN) -s $(notdir $<) $@
129 web/%.html: %.gz web/header.html
130         $(CP) web/header.html $@
131         $(ZCAT) $< | $(GROFF) -m man -Thtml | sed -e '1,/^<body>/d' >> $@
132
133 install: $(all)
134         $(INSTALL) -d $(DESTDIR)$(PREFIX)/lib $(DESTDIR)$(PREFIX)/include \
135                 $(DESTDIR)$(PREFIX)/share/man/man1 $(DESTDIR)$(PREFIX)/share/man/man5 \
136                 $(DESTDIR)$(PREFIX)/share/man/man7 $(DESTDIR)$(PREFIX)/bin
137         $(INSTALL) -m 644 $(REALNAME) $(DESTDIR)$(PREFIX)/lib
138         $(LN) -s $(REALNAME) $(DESTDIR)$(PREFIX)/lib/$(SONAME)
139         $(LN) -s $(SONAME) $(DESTDIR)$(PREFIX)/lib/$(LINKERNAME)
140         $(INSTALL) -m 755 lopsubgen $(DESTDIR)$(PREFIX)/bin
141         $(INSTALL) -m 644 lopsub.h $(DESTDIR)$(PREFIX)/include
142         $(INSTALL) -m 644 lopsub-internal.h $(DESTDIR)$(PREFIX)/include
143         $(INSTALL) -m 644 lopsubgen.1.gz $(DESTDIR)$(PREFIX)/share/man/man1
144         $(INSTALL) -m 644 lopsub-suite.5.gz $(DESTDIR)$(PREFIX)/share/man/man5
145         $(INSTALL) -m 644 lopsub.7.gz $(DESTDIR)$(PREFIX)/share/man/man7
146
147 clean:
148         $(RM) $(all) $(html) *.o *.man
149 distclean: clean
150         $(RM) *.lsg.c *.lsg.h lopsubgen.c config_file.c lopsubgen-stage1 \
151                 lopsub.h lopsub.7 lopsub-suite.5 version.c
152