scl/Makefile

109 lines
3.5 KiB
Makefile
Raw Normal View History

2024-09-28 09:31:23 -04:00
NAME = scl
TARGET = $(NAME).out
2024-09-28 09:31:23 -04:00
SRC_DIR = src
2024-11-02 10:31:55 -04:00
INC_DIR = $(SRC_DIR)/include
BUILD_DIR = build
OBJ_DIR = $(BUILD_DIR)/obj
2024-11-02 10:31:55 -04:00
GRAM_DIR = $(BUILD_DIR)/grammars
TEST_DIR = test
TEST_BUILD_DIR = $(BUILD_DIR)/test
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
CC = clang
LINK = clang
CFLAGS = -Wall -DDBG -ggdb
LDFLAGS = -lm
2025-01-04 10:48:01 -05:00
BATS = bats
2025-01-18 10:41:34 -05:00
BISON = bison
PRINT = echo -e
2024-09-28 09:31:23 -04:00
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
OBJ_FILES = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC_FILES))
OBJ_FILES_NOMAIN = $(filter-out $(OBJ_DIR)/main.o, $(OBJ_FILES)) # Object files without main.c.
2024-11-16 10:15:30 -05:00
GRAM_FILES = $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h
UNITY_DIR = $(TEST_DIR)/Unity
UNITY_C = $(UNITY_DIR)/src/unity.c
UNITY_H = $(UNITY_DIR)/src/unity.h
UNITY_OBJ = $(TEST_BUILD_DIR)/unity.o
TEST_SRC_FILES = $(wildcard $(TEST_DIR)/*.c)
TEST_OBJ_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_OBJ_DIR)/%.o, $(TEST_SRC_FILES))
TEST_BIN_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_BUILD_DIR)/%.out, $(TEST_SRC_FILES))
2025-01-04 10:48:01 -05:00
TEST_VAL_DIR = $(TEST_DIR)/validation
2024-09-28 09:31:23 -04:00
RESETCOLOR = \033[0m
WHITE = $(RESETCOLOR)\033[37m
WHITE_BOLD = $(RESETCOLOR)\033[37;1m
2025-01-20 18:05:56 -05:00
RED_BOLD = $(RESETCOLOR)\033[31;1m
2024-09-28 09:38:01 -04:00
all: $(TARGET)
2024-09-28 09:31:23 -04:00
release: clean
release: CFLAGS = -Wall -O2
release: $(TARGET)
# Run the target.
2024-10-13 23:46:03 -04:00
run: $(TARGET)
./$(TARGET)
2024-10-13 23:46:03 -04:00
2024-11-02 10:31:55 -04:00
# Generate grammars with bison.
2024-11-16 10:15:30 -05:00
$(GRAM_FILES): $(SRC_DIR)/grammar.y
2024-11-02 10:31:55 -04:00
@ mkdir -p $(GRAM_DIR)
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR)"
$(BISON) $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
2024-11-02 10:31:55 -04:00
2024-11-09 11:09:57 -05:00
# Compile grammars.
$(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Compiling grammars...$(RESETCOLOR)"
2024-11-09 11:09:57 -05:00
$(CC) $(CFLAGS) -c $< -o $@
2024-09-28 09:31:23 -04:00
2024-11-02 10:31:55 -04:00
# Lexer depends on grammars.
2024-11-16 10:15:30 -05:00
$(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c $(GRAM_FILES)
2024-11-23 10:21:34 -05:00
@ mkdir -p $(OBJ_DIR)
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
2024-11-09 11:09:57 -05:00
$(CC) $(CFLAGS) -c $< -o $@
2024-11-02 10:31:55 -04:00
# Compile project source objects.
2024-11-02 10:31:55 -04:00
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h
2024-09-28 09:31:23 -04:00
@ mkdir -p $(OBJ_DIR)
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
$(CC) $(CFLAGS) -c $< -o $@
2024-09-28 09:31:23 -04:00
2024-11-09 11:09:57 -05:00
# Link to final binary.
2024-11-16 10:15:30 -05:00
$(TARGET): $(OBJ_DIR)/grammar.o $(OBJ_FILES)
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Linking $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
$(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS)
# Compile Unity object.
$(UNITY_OBJ): $(UNITY_C) $(UNITY_H)
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Compiling Unity...$(RESETCOLOR)"
$(CC) $(CFLAGS) -D UNITY_OUTPUT_COLOR -c $< -o $@
# Compile test object.
$(TEST_OBJ_DIR)/test_%.o: $(TEST_DIR)/test_%.c
2025-01-18 11:10:50 -05:00
@ mkdir -p $(TEST_OBJ_DIR)
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Compiling test object $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
$(CC) $(CFLAGS) -c $< -o $@
# Link final test binary.
$(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/grammar.o $(OBJ_FILES_NOMAIN) $(UNITY_OBJ)
2025-01-18 11:10:50 -05:00
@ mkdir -p $(TEST_BUILD_DIR)
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Linking test binary $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
$(LINK) -o $@ $? $(LDFLAGS)
# Run the test files.
2025-01-12 20:47:06 -05:00
test: $(TARGET) $(TEST_BIN_FILES)
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Running unit tests...$(RESETCOLOR)"
2025-01-20 18:05:56 -05:00
for test in $(TEST_BIN_FILES); do ./$${test} || echo -e "$(RED_BOLD) BAD EXIT ON $${test} $(RESETCOLOR)"; done
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Running validation tests...$(RESETCOLOR)"
2025-01-04 10:48:01 -05:00
$(BATS) $(TEST_VAL_DIR)
2024-09-28 09:38:01 -04:00
clean:
2025-01-18 10:41:34 -05:00
@ $(PRINT) "$(WHITE_BOLD)Cleaning up...$(RESETCOLOR)"
rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) $(GRAM_DIR)/* $(UNITY_OBJ)
2024-10-13 23:46:03 -04:00
.PHONY: all clean test nocolor release run
2024-12-28 12:18:36 -05:00
.PRECIOUS: $(TEST_OBJ_FILES)