]> Sergey Matveev's repositories - nnn.git/blob - Makefile
Clear less'es screen
[nnn.git] / Makefile
1 VERSION = $(shell grep -m1 VERSION $(SRC) | cut -f 2 -d'"')
2
3 PREFIX ?= /usr/local
4 MANPREFIX ?= $(PREFIX)/share/man
5 DESKTOPPREFIX ?= $(PREFIX)/share/applications
6 DESKTOPICONPREFIX ?= $(PREFIX)/share/icons/hicolor
7 STRIP ?= strip
8 PKG_CONFIG ?= pkg-config
9 INSTALL ?= install
10 CP ?= cp
11
12 CFLAGS_OPTIMIZATION ?= -O3
13
14 O_DEBUG := 0  # debug binary
15 O_NORL := 0  # no readline support
16 O_PCRE := 0  # link with PCRE library
17 O_NOLC := 0  # no locale support
18 O_NOMOUSE := 0  # no mouse support
19 O_NOBATCH := 0  # no built-in batch renamer
20 O_NOFIFO := 0  # no FIFO previewer support
21 O_CTX8 := 0  # enable 8 contexts
22 O_ICONS := 0  # support icons-in-terminal
23 O_NERD := 0  # support icons-nerdfont
24 O_EMOJI := 0  # support emoji
25 O_QSORT := 0  # use Alexey Tourbin's QSORT implementation
26 O_BENCH := 0  # benchmark mode (stops at first user input)
27 O_NOSSN := 0  # disable session support
28 O_NOUG := 0  # disable user, group name in status bar
29 O_NOX11 := 0  # disable X11 integration
30 O_MATCHFLTR := 0  # allow filters without matches
31 O_NOSORT := 0  # disable sorting entries on dir load
32
33 # User patches
34 O_COLEMAK := 0 # change key bindings to colemak compatible layout
35 O_GITSTATUS := 0 # add git status to detail view
36 O_NAMEFIRST := 0 # print file name first, add uid and guid to detail view
37 O_RESTOREPREVIEW := 0 # add preview pipe to close and restore preview pane
38
39 # convert targets to flags for backwards compatibility
40 ifneq ($(filter debug,$(MAKECMDGOALS)),)
41         O_DEBUG := 1
42 endif
43 ifneq ($(filter norl,$(MAKECMDGOALS)),)
44         O_NORL := 1
45 endif
46 ifneq ($(filter nolc,$(MAKECMDGOALS)),)
47         O_NORL := 1
48         O_NOLC := 1
49 endif
50
51 ifeq ($(strip $(O_DEBUG)),1)
52         CPPFLAGS += -DDEBUG
53         CFLAGS += -g3
54 endif
55
56 ifeq ($(strip $(O_NORL)),1)
57         CPPFLAGS += -DNORL
58 else ifeq ($(strip $(O_STATIC)),1)
59         CPPFLAGS += -DNORL
60 else
61         LDLIBS += -lreadline
62 endif
63
64 ifeq ($(strip $(O_PCRE)),1)
65         CPPFLAGS += -DPCRE
66         LDLIBS += -lpcre
67 endif
68
69 ifeq ($(strip $(O_NOLC)),1)
70         ifeq ($(strip $(O_ICONS)),1)
71 $(info *** Ignoring O_NOLC since O_ICONS is set ***)
72         else ifeq ($(strip $(O_NERD)),1)
73 $(info *** Ignoring O_NOLC since O_NERD is set ***)
74         else ifeq ($(strip $(O_EMOJI)),1)
75 $(info *** Ignoring O_NOLC since O_EMOJI is set ***)
76         else
77                 CPPFLAGS += -DNOLC
78         endif
79 endif
80
81 ifeq ($(strip $(O_NOMOUSE)),1)
82         CPPFLAGS += -DNOMOUSE
83 endif
84
85 ifeq ($(strip $(O_NOBATCH)),1)
86         CPPFLAGS += -DNOBATCH
87 endif
88
89 ifeq ($(strip $(O_NOFIFO)),1)
90         CPPFLAGS += -DNOFIFO
91 endif
92
93 ifeq ($(strip $(O_CTX8)),1)
94         CPPFLAGS += -DCTX8
95 endif
96
97 ifeq ($(strip $(O_ICONS)),1)
98         ICONS_INCLUDE = icons-generated-icons-in-term.h
99         CPPFLAGS += -DICONS_IN_TERM -DICONS_INCLUDE=\"$(ICONS_INCLUDE)\"
100 endif
101
102 ifeq ($(strip $(O_NERD)),1)
103         ICONS_INCLUDE = icons-generated-nerd.h
104         CPPFLAGS += -DNERD -DICONS_INCLUDE=\"$(ICONS_INCLUDE)\"
105 endif
106
107 ifeq ($(strip $(O_EMOJI)),1)
108         ICONS_INCLUDE = icons-generated-emoji.h
109         CPPFLAGS += -DEMOJI -DICONS_INCLUDE=\"$(ICONS_INCLUDE)\"
110 endif
111
112 ifeq ($(strip $(O_QSORT)),1)
113         CPPFLAGS += -DTOURBIN_QSORT
114 endif
115
116 ifeq ($(strip $(O_BENCH)),1)
117         CPPFLAGS += -DBENCH
118 endif
119
120 ifeq ($(strip $(O_NOSSN)),1)
121         CPPFLAGS += -DNOSSN
122 endif
123
124 ifeq ($(strip $(O_NOUG)),1)
125         CPPFLAGS += -DNOUG
126 endif
127
128 ifeq ($(strip $(O_NOX11)),1)
129         CPPFLAGS += -DNOX11
130 endif
131
132 ifeq ($(strip $(O_MATCHFLTR)),1)
133         CPPFLAGS += -DMATCHFLTR
134 endif
135
136 ifeq ($(strip $(O_NOSORT)),1)
137         CPPFLAGS += -DNOSORT
138 endif
139
140 ifeq ($(shell $(PKG_CONFIG) ncursesw && echo 1),1)
141         CFLAGS_CURSES ?= $(shell $(PKG_CONFIG) --cflags ncursesw)
142         LDLIBS_CURSES ?= $(shell $(PKG_CONFIG) --libs   ncursesw)
143 else ifeq ($(shell $(PKG_CONFIG) ncurses && echo 1),1)
144         CFLAGS_CURSES ?= $(shell $(PKG_CONFIG) --cflags ncurses)
145         LDLIBS_CURSES ?= $(shell $(PKG_CONFIG) --libs   ncurses)
146 else
147         LDLIBS_CURSES ?= -lncurses
148 endif
149
150 CFLAGS += -std=c11 -Wall -Wextra -Wshadow
151 CFLAGS += $(CFLAGS_OPTIMIZATION)
152 CFLAGS += $(CFLAGS_CURSES)
153
154 LDLIBS += $(LDLIBS_CURSES) -lpthread
155
156 # static compilation needs libgpm development package
157 ifeq ($(strip $(O_STATIC)),1)
158         LDFLAGS += -static
159         LDLIBS += -lgpm
160 endif
161
162 DISTFILES = src nnn.1 Makefile README.md LICENSE
163 SRC = src/nnn.c
164 HEADERS = src/nnn.h
165 BIN = nnn
166 DESKTOPFILE = misc/desktop/nnn.desktop
167 LOGOSVG = misc/logo/logo.svg
168 LOGO64X64 = misc/logo/logo-64x64.png
169
170 COLEMAK = patches/colemak
171 GITSTATUS = patches/gitstatus
172 NAMEFIRST = patches/namefirst
173 RESTOREPREVIEW = patches/restorepreview
174
175 # test if we are on Mac OS X and get X.Y.Z OS version with system binary /usr/bin/sw_vers
176 MACOS_VERSION := $(strip $(shell command -v sw_vers >/dev/null && [ "`sw_vers -productName`" = "Mac OS X" ] && sw_vers -productVersion))
177 # if Mac OS X detected, test if its version is below 10.12.0 relying on "sort -c" returning "disorder" message if the input is not sorted
178 ifneq ($(MACOS_VERSION),)
179         MACOS_BELOW_1012 := $(if $(strip $(shell printf '10.12.0\n%s' "$(MACOS_VERSION)" | sort -ct. -k1,1n -k2,2n -k3,3n 2>&1)),1)
180 endif
181 # if Mac OS X version is below 10.12.0, compile in the replacement clock_gettime and define MACOS_BELOW_1012 so that it's included in nnn.c
182 ifneq ($(MACOS_BELOW_1012),)
183         GETTIME_C = misc/macos-legacy/mach_gettime.c
184         GETTIME_H = misc/macos-legacy/mach_gettime.h
185         SRC += $(GETTIME_C)
186         HEADERS += $(GETTIME_H)
187         CPPFLAGS += -DMACOS_BELOW_1012
188 endif
189
190 ifeq ($(strip $(O_DEBUG)),1)
191         HEADERS += src/dbg.h
192 endif
193 ifeq ($(strip $(O_QSORT)),1)
194         HEADERS += src/qsort.h
195 endif
196 ifeq ($(strip $(O_EMOJI)),1)
197         HEADERS += src/icons.h src/$(ICONS_INCLUDE)
198 endif
199 ifeq ($(strip $(O_NERD)),1)
200         HEADERS += src/icons.h src/$(ICONS_INCLUDE)
201 endif
202 ifeq ($(strip $(O_ICONS)),1)
203         HEADERS += src/icons.h src/$(ICONS_INCLUDE) src/icons-in-terminal.h
204 endif
205
206 all: $(BIN)
207
208 $(BIN): $(SRC) $(HEADERS) Makefile
209         @$(MAKE) --silent prepatch
210         $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(GETTIME_C) $< $(LDLIBS)
211         @$(MAKE) --silent postpatch
212
213 # targets for backwards compatibility
214 debug: $(BIN)
215 norl: $(BIN)
216 nolc: $(BIN)
217
218 src/$(ICONS_INCLUDE): src/icons-hash.c src/icons.h src/icons-in-terminal.h
219         $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -DICONS_GENERATE -o src/icons-hash-gen src/icons-hash.c
220         ./src/icons-hash-gen > $@
221
222 install-desktop: $(DESKTOPFILE)
223         $(INSTALL) -m 0755 -d $(DESTDIR)$(DESKTOPPREFIX)
224         $(INSTALL) -m 0644 $(DESKTOPFILE) $(DESTDIR)$(DESKTOPPREFIX)
225         $(INSTALL) -m 0755 -d $(DESTDIR)$(DESKTOPICONPREFIX)/scalable/apps
226         $(INSTALL) -m 0644 $(LOGOSVG) $(DESTDIR)$(DESKTOPICONPREFIX)/scalable/apps/nnn.svg
227         $(INSTALL) -m 0755 -d $(DESTDIR)$(DESKTOPICONPREFIX)/64x64/apps
228         $(INSTALL) -m 0644 $(LOGO64X64) $(DESTDIR)$(DESKTOPICONPREFIX)/64x64/apps/nnn.png
229
230 uninstall-desktop:
231         $(RM) $(DESTDIR)$(DESKTOPPREFIX)/$(DESKTOPFILE)
232         $(RM) $(DESTDIR)$(DESKTOPICONPREFIX)/scalable/apps/nnn.svg
233         $(RM) $(DESTDIR)$(DESKTOPICONPREFIX)/64x64/apps/nnn.png
234
235 install: all
236         $(INSTALL) -m 0755 -d $(DESTDIR)$(PREFIX)/bin
237         $(INSTALL) -m 0755 $(BIN) $(DESTDIR)$(PREFIX)/bin
238         $(INSTALL) -m 0755 -d $(DESTDIR)$(MANPREFIX)/man1
239         $(INSTALL) -m 0644 $(BIN).1 $(DESTDIR)$(MANPREFIX)/man1
240
241 uninstall:
242         $(RM) $(DESTDIR)$(PREFIX)/bin/$(BIN)
243         $(RM) $(DESTDIR)$(MANPREFIX)/man1/$(BIN).1
244
245 strip: $(BIN)
246         $(STRIP) $^
247
248 upx: $(BIN)
249         $(STRIP) $^
250         upx -qqq $^
251
252 static:
253         # regular static binary
254         make O_STATIC=1 strip
255         mv $(BIN) $(BIN)-static
256         # static binary with icons-in-terminal support
257         make O_STATIC=1 O_ICONS=1 strip
258         mv $(BIN) $(BIN)-icons-static
259         # static binary with patched nerd font support
260         make O_STATIC=1 O_NERD=1 strip
261         mv $(BIN) $(BIN)-nerd-static
262         # static binary with emoji support
263         make O_STATIC=1 O_EMOJI=1 strip
264         mv $(BIN) $(BIN)-emoji-static
265
266 musl:
267         cp misc/musl/musl-static-ubuntu.sh .
268         ./musl-static-ubuntu.sh 1
269         rm ./musl-static-ubuntu.sh
270
271 shellcheck:
272         find ./plugins/ -type f -not -name "*.md" -exec shellcheck {} +
273
274 dist:
275         mkdir -p nnn-$(VERSION)
276         $(CP) -r $(DISTFILES) nnn-$(VERSION)
277         tar -cf - nnn-$(VERSION) | gzip > nnn-$(VERSION).tar.gz
278         $(RM) -r nnn-$(VERSION)
279
280 sign:
281         git archive -o nnn-$(VERSION).tar.gz --format tar.gz --prefix=nnn-$(VERSION)/ v$(VERSION)
282         gpg --detach-sign --yes nnn-$(VERSION).tar.gz
283         rm -f nnn-$(VERSION).tar.gz
284
285 upload-local: sign static musl
286         $(eval ID=$(shell curl -s 'https://api.github.com/repos/jarun/nnn/releases/tags/v$(VERSION)' | jq .id))
287         # upload sign file
288         curl -XPOST 'https://uploads.github.com/repos/jarun/nnn/releases/$(ID)/assets?name=nnn-$(VERSION).tar.gz.sig' \
289             -H 'Authorization: token $(NNN_SIG_UPLOAD_TOKEN)' -H 'Content-Type: application/pgp-signature' \
290             --upload-file nnn-$(VERSION).tar.gz.sig
291         # upx compress all static binaries
292         upx -qqq $(BIN)-static
293         upx -qqq $(BIN)-icons-static
294         upx -qqq $(BIN)-nerd-static
295         upx -qqq $(BIN)-emoji-static
296         # upload static binary
297         tar -zcf $(BIN)-static-$(VERSION).x86_64.tar.gz $(BIN)-static
298         curl -XPOST 'https://uploads.github.com/repos/jarun/nnn/releases/$(ID)/assets?name=$(BIN)-static-$(VERSION).x86_64.tar.gz' \
299             -H 'Authorization: token $(NNN_SIG_UPLOAD_TOKEN)' -H 'Content-Type: application/x-sharedlib' \
300             --upload-file $(BIN)-static-$(VERSION).x86_64.tar.gz
301         # upload icons-in-terminal compiled static binary
302         tar -zcf $(BIN)-icons-static-$(VERSION).x86_64.tar.gz $(BIN)-icons-static
303         curl -XPOST 'https://uploads.github.com/repos/jarun/nnn/releases/$(ID)/assets?name=$(BIN)-icons-static-$(VERSION).x86_64.tar.gz' \
304             -H 'Authorization: token $(NNN_SIG_UPLOAD_TOKEN)' -H 'Content-Type: application/x-sharedlib' \
305             --upload-file $(BIN)-icons-static-$(VERSION).x86_64.tar.gz
306         # upload patched nerd font compiled static binary
307         tar -zcf $(BIN)-nerd-static-$(VERSION).x86_64.tar.gz $(BIN)-nerd-static
308         curl -XPOST 'https://uploads.github.com/repos/jarun/nnn/releases/$(ID)/assets?name=$(BIN)-nerd-static-$(VERSION).x86_64.tar.gz' \
309             -H 'Authorization: token $(NNN_SIG_UPLOAD_TOKEN)' -H 'Content-Type: application/x-sharedlib' \
310             --upload-file $(BIN)-nerd-static-$(VERSION).x86_64.tar.gz
311         # upload emoji compiled static binary
312         tar -zcf $(BIN)-emoji-static-$(VERSION).x86_64.tar.gz $(BIN)-emoji-static
313         curl -XPOST 'https://uploads.github.com/repos/jarun/nnn/releases/$(ID)/assets?name=$(BIN)-emoji-static-$(VERSION).x86_64.tar.gz' \
314             -H 'Authorization: token $(NNN_SIG_UPLOAD_TOKEN)' -H 'Content-Type: application/x-sharedlib' \
315             --upload-file $(BIN)-emoji-static-$(VERSION).x86_64.tar.gz
316         # upload musl static binary
317         tar -zcf $(BIN)-musl-static-$(VERSION).x86_64.tar.gz $(BIN)-musl-static
318         curl -XPOST 'https://uploads.github.com/repos/jarun/nnn/releases/$(ID)/assets?name=$(BIN)-musl-static-$(VERSION).x86_64.tar.gz' \
319             -H 'Authorization: token $(NNN_SIG_UPLOAD_TOKEN)' -H 'Content-Type: application/x-sharedlib' \
320             --upload-file $(BIN)-musl-static-$(VERSION).x86_64.tar.gz
321
322 clean:
323         $(RM) -f $(BIN) nnn-$(VERSION).tar.gz *.sig $(BIN)-static $(BIN)-static-$(VERSION).x86_64.tar.gz $(BIN)-icons-static $(BIN)-icons-static-$(VERSION).x86_64.tar.gz $(BIN)-nerd-static $(BIN)-nerd-static-$(VERSION).x86_64.tar.gz $(BIN)-emoji-static $(BIN)-emoji-static-$(VERSION).x86_64.tar.gz $(BIN)-musl-static $(BIN)-musl-static-$(VERSION).x86_64.tar.gz src/icons-hash-gen src/icons-generated-*.h
324
325 checkpatches:
326         ./patches/check-patches.sh
327
328 prepatch:
329 ifeq ($(strip $(O_NAMEFIRST)),1)
330         patch --forward $(PATCH_OPTS) --strip=1 --input=$(NAMEFIRST)/mainline.diff
331 ifeq ($(strip $(O_GITSTATUS)),1)
332         patch --forward $(PATCH_OPTS) --strip=1 --input=$(GITSTATUS)/namefirst.diff
333 endif
334 else ifeq ($(strip $(O_GITSTATUS)),1)
335         patch --forward $(PATCH_OPTS) --strip=1 --input=$(GITSTATUS)/mainline.diff
336 endif
337 ifeq ($(strip $(O_RESTOREPREVIEW)),1)
338         patch --forward $(PATCH_OPTS) --strip=1 --input=$(RESTOREPREVIEW)/mainline.diff
339 endif
340 ifeq ($(strip $(O_COLEMAK)),1)
341         patch --forward $(PATCH_OPTS) --strip=1 --input=$(COLEMAK)/mainline.diff
342 endif
343
344 postpatch:
345 ifeq ($(strip $(O_NAMEFIRST)),1)
346 ifeq ($(strip $(O_GITSTATUS)),1)
347         patch --reverse $(PATCH_OPTS) --strip=1 --input=$(GITSTATUS)/namefirst.diff
348 endif
349         patch --reverse $(PATCH_OPTS) --strip=1 --input=$(NAMEFIRST)/mainline.diff
350 else ifeq ($(strip $(O_GITSTATUS)),1)
351         patch --reverse $(PATCH_OPTS) --strip=1 --input=$(GITSTATUS)/mainline.diff
352 endif
353 ifeq ($(strip $(O_RESTOREPREVIEW)),1)
354         patch --reverse $(PATCH_OPTS) --strip=1 --input=$(RESTOREPREVIEW)/mainline.diff
355 endif
356 ifeq ($(strip $(O_COLEMAK)),1)
357         patch --reverse $(PATCH_OPTS) --strip=1 --input=$(COLEMAK)/mainline.diff
358 endif
359
360 skip: ;
361
362 .PHONY: all install uninstall strip static dist sign upload-local clean install-desktop uninstall-desktop