Update Makefile: Correctly compiles and cleans all subdirectories

This commit is contained in:
sBubshait 2024-06-15 03:44:30 +01:00
parent 3edaa6041b
commit 3e0991e257

View File

@ -1,16 +1,40 @@
CC ?= gcc
CFLAGS ?= -std=c17 -g\
-D_POSIX_SOURCE -D_DEFAULT_SOURCE\
-Wall -Werror -pedantic
# Compiler and flags
CC := gcc
CFLAGS := -std=c17 -g -D_POSIX_SOURCE -D_DEFAULT_SOURCE -Wall -Werror -pedantic
.SUFFIXES: .c .o
# Directories
SRC_DIR := .
UTIL_DIR := util
ASM_DIR := assembler
EMU_DIR := emulator
.PHONY: all clean
# Source files
ASSEMBLE_SRC := $(UTIL_DIR)/fileio.c $(UTIL_DIR)/binary_util.c $(ASM_DIR)/encode.c $(ASM_DIR)/parse.c $(ASM_DIR)/tokenise.c $(ASM_DIR)/string_util.c $(ASM_DIR)/symboltable.c
EMULATE_SRC := $(UTIL_DIR)/fileio.c $(UTIL_DIR)/binary_util.c $(EMU_DIR)/execute.c $(EMU_DIR)/decode.c $(EMU_DIR)/print.c $(EMU_DIR)/machine_util.c
all: emulate assemble
# Object files
ASSEMBLE_OBJ := $(ASSEMBLE_SRC:.c=.o) assemble.o
EMULATE_OBJ := $(EMULATE_SRC:.c=.o) emulate.o
assemble: assemble.o util/fileio.o util/binary_util.o assembler/encode.o assembler/parse.o assembler/tokenise.o assembler/string_util.o assembler/symboltable.o
emulate: emulate.o util/fileio.o emulator/execute.o emulator/decode.o emulator/print.o emulator/machine_util.o util/binary_util.o
# Targets
TARGETS := assemble emulate
# Default target
.PHONY: all
all: $(TARGETS)
# Rules to build each target
assemble: $(ASSEMBLE_OBJ)
$(CC) $(CFLAGS) -o $@ $^
emulate: $(EMULATE_OBJ)
$(CC) $(CFLAGS) -o $@ $^
# Pattern rule to compile C files
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
# Clean up
.PHONY: clean
clean:
$(RM) *.o assemble emulate emulator/execute.o emulator/decode.o
$(RM) $(ASSEMBLE_OBJ) $(EMULATE_OBJ) $(TARGETS)