]> git.sur5r.net Git - i3/i3/blob - common.mk
1e738b045530eca66c5ee62df869bdc6b7024fe4
[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)/VERSION ] && cat $(TOPDIR)/VERSION)'
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 # yajl
130 YAJL_CFLAGS := $(call cflags_for_lib, yajl)
131 YAJL_LIBS   := $(call ldflags_for_lib, yajl,yajl)
132
133 #libev
134 LIBEV_CFLAGS := $(call cflags_for_lib, libev)
135 LIBEV_LIBS   := $(call ldflags_for_lib, libev,ev)
136
137 # libpcre
138 PCRE_CFLAGS := $(call cflags_for_lib, libpcre)
139 ifeq ($(shell $(PKG_CONFIG) --atleast-version=8.10 libpcre 2>/dev/null && echo 1),1)
140 I3_CPPFLAGS += -DPCRE_HAS_UCP=1
141 endif
142 PCRE_LIBS   := $(call ldflags_for_lib, libpcre,pcre)
143
144 # startup-notification
145 LIBSN_CFLAGS := $(call cflags_for_lib, libstartup-notification-1.0)
146 LIBSN_LIBS   := $(call ldflags_for_lib, libstartup-notification-1.0,startup-notification-1)
147
148 # Pango
149 PANGO_CFLAGS := $(call cflags_for_lib, cairo)
150 PANGO_CFLAGS += $(call cflags_for_lib, pangocairo)
151 I3_CPPFLAGS  += -DPANGO_SUPPORT=1
152 ifeq ($(shell $(PKG_CONFIG) --atleast-version=1.14.4 cairo 2>/dev/null && echo 1),1)
153 I3_CPPFLAGS  += -DCAIRO_SUPPORT=1
154 endif
155 PANGO_LIBS   := $(call ldflags_for_lib, cairo)
156 PANGO_LIBS   += $(call ldflags_for_lib, pangocairo)
157
158 # libi3
159 LIBS = -L$(TOPDIR) -li3 -lm
160
161 ## Platform-specific flags
162
163 # Please test if -Wl,--as-needed works on your platform and send me a patch.
164 # it is known not to work on Darwin (Mac OS X)
165 ifneq (,$(filter Linux GNU GNU/%, $(UNAME)))
166 as_needed_LDFLAG = -Wl,--as-needed
167 endif
168
169 ifeq ($(UNAME),NetBSD)
170 # We need -idirafter instead of -I to prefer the system’s iconv over GNU libiconv
171 I3_CFLAGS += -idirafter /usr/pkg/include
172 I3_LDFLAGS += -Wl,-rpath,/usr/local/lib -Wl,-rpath,/usr/pkg/lib
173 endif
174
175 ifeq ($(UNAME),OpenBSD)
176 I3_CFLAGS += -I${X11BASE}/include
177 LIBS += -liconv
178 I3_LDFLAGS += -L${X11BASE}/lib
179 endif
180
181 ifeq ($(UNAME),FreeBSD)
182 LIBS += -liconv
183 endif
184
185 ifeq ($(UNAME),Darwin)
186 LIBS += -liconv
187 else ifneq ($(UNAME),OpenBSD)
188 # Darwin (Mac OS X) and OpenBSD do not have librt
189 LIBS += -lrt
190 endif
191
192 ifeq ($(UNAME),SunOS)
193 LIBS += -lsocket -liconv -lgen
194 endif
195
196 ifneq (,$(filter Linux GNU GNU/%, $(UNAME)))
197 I3_CPPFLAGS += -D_GNU_SOURCE
198 endif
199
200
201 ifeq ($(COVERAGE),1)
202 I3_CFLAGS += -fprofile-arcs -ftest-coverage
203 LIBS += -lgcov
204 endif
205
206 V ?= 0
207 ifeq ($(V),0)
208 # Don’t print command lines which are run
209 .SILENT:
210
211 # echo-ing vars
212 V_ASCIIDOC = echo ASCIIDOC $@;
213 V_POD2HTML = echo POD2HTML $@;
214 V_POD2MAN = echo POD2MAN $@;
215 V_A2X = echo A2X $@;
216 endif
217
218 # Always remake the following targets
219 .PHONY: install clean dist distclean
220