]> git.sur5r.net Git - i3/i3/blob - common.mk
Merge pull request #2232 from Airblader/bug-2229
[i3/i3] / common.mk
1 UNAME=$(shell uname)
2 DEBUG=1
3 ASAN=0
4 INSTALL=install
5 LN=ln
6 PKG_CONFIG=pkg-config
7 ifndef PREFIX
8   PREFIX=/usr
9 endif
10 ifndef EXEC_PREFIX
11   EXEC_PREFIX=$(PREFIX)
12 endif
13 ifndef SYSCONFDIR
14   ifeq ($(PREFIX),/usr)
15     SYSCONFDIR=/etc
16   else
17     SYSCONFDIR=$(PREFIX)/etc
18   endif
19 endif
20
21 # In dist tarballs, the version is stored in the I3_VERSION and VERSION files.
22 I3_VERSION := '$(shell [ -f $(TOPDIR)/I3_VERSION ] && cat $(TOPDIR)/I3_VERSION)'
23 VERSION := '$(shell [ -f $(TOPDIR)/VERSION ] && cat $(TOPDIR)/VERSION)'
24 ifeq ('',$(I3_VERSION))
25 VERSION := $(shell git describe --tags --abbrev=0)
26 I3_VERSION := '$(shell git describe --tags --always) ($(shell git log --pretty=format:%cd --date=short -n1), branch \"$(shell git describe --tags --always --all | sed s:heads/::)\")'
27 endif
28
29 MAJOR_VERSION := $(shell echo ${VERSION} | cut -d '.' -f 1)
30 MINOR_VERSION := $(shell echo ${VERSION} | cut -d '.' -f 2)
31 PATCH_VERSION := $(shell echo ${VERSION} | cut -d '.' -f 3)
32 ifeq (${PATCH_VERSION},)
33 PATCH_VERSION := 0
34 endif
35
36 ## Generic flags
37
38 # Default CFLAGS that users should be able to override
39 ifeq ($(DEBUG),1)
40 # Extended debugging flags, macros shall be available in gcc
41 CFLAGS ?= -pipe -gdwarf-2 -g3
42 else
43 CFLAGS ?= -pipe -O2 -freorder-blocks-and-partition
44 endif
45
46 ifeq ($(ASAN),1)
47 CFLAGS += -fsanitize=address -DI3_ASAN_ENABLED
48 LDFLAGS += -fsanitize=address
49 endif
50
51 # Default LDFLAGS that users should be able to override
52 LDFLAGS ?= $(as_needed_LDFLAG)
53
54 # Common CFLAGS for all i3 related binaries
55 I3_CFLAGS  = -std=c99
56 I3_CFLAGS += -Wall
57 # unused-function, unused-label, unused-variable are turned on by -Wall
58 # We don’t want unused-parameter because of the use of many callbacks
59 I3_CFLAGS += -Wunused-value
60 I3_CFLAGS += -Iinclude
61
62 I3_CPPFLAGS  = -DI3_VERSION=\"${I3_VERSION}\"
63 I3_CPPFLAGS += -DMAJOR_VERSION=${MAJOR_VERSION}
64 I3_CPPFLAGS += -DMINOR_VERSION=${MINOR_VERSION}
65 I3_CPPFLAGS += -DPATCH_VERSION=${PATCH_VERSION}
66 I3_CPPFLAGS += -DSYSCONFDIR=\"${SYSCONFDIR}\"
67 I3_CPPFLAGS += -DI3__FILE__=__FILE__
68
69
70 ## Libraries flags
71
72 ifeq ($(shell which $(PKG_CONFIG) 2>/dev/null 1>/dev/null || echo 1),1)
73 $(error "pkg-config was not found")
74 endif
75
76 # An easier way to get CFLAGS and LDFLAGS falling back in case there's
77 # no pkg-config support for certain libraries.
78 #
79 # NOTE that you must not use a blank after comma when calling this:
80 #     $(call ldflags_for_lib name, fallback) # bad
81 #     $(call ldflags_for_lib name,fallback) # good
82 # Otherwise, the compiler will get -l foo instead of -lfoo
83 #
84 # We redirect stderr to /dev/null because pkg-config prints an error if support
85 # for gnome-config was enabled but gnome-config is not actually installed.
86 cflags_for_lib = $(shell $(PKG_CONFIG) --silence-errors --cflags $(1) 2>/dev/null)
87 ldflags_for_lib = $(shell $(PKG_CONFIG) --exists 2>/dev/null $(1) && $(PKG_CONFIG) --libs $(1) 2>/dev/null || echo -l$(2))
88
89 # XCB common stuff
90 XCB_CFLAGS  := $(call cflags_for_lib, xcb)
91 XCB_CFLAGS  += $(call cflags_for_lib, xcb-event)
92 XCB_LIBS    := $(call ldflags_for_lib, xcb,xcb)
93 XCB_LIBS    += $(call ldflags_for_lib, xcb-event,xcb-event)
94 ifeq ($(shell $(PKG_CONFIG) --exists xcb-util 2>/dev/null || echo 1),1)
95 XCB_CFLAGS  += $(call cflags_for_lib, xcb-atom)
96 XCB_CFLAGS  += $(call cflags_for_lib, xcb-aux)
97 XCB_LIBS    += $(call ldflags_for_lib, xcb-atom,xcb-atom)
98 XCB_LIBS    += $(call ldflags_for_lib, xcb-aux,xcb-aux)
99 XCB_CPPFLAGS+= -DXCB_COMPAT
100 else
101 XCB_CFLAGS  += $(call cflags_for_lib, xcb-util)
102 XCB_LIBS    += $(call ldflags_for_lib, xcb-util)
103 endif
104 XCB_XKB_LIBS := $(call ldflags_for_lib, xcb-xkb,xcb-xkb)
105
106 # XCB keyboard stuff
107 XCB_KBD_CFLAGS := $(call cflags_for_lib, xcb-keysyms)
108 XCB_KBD_LIBS   := $(call ldflags_for_lib, xcb-keysyms,xcb-keysyms)
109
110 # XCB WM stuff
111 XCB_WM_CFLAGS := $(call cflags_for_lib, xcb-icccm)
112 XCB_WM_CFLAGS += $(call cflags_for_lib, xcb-xinerama)
113 XCB_WM_CFLAGS += $(call cflags_for_lib, xcb-randr)
114 XCB_WM_LIBS   := $(call ldflags_for_lib, xcb-icccm,xcb-icccm)
115 XCB_WM_LIBS   += $(call ldflags_for_lib, xcb-xinerama,xcb-xinerama)
116 XCB_WM_LIBS   += $(call ldflags_for_lib, xcb-randr,xcb-randr)
117
118 # XCB cursor
119 XCB_CURSOR_CFLAGS := $(call cflags_for_lib, xcb-cursor)
120 XCB_CURSOR_LIBS   := $(call ldflags_for_lib, xcb-cursor,xcb-cursor)
121
122 XKB_COMMON_CFLAGS := $(call cflags_for_lib, xkbcommon,xkbcommon)
123 XKB_COMMON_LIBS := $(call ldflags_for_lib, xkbcommon,xkbcommon)
124 XKB_COMMON_X11_CFLAGS := $(call cflags_for_lib, xkbcommon-x11,xkbcommon-x11)
125 XKB_COMMON_X11_LIBS := $(call ldflags_for_lib, xkbcommon-x11,xkbcommon-x11)
126
127 # yajl
128 YAJL_CFLAGS := $(call cflags_for_lib, yajl)
129 YAJL_LIBS   := $(call ldflags_for_lib, yajl,yajl)
130
131 #libev
132 LIBEV_CFLAGS := $(call cflags_for_lib, libev)
133 LIBEV_LIBS   := $(call ldflags_for_lib, libev,ev)
134
135 # libpcre
136 PCRE_CFLAGS := $(call cflags_for_lib, libpcre)
137 ifeq ($(shell $(PKG_CONFIG) --atleast-version=8.10 libpcre 2>/dev/null && echo 1),1)
138 I3_CPPFLAGS += -DPCRE_HAS_UCP=1
139 endif
140 PCRE_LIBS   := $(call ldflags_for_lib, libpcre,pcre)
141
142 # startup-notification
143 LIBSN_CFLAGS := $(call cflags_for_lib, libstartup-notification-1.0)
144 LIBSN_LIBS   := $(call ldflags_for_lib, libstartup-notification-1.0,startup-notification-1)
145
146 # Pango
147 PANGO_CFLAGS := $(call cflags_for_lib, cairo)
148 PANGO_CFLAGS += $(call cflags_for_lib, pangocairo)
149 I3_CPPFLAGS  += -DPANGO_SUPPORT=1
150 ifeq ($(shell $(PKG_CONFIG) --atleast-version=1.14.4 cairo 2>/dev/null && echo 1),1)
151 I3_CPPFLAGS  += -DCAIRO_SUPPORT=1
152 endif
153 PANGO_LIBS   := $(call ldflags_for_lib, cairo)
154 PANGO_LIBS   += $(call ldflags_for_lib, pangocairo)
155
156 # libi3
157 LIBS = -L$(TOPDIR) -li3 -lm
158
159 ## Platform-specific flags
160
161 # Please test if -Wl,--as-needed works on your platform and send me a patch.
162 # it is known not to work on Darwin (Mac OS X)
163 ifneq (,$(filter Linux GNU GNU/%, $(UNAME)))
164 as_needed_LDFLAG = -Wl,--as-needed
165 endif
166
167 ifeq ($(UNAME),NetBSD)
168 # We need -idirafter instead of -I to prefer the system’s iconv over GNU libiconv
169 I3_CFLAGS += -idirafter /usr/pkg/include
170 I3_LDFLAGS += -Wl,-rpath,/usr/local/lib -Wl,-rpath,/usr/pkg/lib
171 endif
172
173 ifeq ($(UNAME),OpenBSD)
174 I3_CFLAGS += -I${X11BASE}/include
175 LIBS += -liconv
176 I3_LDFLAGS += -L${X11BASE}/lib
177 endif
178
179 ifeq ($(UNAME),FreeBSD)
180 LIBS += -liconv
181 endif
182
183 ifeq ($(UNAME),Darwin)
184 LIBS += -liconv
185 else ifneq ($(UNAME),OpenBSD)
186 # Darwin (Mac OS X) and OpenBSD do not have librt
187 LIBS += -lrt
188 endif
189
190 ifeq ($(UNAME),SunOS)
191 LIBS += -lsocket -liconv -lgen
192 endif
193
194 ifneq (,$(filter Linux GNU GNU/%, $(UNAME)))
195 I3_CPPFLAGS += -D_GNU_SOURCE
196 endif
197
198
199 ifeq ($(COVERAGE),1)
200 I3_CFLAGS += -fprofile-arcs -ftest-coverage
201 LIBS += -lgcov
202 endif
203
204 V ?= 0
205 ifeq ($(V),0)
206 # Don’t print command lines which are run
207 .SILENT:
208
209 # echo-ing vars
210 V_ASCIIDOC = echo ASCIIDOC $@;
211 V_POD2HTML = echo POD2HTML $@;
212 V_POD2MAN = echo POD2MAN $@;
213 V_A2X = echo A2X $@;
214 endif
215
216 # Always remake the following targets
217 .PHONY: install clean dist distclean
218