-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
379 lines (329 loc) · 11.1 KB
/
Makefile
File metadata and controls
379 lines (329 loc) · 11.1 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
# EDN.C Makefile
# Supports x86_64 (SSE4.2, AVX2) and ARM64 (NEON) SIMD
# WebAssembly support via Emscripten
# Native build tools
CC ?= gcc
AR ?= ar
# WebAssembly build tools (Emscripten)
EMCC ?= emcc
EMAR ?= emar
CFLAGS = -std=c11 -Wall -Wextra -Wpedantic -O2
INCLUDES = -Iinclude -Isrc
LDFLAGS =
# Detect platform
UNAME_M := $(shell uname -m)
UNAME_S := $(shell uname -s)
# Platform-specific flags
ifeq ($(UNAME_M),arm64)
# Apple Silicon M1/M2 - NEON enabled by default
ARCH_FLAGS =
ARCH = arm64
else ifeq ($(UNAME_M),aarch64)
# ARM64 Linux
ARCH_FLAGS =
ARCH = arm64
else ifeq ($(UNAME_M),x86_64)
# x86_64 - use SSE4.2 as baseline
ARCH_FLAGS = -msse4.2
ARCH = x86_64
else
# Unknown architecture
ARCH_FLAGS =
ARCH = generic
endif
# Platform-specific libraries
ifeq ($(UNAME_S),Linux)
LDLIBS = -lm
else
LDLIBS =
endif
# Debug build
DEBUG ?= 0
ifeq ($(DEBUG),1)
CFLAGS += -g -O0 -DDEBUG
# Note: Sanitizers added only to LDFLAGS to avoid static library linking issues
LDFLAGS += -fsanitize=address,leak,undefined
else
CFLAGS += -DNDEBUG
endif
# Clojure extension features (disabled by default)
# Includes: map namespace syntax, extended characters, metadata, ratio, extended integers
CLOJURE_EXTENSION ?= 0
ifneq (,$(filter 1,$(CLOJURE_EXTENSION) $(ALL)))
CFLAGS += -DEDN_ENABLE_CLOJURE_EXTENSION
endif
# Experimental extension features (disabled by default)
# Includes: text blocks, underscore in numeric literals
EXPERIMENTAL_EXTENSION ?= 0
ifneq (,$(filter 1,$(EXPERIMENTAL_EXTENSION) $(ALL)))
CFLAGS += -DEDN_ENABLE_EXPERIMENTAL_EXTENSION
endif
# Verbose mode
VERBOSE ?= 0
ifeq ($(VERBOSE),1)
Q =
else
Q = @
endif
# Source files
SRCS = src/edn.c src/arena.c src/simd.c src/string.c src/number.c src/character.c src/identifier.c src/symbolic.c src/equality.c src/uniqueness.c src/collection.c src/tagged.c src/discard.c src/reader.c src/metadata.c src/newline_finder.c
# Native build objects and library
OBJS = $(SRCS:.c=.o)
LIB = libedn.a
# Shared library
ifeq ($(UNAME_S),Darwin)
SHARED_LIB = libedn.dylib
SHARED_FLAGS = -dynamiclib -install_name @rpath/$(SHARED_LIB)
else
SHARED_LIB = libedn.so
SHARED_FLAGS = -shared -fPIC
endif
# WebAssembly build objects and outputs
WASM_SRCS = $(SRCS) bindings/wasm/wasm_edn.c
WASM_OBJS = $(WASM_SRCS:.c=.wasm.o)
WASM_LIB = libedn.wasm.a
WASM_MODULE = edn.wasm
WASM_JS = edn.js
# WebAssembly SIMD128 flags
WASM_COMPILE_FLAGS = -msimd128 -mbulk-memory
WASM_LINK_FLAGS = -msimd128 -mbulk-memory
WASM_LINK_FLAGS += -s EXPORTED_RUNTIME_METHODS=ccall,cwrap,Emval
WASM_LINK_FLAGS += -s ALLOW_MEMORY_GROWTH=1
WASM_LINK_FLAGS += -s INITIAL_MEMORY=33554432
WASM_LINK_FLAGS += -s MAXIMUM_MEMORY=4294967296
WASM_LINK_FLAGS += -s EXPORT_ALL=0
WASM_LINK_FLAGS += -lembind
WASM_LINK_FLAGS += -s STACK_SIZE=5242880
# Test files
TEST_SRCS = $(wildcard test/*.c)
TEST_BINS = $(TEST_SRCS:.c=)
# Benchmark files
BENCH_SRCS = $(wildcard bench/*.c)
BENCH_BINS = $(BENCH_SRCS:.c=)
# Example files
EXAMPLES_SRCS = $(wildcard examples/*.c)
EXAMPLES_BINS = $(EXAMPLES_SRCS:.c=)
# Default target
.PHONY: all
all: $(LIB)
@echo "✓ Build complete: $(LIB)"
@echo " Run 'make test' to run tests"
@echo " Run 'make shared' to build shared library ($(SHARED_LIB))"
@echo " Run 'make info' to see build configuration"
# Build shared library target
.PHONY: shared
shared: $(SHARED_LIB)
@echo "✓ Shared library build complete: $(SHARED_LIB)"
@echo " Use this for FFI bindings (Java, Python, etc.)"
# Build static library
$(LIB): $(OBJS)
@echo " AR $@"
$(Q)$(AR) rcs $@ $^
# Build shared library
$(SHARED_LIB): $(SRCS)
@echo " CC $@ (shared)"
$(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) $(INCLUDES) $(SHARED_FLAGS) -fPIC $^ -o $@ $(LDLIBS)
# Compile source files
%.o: %.c
@echo " CC $@"
$(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) $(INCLUDES) -c $< -o $@
# Build and run tests
.PHONY: test
test: $(TEST_BINS)
@echo "Running tests..."
$(Q)for test in $(TEST_BINS); do \
echo ""; \
./$$test || exit 1; \
done
# Build individual test
test/%: test/%.c $(LIB)
@echo " CC $@"
$(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) $(INCLUDES) $< $(LIB) $(LDFLAGS) $(LDLIBS) -o $@
@./$@
# Build and run quick benchmark
.PHONY: bench
bench: bench/bench_integration
@./bench/bench_integration
# Run Clojure benchmarks (clojure.edn and fast-edn)
.PHONY: bench-clj
bench-clj:
@clojure -M -m bench-integration
# Build and run comparison benchmarks (C, then Clojure)
.PHONY: bench-compare
bench-compare: bench bench-clj
# Build and run WebAssembly benchmarks
.PHONY: bench-wasm
bench-wasm: wasm
@echo "Running WebAssembly benchmarks..."
@node --expose-gc bench/bench_wasm.js
# Build and run all benchmarks (C + WASM)
.PHONY: bench-all
bench-all: $(BENCH_BINS) bench-wasm
@echo "Running all benchmarks..."
$(Q)for benchmark in $(BENCH_BINS); do \
echo ""; \
./$$benchmark || exit 1; \
done
# Build benchmarks only
.PHONY: bench-build
bench-build: $(BENCH_BINS)
bench/%: bench/%.c $(LIB)
@echo " CC $@"
$(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) $(INCLUDES) -D_POSIX_C_SOURCE=200809L -O3 $< $(LIB) $(LDFLAGS) $(LDLIBS) -o $@
# Build examples
.PHONY: examples
examples: $(EXAMPLES_BINS)
examples/%: examples/%.c $(LIB)
@echo " CC $@"
$(Q)$(CC) $(CFLAGS) $(ARCH_FLAGS) $(INCLUDES) $< $(LIB) $(LDFLAGS) $(LDLIBS) -o $@
# Build CLI tool
.PHONY: cli
cli: examples/edn_cli
# Build TUI tool
.PHONY: tui
tui: examples/edn_tui
# Debug build
.PHONY: debug
debug:
$(MAKE) DEBUG=1
# WebAssembly targets
.PHONY: wasm
wasm: $(WASM_MODULE)
@echo "✓ WebAssembly build complete: $(WASM_MODULE), $(WASM_JS)"
@echo " SIMD128 support enabled"
@echo ""
@echo "Files created in project root:"
@echo " - edn.wasm (WebAssembly module)"
@echo " - edn.js (JavaScript loader)"
@echo ""
@echo "To test in Node.js, run:"
@echo " node examples/wasm_example.js"
# Build WebAssembly module with JavaScript glue code
$(WASM_MODULE): $(WASM_OBJS)
@echo " EMCC $@"
$(Q)$(EMCC) -std=gnu11 -O2 $(WASM_LINK_FLAGS) $(INCLUDES) $(WASM_OBJS) -o $(WASM_JS)
# Build WebAssembly static library
$(WASM_LIB): $(WASM_OBJS)
@echo " EMAR $@"
$(Q)$(EMAR) rcs $@ $^
# Compile WebAssembly object files
%.wasm.o: %.c
@echo " EMCC $@"
$(Q)$(EMCC) -std=gnu11 -Wall -Wextra -Wpedantic -O2 \
-Wno-dollar-in-identifier-extension \
-Wno-gnu-zero-variadic-macro-arguments \
$(filter -D%, $(CFLAGS)) $(WASM_COMPILE_FLAGS) $(INCLUDES) -c $< -o $@
# Clean build artifacts
.PHONY: clean
clean:
@echo " CLEAN"
$(Q)rm -f $(OBJS) $(LIB) $(SHARED_LIB)
$(Q)rm -f $(WASM_OBJS) $(WASM_LIB) $(WASM_MODULE) $(WASM_JS) edn.wasm.map
$(Q)rm -f $(TEST_BINS)
$(Q)rm -f $(BENCH_BINS)
$(Q)rm -f $(EXAMPLES_BINS)
$(Q)rm -rf *.dSYM test/*.dSYM bench/*.dSYM
$(Q)rm -rf profile_*.trace
# Print configuration
.PHONY: info
info:
@echo "EDN.C Build Configuration"
@echo "========================="
@echo "Architecture: $(ARCH)"
@echo "Compiler: $(CC)"
@echo "CFLAGS: $(CFLAGS) $(ARCH_FLAGS)"
@echo "Debug: $(DEBUG)"
@echo "Sources: $(SRCS)"
@echo "Tests: $(TEST_SRCS)"
@echo "Benchmarks: $(BENCH_SRCS)"
@echo ""
@echo "WebAssembly Configuration"
@echo "========================="
@echo "Compiler: $(EMCC)"
@echo "CFLAGS: $(CFLAGS) $(WASM_FLAGS)"
@echo "WASM Sources: $(WASM_SRCS)"
.PHONY: info-wasm
info-wasm:
@echo "EDN.C WebAssembly Build Configuration"
@echo "======================================"
@echo "Compiler: $(EMCC)"
@echo "CFLAGS: $(CFLAGS) $(WASM_FLAGS)"
@echo "SIMD: SIMD128 enabled"
@echo "Sources: $(WASM_SRCS)"
@echo ""
@echo "Optional features:"
@echo " CLOJURE_EXTENSION: $(CLOJURE_EXTENSION)"
@echo " EXPERIMENTAL_EXTENSION: $(EXPERIMENTAL_EXTENSION)"
# Help
# Format all source files with clang-format
.PHONY: format
format:
@echo " FORMAT all C files"
$(Q)find src include test bench examples bindings -name '*.[ch]' -exec clang-format -i {} +
# Check formatting without modifying files
.PHONY: format-check
format-check:
@echo " FORMAT-CHECK"
$(Q)find src include test bench examples bindings -name '*.[ch]' -exec clang-format --dry-run -Werror {} +
# Generate compile_commands.json and .clangd for LSP
.PHONY: compile-commands
compile-commands: clean
@echo " GEN compile_commands.json + .clangd"
@if command -v bear >/dev/null 2>&1; then \
bear -- $(MAKE) all test; \
elif command -v compiledb >/dev/null 2>&1; then \
compiledb make all test; \
else \
echo "Error: Please install 'bear' or 'compiledb' to generate compile_commands.json"; \
echo " macOS: brew install bear"; \
echo " Linux: apt-get install bear (or pip install compiledb)"; \
exit 1; \
fi
@./generate_clangd.sh
# Generate .clangd configuration with auto-detected Emscripten paths
.PHONY: clangd
clangd:
@./generate_clangd.sh
.PHONY: help
help:
@echo "EDN.C Makefile targets:"
@echo ""
@echo "Native builds:"
@echo " make - Build static library ($(LIB))"
@echo " make shared - Build shared library ($(SHARED_LIB)) for FFI"
@echo " make test - Build and run all tests"
@echo " make cli - Build CLI tool (examples/edn_cli)"
@echo " make tui - Build TUI viewer (examples/edn_tui)"
@echo " make examples - Build all example programs"
@echo " make bench - Build and run quick benchmark (C integration)"
@echo " make bench-clj - Run Clojure benchmarks (clojure.edn and fast-edn)"
@echo " make bench-compare - Run C and Clojure benchmarks for comparison"
@echo " make bench-all - Build and run all benchmarks (C + WASM)"
@echo " make bench-build - Build benchmarks only (don't run)"
@echo " make debug - Build with debug symbols and sanitizers"
@echo ""
@echo "WebAssembly builds:"
@echo " make wasm - Build WebAssembly module ($(WASM_MODULE))"
@echo " make bench-wasm - Build and run WebAssembly benchmarks"
@echo " make info-wasm - Print WebAssembly build configuration"
@echo ""
@echo "Development tools:"
@echo " make format - Format all C files with clang-format"
@echo " make format-check - Check formatting without modifying files"
@echo " make compile-commands - Generate compile_commands.json + .clangd for LSP"
@echo " make clangd - Regenerate .clangd configuration only"
@echo " make clean - Remove all build artifacts"
@echo " make info - Print build configuration (native + WASM)"
@echo " make help - Show this help message"
@echo ""
@echo "Options (apply to both native and WASM builds):"
@echo " CLOJURE_EXTENSION=1 - Enable Clojure extensions (ratio, extended integers, metadata, etc.)"
@echo " EXPERIMENTAL_EXTENSION=1 - Enable experimental features (text blocks, underscores in numbers)"
@echo " ALL=1 - Enable all features at once"
@echo " VERBOSE=1 - Show full compiler commands"
@echo " DEBUG=1 - Enable debug build (native only)"
@echo ""
@echo "WebAssembly requirements:"
@echo " - Emscripten SDK (https://emscripten.org/)"
@echo " - Node.js with WASM SIMD support (v16.4+ or use --experimental-wasm-simd)"
.DEFAULT_GOAL := all