aboutsummaryrefslogtreecommitdiffstats
path: root/Makefile
blob: ff8a6c2f38030c09f2a8206ecdd1e2c8e2297820 (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
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
.POSIX:
.SUFFIXES:

# Source code location and files to watch for changes.
SRC_DIR     := src
BUILD_DIR   := build
TESTS_DIR   := tests
TEST_FILES  := $(wildcard $(TESTS_DIR)/*.bad)
SRC_MAIN    := $(SRC_DIR)/main.c
SRC_RUN     := tests/semantics.bad
WATCH_SRC   := $(shell find $(SRC_DIR) -name "*.c" -or -name "*.s" -or -name "*.h")
INC_DIRS    := $(shell find $(SRC_DIR) -type d)
INC_FLAGS   := $(addprefix -I,$(INC_DIRS))

# Graphviz viz command.
DOT := dot -Gmargin=0.7 -Gcolor=white -Gfontcolor=white \
		-Ncolor=white -Nfontcolor=white \
		-Ecolor=white -Efontcolor=white -Efontsize=15 \
		-Efontname="Iosevka Nerd Font" \
		-Nfontname="Iosevka Nerd Font" -Nfontsize=20 \
		-T png | kitty +kitten icat

# Output executable.
TARGET := badc
BIN    := $(BUILD_DIR)/$(TARGET)

# Compiler and linker configuration.
CC             := cc
CFLAGS         := -Wall -Wextra -pedantic -std=c11 -DBIN_NAME=\"$(TARGET)\"
CFLAGS         += $(INC_FLAGS)
LDFLAGS        :=
LDLIBS         :=
RELEASE_CFLAGS := -DNDEBUG -O2
DEBUG_CFLAGS   := -DDEBUG -g -fsanitize=address,undefined

.PHONY: build tests clean

# Setup debug/release builds.
#     make clean && make <target> DEBUG=0
#     make clean && make <target> DEBUG=1
DEBUG ?= 0
ifeq ($(DEBUG), 1)
    CFLAGS += $(DEBUG_CFLAGS)
else
    CFLAGS += $(RELEASE_CFLAGS)
endif

main: $(BIN)

$(BIN): $(SRC_MAIN) $(WATCH_SRC) | $(BUILD_DIR)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $(BIN) $(SRC_MAIN) $(LDLIBS)

# Create build directory if needed.
$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

run: $(BIN)
	$(BIN) $(SRC_RUN)

graph-tokens: $(BIN)
	$(BIN) -pl $(SRC_RUN)

graph-parse: $(BIN)
	@echo "parsing tree for: '$(SRC_RUN)'"
	@$(BIN) -pp $(SRC_RUN) | $(DOT)

graph-semantic: $(BIN)
	@echo "semantic tree for: '$(SRC_RUN)'"
	@$(BIN) -ps $(SRC_RUN) | $(DOT)

graph-symbols: $(BIN)
	@echo "symbol table for: '$(SRC_RUN)'"
	@$(BIN) -pt $(SRC_RUN) | $(DOT)

tests: $(BIN)
	@for name in $(TEST_FILES); do\
		COL_OK="\033[32m";\
		COL_RESET="\033[39m";\
		line="                                      $${COL_OK}OK$${COL_RESET}";\
		printf "$${name}\r\033[31m" ;\
		$(BIN) $${name} \
			&& printf "$$line\r" \
			&& printf "$${name}\n";\
	done

# Remove build directory.
clean:
	rm -rf $(BUILD_DIR)