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