summaryrefslogtreecommitdiff
path: root/Makefile
blob: 4dcc90a3f4cf3f829acbe07725481f8543f3e057 (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
# Compiler and flags
CC = gcc
CFLAGS = -Wall -O2

# Source and target names
TARGET = chproc
SRC = $(TARGET).c
PRE = $(TARGET).i
ASM = $(TARGET).s
OBJ = $(TARGET).o

# Example argument for test
ARG = --list init

# Default: build executable
all: $(TARGET)

# Stage 1: Preprocess
$(PRE): $(SRC)
	$(CC) -E $(CFLAGS) $< -o $@

# Stage 2: Compile to assembly
$(ASM): $(PRE)
	$(CC) -S $(CFLAGS) $< -o $@

# Stage 3: Assemble to object file
$(OBJ): $(ASM)
	$(CC) -c $(CFLAGS) $< -o $@

# Stage 4: Link object to executable
$(TARGET): $(OBJ)
	$(CC) $(CFLAGS) $< -o $@

# Run tests
define run_test
	@name="$(1) $(2)"; \
	width=40; \
	printf "%s" "$$name"; \
	dots=$$((width - $${#name})); \
	for i in $$(seq 1 $$dots); do printf "."; done; \
	./$(TARGET) $(2) >/dev/null 2>&1; \
	ec=$$?; \
	if [ $$ec -eq 0 ] || [ $$ec -eq 2 ]; then \
		echo " OK"; \
	else \
		echo " FAILED (exit $$ec)"; \
		exit 1; \
	fi
endef

test: $(TARGET)
	$(call run_test,$(TARGET) no args,)
	$(call run_test,$(TARGET),$(ARG),)

# Clean all generated files
clean:
	rm -v -f $(PRE) $(ASM) $(OBJ) $(TARGET)