aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: c6323b0a7bae7f702c29dd3c5a0bae772b112672 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
.POSIX:
DATE         = 1970-01-01
VERSION      = 0.1.0
NAME         = gistatic
NAME_UC      = $(NAME)
## Installation prefix.  Defaults to "/usr".
PREFIX       = /usr
BINDIR       = $(PREFIX)/bin
LIBDIR       = $(PREFIX)/lib
GOLIBDIR     = $(LIBDIR)/go
INCLUDEDIR   = $(PREFIX)/include
SRCDIR       = $(PREFIX)/src/$(NAME)
SHAREDIR     = $(PREFIX)/share
LOCALEDIR    = $(SHAREDIR)/locale
MANDIR       = $(SHAREDIR)/man
EXEC         = ./
## Where to store the installation.  Empty by default.
DESTDIR      =
LDLIBS       = --static
GOCFLAGS     = -I $(GOLIBDIR)
GOLDFLAGS    = -L $(GOLIBDIR)
N            = `nproc`



.SUFFIXES:
.SUFFIXES: .go .a .bin .bin-check .adoc .po .mo

.go.a:
	go tool compile -I $(@D) $(GOCFLAGS)  -o $@ -p $(*F) \
		`find $< $$(if [ $(*F) != main ]; then \
			echo src/$(NAME).go src/meta.go; fi) | uniq`

.a.bin:
	go tool link    -L $(@D) $(GOLDFLAGS) -o $@ --extldflags '$(LDLIBS)' $<

.adoc:
	asciidoctor -b manpage -o $@ $<

.po.mo:
	msgfmt -cfv -o $@ $<



all:
include deps.mk


libs.a           = $(libs.go:.go=.a)
mains.a          = $(mains.go:.go=.a)
mains.bin        = $(mains.go:.go=.bin)
functional/lib.a = $(functional/lib.go:.go=.a)
fuzz/lib.a       = $(fuzz/lib.go:.go=.a)
benchmarks/lib.a = $(benchmarks/lib.go:.go=.a)
manpages.N.adoc  = $(manpages.en.N.adoc) $(manpages.XX.N.adoc)
manpages.N       = $(manpages.N.adoc:.adoc=)
sources.mo       = $(sources.po:.po=.mo)

sources = \
	src/$(NAME).go                 \
	src/meta.go                    \
	src/main.go                    \


derived-assets = \
	src/meta.go                    \
	$(libs.a)                      \
	$(mains.a)                     \
	$(mains.bin)                   \
	$(NAME).bin                    \
	$(manpages.XX.N.adoc)          \
	$(manpages.N)                  \
	$(sources.mo)                  \

side-assets = \
	tests/fuzz/corpus/             \
	tests/benchmarks/*/main.txt    \



## Default target.  Builds all artifacts required for testing
## and installation.
all: $(derived-assets)


$(libs.a): Makefile deps.mk
$(libs.a): src/$(NAME).go src/meta.go


$(fuzz/lib.a):
	go tool compile $(GOCFLAGS) -o $@ -p $(NAME) -d=libfuzzer \
		$*.go src/$(NAME).go src/meta.go

src/meta.go: Makefile
	echo 'package $(NAME)'                   > $@
	echo 'const Version   = "$(VERSION)"'   >> $@
	echo 'const Name      = "$(NAME)"'      >> $@
	echo 'const LOCALEDIR = "$(LOCALEDIR)"' >> $@

$(NAME).bin: src/main.bin
	ln -fs src/main.bin $@

$(manpages.XX.N.adoc): po/doc/po4a.cfg
	po4a --no-update --translate-only $@ po/doc/po4a.cfg



tests.bin-check = \
	tests/main.bin-check \
	$(functional/main.go:.go=.bin-check) \

$(tests.bin-check):
	$(EXEC)$*.bin

check-unit: $(tests.bin-check)


integration-tests = \
	tests/cli-opts.sh              \
	tests/integration.sh           \

.PRECIOUS: $(integration-tests)
$(integration-tests): $(NAME).bin
$(integration-tests): ALWAYS
	sh $@

check-integration: $(integration-tests)
check-integration: fuzz


## Run all tests.  Each test suite is isolated, so that a parallel
## build can run tests at the same time.  The required artifacts
## are created if missing.
check: check-unit check-integration



FUZZSEC=1
fuzz/main.bin-check = $(fuzz/main.go:.go=.bin-check)
$(fuzz/main.bin-check):
	$(EXEC)$*.bin --test.fuzztime=$(FUZZSEC)s --test.parallel=$N \
		--test.fuzz='.*' --test.fuzzcachedir=tests/fuzz/corpus

fuzz: $(fuzz/main.bin-check)



benchmarks/main.bin-check = $(benchmarks/main.go:.go=.bin-check)
$(benchmarks/main.bin-check):
	printf '%s\n' '$(EXEC)$*.bin'            > $*.txt
	LANG=POSIX.UTF-8 time -p $(EXEC)$*.bin 2>> $*.txt
	printf '%s\n' '$*.txt'

bench: $(benchmarks/main.bin-check)



i18n-doc:
	po4a po/doc/po4a.cfg

i18n-code:
	gotext src/$(NAME).go > po/$(NAME)/$(NAME).pot
	po4a po/$(NAME)/po4a.cfg

i18n: i18n-doc i18n-code



## Remove *all* derived artifacts produced during the build.
## A dedicated test asserts that this is always true.
clean:
	rm -rf $(derived-assets) $(side-assets)


## Installs into $(DESTDIR)$(PREFIX).  Its dependency target
## ensures that all installable artifacts are crafted beforehand.
install: all
	mkdir -p \
		'$(DESTDIR)$(BINDIR)'     \
		'$(DESTDIR)$(GOLIBDIR)'   \
		'$(DESTDIR)$(SRCDIR)'     \

	cp $(NAME).bin       '$(DESTDIR)$(BINDIR)'/$(NAME)
	cp src/$(NAME).a     '$(DESTDIR)$(GOLIBDIR)'
	cp $(sources)        '$(DESTDIR)$(SRCDIR)'
	instool              '$(DESTDIR)$(MANDIR)'    install man $(manpages.N)
	instool              '$(DESTDIR)$(LOCALEDIR)' install mo  $(sources.mo)

## Uninstalls from $(DESTDIR)$(PREFIX).  This is a perfect mirror
## of the "install" target, and removes *all* that was installed.
## A dedicated test asserts that this is always true.
uninstall:
	rm -rf \
		'$(DESTDIR)$(BINDIR)'/$(NAME)         \
		'$(DESTDIR)$(GOLIBDIR)'/$(NAME).a     \
		'$(DESTDIR)$(SRCDIR)'                 \

	instool '$(DESTDIR)$(MANDIR)'    uninstall man $(manpages.N)
	instool '$(DESTDIR)$(LOCALEDIR)' uninstall mo  $(sources.mo)



ALWAYS: