-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
396 lines (327 loc) · 14.8 KB
/
Copy pathMakefile
File metadata and controls
396 lines (327 loc) · 14.8 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
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
# npad Makefile
# Cross-platform text editor
# Compiler settings
CC ?= gcc
CFLAGS = -Wall -Wextra -std=c99 -O2
LDFLAGS =
# Platform detection
UNAME_S := $(shell uname -s 2>/dev/null || echo "Windows")
UNAME_M := $(shell uname -m 2>/dev/null || echo "unknown")
# Cross-compilation support
ifdef CROSS_COMPILE
CC = $(CROSS_COMPILE)gcc
endif
# Windows toolchain: cross-compile from Linux by default, or use the native
# MinGW toolchain when building on Windows (MSYS2 / Git Bash)
ifeq ($(OS),Windows_NT)
MINGW_CC ?= gcc
MINGW_WINDRES ?= windres
MINGW_STRIP ?= strip
MINGW_DLLTOOL ?= dlltool
else
MINGW_CC ?= x86_64-w64-mingw32-gcc
MINGW_WINDRES ?= x86_64-w64-mingw32-windres
MINGW_STRIP ?= x86_64-w64-mingw32-strip
MINGW_DLLTOOL ?= x86_64-w64-mingw32-dlltool
endif
# Source files
CORE_SOURCES = src/core/editor.c src/core/file_ops.c src/core/settings.c src/core/session.c src/core/thread_safety.c src/core/error.c src/core/startup_prof.c src/core/list_ops.c src/core/update_check.c src/core/html_md.c
SHARED_SOURCES = src/ui_interface.c
# Test sources
TEST_FRAMEWORK_SOURCES = tests/test_framework.c
TEST_CORE_SOURCES = src/core/file_ops.c src/core/settings.c src/core/session.c src/core/thread_safety.c src/core/error.c src/core/list_ops.c src/core/update_check.c src/core/html_md.c # Core sources without UI dependencies
# Windows GUI specific
WINDOWS_GUI_SOURCES = src/platform/ui_win32.c
# Delay-loaded DLLs: none of these are needed to open a window, so binding them
# statically made the loader page them in on every launch - which costs most on
# a cold start, exactly when startup feels slow. winhttp/bcrypt serve only the
# opt-in update check, comdlg32 only the file/font dialogs, msimg32 only the
# Highlight All wash. dlltool builds a delay-import library from each .def in
# src/platform/delay/, and libdelayimp supplies the resolver that loads the DLL
# on the first real call. The matching -l flags must NOT also be passed, or the
# normal import library wins and the DLL is bound statically again.
DELAY_DLLS = winhttp bcrypt comdlg32 msimg32
DELAY_LIBS = $(patsubst %,build/lib%_delay.a,$(DELAY_DLLS))
WINDOWS_GUI_LIBS = -mwindows -lcomctl32 -lgdi32 -lkernel32 -lshell32 -luser32 -lole32 -luuid $(DELAY_LIBS) -ldelayimp
WINDOWS_GUI_TARGET = npad.exe
# Windows Terminal specific
WINDOWS_TERMINAL_SOURCES = src/platform/ui_win32_terminal.c
WINDOWS_TERMINAL_LIBS = -lkernel32
WINDOWS_TERMINAL_TARGET = npad-terminal.exe
# macOS specific (future)
MACOS_SOURCES = src/platform/ui_cocoa.m
MACOS_LIBS = -framework Cocoa
MACOS_TARGET = npad-macos
# Linux X11 specific
LINUX_X11_SOURCES = src/platform/ui_x11.c
LINUX_X11_LIBS = -lX11 -lpthread
LINUX_X11_TARGET = npad-linux-x11
# Linux Wayland specific (future)
LINUX_WAYLAND_SOURCES = src/platform/ui_wayland.c
LINUX_WAYLAND_LIBS = -lwayland-client -lpthread
LINUX_WAYLAND_TARGET = npad-linux-wayland
# Linux Terminal (ncurses) specific
LINUX_TERMINAL_SOURCES = src/platform/ui_ncurses.c
LINUX_TERMINAL_LIBS = -lncurses -lpthread
LINUX_TERMINAL_TARGET = npad-linux-terminal
# Version information - use header version or fallback to git
# Not using awk here due to an issue found with awk 5.2.1 that I could not fix and was mangling strings
MAJOR = $(shell grep '^#define NPAD_VERSION_MAJOR' src/main.h | cut -d' ' -f3)
MINOR = $(shell grep '^#define NPAD_VERSION_MINOR' src/main.h | cut -d' ' -f3)
PATCH = $(shell grep '^#define NPAD_VERSION_PATCH' src/main.h | cut -d' ' -f3)
RELEASE = $(shell grep '^#define NPAD_VERSION_RELEASE' src/main.h | cut -d' ' -f3 | tr -d '"')
BASE_VERSION = v$(MAJOR).$(MINOR).$(PATCH)
HEADER_VERSION = $(BASE_VERSION)-$(RELEASE)
GIT_EXACT_TAG = $(shell git describe --tags --exact-match --dirty 2>/dev/null)
# Bare short hash (plus -dirty), NOT git describe --tags: describe leads with
# the most recent tag, so a dev build's About box would read "v0.12.0-dev
# (v0.11.0-2-gabc123)" and look like the wrong version.
GIT_COMMIT_RAW = $(shell git rev-parse --short HEAD 2>/dev/null)
ifneq ($(GIT_COMMIT_RAW),)
GIT_COMMIT = $(GIT_COMMIT_RAW)$(shell git diff-index --quiet HEAD -- 2>/dev/null || echo -dirty)
else
GIT_COMMIT =
endif
# Building exactly at the matching release tag (clean tree): plain "v0.10.1".
# Otherwise a dev build: "v0.10.2-dev (abc1234)"; without git available
# (e.g. some CI shells), just "v0.10.2-dev" - never empty parens.
ifeq ($(GIT_EXACT_TAG),$(BASE_VERSION))
VERSION ?= $(BASE_VERSION)
else ifneq ($(GIT_COMMIT),)
VERSION ?= $(HEADER_VERSION) ($(GIT_COMMIT))
else
VERSION ?= $(HEADER_VERSION)
endif
CFLAGS += -DNPAD_VERSION='"$(VERSION)"'
# Debug build support
ifdef DEBUG
CFLAGS += -g -DDEBUG -O0
LDFLAGS += -g
else
# Release build optimizations
CFLAGS += -Os -DNDEBUG
LDFLAGS += -s
endif
# Default target - detect platform and build
.DEFAULT_GOAL := detect-platform
# Build every variant this system can build (Windows cross + Linux native)
all: windows linux
# Note: macOS builds require macOS host system with Xcode/clang
detect-platform:
ifeq ($(CC),i686-w64-mingw32-gcc)
$(MAKE) windows
else ifeq ($(CC),x86_64-w64-mingw32-gcc)
$(MAKE) windows
else ifeq ($(findstring mingw,$(CC)),mingw)
$(MAKE) windows
else ifeq ($(OS),Windows_NT)
$(MAKE) windows
else ifeq ($(UNAME_S),Darwin)
$(MAKE) macos
else ifeq ($(UNAME_S),Linux)
$(MAKE) linux
else
@echo "Unknown platform: $(UNAME_S)"
@echo "Trying Linux build..."
$(MAKE) linux
endif
# Platform-specific builds
windows: windows-gui windows-terminal
windows-gui: $(WINDOWS_GUI_TARGET)
# Delay-import libraries, one per DLL, built from the .def files that document
# exactly which entry points npad uses
build/lib%_delay.a: src/platform/delay/%.def
@mkdir -p build
$(MINGW_DLLTOOL) -d $< -D $*.dll -y $@
$(WINDOWS_GUI_TARGET): $(CORE_SOURCES) $(SHARED_SOURCES) $(WINDOWS_GUI_SOURCES) src/main.c src/platform/npad.rc $(DELAY_LIBS)
cd src/platform && $(MINGW_WINDRES) npad.rc -O coff -o npad.res
$(MINGW_CC) $(CFLAGS) -o $@ $(CORE_SOURCES) $(SHARED_SOURCES) $(WINDOWS_GUI_SOURCES) src/main.c src/platform/npad.res $(WINDOWS_GUI_LIBS) $(LDFLAGS)
ifndef DEBUG
$(MINGW_STRIP) $@
endif
@echo "Windows GUI build complete: $(WINDOWS_GUI_TARGET)"
macos: $(MACOS_TARGET)
$(MACOS_TARGET): $(CORE_SOURCES) $(SHARED_SOURCES) $(MACOS_SOURCES) src/main.c
$(CC) $(CFLAGS) -o $@ $^ $(MACOS_LIBS) $(LDFLAGS)
@echo "macOS build complete: $(MACOS_TARGET)"
linux-x11: $(LINUX_X11_TARGET)
$(LINUX_X11_TARGET): $(CORE_SOURCES) $(SHARED_SOURCES) $(LINUX_X11_SOURCES) src/main.c
$(CC) $(CFLAGS) -o $@ $^ $(LINUX_X11_LIBS) $(LDFLAGS)
@echo "Linux X11 build complete: $(LINUX_X11_TARGET)"
linux-wayland: $(LINUX_WAYLAND_TARGET)
$(LINUX_WAYLAND_TARGET): $(CORE_SOURCES) $(SHARED_SOURCES) $(LINUX_WAYLAND_SOURCES) src/main.c
$(CC) $(CFLAGS) -o $@ $^ $(LINUX_WAYLAND_LIBS) $(LDFLAGS)
@echo "Linux Wayland build complete: $(LINUX_WAYLAND_TARGET)"
linux: linux-x11 linux-wayland linux-terminal
windows-terminal: $(WINDOWS_TERMINAL_TARGET)
$(WINDOWS_TERMINAL_TARGET): $(CORE_SOURCES) $(SHARED_SOURCES) $(WINDOWS_TERMINAL_SOURCES) src/main.c
$(MINGW_CC) $(CFLAGS) -o $@ $^ $(WINDOWS_TERMINAL_LIBS) $(LDFLAGS)
ifndef DEBUG
$(MINGW_STRIP) $@
endif
@echo "Windows Terminal build complete: $(WINDOWS_TERMINAL_TARGET)"
linux-terminal: $(LINUX_TERMINAL_TARGET)
$(LINUX_TERMINAL_TARGET): $(CORE_SOURCES) $(SHARED_SOURCES) $(LINUX_TERMINAL_SOURCES) src/main.c
$(CC) $(CFLAGS) -o $@ $^ $(LINUX_TERMINAL_LIBS) $(LDFLAGS)
@echo "Linux Terminal build complete: $(LINUX_TERMINAL_TARGET)"
# Cross-platform terminal builds
terminal: windows-terminal linux-terminal
# Development targets
debug:
$(MAKE) DEBUG=1
debug-linux:
$(MAKE) linux DEBUG=1
debug-windows:
$(MAKE) windows DEBUG=1
debug-windows-gui:
$(MAKE) windows-gui DEBUG=1
debug-windows-terminal:
$(MAKE) windows-terminal DEBUG=1
debug-linux-terminal:
$(MAKE) linux-terminal DEBUG=1
debug-linux-x11:
$(MAKE) linux-x11 DEBUG=1
debug-linux-wayland:
$(MAKE) linux-wayland DEBUG=1
# Cross-compilation targets
windows-cross:
$(MAKE) windows CC=x86_64-w64-mingw32-gcc
# Code quality
#
# Linting is canonical against cppcheck 2.13 (what ubuntu-latest CI runs).
# Older versions are LESS strict and have twice passed changes that CI then
# rejected, so if your distro ships an older one, build 2.13 and point
# CPPCHECK at it rather than trusting a local pass.
CPPCHECK ?= cppcheck
lint:
@command -v $(CPPCHECK) >/dev/null 2>&1 || { echo "$(CPPCHECK) not found. Install with: sudo apt-get install cppcheck"; exit 1; }
$(CPPCHECK) --enable=all --std=c99 --platform=win32A \
--suppress=missingIncludeSystem \
--suppress=unusedFunction \
--inline-suppr \
--error-exitcode=1 \
src/
# Formatting is canonical against clang-format 18 (what ubuntu-latest CI
# runs). If your distro ships another major version, install a pinned one
# (e.g. pip install clang-format==18.1.8) and point CLANG_FORMAT at it.
CLANG_FORMAT ?= clang-format
format:
@command -v $(CLANG_FORMAT) >/dev/null 2>&1 || { echo "$(CLANG_FORMAT) not found. Install with: sudo apt-get install clang-format"; exit 1; }
find src/ -name "*.c" -o -name "*.h" | xargs $(CLANG_FORMAT) -i
format-check:
@command -v $(CLANG_FORMAT) >/dev/null 2>&1 || { echo "$(CLANG_FORMAT) not found. Install with: sudo apt-get install clang-format"; exit 1; }
find src/ -name "*.c" -o -name "*.h" | xargs $(CLANG_FORMAT) --dry-run --Werror
# Testing targets
test: test-file-ops test-error test-encoding test-session test-list-ops test-update-check test-settings test-html-md
@echo "All tests completed"
test-file-ops: tests/test_file_ops
@echo "Running file operations tests..."
./tests/test_file_ops
test-error: tests/test_error
@echo "Running error system tests..."
./tests/test_error
test-encoding: tests/test_encoding
@echo "Running encoding tests..."
./tests/test_encoding
test-session: tests/test_session
@echo "Running session recovery tests..."
./tests/test_session
test-list-ops: tests/test_list_ops
@echo "Running list operations tests..."
./tests/test_list_ops
test-update-check: tests/test_update_check
@echo "Running update-check tests..."
./tests/test_update_check
test-settings: tests/test_settings
@echo "Running settings tests..."
./tests/test_settings
test-html-md: tests/test_html_md
@echo "Running HTML/Markdown tests..."
./tests/test_html_md
tests/test_file_ops: $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_file_ops.c
@mkdir -p tests
$(CC) $(CFLAGS) -o $@ $^ -lpthread
tests/test_error: $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_error.c
@mkdir -p tests
$(CC) $(CFLAGS) -o $@ $^ -lpthread
tests/test_encoding: $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_encoding.c
@mkdir -p tests
$(CC) $(CFLAGS) -o $@ $^ -lpthread
tests/test_session: $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_session.c
@mkdir -p tests
$(CC) $(CFLAGS) -o $@ $^ -lpthread
tests/test_list_ops: $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_list_ops.c
@mkdir -p tests
$(CC) $(CFLAGS) -o $@ $^ -lpthread
tests/test_update_check: $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_update_check.c
$(CC) $(CFLAGS) -o tests/test_update_check $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_update_check.c
tests/test_settings: $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_settings.c
$(CC) $(CFLAGS) -o tests/test_settings $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_settings.c
tests/test_html_md: $(TEST_CORE_SOURCES) $(TEST_FRAMEWORK_SOURCES) tests/test_html_md.c
@mkdir -p tests
$(CC) $(CFLAGS) -o $@ $^ -lpthread
# Cleanup
clean:
rm -f $(WINDOWS_GUI_TARGET) $(WINDOWS_TERMINAL_TARGET) $(MACOS_TARGET) $(LINUX_X11_TARGET) $(LINUX_WAYLAND_TARGET) $(LINUX_TERMINAL_TARGET)
rm -f npad-*.exe npad-*linux* npad-*win32*
find src tests -name '*.o' -delete 2>/dev/null || true
rm -f src/platform/npad.res
rm -rf build
rm -f tests/test_file_ops tests/test_error tests/test_encoding tests/test_session tests/test_list_ops tests/test_update_check tests/test_settings tests/test_html_md
rm -f tests/test_file_ops.exe tests/test_error.exe tests/test_encoding.exe tests/test_session.exe tests/test_list_ops.exe tests/test_update_check.exe tests/test_settings.exe tests/test_html_md.exe
# Installation
install:
ifeq ($(OS),Windows_NT)
@echo "On Windows, copy npad.exe to a directory on your PATH, for example:"
@echo " mkdir %LOCALAPPDATA%\Programs\npad"
@echo " copy npad.exe %LOCALAPPDATA%\Programs\npad\"
else
install -D $(LINUX_X11_TARGET) $(DESTDIR)/usr/local/bin/npad-x11
install -D $(LINUX_WAYLAND_TARGET) $(DESTDIR)/usr/local/bin/npad-wayland
install -D $(LINUX_TERMINAL_TARGET) $(DESTDIR)/usr/local/bin/npad-terminal
ln -sf npad-x11 $(DESTDIR)/usr/local/bin/npad
endif
uninstall:
ifeq ($(OS),Windows_NT)
@echo "Remove the directory you copied npad.exe into, for example:"
@echo " rmdir /s %LOCALAPPDATA%\Programs\npad"
else
rm -f $(DESTDIR)/usr/local/bin/npad $(DESTDIR)/usr/local/bin/npad-x11 $(DESTDIR)/usr/local/bin/npad-wayland $(DESTDIR)/usr/local/bin/npad-terminal
endif
# Help
help:
@echo "npad build system"
@echo ""
@echo "Targets:"
@echo " (default) - Auto-detect platform and build"
@echo " all - Build Windows and Linux variants"
@echo " windows - Build all Windows variants (GUI + Terminal)"
@echo " windows-gui - Build Windows GUI version"
@echo " windows-terminal - Build Windows Terminal version"
@echo " linux - Build all Linux variants (X11 + Wayland + Terminal)"
@echo " linux-x11 - Build for Linux with X11"
@echo " linux-wayland - Build for Linux with Wayland"
@echo " linux-terminal - Build for Linux Terminal (ncurses)"
@echo " terminal - Build all Terminal variants (Windows + Linux)"
@echo " macos - Build for macOS"
@echo " debug - Build with debug symbols"
@echo " lint - Run code linting"
@echo " format - Format code with clang-format"
@echo " format-check - Check code formatting"
@echo " test - Run all tests"
@echo " test-file-ops - Run file operations tests"
@echo " test-error - Run error system tests"
@echo " test-encoding - Run encoding/line-ending tests"
@echo " clean - Remove build artifacts"
@echo " install - Install to system (Linux)"
@echo " uninstall - Remove from system (Linux)"
@echo " help - Show this help"
@echo ""
@echo "Variables:"
@echo " CC - C compiler to use"
@echo " MINGW_CC - MinGW compiler for Windows builds"
@echo " CPPCHECK - cppcheck binary (CI uses 2.13; older is less strict)"
@echo " CLANG_FORMAT - clang-format binary (CI uses 18)"
@echo " DEBUG=1 - Enable debug build"
@echo " VERSION - Version string (auto-detected from git)"
.PHONY: all windows windows-gui windows-terminal linux linux-x11 linux-wayland linux-terminal terminal macos debug debug-windows debug-linux clean install uninstall lint format format-check test test-file-ops test-error test-encoding test-session test-list-ops test-update-check test-settings test-html-md help detect-platform