Make the build reproducible.
[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 PREFIX ?= /usr/local
13 M4 := m4
14 LN := ln -f
15 LEX := lex
16 RM := rm -f
17 AR := ar
18 GROFF := groff
19 CP := cp
20 INSTALL := install
21 GZIP := gzip -fn9
22 ZCAT := zcat
23
24 DATE_FMT := +%B %Y
25 # To get a reproducible build, we use $(SOURCE_DATE_EPOCH) instead of the
26 # current time if this variable is set.
27 ifdef SOURCE_DATE_EPOCH
28 DATE := $(shell LC_ALL=C date -u -d '@$(SOURCE_DATE_EPOCH)' \
29 '$(DATE_FMT)' 2>/dev/null || LC_ALL=C date -u '$(DATE_FMT)')
30 else
31 DATE := $(shell date '$(DATE_FMT)')
32 endif
33 GIT_VERSION := $(shell ./version-gen.sh)
34 PLAIN_VERSION := $(firstword $(subst -, , $(GIT_VERSION)))
35 MAJOR_VERSION := $(firstword $(subst ., , $(PLAIN_VERSION)))
36 SONAME := liblopsub.so.$(MAJOR_VERSION)
37 REALNAME := liblopsub.so.$(PLAIN_VERSION)
38 LINKERNAME:=liblopsub.so
39
40 m4_man_pages := lopsub-suite.5.gz lopsub.7.gz
41 all := $(m4_man_pages) $(REALNAME) lopsubgen lopsubgen.1.gz \
42 lopsubex lopsubex.1.gz
43 all: $(all)
44
45 # deps
46 lopsubgen.o: lsg.h
47 lopsub.o lsg.o: lopsub.h lopsub-internal.h
48 lsg.o: lopsubgen.lsg.h lopsub-internal.h
49 lopsubex.o: lopsubex.lsg.h lopsub.h
50 config_file.c: lopsub-internal.h lopsub.h
51 version.o: version.c
52
53 # m4 stuff
54 gendoc := gendoc/gendoc.m4
55 %.h: %.h.m4 $(gendoc)
56 $(M4) -DOUTPUT_MODE=C $(gendoc) $< > $@
57 $(m4_man_pages): %.gz: %.m4 version.c
58 $(M4) -DGIT_VERSION=$(GIT_VERSION) -DDATE="$(DATE)" $< | $(GZIP) > $@
59
60 # flex
61 %.c: %.l
62 $(LEX) -o $@ $<
63
64 # lopsubgen
65 lopsubgen.lsg.c lopsubgen.lsg.h: lopsubgen.suite lopsubgen-stage1 \
66 lopsub-internal.h
67 ./lopsubgen-stage1 < $<
68 %.lsg.c: %.suite lopsubgen
69 ./lopsubgen --gen-c < $<
70 %.lsg.h: %.suite lopsubgen
71 ./lopsubgen --gen-header < $<
72 %.1.gz: %.suite lopsubgen
73 ./lopsubgen --gen-man=${@:.gz=} --version-string $(GIT_VERSION) < $<
74 $(GZIP) ${@:.gz=}
75
76 # compiling
77 lsg1_objs := lopsubgen.o lsg1.o version.o
78 lsg_objs := lopsubgen.o lsg.o lopsubgen.lsg.o lopsub.o version.o
79 liblopsub_objs := config_file.o lopsub.o version.o
80 lopsubex_objs := lopsubex.o lopsubex.lsg.o $(liblopsub_objs)
81
82 $(lsg_objs) $(liblopsub_objs) $(lopsubex_objs): %.o: %.c
83 lopsubgen.o config_file.o:
84 $(CC) -g -c -fPIC -o $@ ${@:.o=.c}
85 lsg1.o: lsg.c lsg.h
86 $(CC) -g -DSTAGE1 -Wall -g -c $< -o $@
87 %.o: %.c
88 $(CC) -Wall -I. -g -c -fPIC -o $@ $<
89
90 # linking
91 lopsubgen-stage1: $(lsg1_objs)
92 $(CC) -Wall -g $(lsg1_objs) -o $@
93 lopsubgen: $(lsg_objs)
94 $(CC) -Wall -g -o $@ $(lsg_objs)
95 $(REALNAME): $(liblopsub_objs)
96 $(CC) --shared -Wl,-soname,liblopsub.so.$(MAJOR_VERSION) -o $@ $^
97 lopsubex: $(lopsubex_objs) $(REALNAME)
98 $(CC) -Wall -g -o $@ $(lopsubex_objs)
99
100 # web
101 html := $(addprefix web/, $(addsuffix .html, \
102 index lopsub-api lopsubgen.1 lopsubex.1 $(m4_man_pages)))
103 $(html): $(addprefix web/, $(addsuffix .html, header footer))
104
105 www: $(html)
106
107 web/lopsub-api.html: lopsub.h.m4
108 $(M4) -DOUTPUT_MODE=HTML web/header.html $(gendoc) \
109 $< web/footer.html > $@
110 web/index.html: web/lopsub.7.html
111 $(LN) -s $(notdir $<) $@
112 web/%.html: %.gz
113 $(CP) web/header.html $@
114 $(ZCAT) $< | $(GROFF) -m man -Thtml | sed -e '1,/^<body>/d' >> $@
115
116 install: $(all)
117 $(INSTALL) -d $(DESTDIR)$(PREFIX)/lib $(DESTDIR)$(PREFIX)/include \
118 $(DESTDIR)$(PREFIX)/share/man/man1 $(DESTDIR)$(PREFIX)/share/man/man5 \
119 $(DESTDIR)$(PREFIX)/share/man/man7 $(DESTDIR)$(PREFIX)/bin
120 $(INSTALL) -m 644 $(REALNAME) $(DESTDIR)$(PREFIX)/lib
121 $(LN) -s $(REALNAME) $(DESTDIR)$(PREFIX)/lib/$(SONAME)
122 $(LN) -s $(SONAME) $(DESTDIR)$(PREFIX)/lib/$(LINKERNAME)
123 $(INSTALL) -m 755 lopsubgen $(DESTDIR)$(PREFIX)/bin
124 $(INSTALL) -m 644 lopsub.h $(DESTDIR)$(PREFIX)/include
125 $(INSTALL) -m 644 lopsub-internal.h $(DESTDIR)$(PREFIX)/include
126 $(INSTALL) -m 644 lopsubgen.1.gz $(DESTDIR)$(PREFIX)/share/man/man1
127 $(INSTALL) -m 644 lopsub-suite.5.gz $(DESTDIR)$(PREFIX)/share/man/man5
128 $(INSTALL) -m 644 lopsub.7.gz $(DESTDIR)$(PREFIX)/share/man/man7
129
130 clean:
131 $(RM) $(all) $(html) *.o *.man
132 distclean: clean
133 $(RM) *.lsg.c *.lsg.h lopsubgen.c config_file.c lopsubgen-stage1 \
134 lopsub.h lopsub.7 lopsub-suite.5 version.c
135