-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
217 lines (174 loc) · 7.42 KB
/
Copy pathmakefile
File metadata and controls
217 lines (174 loc) · 7.42 KB
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
CC=LD_LIBRARY_PATH=/usr/lib gcc
BASE_CFLAGS = $(shell cat compile_flags.txt | tr '\n' ' ')
CFLAGS = $(BASE_CFLAGS) -MMD -MP
# Version derived from `git describe` at build time so the binary
# reports the exact tag/commit it was built from. Falls back to "dev"
# when git metadata is unavailable (e.g. release tarballs)
HED_VERSION := $(shell git describe --tags --always --dirty 2>/dev/null)
ifeq ($(HED_VERSION),)
HED_VERSION := d
endif
CFLAGS += -DHED_VERSION='"$(HED_VERSION)"'
# Absolute path to the in-tree man pages (generated by `make man`). Baked
# into the binary so the `man` plugin can resolve `:man hed-<plugin>`
# against the bundled pages without a system install. Override with
# MAN_DIR=path; set MAN_DIR= (empty) to disable the bundled lookup.
MAN_DIR ?= $(abspath man)
ifneq ($(MAN_DIR),)
CFLAGS += -DHED_MAN_DIR='"$(MAN_DIR)"'
endif
DEBUG ?= 0
# DEBUG=1 adds -ggdb for gdb-friendly debug symbols.
ifeq ($(DEBUG),1)
CFLAGS += -ggdb
CFLAGS += -O0
endif
SRC_DIR = src
BUILD_DIR = build
VENDOR_BUILD_DIR = vendor/build
PLUGINS_DIR ?= plugins
# Additional plugin dirs compiled on top of $(PLUGINS_DIR) — space-
# separated, e.g. EXTRA_PLUGIN_DIRS=$(HOME)/my-hed-plugins. Plugin
# symbol names must be unique across all dirs (duplicates are a link
# error, by design).
EXTRA_PLUGIN_DIRS ?=
PLUGIN_DIRS = $(PLUGINS_DIR) $(EXTRA_PLUGIN_DIRS)
PLUGIN_INC = $(foreach d,$(PLUGIN_DIRS),-I$(d))
# Optional user config, compiled in when present. It defines
# config_user_init(), which src/config.h calls (weakly) after the
# stock defaults — additive, last-write-wins on keybinds.
USER_CONFIG ?= $(wildcard $(HOME)/.config/hed/config.c)
WITH_TREESITTER ?= 1
# The tree-sitter runtime is a frozen vendored amalgam; building it is the
# single biggest cost in a clean build (~3s of a ~10s build). Park its
# archive under vendor/build/ so `make clean` doesn't nuke it. Use
# `make distclean` to force a rebuild (e.g. after changing CFLAGS).
TS_DIR := vendor/tree-sitter
TS_LIB_DIR := $(TS_DIR)/lib
TS_LIB_A := $(VENDOR_BUILD_DIR)/libtree-sitter.a
TARGET = $(BUILD_DIR)/hed
TSI = $(BUILD_DIR)/tsi
INSTALL_DIR ?= $(HOME)/.local/bin
# Core sources: everything under src/ except the in-tree plugins subtree.
CORE_SOURCES = $(shell find $(SRC_DIR) -type f -name "*.c" -not -path "$(SRC_DIR)/plugins/*")
PLUGIN_SOURCES = $(shell find $(PLUGINS_DIR) -type f -name "*.c" 2>/dev/null)
ifeq ($(WITH_TREESITTER),1)
TS_LDFLAGS := $(TS_LIB_A) -ldl
TS_DEPS := $(TS_LIB_A)
else
PLUGIN_SOURCES := $(filter-out $(PLUGINS_DIR)/treesitter/%,$(PLUGIN_SOURCES))
TS_LDFLAGS := -ldl
TS_DEPS :=
endif
SOURCES = $(CORE_SOURCES) $(PLUGIN_SOURCES)
HEADERS = $(shell find $(SRC_DIR) -type f -name "*.h") \
$(foreach d,$(PLUGIN_DIRS),$(shell find $(d) -type f -name "*.h" 2>/dev/null))
CORE_OBJECTS = $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(CORE_SOURCES))
PLUGIN_OBJECTS = $(patsubst $(PLUGINS_DIR)/%.c,$(BUILD_DIR)/plugins/%.o,$(PLUGIN_SOURCES))
# Each extra plugin dir gets its own object subtree under
# build/plugins_ext/<id>/ (id = absolute path with / -> _) so two
# dirs containing a same-named plugin can't collide on object paths.
extra_dir_id = $(subst /,_,$(patsubst /%,%,$(abspath $(1))))
EXTRA_PLUGIN_OBJECTS =
define EXTRA_PLUGIN_DIR_template
EXTRA_SRC_$(2) := $$(shell find $(1) -type f -name "*.c" 2>/dev/null)
EXTRA_PLUGIN_OBJECTS += $$(patsubst $(1)/%.c,$$(BUILD_DIR)/plugins_ext/$(2)/%.o,$$(EXTRA_SRC_$(2)))
$$(BUILD_DIR)/plugins_ext/$(2)/%.o: $(1)/%.c
@mkdir -p $$(dir $$@)
$$(CC) $$(CFLAGS) $$(PLUGIN_INC) -c $$< -o $$@
endef
$(foreach d,$(EXTRA_PLUGIN_DIRS),$(eval $(call EXTRA_PLUGIN_DIR_template,$(patsubst %/,%,$(d)),$(call extra_dir_id,$(d)))))
ifneq ($(USER_CONFIG),)
USER_CONFIG_OBJ = $(BUILD_DIR)/user_config.o
endif
OBJECTS = $(CORE_OBJECTS) $(PLUGIN_OBJECTS) $(EXTRA_PLUGIN_OBJECTS) $(USER_CONFIG_OBJ)
.PHONY: all clean distclean run test test_args ts-langs strip_build install uninstall publish fmt tags man install-man
all: $(TARGET) $(TSI)
test:
$(MAKE) -C test test
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
$(VENDOR_BUILD_DIR):
mkdir -p $(VENDOR_BUILD_DIR)
# -Wl,--gc-sections drops object sections that nothing references,
# so plugins removed from config.h's plugin_load() list are GC'd out
# of the final binary without having to touch the Makefile. Paired
# with -ffunction-sections -fdata-sections in compile_flags.txt.
$(TARGET): $(OBJECTS) $(TS_DEPS)
$(CC) -o $@ $(OBJECTS) $(LDFLAGS) -Wl,--gc-sections $(TS_LDFLAGS)
# Pull in auto-generated header dependencies (created by -MMD).
-include $(OBJECTS:.o=.d)
$(TS_LIB_A): $(TS_LIB_DIR)/src/lib.c | $(VENDOR_BUILD_DIR)
$(CC) -O2 -std=c11 -fPIC -D_GNU_SOURCE \
-I$(TS_LIB_DIR)/include -I$(TS_LIB_DIR)/src \
-c $< -o $(VENDOR_BUILD_DIR)/ts-lib.o
$(AR) rcs $@ $(VENDOR_BUILD_DIR)/ts-lib.o
$(BUILD_DIR)/plugins/%.o: $(PLUGINS_DIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(PLUGIN_INC) -c $< -o $@
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(PLUGIN_INC) -c $< -o $@
ifneq ($(USER_CONFIG),)
$(USER_CONFIG_OBJ): $(USER_CONFIG)
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(PLUGIN_INC) -c $< -o $@
endif
$(TSI): ts/ts_lang_install.c
$(CC) -Wall -Wextra -O2 -I/usr/include -o $@ $<
install: $(TARGET) $(TSI)
@mkdir -p $(INSTALL_DIR)
ln -sf $(abspath $(TARGET)) $(INSTALL_DIR)/hed
ln -sf $(abspath $(TSI)) $(INSTALL_DIR)/tsi
@echo "Symlinked hed and tsi -> $(INSTALL_DIR)"
publish: $(TARGET) $(TSI)
@echo "Publishing hed and tsi to /usr/local/bin/"
cp $(TARGET) /usr/local/bin/hed
cp $(TSI) /usr/local/bin/tsi
uninstall:
rm -f $(INSTALL_DIR)/hed $(INSTALL_DIR)/tsi
@echo "Removed hed and tsi symlinks from $(INSTALL_DIR)"
# Generate roff man pages from readme.md + plugins/*/README.md into
# $(MAN_DIR)/man1 (default ./man/man1). Requires pandoc.
man:
./scripts/gen-man.sh $(if $(MAN_DIR),$(MAN_DIR)/man1)
# Install the generated pages into a directory on the system MANPATH so
# `man hed`, `man hed-tmux`, `apropos hed` work outside the editor too.
MAN_INSTALL_DIR ?= $(HOME)/.local/share/man/man1
install-man: man
@mkdir -p $(MAN_INSTALL_DIR)
cp $(if $(MAN_DIR),$(MAN_DIR),man)/man1/*.1 $(MAN_INSTALL_DIR)/
@echo "Installed hed man pages -> $(MAN_INSTALL_DIR)"
ts-langs: $(BUILD_DIR)
@echo "Staging tree-sitter grammars into $(BUILD_DIR)/ts-langs"
@ts_user="$${XDG_CONFIG_HOME:-$$HOME/.config}/hed/ts"; \
ts_src=""; \
if [ -d ts-langs ]; then ts_src=ts-langs; \
elif [ -d "$$ts_user" ]; then ts_src="$$ts_user"; fi; \
if [ -z "$$ts_src" ]; then \
echo " warning: no grammars found (ts-langs/ or $$ts_user) — skipping"; \
else \
echo " from $$ts_src"; \
mkdir -p $(BUILD_DIR)/ts-langs; \
cp -rf "$$ts_src/." $(BUILD_DIR)/ts-langs/; \
if [ "$$ts_src" = ts-langs ]; then mkdir -p "$$ts_user"; cp -rf ts-langs/. "$$ts_user/"; fi; \
fi
strip_build: $(BUILD_DIR) $(TARGET) $(TSI) ts-langs
@# Strip debug/symbol tables from the final binaries
strip --strip-all $(TARGET) $(TSI)
@# Keep only the final binaries and the staged grammars in $(BUILD_DIR)
@find $(BUILD_DIR) -mindepth 1 -maxdepth 1 \
! -name 'hed' ! -name 'tsi' ! -name 'ts-langs' -exec rm -rf {} +
clean:
rm -rf $(BUILD_DIR)
# Also drops the vendored tree-sitter archive. Use this after changing
# the compiler or CFLAGS — Make doesn't track flag changes, so a stale
# libtree-sitter.a built with different flags would otherwise persist.
distclean: clean
rm -rf $(VENDOR_BUILD_DIR)
fmt:
clang-format -i $(SOURCES) $(HEADERS)
tags:
ctags -R --languages=C $(SRC_DIR) $(PLUGIN_DIRS)
run: $(TARGET)
./$(TARGET)