From 315b59ec29d8bb20e751b97877e378cd8e07ce09 Mon Sep 17 00:00:00 2001 From: AnnaSnoeijs Date: Sat, 14 Jun 2025 22:13:29 +0200 Subject: [PATCH] Commented makefile --- makefile | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index f08e653..707ac60 100644 --- a/makefile +++ b/makefile @@ -1,27 +1,46 @@ #!/usr/bin/make -f + +# Target architecture for compilation +ARCH = native + +# Optimization level +O = 3 + +# Flags for the compiler FLAGS = -std=c23 -FLAGS += -O3 -FLAGS += -march=native +FLAGS += -O$(O) +FLAGS += -march=$(ARCH) +# Only override gcc default tuning if specified in make command line +ifneq (, $(TUNE)) + FLAGS += -mtune=$(TUNE) +endif FLAGS += -pedantic FLAGS += -Wall FLAGS += -Wextra FLAGS += -Werror +# This flag is because of the compiler otherwise giving too much warning FLAGS += -Wformat-truncation=0 +# Compile flag for defining GITHASH to put at the end of the version string GITFLAG = -D'GITHASH=$(shell git rev-parse --short=1 HEAD)' +# Directories where .o files and excecutables will be placed OBJDIR = obj OUTDIR = out +# The UI to compile for by default using make run #UI_TARGET = vt100 UI_TARGET = ncurses +# The URL of the plaintext version of the lisence, for the --license option LICENSEURL = https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt +# C compiler CC = cc .NOTINTERMEDIATE: +# Messages to print during different steps in making MSG_FETCHING = FETCHING: MSG_LINKING = Linking: MSG_COMPILING = Compiling C: @@ -29,44 +48,57 @@ MSG_CLEANING = Cleaning: MSG_CLEANING_OBJ = Cleaning $(OBJDIR): MSG_CLEANING_OUT = Cleaning $(OUTDIR): +# Compile for all UI targets all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf) +# Compile and run the default UI target run: $(OUTDIR)/connect4_$(UI_TARGET).elf ./$< +# Compile and run with a specific UI target run_%: $(OUTDIR)/connect4_%.elf ./$< +# Compile specifically the ncurses version +# This one's special because it needs -lncursesw $(OUTDIR)/connect4_ncurses.elf: $(addprefix $(OBJDIR)/,ui_ncurses.o logic.o connect4.o LICENSE.o) @echo $(MSG_LINKING) $@ @mkdir -p $(@D) $(CC) $(FLAGS) -lncursesw -o $@ $^ +# Compile the final excecutable for a given UI target $(OUTDIR)/connect4_%.elf: $(addprefix $(OBJDIR)/,ui_%.o logic.o connect4.o LICENSE.o) @echo $(MSG_LINKING) $@ @mkdir -p $(@D) $(CC) $(FLAGS) -o $@ $^ +# Make an object file for the license so it does not all need to be in the code $(OBJDIR)/LICENSE.o: $(OBJDIR)/LICENSE @echo $(MSG_LINKING) $@ @mkdir -p $(@D) cd $(@D); ld -r -b binary -o $(@F) $(^F) +# Compile the object file of the main code. +# This one's special because it includes the git hash $(OBJDIR)/connect4.o: connect4.c @echo $(MSG_COMPILING) $^ @mkdir -p $(@D) $(CC) -c $(FLAGS) $(GITFLAG) -o $@ $^ +# Generic object file compile $(OBJDIR)/%.o: %.c @echo $(MSG_COMPILING) $^ @mkdir -p $(@D) $(CC) -c $(FLAGS) -o $@ $^ +# Download the license file in non-markdown form. +# This is not cleaned as that would be too silly $(OBJDIR)/LICENSE: @echo $(MSG_FETCHING) $@ curl $(LICENSEURL) \ | sed -n '/END OF TERMS AND CONDITIONS/q;p' > $(OBJDIR)/LICENSE +# Remove all compilation output and intermediate files clean: @echo $(MSG_CLEANING) @echo $(MSG_CLEANING_OBJ)