1CFLAGS ?= -g -O2 -funroll-loops -ftree-vectorize
2CFLAGS += -std=gnu99 -Wall -Wextra -pedantic
3LIBS=-lm -lrt
4PREFIX=/usr/local
5BINDIR=$(PREFIX)/bin
6MAN1DIR=$(PREFIX)/share/man/man1
7
8SRCS=ioping.c
9BINARY=ioping
10MANS=ioping.1
11MANS_F=$(MANS:.1=.txt) $(MANS:.1=.pdf)
12DOCS=README.md LICENSE changelog
13SPEC=ioping.spec
14
15PACKAGE=ioping
16GIT_VER:=$(shell test -d .git && git describe --tags --match 'v[0-9]*' \
17		--abbrev=0 | sed 's/v//')
18SRC_VER:=$(shell sed -ne 's/\# define VERSION \"\(.*\)\"/\1/p' ioping.c)
19EXTRA_VERSION:=$(shell test -d .git && git describe --tags --match 'v[0-9]*' \
20		--dirty=+ | sed 's/^v[^-]*//;s/-/./g')
21VERSION:=$(SRC_VER)$(EXTRA_VERSION)
22DISTDIR=$(PACKAGE)-$(VERSION)
23DISTFILES=$(SRCS) $(MANS) $(DOCS) $(SPEC) Makefile
24PACKFILES=$(BINARY) $(MANS) $(MANS_F) $(DOCS)
25CPPFLAGS+=-DEXTRA_VERSION=\"${EXTRA_VERSION}\"
26
27STRIP=strip
28TARGET=$(shell ${CC} -dumpmachine)
29
30ifneq (,$(findstring -apple-,${TARGET}))
31LIBS=-lm
32endif
33
34ifdef MINGW
35CC=i686-w64-mingw32-gcc
36STRIP=i686-w64-mingw32-strip
37TARGET=win32
38BINARY:=$(BINARY:=.exe)
39LIBS=-lm
40endif
41
42all: checkver $(BINARY)
43
44version: checkver
45	@echo ${VERSION}
46
47checkver:
48	@if test -n "$(GIT_VER)" -a "$(GIT_VER)" != "$(SRC_VER)"; then \
49		echo "ERROR: Version mismatch between git and source"; \
50		echo git: $(GIT_VER), src: $(SRC_VER); \
51		exit 1; \
52	fi
53
54clean:
55	$(RM) -f $(BINARY) $(MANS_F) ioping.tmp
56
57strip: $(BINARY)
58	$(STRIP) $^
59
60test: $(BINARY)
61	./$(BINARY) -i 10ms -c 3 -s 512 -S 16k ${PWD}
62	./$(BINARY) -w 10ms -R -k -S 1m .
63	./$(BINARY) -w 10ms -RL ioping.tmp
64	rm ioping.tmp
65
66install: $(BINARY) $(MANS)
67	mkdir -p $(DESTDIR)$(BINDIR)
68	install -m 0755 $(BINARY) $(DESTDIR)$(BINDIR)
69	mkdir -p $(DESTDIR)$(MAN1DIR)
70	install -m 644 $(MANS) $(DESTDIR)$(MAN1DIR)
71
72%.ps: %.1
73	man -t ./$< > $@
74
75%.pdf: %.ps
76	ps2pdf $< $@
77
78%.txt: %.1
79	MANWIDTH=80 man ./$< | col -b > $@
80
81$(BINARY): $(SRCS)
82	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LIBS)
83
84dist: checkver $(DISTFILES)
85	tar -cz --transform='s,^,$(DISTDIR)/,S' $^ -f $(DISTDIR).tar.gz
86
87binary-tgz: checkver $(PACKFILES)
88	${STRIP} ${BINARY}
89	tar -cz --transform='s,^,$(DISTDIR)/,S' -f ${PACKAGE}-${VERSION}-${TARGET}.tgz $^
90
91binary-zip: checkver $(PACKFILES)
92	${STRIP} ${BINARY}
93	ln -s . $(DISTDIR)
94	zip ${PACKAGE}-${VERSION}-${TARGET}.zip $(addprefix $(DISTDIR)/,$^)
95	rm $(DISTDIR)
96
97.PHONY: all version checkver clean strip test install dist binary-tgz binary-zip
98