-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathMakefile
More file actions
93 lines (69 loc) · 1.69 KB
/
Makefile
File metadata and controls
93 lines (69 loc) · 1.69 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
# compiler and flags
CC = gcc
CFLAGS = -g -Wall -Wextra -std=c99
LDFLAGS = -ljansson
# lexer and flags
LEX = flex
LFLAGS =
# parser and flags
YACC = bison
YFLAGS = -d
# make directory
MKDIR_P = mkdir -p
# directories
SRC_DIR = src
BIN_DIR = bin
OBJ_DIR = build
# source files
LEX_SRC = $(SRC_DIR)/lexer.l
YACC_SRC = $(SRC_DIR)/parser.y
C_SRC = $(wildcard $(SRC_DIR)/*.c)
# lexer and parser outputs
LEX_OUT = $(OBJ_DIR)/lex.yy.c
YACC_OUT = $(OBJ_DIR)/parser.tab.c
# target file
TARGET = $(BIN_DIR)/compiler
# example directory and file
EXAMPLES_DIR = examples/simple_c_examples
EXAMPLE = $(EXAMPLES_DIR)/example1.c
# tests related
PYTHON = python3
TEST_SCRIPT = tests/test.py
.PHONY: all lex syntax semantic ir clean run clean-run test
# Default target
all: $(TARGET)
$(TARGET): $(YACC_OUT) $(LEX_OUT) | $(BIN_DIR)
@$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(YACC_OUT) $(LEX_OUT) $(C_SRC)
$(LEX_OUT): $(LEX_SRC) | $(OBJ_DIR)
@$(LEX) $(LFLAGS) -o $@ $<
$(YACC_OUT): $(YACC_SRC) | $(OBJ_DIR)
@$(YACC) $(YFLAGS) -o $@ $<
$(BIN_DIR):
@$(MKDIR_P) $@
$(OBJ_DIR):
@$(MKDIR_P) $@
# Lexical analysis
lex: CFLAGS += -DCOMPILER_STAGE=1
lex: $(TARGET)
# Syntax analysis
syntax: CFLAGS += -DCOMPILER_STAGE=2
syntax: $(TARGET)
# Semantic analysis
semantic: CFLAGS += -DCOMPILER_STAGE=3
semantic: $(TARGET)
# Intermediate code generation
ir: CFLAGS += -DCOMPILER_STAGE=4
ir: $(TARGET)
# Clean build files
clean:
@rm -rf $(BIN_DIR) $(OBJ_DIR)
# Run the program
run:
@./$(TARGET) $(EXAMPLE)
# Clean run files
clean-run:
@rm *.out *.json *.ic
# Tests
test: CFLAGS = -g -Wall -std=c99 -DDEBUG=0 -DSTOP_ON_ERROR=1 -DCOMPILER_STAGE=3
test: $(TARGET)
@$(PYTHON) $(TEST_SCRIPT) --binary-path $< --example-dir $(EXAMPLES_DIR)