From 4b4ded94b693e50404bf3ee4334248f8caebe58e Mon Sep 17 00:00:00 2001 From: AnnaSnoeijs Date: Sat, 16 Aug 2025 12:33:59 +0200 Subject: [PATCH] Added cross-compiling to windows --- logic.c | 22 +++++++++++----------- makefile | 51 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/logic.c b/logic.c index 92754a8..452f267 100644 --- a/logic.c +++ b/logic.c @@ -17,13 +17,11 @@ * along with this program. If not, see . */ #include -#include -#include #include "logic.h" #include "macros.h" static column_t heightMask( const rowsint_t a ){ - return ( 1 << (column_t)a ) - (column_t)1; + return (column_t)( (column_t)1 << (column_t)a ) - (column_t)1; } static column_t safeHeightMask( const rowsint_t a ){ @@ -35,16 +33,18 @@ static column_t bottomHeightMask( const rowsint_t a, const rowsint_t b ){ } static void updateTotal( wincount_t *count ){ - count->total = + count->total = (winint_t)( count->horizontal + count->vertical + count->diagonalUp - + count->diagonalDown; + + count->diagonalDown); } // popcount for a column -static winint_t popcnt_column( column_t column ){ - winint_t i = (winint_t)__builtin_popcountg( (uint64_t)column ); +static winint_t popcnt_column( const column_t column ){ + // winint_t i = (winint_t)__builtin_popcountg( (uint64_t)column ); + winint_t i = 0; + for( column_t c = column; c != 0; i++ ) c &= c - 1; return i; } @@ -120,7 +120,7 @@ void calcWins_slow( board_t *boardptr ){ & safeHeightMask( boardptr->height[ column + 1 ] ) & safeHeightMask( boardptr->height[ column + 2 ] ) & safeHeightMask( boardptr->height[ column + 3 ] ) - & ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) + & safeHeightMask( boardptr->rows ) ); boardptr->count1.horizontal += popcnt_column( boardptr->column[ column ] @@ -138,7 +138,7 @@ void calcWins_slow( board_t *boardptr ){ & safeHeightMask( boardptr->height[ column + 1 ] - 1 ) & safeHeightMask( boardptr->height[ column + 2 ] - 2 ) & safeHeightMask( boardptr->height[ column + 3 ] - 3 ) - & ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) + & safeHeightMask( boardptr->rows ) ); boardptr->count1.diagonalUp += popcnt_column( boardptr->column[ column ] @@ -151,7 +151,7 @@ void calcWins_slow( board_t *boardptr ){ | boardptr->column[ column + 1 ] << 1 | boardptr->column[ column + 2 ] << 2 | boardptr->column[ column + 3 ] << 3 - | 0x07 // A line diagonal down don't start at one of the bottom 3 + | (column_t)( 0x07 ) // A line diagonal down don't start at one of the bottom 3 ) & bottomHeightMask( bottom( boardptr->height[ column ], @@ -161,7 +161,7 @@ void calcWins_slow( board_t *boardptr ){ boardptr->height[ column + 3 ] + 3 ) ) - & ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) + & safeHeightMask( boardptr->rows ) ); boardptr->count1.diagonalDown += popcnt_column( boardptr->column[ column ] diff --git a/makefile b/makefile index 604f492..0baf14a 100644 --- a/makefile +++ b/makefile @@ -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)/*