]> git.sur5r.net Git - i3/i3/blob - common.mk
common.mk: Rework version usage
[i3/i3] / common.mk
1 UNAME=$(shell uname)
2 DEBUG=1
3 COVERAGE=0
4 INSTALL=install
5 FLEX=flex
6 BISON=bison
7 ifndef PREFIX
8   PREFIX=/usr
9 endif
10 ifndef SYSCONFDIR
11   ifeq ($(PREFIX),/usr)
12     SYSCONFDIR=/etc
13   else
14     SYSCONFDIR=$(PREFIX)/etc
15   endif
16 endif
17
18 I3_VERSION := '$(shell [ -f $(TOPDIR)/VERSION ] && cat $(TOPDIR)/VERSION)'
19 ifeq ('',$(I3_VERSION))
20 VERSION := $(shell git describe --tags --abbrev=0)
21 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/::)\")'
22 else
23 VERSION := ${I3_VERSION}
24 endif
25
26
27 ## Generic flags
28
29 # Default CFLAGS that users should be able to override
30 ifeq ($(DEBUG),1)
31 # Extended debugging flags, macros shall be available in gcc
32 CFLAGS ?= -pipe -gdwarf-2 -g3
33 else
34 CFLAGS ?= -pipe -O2 -freorder-blocks-and-partition
35 endif
36
37 # Default LDFLAGS that users should be able to override
38 LDFLAGS ?= $(as_needed_LDFLAG)
39
40 # Common CFLAGS for all i3 related binaries
41 I3_CFLAGS  = -std=c99
42 I3_CFLAGS += -Wall
43 # unused-function, unused-label, unused-variable are turned on by -Wall
44 # We don’t want unused-parameter because of the use of many callbacks
45 I3_CFLAGS += -Wunused-value
46 I3_CFLAGS += -Iinclude
47
48 I3_CPPFLAGS  = -DI3_VERSION=\"${I3_VERSION}\"
49 I3_CPPFLAGS += -DSYSCONFDIR=\"${SYSCONFDIR}\"
50
51
52 ## Libraries flags
53
54 ifeq ($(shell which pkg-config 2>/dev/null 1>/dev/null || echo 1),1)
55 $(error "pkg-config was not found")
56 endif
57
58 # An easier way to get CFLAGS and LDFLAGS falling back in case there's
59 # no pkg-config support for certain libraries.
60 #
61 # NOTE that you must not use a blank after comma when calling this:
62 #     $(call ldflags_for_lib name, fallback) # bad
63 #     $(call ldflags_for_lib name,fallback) # good
64 # Otherwise, the compiler will get -l foo instead of -lfoo
65 #
66 # We redirect stderr to /dev/null because pkg-config prints an error if support
67 # for gnome-config was enabled but gnome-config is not actually installed.
68 cflags_for_lib = $(shell pkg-config --silence-errors --cflags $(1) 2>/dev/null)
69 ldflags_for_lib = $(shell pkg-config --exists 2>/dev/null $(1) && pkg-config --libs $(1) 2>/dev/null || echo -l$(2))
70
71 # XCB common stuff
72 XCB_CFLAGS  := $(call cflags_for_lib, xcb)
73 XCB_CFLAGS  += $(call cflags_for_lib, xcb-event)
74 XCB_LIBS    := $(call ldflags_for_lib, xcb,xcb)
75 XCB_LIBS    += $(call ldflags_for_lib, xcb-event,xcb-event)
76 ifeq ($(shell pkg-config --exists xcb-util 2>/dev/null || echo 1),1)
77 XCB_CFLAGS  += $(call cflags_for_lib, xcb-atom)
78 XCB_CFLAGS  += $(call cflags_for_lib, xcb-aux)
79 XCB_LIBS    += $(call ldflags_for_lib, xcb-atom,xcb-atom)
80 XCB_LIBS    += $(call ldflags_for_lib, xcb-aux,xcb-aux)
81 else
82 XCB_CFLAGS  += $(call cflags_for_lib, xcb-util)
83 XCB_LIBS    += $(call ldflags_for_lib, xcb-util)
84 endif
85
86 # XCB keyboard stuff
87 XCB_KBD_CFLAGS := $(call cflags_for_lib, xcb-keysyms)
88 XCB_KBD_LIBS   := $(call ldflags_for_lib, xcb-keysyms,xcb-keysyms)
89
90 # XCB WM stuff
91 XCB_WM_CFLAGS := $(call cflags_for_lib, xcb-icccm)
92 XCB_WM_CFLAGS += $(call cflags_for_lib, xcb-xinerama)
93 XCB_WM_CFLAGS += $(call cflags_for_lib, xcb-randr)
94 XCB_WM_LIBS   := $(call ldflags_for_lib, xcb-icccm,xcb-icccm)
95 XCB_WM_LIBS   += $(call ldflags_for_lib, xcb-xinerama,xcb-xinerama)
96 XCB_WM_LIBS   += $(call ldflags_for_lib, xcb-randr,xcb-randr)
97
98 # Xlib
99 X11_CFLAGS := $(call cflags_for_lib, x11)
100 X11_LIBS   := $(call ldflags_for_lib, x11,X11)
101
102 # Xcursor
103 XCURSOR_CFLAGS := $(call cflags_for_lib, xcursor)
104 XCURSOR_LIBS   := $(call ldflags_for_lib, xcursor,Xcursor)
105
106 # yajl
107 YAJL_CFLAGS := $(call cflags_for_lib, yajl)
108 # Fallback for libyajl 1 which did not include yajl_version.h. We need
109 # YAJL_MAJOR from that file to decide which code path should be used.
110 YAJL_CFLAGS += -idirafter $(TOPDIR)/yajl-fallback
111 YAJL_LIBS   := $(call ldflags_for_lib, yajl,yajl)
112
113 #libev
114 LIBEV_CFLAGS := $(call cflags_for_lib, libev)
115 LIBEV_LIBS   := $(call ldflags_for_lib, libev,ev)
116
117 # libpcre
118 PCRE_CFLAGS := $(call cflags_for_lib, libpcre)
119 ifeq ($(shell pkg-config --atleast-version=8.10 libpcre 2>/dev/null && echo 1),1)
120 I3_CPPFLAGS += -DPCRE_HAS_UCP=1
121 endif
122 PCRE_LIBS   := $(call ldflags_for_lib, libpcre,pcre)
123
124 # startup-notification
125 LIBSN_CFLAGS := $(call cflags_for_lib, libstartup-notification-1.0)
126 LIBSN_LIBS   := $(call ldflags_for_lib, libstartup-notification-1.0,startup-notification-1)
127
128 # libi3
129 LIBS = -L$(TOPDIR) -li3
130
131 ## Platform-specific flags
132
133 # Please test if -Wl,--as-needed works on your platform and send me a patch.
134 # it is known not to work on Darwin (Mac OS X)
135 ifneq (,$(filter Linux GNU GNU/%, $(UNAME)))
136 as_needed_LDFLAG = -Wl,--as-needed
137 endif
138
139 ifeq ($(UNAME),NetBSD)
140 # We need -idirafter instead of -I to prefer the system’s iconv over GNU libiconv
141 I3_CFLAGS += -idirafter /usr/pkg/include
142 I3_LDFLAGS += -Wl,-rpath,/usr/local/lib -Wl,-rpath,/usr/pkg/lib
143 endif
144
145 ifeq ($(UNAME),OpenBSD)
146 I3_CFLAGS += -I${X11BASE}/include
147 LIBS += -liconv
148 I3_LDFLAGS += -L${X11BASE}/lib
149 endif
150
151 ifeq ($(UNAME),FreeBSD)
152 LIBS += -liconv
153 endif
154
155 ifeq ($(UNAME),Darwin)
156 LIBS += -liconv
157 else
158 # Darwin (Mac OS X) doesn’t have librt
159 LIBS += -lrt
160 endif
161
162 ifneq (,$(filter Linux GNU GNU/%, $(UNAME)))
163 I3_CPPFLAGS += -D_GNU_SOURCE
164 endif
165
166
167 ifeq ($(COVERAGE),1)
168 I3_CFLAGS += -fprofile-arcs -ftest-coverage
169 LIBS += -lgcov
170 endif
171
172 V ?= 0
173 ifeq ($(V),0)
174 # Don’t print command lines which are run
175 .SILENT:
176
177 # echo-ing vars
178 V_ASCIIDOC = echo ASCIIDOC $@;
179 V_A2X = echo A2X $@;
180 endif
181
182 # Always remake the following targets
183 .PHONY: install clean dist distclean
184