Added cross-compiling to windows

This commit is contained in:
AnnaSnoeijs 2025-08-16 12:33:59 +02:00
parent bc67a1f7f9
commit 4b4ded94b6
2 changed files with 47 additions and 26 deletions

View file

@ -24,9 +24,8 @@ FLAGS += -Wlogical-op
FLAGS += -Wdisabled-optimization
FLAGS += -fanalyzer
FLAGS += -flto
FLAGS += -fuse-ld=gold
FLAGS += -s
flags += -Wl,--gc-sections
FLAGS += -Wl,--gc-sections
# These flags are because of the compiler otherwise giving too much warning
#FLAGS += -Wno-sign-conversion
#FLAGS += -Wformat-truncation=0
@ -37,11 +36,12 @@ endif
# Compile flag for defining GITHASH to put at the end of the version string
GITFLAG = -D'GITHASH=$(shell git rev-parse --short=1 HEAD)'
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
@ -53,6 +53,9 @@ 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
@ -67,9 +70,10 @@ 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)
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
@ -80,19 +84,22 @@ run_%: $(OUTDIR)/connect4_%.elf
./$<
# Show result of test
test: test.diff
test: $(TESTDIR)/test.diff
# Diff between the tests
test.diff: test.log test.log.slow
$(TESTDIR)/test.diff: $(TESTDIR)/test.log $(TESTDIR)/test.log.slow
@mkdir -p $(@D)
diff -y $^ > $@ || tail $@
diff -q $^
# Log of testing fast logic
test.log: $(OUTDIR)/connect4_$(UI_TESTING).elf
$(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
test.log.slow: $(OUTDIR)/connect4_$(UI_TESTING).elf
$(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
@ -108,25 +115,36 @@ $(OUTDIR)/connect4_%.elf: $(addprefix $(OBJDIR)/,ui_%.o logic.o connect4.o LICEN
@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)
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) $^
# 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)
$(CC) -c $(FLAGS) $(GITFLAG) -o $@ $^
cd $(@D); $(LD_W64) -r -b binary -o $(@F) $(^F)
# Generic object file compile
# 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:
@ -141,3 +159,6 @@ clean:
rm -f $(OBJDIR)/*.o
@echo $(MSG_CLEANING_OUT)
rm -f $(OUTDIR)/*.elf
rm -f $(OUTDIR)/*.exe
@echo $(MSG_CLEANING_TEST)
rm -f $(TESTDIR)/*