Makefile.common 2.81 KB
Newer Older
1
UNAME = $(shell uname)
2
GITVER = $(shell git describe --always --dirty)
3 4 5 6 7
VERSION = $(GITVER)

ifeq ($(VERSION),)
# mw.rev is created after an export so we can rely on it to provide the version here
VERSION = $(shell cat $(SRCROOT)/mw.rev)
8
endif
9 10 11

MWVERSION = mw3-$(VERSION)

Andrew Price's avatar
Andrew Price committed
12 13 14
# Build with `make RELEASE_BUILD=1` to disable debugging features and enable optimisation
RELEASE_BUILD ?= 0

Andrew Price's avatar
Andrew Price committed
15
prefix ?= /usr
16 17 18 19
libdir ?= $(prefix)/lib
bindir ?= $(prefix)/bin
datadir ?= $(prefix)/share
localstatedir ?= /var
20
initddir ?= /etc/init.d
21
sysconfdir ?= /etc
22

23 24 25 26 27 28 29 30 31
LOGDIR := $(localstatedir)/log/mw
MSGDIR := $(localstatedir)/run/mw
STATEDIR := $(localstatedir)/lib/mw
HOMEPATH := $(libdir)/mw

GCCMAJOR := $(echo __GNUC__ | $compiler -E -xc - | tail -n 1)
GCCMINOR := $(echo __GNUC_MINOR__ | $compiler -E -xc - | tail -n 1)
GCCVER := $(shell printf "%02d%02d" $(GCCMAJOR) $(GCCMINOR))

Andrew Price's avatar
Andrew Price committed
32 33 34 35 36 37 38 39 40 41 42
WARNINGS = \
	-Wall \
	-Wshadow \
	-Wmissing-prototypes \
	-Wwrite-strings \
	-Wcast-align \
	-Wbad-function-cast \
	-Wmissing-format-attribute \
	-Wformat=2 \
	-Wformat-security \
	-Wformat-nonliteral \
Andrew Price's avatar
Andrew Price committed
43
	-Wtype-limits \
Andrew Price's avatar
Andrew Price committed
44 45
	-Wno-long-long \
	-Wno-strict-aliasing \
46 47

# until gcc catches up (4.7.x is good)
Andrew Price's avatar
Andrew Price committed
48
WARNINGS += $(shell if [ $(GCCVER) -lt 0406 ] ; then echo "-Wno-multichar"; fi )
49

50 51
CCSEC = -fpie -fstack-protector-all
LDSEC = -pie -Wl,-z,relro,-z,now
52 53 54

# info strings, do not edit.
DEFS:= -DBUILD_DATE=\"$(shell date +%Y%m%d)\"
55
DEFS+= -DBUILD_USER=\"$(shell whoami)\"
56
DEFS+= -DVERSION=\"$(VERSION)\"
57 58 59 60
DEFS+= -DHOMEPATH=\"$(HOMEPATH)\"
DEFS+= -DLOGDIR=\"$(LOGDIR)\"
DEFS+= -DSTATEDIR=\"$(STATEDIR)\"
DEFS+= -DMSGDIR=\"$(MSGDIR)\"
61
DEFS+= -DSYSCONFDIR=\"$(sysconfdir)\"
62

Andrew Price's avatar
Andrew Price committed
63
DEFS+= -D_GNU_SOURCE
64

Andrew Price's avatar
Andrew Price committed
65 66 67 68 69 70 71
# Set debugging and optimisation features depending on the build type
MWLDFLAGS += $(LDSEC)
ifneq ($(RELEASE_BUILD),0)
MWLDFLAGS += -O2
else
MWLDFLAGS += -O0
endif
72

Andrew Price's avatar
Andrew Price committed
73
MWCFLAGS = -std=gnu99 -g $(DEFS) $(CCSEC) $(WARNINGS)
74 75 76
ifeq ($(UNAME),FreeBSD)
MWCFLAGS += -I/usr/local/include
MWLDFLAGS = -L/usr/local/lib
Andrew Price's avatar
Andrew Price committed
77 78
else
MWLDFLAGS += -Wl,--as-needed
79
endif
Andrew Price's avatar
Andrew Price committed
80 81 82 83 84
ifneq ($(RELEASE_BUILD),0)
MWCFLAGS += -O2
# This requires optimisation so add it here instead of CCSEC
MWCFLAGS += -D_FORTIFY_SOURCE=2
else
85
MWCFLAGS += -O0 -Werror -Wno-error=suggest-attribute=format
Andrew Price's avatar
Andrew Price committed
86
endif
87

Andrew Price's avatar
Andrew Price committed
88 89
# Let the user add some flags at the end
CFLAGS_APPEND =
Andrew Price's avatar
Andrew Price committed
90
LDFLAGS_APPEND =
91

Andrew Price's avatar
Andrew Price committed
92
CFLAGS = $(MWCFLAGS) $(CFLAGS_APPEND)
Andrew Price's avatar
Andrew Price committed
93
LDFLAGS = $(MWLDFLAGS) $(LDFLAGS_APPEND)
Andrew Price's avatar
Andrew Price committed
94

95 96 97 98 99
CODE=$(wildcard *.c)
HDRS=$(wildcard *.h)

all: build

Andrew Price's avatar
Andrew Price committed
100 101 102
# Using -MMD -MF <filename> outputs make rules for the required .o into
# <filename> with automatically generated dependencies on the .c file and the
# local headers included by the .c file
103
%.o: %.c
Andrew Price's avatar
Andrew Price committed
104
	$(CC) $(CFLAGS) -MMD -MF .$(@:.o=.d) -c -o $@ $<
105

Andrew Price's avatar
Andrew Price committed
106 107 108
# The generated rule for the requested .o file is then included, dependencies
# are checked and the rule is executed without the -M* options.
-include $(CODE:%.c=.%.d)
109

110
.PHONY: build install clean test all testclean
111