#!/usr/bin/make -f # Target architecture for compilation ARCH = native # Optimization level O = s # Flags for the compiler FLAGS = -std=c99 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 FLAGS += -Wconversion FLAGS += -Wmissing-prototypes FLAGS += -Wlogical-op FLAGS += -Wdisabled-optimization FLAGS += -fanalyzer FLAGS += -flto FLAGS += -s FLAGS += -Wl,--gc-sections # These flags are because of the compiler otherwise giving too much warning #FLAGS += -Wno-sign-conversion #FLAGS += -Wformat-truncation=0 ifneq (, $(EXTRA_FLAGS)) FLAGS += $(EXTRA_FLAGS) endif # Compile flag for defining GITHASH to put at the end of the version string FLAGS += -D'GITHASH=$(shell git rev-parse --short=1 HEAD)' # Directories where .o files and excecutables will be placed OBJDIR = obj OUTDIR = out TESTDIR = test # The UI to compile for by default using make run #UI_TARGET = vt100 UI_TARGET = ncurses UI_TESTING = stub # 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 LD = ld CC_W64 = x86_64-w64-mingw32-gcc LD_W64 = x86_64-w64-mingw32-ld # Test options TEST_COLUMNS = 1000 TEST_ROWS = 30 .NOTINTERMEDIATE: # Messages to print during different steps in making MSG_FETCHING = FETCHING: MSG_LINKING = Linking: MSG_COMPILING = Compiling C: MSG_CLEANING = Cleaning: MSG_CLEANING_OBJ = Cleaning $(OBJDIR): MSG_CLEANING_OUT = Cleaning $(OUTDIR): MSG_CLEANING_TEST = Cleaning $(TESTDIR): # Compile for all UI targets all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf stub.elf stub.exe) # 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 and run with a specific UI target w64_run_%: $(OUTDIR)/connect4_%.exe wine ./$< # Show result of test test: $(TESTDIR)/test.diff # Diff between the tests $(TESTDIR)/test.diff: $(TESTDIR)/test.log $(TESTDIR)/test.log.slow @mkdir -p $(@D) diff -y $^ > $@ || tail $@ diff -q $^ # Log of testing fast logic $(TESTDIR)/test.log: $(OUTDIR)/connect4_$(UI_TESTING).elf @mkdir -p $(@D) ./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves # Log of testing slow logic $(TESTDIR)/test.log.slow: $(OUTDIR)/connect4_$(UI_TESTING).elf @mkdir -p $(@D) ./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves --slow-calcWins # 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 $@ $^ # Compile a windows excecutable $(OUTDIR)/connect4_%.exe: $(addprefix $(OBJDIR)/w64_,ui_%.o logic.o connect4.o LICENSE.o) @echo $(MSG_LINKING) $@ @mkdir -p $(@D) $(CC_W64) $(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) # Make an object file for the license so it does not all need to be in the code $(OBJDIR)/w64_LICENSE.o: $(OBJDIR)/LICENSE @echo $(MSG_LINKING) $@ @mkdir -p $(@D) cd $(@D); $(LD_W64) -r -b binary -o $(@F) $(^F) # Linux object file compile $(OBJDIR)/%.o: %.c @echo $(MSG_COMPILING) $^ @mkdir -p $(@D) $(CC) -c $(FLAGS) -o $@ $^ # Windows object file compile $(OBJDIR)/w64_%.o: %.c @echo $(MSG_COMPILING) $^ @mkdir -p $(@D) $(CC_W64) -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) rm -f $(OBJDIR)/*.o @echo $(MSG_CLEANING_OUT) rm -f $(OUTDIR)/*.elf rm -f $(OUTDIR)/*.exe @echo $(MSG_CLEANING_TEST) rm -f $(TESTDIR)/*