aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Makefile
blob: d177e272fb51457c5c29cefeb925f9e249b26a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# Copyright 2007-2022 Mitchell. See LICENSE.

# Base flags and options.

CFLAGS := -Os
CXXFLAGS := -Os -std=c++17
WGET = wget -O $@

# Define these once for speed and reference them later.
ifdef GTK2
  gtk_flags_linux := $(shell pkg-config --cflags gtk+-2.0 gmodule-2.0)
else
  gtk_flags_linux := $(shell pkg-config --cflags gtk+-3.0 gmodule-2.0) -Wno-deprecated-declarations
endif
glib_flags_linux := $(shell pkg-config --cflags glib-2.0)
curses_flags_linux := $(shell pkg-config --silence-errors --cflags ncursesw || \
  pkg-config --silence-errors --cflags ncurses || pkg-config --silence-errors --cflags curses)
gtk_flags_win := $(shell PKG_CONFIG_PATH=`pwd`/gtkwin/lib/pkgconfig \
  pkg-config --define-variable=prefix=gtkwin --cflags gtk+-3.0)
glib_flags_win := $(shell PKG_CONFIG_PATH=`pwd`/gtkwin/lib/pkgconfig \
  pkg-config --define-variable=prefix=gtkwin --cflags glib-2.0)
gtk_flags_osx := $(shell PKG_CONFIG_PATH=`pwd`/gtkosx/lib/pkgconfig \
  pkg-config --define-variable=prefix=gtkosx --cflags gtk+-3.0)
glib_flags_osx := $(shell PKG_CONFIG_PATH=`pwd`/gtkosx/lib/pkgconfig \
  pkg-config --define-variable=prefix=gtkosx --cflags glib-2.0)

# Utility functions.

objs = $(addsuffix .o, $(filter-out $(2), $(basename $(notdir $(wildcard $(1))))))
curses-objs = $(addsuffix -curses.o, $(basename $(notdir $(1))))
win-objs = $(addprefix win-, $(1))
osx-objs = $(addprefix osx-, $(1))
all-objs = $(1) $(call win-objs, $(1)) $(call osx-objs, $(1))

define build-cxx =
  $(CXX) $(CXXFLAGS) -c $< -o $@
endef
define build-cc =
  $(CC) $(CFLAGS) -c $< -o $@
endef
define build-exe =
  $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)
endef

# Textadept.

textadept_gtk_objs := $(call objs, textadept.c)
textadept_curses_objs := $(call curses-objs, textadept.c)
textadept_objs := $(textadept_gtk_objs) $(textadept_curses_objs)
textadept_rc_objs := textadept_rc.o

ta_flags := -std=c99 -pedantic -Iscintilla/include -Igtdialog -Wall

$(call all-objs, $(textadept_objs)): CFLAGS += $(lua_flags) $(ta_flags)
$(call all-objs, $(textadept_gtk_objs)): CFLAGS += -DGTK $(gtk_flags)
$(call all-objs, $(textadept_curses_objs)): CFLAGS += -DCURSES -Iscintilla/curses -Itermkey -Icdk \
  $(curses_flags)

$(call all-objs, $(textadept_objs)): textadept.c ; $(build-cc)
$(textadept_rc_objs): textadept.rc ; $(WINDRES) $< $@

# Scintilla.

sci_objs := $(call objs, scintilla/src/*.cxx)
sci_lexlib_objs := $(call objs, lexilla/lexlib/*.cxx)
sci_lexer_gtk_objs := $(call objs, LexLPeg.cxx)
sci_lexer_curses_objs := $(call curses-objs, LexLPeg.cxx)
sci_lexer_objs := $(sci_lexer_gtk_objs) $(sci_lexer_curses_objs)
sci_gtk_objs := $(call objs, scintilla/gtk/*.cxx)
sci_gtk_c_objs := $(call objs, scintilla/gtk/*.c)
sci_curses_objs := $(call objs, scintilla/curses/*.cxx)

sci_flags := -pedantic -DSCI_LEXER -DNDEBUG -Iscintilla/include -Iscintilla/src -Ilexilla/include \
  -Ilexilla/lexlib -Wall

$(call all-objs, $(sci_objs)): CXXFLAGS += $(sci_flags)
$(call all-objs, $(sci_lexlib_objs)): CXXFLAGS += $(sci_flags)
$(call all-objs, $(sci_lexer_objs)): CXXFLAGS += $(sci_flags) $(lua_flags) -DNO_SCITE -DNO_DLL
$(call all-objs, $(sci_lexer_gtk_objs)): CXXFLAGS += -DGTK
$(call all-objs, $(sci_lexer_curses_objs)): CXXFLAGS += -DCURSES $(curses_flags)
$(call all-objs, $(sci_gtk_objs)): CXXFLAGS += $(sci_flags) -DGTK $(gtk_flags)
$(call all-objs, $(sci_gtk_c_objs)): CFLAGS += $(gtk_flags)
$(call all-objs, $(sci_curses_objs)): CXXFLAGS += $(sci_flags) -DCURSES $(curses_flags)

$(sci_objs): %.o: scintilla/src/%.cxx ; $(build-cxx)
$(call win-objs, $(sci_objs)): win-%.o: scintilla/src/%.cxx ; $(build-cxx)
$(call osx-objs, $(sci_objs)): osx-%.o: scintilla/src/%.cxx ; $(build-cxx)
$(sci_lexlib_objs): %.o: lexilla/lexlib/%.cxx ; $(build-cxx)
$(call win-objs, $(sci_lexlib_objs)): win-%.o: lexilla/lexlib/%.cxx ; $(build-cxx)
$(call osx-objs, $(sci_lexlib_objs)): osx-%.o: lexilla/lexlib/%.cxx ; $(build-cxx)
$(call all-objs, $(sci_lexer_objs)): LexLPeg.cxx ; $(build-cxx)
$(sci_gtk_objs): %.o: scintilla/gtk/%.cxx ; $(build-cxx)
$(call win-objs, $(sci_gtk_objs)): win-%.o: scintilla/gtk/%.cxx ; $(build-cxx)
$(call osx-objs, $(sci_gtk_objs)): osx-%.o: scintilla/gtk/%.cxx ; $(build-cxx)
$(sci_gtk_c_objs): %.o: scintilla/gtk/%.c ; $(build-cc)
$(call win-objs, $(sci_gtk_c_objs)): win-%.o: scintilla/gtk/%.c ; $(build-cc)
$(call osx-objs, $(sci_gtk_c_objs)): osx-%.o: scintilla/gtk/%.c ; $(build-cc)
$(sci_curses_objs): %.o: scintilla/curses/%.cxx ; $(build-cxx)
$(call win-objs, $(sci_curses_objs)): win-%.o: scintilla/curses/%.cxx ; $(build-cxx)
$(call osx-objs, $(sci_curses_objs)): osx-%.o: scintilla/curses/%.cxx ; $(build-cxx)

# Lua.

lua_objs := $(call objs, lua/src/*.c, lua luac loslib)
lua_oslib_gtk_objs := $(call objs, lua/src/loslib.c)
lua_oslib_curses_objs := $(call curses-objs, loslib.c)
lua_oslib_objs := $(lua_oslib_gtk_objs) $(lua_oslib_curses_objs)
lua_lib_objs := $(call objs, lua/src/lib/*.c)

lua_flags := -Ilua/src

$(call all-objs, $(lua_objs)): CFLAGS += $(lua_flags) -ULUA_LIB
$(call all-objs, $(lua_oslib_objs)): CFLAGS += -std=c99 -pedantic $(lua_flags) -ULUA_LIB
$(call all-objs, $(lua_oslib_gtk_objs)): CFLAGS += -DGTK $(glib_flags)
$(call all-objs, $(lua_lib_objs)): CFLAGS += $(lua_flags)

$(lua_objs): %.o: lua/src/%.c ; $(build-cc)
$(call win-objs, $(lua_objs)): win-%.o: lua/src/%.c ; $(build-cc)
$(call osx-objs, $(lua_objs)): osx-%.o: lua/src/%.c ; $(build-cc)
$(call all-objs, $(lua_oslib_objs)): lua/src/loslib.c ; $(build-cc)
$(lua_lib_objs): %.o: lua/src/lib/%.c ; $(build-cc)
$(call win-objs, $(lua_lib_objs)): win-%.o: lua/src/lib/%.c ; $(build-cc)
$(call osx-objs, $(lua_lib_objs)): osx-%.o: lua/src/lib/%.c ; $(build-cc)

# gtDialog.

gtdialog_gtk_objs := $(call objs, gtdialog/gtdialog.c)
gtdialog_curses_objs := $(call curses-objs, gtdialog/gtdialog.c)
gtdialog_objs := $(gtdialog_gtk_objs) $(gtdialog_curses_objs)

$(call all-objs, $(gtdialog_objs)): CFLAGS += -std=c99 -pedantic -DNOHELP -DLIBRARY
$(call all-objs, $(gtdialog_gtk_objs)): CFLAGS += -DGTK $(gtk_flags)
$(call all-objs, $(gtdialog_curses_objs)): CFLAGS += -DCURSES -I. -Icdk $(curses_flags)

$(call all-objs, $(gtdialog_objs)): gtdialog/gtdialog.c ; $(build-cc)

# Termkey.

termkey_objs := $(call objs, termkey/termkey.c)
termkey_unix_objs := $(call objs, termkey/driver-ti.c termkey/driver-csi.c)
termkey_win_objs := $(call objs, termkey/driver-win-pdcurses.c)
termkey_all_objs := $(termkey_objs) $(termkey_unix_objs) $(termkey_win_objs)

$(call all-objs, $(termkey_all_objs)): CFLAGS += -std=c99 $(curses_flags)

$(termkey_all_objs): %.o: termkey/%.c ; $(build-cc)
$(call win-objs, $(termkey_all_objs)): win-%.o: termkey/%.c ; $(build-cc)
$(call osx-objs, $(termkey_all_objs)): osx-%.o: termkey/%.c ; $(build-cc)

# CDK.
# Only compile a subset of cdk and use a prefix to avoid object name conflicts on case-insensitive
# filesystems.

cdk_src := $(addprefix cdk/, $(addsuffix .c, binding buttonbox cdk cdk_display cdk_objs \
  cdkscreen draw entry fselect itemlist label mentry popup_label scroll scroller select_file \
  selection slider traverse version))
cdk_objs := $(addprefix cdk-, $(call objs, $(cdk_src)))

$(call all-objs, $(cdk_objs)): CFLAGS += -D_GNU_SOURCE -Itermkey -Icdk $(curses_flags)

$(cdk_objs): cdk-%.o: cdk/%.c ; $(build-cc)
$(call win-objs, $(cdk_objs)): win-cdk-%.o: cdk/%.c ; $(build-cc)
$(call osx-objs, $(cdk_objs)): osx-cdk-%.o: cdk/%.c ; $(build-cc)

# Add debugging symbols and disable optimizations when DEBUG=1.
# Notes:
# In order to profile with gprof (-pg), do not retain symbols in LDFLAGS.
# In order to report code coverage with gcov (-fprofile-arcs -ftest-coverage):
#   Run test suite with textadept, run gcov-9 textadept.c, then examine
#     textadept.c.gcov
#   Run test suite with textadept-curses, run gcov-9 textadept-curses.c, then
#     examine textadept.c.gcov.
ifdef DEBUG
  CFLAGS += -g -O0
  CXXFLAGS += -g -O0
  lua_flags += -DLUA_USE_APICHECK
  sci_flags += -UNDEBUG -DDEBUG
endif

# Compilers and platform-specific flags for all objects.

common_objs := $(sci_objs) $(sci_lexlib_objs) $(lua_objs) $(lua_lib_objs)
gui_objs := $(sci_lexer_gtk_objs) $(sci_gtk_objs) $(sci_gtk_c_objs) $(textadept_gtk_objs) \
  $(lua_oslib_gtk_objs) $(gtdialog_gtk_objs)
curses_objs := $(sci_lexer_curses_objs) $(sci_curses_objs) $(textadept_curses_objs) \
  $(lua_oslib_curses_objs) $(gtdialog_curses_objs) $(termkey_objs) $(cdk_objs)

linux_objs := $(common_objs) $(gui_objs) $(curses_objs) $(termkey_unix_objs)
win_objs := $(call win-objs, $(common_objs) $(gui_objs) $(curses_objs)) $(termkey_win_objs) \
  $(textadept_rc_objs)
osx_objs := $(call osx-objs, $(common_objs) $(gui_objs) $(curses_objs) $(termkey_unix_objs))

# Compile natively for Linux or BSD.
ifeq (Linux, $(shell uname -s))
$(linux_objs): CC := gcc
$(linux_objs): CXX := g++
else
$(linux_objs): CC := cc
$(linux_objs): CXX := c++
endif
$(linux_objs): lua_flags += -DLUA_USE_LINUX
$(linux_objs): gtk_flags := $(gtk_flags_linux)
$(linux_objs): glib_flags := $(glib_flags_linux)
$(linux_objs): curses_flags := $(curses_flags_linux)

# Cross-compile for Windows.
$(win_objs): CC := x86_64-w64-mingw32-gcc-posix
$(win_objs): CXX := x86_64-w64-mingw32-g++-posix
$(win_objs): CFLAGS += -mms-bitfields
$(win_objs): CXXFLAGS += -mms-bitfields
$(win_objs): WINDRES := x86_64-w64-mingw32-windres
$(win_objs): lua_flags += -DLUA_BUILD_AS_DLL -DLUA_LIB
$(win_objs): gtk_flags := $(gtk_flags_win)
$(win_objs): glib_flags := $(glib_flags_win)
$(win_objs): curses_flags := -DLIBICONV_STATIC -Ipdcurses

# Cross-compile for macOS.
$(osx_objs): CC := x86_64-apple-darwin17-cc
$(osx_objs): CXX := x86_64-apple-darwin17-c++
$(osx_objs): CFLAGS += -mdynamic-no-pic
$(osx_objs): CXXFLAGS += -mdynamic-no-pic -stdlib=libc++ -Wno-register
$(osx_objs): lua_flags += -DLUA_USE_MACOSX
$(osx_objs): gtk_flags := $(gtk_flags_osx)
$(osx_objs): glib_flags := $(glib_flags_osx)

# Executables.

linux_exes := $(addprefix ../, textadept textadept-curses)
win_exes := $(addprefix ../, textadept.exe textadept-curses.exe)
osx_exes := $(addprefix ../, textadept-osx textadept-osx-curses)

.PHONY: all curses win osx
.DEFAULT_GOAL := all
all: $(linux_exes)
curses: ../textadept-curses
win: $(win_exes)
osx: $(osx_exes)

# Compile natively for Linux or BSD.

$(linux_exes): $(common_objs)
../textadept: $(gui_objs)
../textadept-curses: $(curses_objs) $(termkey_unix_objs)

ifeq (Linux, $(shell uname -s))
$(linux_exes): CXX := g++
else
$(linux_exes): CXX := c++
endif
$(linux_exes): LDFLAGS := -rdynamic -Wl,--retain-symbols-file -Wl,$(shell pwd)/lua.sym
ifdef GTK2
../textadept: LDFLAGS += $(shell pkg-config --libs gtk+-2.0 gmodule-2.0)
else
../textadept: LDFLAGS += $(shell pkg-config --libs gtk+-3.0 gmodule-2.0)
endif
../textadept-curses: LDFLAGS += $(shell pkg-config --silence-errors --libs ncursesw || \
  pkg-config --silence-errors --libs ncurses || pkg-config --silence-errors --libs curses || \
  echo -lncursesw) -lpthread
ifeq (Linux, $(shell uname -s))
$(linux_exes): LDLIBS := -ldl
else
$(linux_exes): LDLIBS := -liconv
endif

$(linux_exes): ; $(build-exe)

# Cross-compile for Windows.

$(win_exes): $(call win-objs, $(common_objs)) $(textadept_rc_objs)
../textadept.exe: $(call win-objs, $(gui_objs))
../textadept-curses.exe: $(call win-objs, $(curses_objs)) $(termkey_win_objs)

$(win_exes): CXX := x86_64-w64-mingw32-g++-posix
../textadept.exe: CXXFLAGS += -mwindows
$(win_exes): LDFLAGS := -static-libgcc -static-libstdc++ \
  -Wl,--retain-symbols-file -Wl,$(shell pwd)/lua.sym
../textadept.exe: LDFLAGS += $(shell PKG_CONFIG_PATH=`pwd`/gtkwin/lib/pkgconfig \
  pkg-config --define-variable=prefix=gtkwin --libs gtk+-3.0 gmodule-2.0)
../textadept.exe: LDLIBS := -liconv
../textadept-curses.exe: LDLIBS := pdcurses/pdcurses.a pdcurses/libiconv.dll.a

$(win_exes): ; $(build-exe)

# Cross-compile for macOS.

$(osx_exes): $(call osx-objs, $(common_objs))
../textadept-osx: $(call osx-objs, $(gui_objs))
../textadept-osx-curses: $(call osx-objs, $(curses_objs) $(termkey_unix_objs))

$(osx_exes): CXX := x86_64-apple-darwin17-c++
$(osx_exes): CXXFLAGS += -mdynamic-no-pic -stdlib=libc++ -Wno-register
../textadept-osx: LDFLAGS += $(shell PKG_CONFIG_PATH=`pwd`/gtkosx/lib/pkgconfig \
  pkg-config --define-variable=prefix=gtkosx --libs gtk+-3.0 gmodule-2.0 gtk-mac-integration) \
  -framework Cocoa
$(osx_exes): LDLIBS := -liconv
../textadept-osx-curses: LDLIBS += -lncurses

$(osx_exes): ; $(build-exe)

# Install/uninstall.

PREFIX ?= /usr/local
bin_dir := $(PREFIX)/bin
data_dir := $(PREFIX)/share/textadept
XDG_DATA_DIR ?= $(PREFIX)/share/applications
PIXMAPS_DIR ?= /usr/share/pixmaps
desktop_files := $(wildcard *.desktop)
ifeq (, $(findstring curses, $(MAKECMDGOALS)))
  install_targets := ../textadept
else
  install_targets := ../textadept-curses
endif

.PHONY: install uninstall
install: $(install_targets) | ../core ../docs ../init.lua ../lexers ../LICENSE ../modules ../themes
	install -d $(DESTDIR)$(bin_dir) $(DESTDIR)$(data_dir)
	install $^ $(DESTDIR)$(data_dir)
	cp -rL $| $(DESTDIR)$(data_dir)
	ln -s $(subst .., $(data_dir), $^) $(DESTDIR)$(bin_dir)
	if [ -d "$(XDG_DATA_DIR)" ]; then \
		install -d $(DESTDIR)$(XDG_DATA_DIR); \
		install $(desktop_files) $(DESTDIR)$(XDG_DATA_DIR); \
	fi
	if [ -d "$(PIXMAPS_DIR)" ]; then \
		install -d $(DESTDIR)$(PIXMAPS_DIR); \
		ln -s $(data_dir)/core/images/textadept.svg $(DESTDIR)$(PIXMAPS_DIR); \
		ln -s $(data_dir)/core/images/ta_48x48.png $(DESTDIR)$(PIXMAPS_DIR)/textadept.png; \
	fi
uninstall:
	rm -r $(DESTDIR)$(bin_dir)/textadept* $(DESTDIR)$(data_dir)
	if [ -d "$(XDG_DATA_DIR)" ]; then rm $(DESTDIR)$(XDG_DATA_DIR)/textadept*.desktop; fi
	if [ -d "$(PIXMAPS_DIR)" ]; then rm $(DESTDIR)$(PIXMAPS_DIR)/textadept*; fi

# Clean.

.PHONY: clean clean-win clean-osx clean-all
clean: ; rm -f $(linux_objs) $(linux_exes)
clean-win: ; rm -f $(win_objs) $(win_exes)
clean-osx: ; rm -f $(osx_objs) $(osx_exes)
clean-all: clean clean-win clean-osx

# Documentation.

version := $(shell grep -m 1 _RELEASE ../core/init.lua | cut -d ' ' -f4- | tr ' ' '_' | tr -d "'")

ifndef ALL_MODULES
  modules := $(addprefix ../modules/, ansi_c lua textadept)
else
  modules := ../modules
endif
ta_home := $(shell dirname `pwd`)

.PHONY: docs manual luadoc
docs: luadoc manual
manual: ../docs/api.md $(wildcard ../docs/*.md) | ../docs/_layouts/default.html
	for file in $(basename $^); do cat $| | ../scripts/fill_layout.lua $$file.md > $$file.html; done
	sed -i "s/\(\# Textadept\).\+\?\(Manual\|API\)/\1 $(subst _, ,$(version)) \2/;" ../docs/*.md
luadoc: ../core $(modules) ../lexers/lexer.lua | ../modules/lua/lua.luadoc
	cd ../scripts && luadoc --doclet markdowndoc $^ > ../docs/api.md
	cd ../modules && luadoc -d lua --doclet lua/tadoc $^ --ta-home=$(ta_home)
	cd ../modules/lua && mv tags ta_tags && mv api ta_api
	cd ../modules && luadoc -d lua --doclet lua/tadoc $| --ta-home=$(ta_home)

# Releases.

ifndef NIGHTLY
  basedir := textadept_$(version)
else
  basedir := textadept_nightly_$(shell date +"%F")
endif
linux_dir := $(basedir).linux
win_dir := $(basedir).win
osx_dir := $(basedir).macOS

.PHONY: release release-linux release-win release-osx
release: $(linux_dir).tgz $(win_dir).zip $(osx_dir).zip | deps win-deps osx-deps docs

ifneq (, $(shell hg summary 2>/dev/null))
  archive = hg archive -X ".hg*" $(1)
else
  archive = cd ../ && git archive HEAD --prefix src/$(1)/ | tar -xf -
endif

# TODO: CXXFLAGS="$(CXXFLAGS) -static-libstdc++"
$(linux_dir): $(linux_exes)
	$(call archive,$@)
	cp -rL ../docs ../lexers $^ $@
$(linux_dir).tgz: $(linux_dir) ; tar czf $<.tgz $< && rm -r $<

$(win_dir): $(win_exes)
	$(call archive,$@)
	cp gtkwin/bin/*.dll $@ && cp -r $(addprefix gtkwin/, etc lib share) $@
	rm -r $(addprefix $@/lib/, *.a glib-2.0 pkgconfig)
	cp -rL ../docs ../lexers $^ $@
$(win_dir).zip: $(win_dir) ; zip -r $<.zip $< && rm -r $<

osxapp := Textadept.app
osxapp_bin := $(osxapp)/Contents/MacOS
osxapp_res := $(osxapp)/Contents/Resources
$(osx_dir): $(osx_exes)
	mkdir -p $@ $@/$(osxapp_bin) $@/$(osxapp_res)
	cp ../scripts/osx/ta $@
	cp Info.plist $@/$(osxapp_bin)/../
	cp ../textadept-osx $@/$(osxapp_bin)/textadept
	cp ../textadept-osx-curses $@/$(osxapp_bin)/textadept-curses
	cp ../scripts/osx/textadept_osx $@/$(osxapp_bin)
	$(call archive,$@/$(osxapp_res))
	cp -rL ../docs ../lexers $@/$(osxapp_res)
	mv $@/$(osxapp_res)/core/images/textadept.icns $@/$(osxapp_res)
	cp -r $(addprefix gtkosx/, etc lib share) $@/$(osxapp_res)
	rm -r $(addprefix $@/$(osxapp_res)/lib/, glib-2.0 pkgconfig)
	find $@/$(osxapp_res)/lib -type l -delete
$(osx_dir).zip: $(osx_dir) ; zip -r $<.zip $< && rm -r $<

# External dependencies.

.PHONY: deps win-deps osx-deps
base_deps := scintilla lexilla ../lexers LexLPeg.h LexLPeg.cxx scintilla/curses lua \
  $(addprefix lua/src/lib/, lpeg lfs) gtdialog cdk
deps: $(base_deps) termkey
win-deps: $(base_deps) gtkwin pdcurses
osx-deps: $(base_deps) gtkosx termkey

root_url := https://github.com/orbitalquark

ifndef NIGHTLY
  scinterm_url = $(root_url)/scinterm/archive/$@
  scintillua_url = $(root_url)/scintillua/archive/$@
  gtdialog_url = $(root_url)/gtdialog/archive/$@
else
  scinterm_url := $(root_url)/scinterm/archive/default.zip
  scintillua_url := $(root_url)/scintillua/archive/default.zip
  gtdialog_url := $(root_url)/gtdialog/archive/default.zip
endif

scintilla_tgz := scintilla520.tgz
lexilla_tgz := lexilla510.tgz
scinterm_zip := 475d8d43f3418590c28bd2fb07ee9229d1fa2d07.zip
scintillua_zip := 4cb1464ef738a098f008d6530b776fe780b19c34.zip
lua_tgz := lua-5.4.4.tar.gz
lpeg_tgz := lpeg-1.0.2.tar.gz
lfs_zip := v1_8_0.zip
gtdialog_zip := gtdialog_1.5.zip
cdk_tgz := cdk-5.0-20200923.tgz
termkey_tgz := libtermkey-0.20.tar.gz
gtkwin_zip := gtkwin-3.24.31.zip
pdcurses_zip := PDCurses-3.9.zip
gtkosx_tgz := gtkosx-3.24.30.tar.gz
cloc_zip := v1.90.zip

$(scintilla_tgz): ; $(WGET) https://www.scintilla.org/$@
scintilla: scintilla.patch | $(scintilla_tgz)
	if [ -d $@/.hg ]; then \
		hg --cwd $@ update -C -r `hg --cwd $@ summary | head -1 | cut -d: -f2`; \
	else \
		if [ -d $@ ]; then rm -r $@; fi; \
		tar xzf $|; \
	fi
	for patch in $^; do echo Applying $$patch; patch -d $@ -N -p1 < $$patch; done
$(lexilla_tgz): ; $(WGET) https://www.scintilla.org/$@
lexilla: | $(lexilla_tgz) ; tar xzf $|
$(scinterm_zip): ; $(WGET) $(scinterm_url) -O $@
scintilla/curses: | $(scinterm_zip)
	unzip -d $@ $| && mv $@/*/* $@
	$(MAKE) -C $@ patch
$(scintillua_zip): ; $(WGET) $(scintillua_url) -O $@
../lexers: | $(scintillua_zip) ; unzip -d $@ -j $| "*/lexers/*" -x "*/themes/*"
LexLPeg.h LexLPeg.cxx: | $(scintillua_zip) ; unzip -j $| "*/*.cxx" "*/*.h"
$(lua_tgz): ; $(WGET) https://www.lua.org/ftp/$@
$(lpeg_tgz): ; $(WGET) http://www.inf.puc-rio.br/~roberto/lpeg/$@
$(lfs_zip): ; $(WGET) https://github.com/keplerproject/luafilesystem/archive/$@
lua: lua.patch | $(lua_tgz)
	if [ -d $@ ]; then rm -rf $@; fi
	mkdir $@ && tar xzf $| -C $@ && mv $@/*/* $@
	patch -d $@ -N -p1 < $<
lua/src/lib/lpeg: lua | $(lpeg_tgz)
	mkdir -p $@ && tar xzf $| -C $@ && mv $@/*/*.c $@/*/*.h $(dir $@)
lua/src/lib/lfs: lua | $(lfs_zip)
	if [ -d $@ ]; then rm -r $@; fi
	mkdir -p $@ && unzip -d $(dir $@) -j $| "*/src/*.c" "*/src/*.h"
$(gtdialog_zip): ; $(WGET) $(gtdialog_url) -O $@
gtdialog: | $(gtdialog_zip) ; unzip -d $@ -j $| "*/*.c" "*/*.h"
$(cdk_tgz): ; $(WGET) https://invisible-mirror.net/archives/cdk/$@
cdk: cdk.patch | $(cdk_tgz)
	if [ -d $@ ]; then rm -r $@; fi
	mkdir $@ && tar xzf $| -C $@ && mv $@/*/* $@
	mv $@/include/*.h $@
	$@/gen-scale.sh SLIDER Slider Int int $@/gen-slider.h > $@/slider.h
	$@/gen-scale.sh SLIDER Slider Int int $@/gen-slider.c > $@/slider.c
	patch -d $@ -N -p1 < $<
$(termkey_tgz): ; $(WGET) http://www.leonerd.org.uk/code/libtermkey/$@
termkey: termkey.patch | $(termkey_tgz)
	if [ -d $@ ]; then rm -r $@; fi
	mkdir $@ && tar xzf $| -C $@ && mv $@/*/* $@ && chmod +w $@/termkey.h
	patch -d $@ -N -p1 < $<
$(gtkwin_zip): ; $(WGET) $(root_url)/textadept-build/raw/default/$@
gtkwin: | $(gtkwin_zip) ; unzip $|
$(pdcurses_zip): ; $(WGET) http://prdownloads.sourceforge.net/pdcurses/$@
pdcurses: pdcurses.patch | $(pdcurses_zip)
	unzip -d $@ $| && mv $@/PDCurses*/* $@
	cp gtkwin/include/iconv.h gtkwin/lib/libiconv.dll.a $@
	patch -d $@ -N -p1 < $<
	# TODO: parameterize CROSS, CC, and CFLAGS.
	cd $@/wincon && $(MAKE) CC="x86_64-w64-mingw32-gcc-posix -Os -mms-bitfields" \
		LIBEXE=x86_64-w64-mingw32-ar LINK="x86_64-w64-mingw32-gcc-posix -Os -mms-bitfields" WIDE=Y \
		UTF8=Y libs
	mv $@/wincon/pdcurses.a $@
$(gtkosx_tgz): ; $(WGET) $(root_url)/textadept-build/raw/default/$@
gtkosx: | $(gtkosx_tgz) ; mkdir $@ && tar xzf $| -C $@ && mv $@/*/* $@
$(cloc_zip): ; $(WGET) https://github.com/AlDanial/cloc/archive/refs/tags/$@
cloc: | $(cloc_zip) ; unzip -j $| "*/$@" -x "*/Unix/$@"

.PHONY: clean-deps
clean-deps:
	rm -rf $(base_deps) termkey gtkwin pdcurses gtkosx $(scinterm_zip) $(scintillua_zip) \
		$(gtdialog_zip)

# Count lines of code.

.PHONY: count plots
sources := ../core ../modules/ansi_c ../modules/lua ../modules/textadept textadept.c Makefile \
  ../init.lua
count: cloc ; perl $< $(sources) --not-match-f=tadoc.lua --exclude-lang=SVG
plots: ; ../scripts/gen_plots.sh