-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
87 lines (66 loc) · 2.46 KB
/
Copy pathMakefile
File metadata and controls
87 lines (66 loc) · 2.46 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
# Makefile for armvm - ARM assembler and virtual machine
# Supports Linux and Mac
# Compiler settings
CC = gcc
CFLAGS = -Wall -Wextra -O2
LDFLAGS = -lm
# Directories
SRCDIR = armvm
TESTDIR = test
OBJDIR = build
# Source files
SRCS = $(SRCDIR)/armvm.c $(SRCDIR)/compiler.c $(SRCDIR)/armcomp.c \
$(SRCDIR)/expr.c $(SRCDIR)/memory.c $(SRCDIR)/libpvm.c
# Object files
OBJS = $(OBJDIR)/armvm.o $(OBJDIR)/compiler.o $(OBJDIR)/armcomp.o \
$(OBJDIR)/expr.o $(OBJDIR)/memory.o $(OBJDIR)/libpvm.o
# Test files
TEST_SRCS = $(TESTDIR)/armtest.c
TEST_OBJS = $(TEST_SRCS:$(TESTDIR)/%.c=$(OBJDIR)/test_%.o)
# Output executable
TARGET = armvm-compiler
TEST_TARGET = $(OBJDIR)/armtest
# Default target - build the compiler and VM
all: $(TARGET)
# Create build directory
$(OBJDIR):
mkdir -p $(OBJDIR)
# Compile object files (pattern rule)
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
# Explicit dependencies for object files
$(OBJDIR)/armvm.o: $(SRCDIR)/armvm.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/compiler.o: $(SRCDIR)/compiler.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/armcomp.o: $(SRCDIR)/armcomp.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/expr.o: $(SRCDIR)/expr.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/memory.o: $(SRCDIR)/memory.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/libpvm.o: $(SRCDIR)/libpvm.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
# Link the main executable
$(TARGET): $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -o $(TARGET)
# Compile test object files
$(OBJDIR)/test_%.o: $(TESTDIR)/%.c | $(OBJDIR)
$(CC) $(CFLAGS) -I$(SRCDIR) -c $< -o $@
# Link test executable
# Note: This uses -Dmain=_unused_main to rename the main() in compiler.c when compiling it
# for tests, allowing the test suite's main() to be used instead. This is a simple workaround
# to avoid having to refactor compiler.c. If compiler.c is refactored in the future to separate
# test helper functions from main(), this approach should be updated to link test_program() and
# run_program() from a separate object file.
$(TEST_TARGET): $(TEST_OBJS) $(filter-out $(OBJDIR)/compiler.o,$(OBJS)) $(SRCDIR)/compiler.c | $(OBJDIR)
$(CC) $(TEST_OBJS) $(filter-out $(OBJDIR)/compiler.o,$(OBJS)) $(SRCDIR)/compiler.c -Dmain=_unused_main $(CFLAGS) $(LDFLAGS) -o $(TEST_TARGET)
# Run tests
test: $(TEST_TARGET)
@echo "Running tests..."
@$(TEST_TARGET)
# Clean build artifacts
clean:
rm -rf $(OBJDIR) $(TARGET)
# Phony targets
.PHONY: all test clean