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