From ff22321577de8ecbfea01fbdf97b992195e557b5 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 9 Feb 2025 23:15:28 -0500 Subject: [PATCH] Adds more things. --- .gitignore | 8 ++++++++ Makefile | 48 ++++++++++++++++++++++++++++++++++++++++++++++ config.mk | 28 +++++++++++++++++++++++++++ src/include/main.h | 6 ++++++ src/main.c | 8 ++++++++ 5 files changed, 98 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 config.mk create mode 100644 src/include/main.h create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0864e6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.o +*.so +tags +*.out +.cache +build/* +vgcore.* +compile_commands.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f596937 --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ +include config.mk + +all: $(TARGET) + +release: clean +release: CFLAGS = -Wall -O2 +release: $(TARGET) + +# Run the target. +run: $(TARGET) + ./$(TARGET) + +# Generate grammars with bison. +$(GRAM_FILES): $(SRC_DIR)/grammar.y + @ mkdir -p $(GRAM_DIR) + @ $(PRINT) "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR)" + $(BISON) $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h + +# Compile grammars. +$(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o + @ $(PRINT) "$(WHITE_BOLD)Compiling grammars...$(RESETCOLOR)" + $(CC) $(CFLAGS) -c $< -o $@ + +# Lexer depends on grammars. +$(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c $(GRAM_FILES) + @ mkdir -p $(OBJ_DIR) + @ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)" + $(CC) $(CFLAGS) -c $< -o $@ + +# Compile project source objects. +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h + @ mkdir -p $(OBJ_DIR) + @ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)" + $(CC) $(CFLAGS) -c $< -o $@ + +# Link to final binary. +$(TARGET): $(OBJ_DIR)/grammar.o $(OBJ_FILES) + @ $(PRINT) "$(WHITE_BOLD)Linking $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)" + $(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS) + +clean: + @ $(PRINT) "$(WHITE_BOLD)Cleaning up...$(RESETCOLOR)" + rm -rf $(OBJ_DIR)/*.o $(TARGET) $(GRAM_DIR)/* + +lines: + @ wc -l $(SRC_FILES) $(INC_FILES) $(GRAM_SRC) + +.PHONY: all clean release run lines diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..bfe0da2 --- /dev/null +++ b/config.mk @@ -0,0 +1,28 @@ +NAME = yamth + +TARGET = $(NAME).out + +SRC_DIR = src +INC_DIR = $(SRC_DIR)/include +BUILD_DIR = build +OBJ_DIR = $(BUILD_DIR)/obj +GRAM_DIR = $(BUILD_DIR)/grammars + +CC = clang +LINK = clang +CFLAGS = -Wall -DDBG -ggdb -fsanitize=leak +LDFLAGS = -lm +BISON = bison +PRINT = echo -e + +SRC_FILES = $(wildcard $(SRC_DIR)/*.c) +INC_FILES = $(wildcard $(INC_DIR)/*.h) +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. +GRAM_SRC = $(SRC_DIR)/grammar.y +GRAM_FILES = $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h + +RESETCOLOR = \033[0m +WHITE = $(RESETCOLOR)\033[37m +WHITE_BOLD = $(RESETCOLOR)\033[37;1m +RED_BOLD = $(RESETCOLOR)\033[31;1m diff --git a/src/include/main.h b/src/include/main.h new file mode 100644 index 0000000..bb01010 --- /dev/null +++ b/src/include/main.h @@ -0,0 +1,6 @@ +#ifndef MAIN_H +#define MAIN_H + +// Not much to see here... + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..1ff7e91 --- /dev/null +++ b/src/main.c @@ -0,0 +1,8 @@ +#include +#include "include/main.h" + +int main(int argc, char** argv) { + printf("Hello, world.\n"); + + return 0; +}