diff --git a/.gitignore b/.gitignore index e0a4cc4..5a7c54e 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,8 @@ debug brchess *.o *.s +*.old +*.save /test/ /obj/ /lib/ diff --git a/Makefile b/Makefile index 69987f5..665cf45 100644 --- a/Makefile +++ b/Makefile @@ -1,44 +1,56 @@ # Makefile - GNU make only. # -# Copyright (C) 2021 Bruno Raoult ("br") +# Copyright (C) 2021-2023 Bruno Raoult ("br") # Licensed under the GNU General Public License v3.0 or later. # Some rights reserved. See COPYING. -# * You should have received a copy of the GNU General Public License along with this -# program. If not, see . +# +# You should have received a copy of the GNU General Public License along with this +# program. If not, see . # # SPDX-License-Identifier: GPL-3.0-or-later # -BINDIR := ./bin -SRCDIR := ./src -OBJDIR := ./obj -DEPS := make.deps +SHELL := /bin/bash +CC := gcc +BEAR := bear +CCLSFILE := compile_commands.json +DEPFILE := make.deps -SRC=$(wildcard $(SRCDIR)/*.c) -INC=$(wildcard $(SRCDIR)/*.h) -SRC_S=$(notdir $(SRC)) +BINDIR := ./bin +SRCDIR := ./src +OBJDIR := ./obj +INCDIR := ./include +LIBDIR := ./lib +LIBSRCDIR := ./libsrc +LDFLAGS := -L$(LIBDIR) +LIB := br_$(shell uname -m) +SLIB := $(LIBDIR)/lib$(LIB).a +DLIB := $(LIBDIR)/lib$(LIB).so +LIBSRC := $(wildcard $(LIBSRCDIR)/*.c) +LIBOBJ := $(patsubst %.c,%.o,$(LIBSRC)) -CC=gcc +SRC := $(wildcard $(SRCDIR)/*.c) +INC := $(wildcard $(SRCDIR)/*.h) +SRC_S := $(notdir $(SRC)) .SECONDEXPANSION: OBJ=$(addprefix $(OBJDIR)/,$(SRC_S:.c=.o)) -BIN=fen pool piece move debug eval bits bodichess +BIN=fen piece move eval brchess -LIBS = -lreadline -lncurses -CFLAGS += -std=gnu99 +LIBS = -l$(LIB) -lreadline -lncurses + +CFLAGS += -std=gnu11 #CFLAGS += -O2 CFLAGS += -g CFLAGS += -Wall CFLAGS += -Wextra CFLAGS += -march=native -#CFLAGS += -pedantic -#CFLAGS += -Wno-pointer-arith -#CFLAGS += -Werror CFLAGS += -Wmissing-declarations ##################################### DEBUG flags CFLAGS += -DDEBUG # global +CFLAGS += -DDEBUG_DEBUG # enable log() functions CFLAGS += -DDEBUG_POOL # memory pools management CFLAGS += -DDEBUG_FEN # FEN decoding CFLAGS += -DDEBUG_MOVE # move generation @@ -51,33 +63,50 @@ CFLAGS += -DDEBUG_PIECE # piece list management #CFLAGS += -DDEBUG_EVAL3 # eval 3 #CFLAGS += -DDEBUG_MEM # malloc -.PHONY: all cflags clean +##################################### General targets +.PHONY: compile cflags all clean -compile: cflags $(OBJ) $(BIN) +compile: lib $(OBJ) $(BIN) cflags: @echo CFLAGS used: $(CFLAGS) all: clean compile -$(DEPS): $(SRC) $(INC) - @echo generating dependancies. - @$(CC) -MM $(SRC) > $@ - @sed -i "s|\(.*\.o\):|${OBJDIR}/\0:|" $@ - -include $(DEPS) - clean: rm -rf $(OBJ) core $(BIN) +##################################### Generate and include dependencies +.PHONY: deps cleandeps $(DEPFILE) + +cleandeps: + rm -f $(DEPFILE) + +deps: $(DEPFILE) + +$(DEPFILE): $(SRC) $(INC) + @echo generating dependancies. + $(CC) -MM -MF $(DEPFILE) -I $(INCDIR) $(SRC) + #cp $@ $@.sav + sed -i "s|\(.*\.o\):|${OBJDIR}/\0:|" $@ + +include $(DEPFILE) + +##################################### objects +.PHONY: obj + +obj: $(OBJ) #$(OBJ): $(OBJDIR)/%.o: $(SRCDIR)/%.c # @mkdir -p $(@D) # $(CC) -c $(CFLAGS) -o $@ $< -$(OBJDIR)/%.o: - @mkdir -p $(@D) - @echo compiling $@. - @$(CC) -c $(CFLAGS) -o $@ $< +$(OBJDIR)/%.o: $(SRCDIR)/%.c + echo SRC_S=$(SRC_S) + echo O=$(OBJ) + mkdir -p $(@D) + @echo compiling A=$@ I=$< + $(CC) -c $(CFLAGS) -I $(INCDIR) -o $@ $< +##################################### binaries #fen: CFLAGS+=-DBIN_$$@ #$(BIN): $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c # @echo compiling $@. @@ -87,7 +116,7 @@ $(OBJDIR)/%.o: $(BIN): $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c @echo compiling $@. @echo NEED_TO_CHANGE_THIS=$^ - @$(CC) -DBIN_$@ $(CFLAGS) $^ $(LIBS) -o $@ + $(CC) -DBIN_$@ $(CFLAGS) -I $(INCDIR) $^ $(LDFLAGS) $(LIBS) -o $@ #pool: CFLAGS+=-DPOOLBIN #pool: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c @@ -109,3 +138,27 @@ $(BIN): $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c #bits2: src/bits.c # $(CC) $(CFLAGS) -S $^ -o $@.s # $(CC) $(CFLAGS) $^ -o $@ + +##################################### br library +.PHONY: cleanlib lib + +cleanlib: clean + @$(RM) -f $(SLIB) $(DLIB) $(LIBOBJ) + +lib: $(DLIB) $(SLIB) + +$(SLIB): $(LIBOBJ) + @echo building $@ static library. + @mkdir -p $(LIBDIR) + @$(AR) $(ARFLAGS) -o $@ $^ + +$(DLIB): CFLAGS += -fPIC +$(DLIB): LDFLAGS += -shared +$(DLIB): $(LIBOBJ) + @echo building $@ shared library. + @mkdir -p $(LIBDIR) + @$(CC) $(LDFLAGS) $^ -o $@ + +.c.o: + @echo compiling $<. + @$(CC) -c $(CFLAGS) $(LDFLAGS) -I $(INCDIR) -o $@ $< diff --git a/src/board.h b/src/board.h index 50da4f9..93f12ab 100644 --- a/src/board.h +++ b/src/board.h @@ -5,7 +5,7 @@ * Some rights reserved. See COPYING. * * You should have received a copy of the GNU General Public License along with this - * program. If not, see . + * program. If not, see . * * SPDX-License-Identifier: GPL-3.0-or-later * @@ -79,7 +79,7 @@ enum x88_square { /* necessary not to become mad to set bitboards */ -enum bb_square{ +enum bb_square { A1=(u64)1, B1=(u64)A1<<1, C1=(u64)B1<<1, D1=(u64)C1<<1, E1=(u64)D1<<1, F1=(u64)E1<<1, G1=(u64)F1<<1, H1=(u64)G1<<1, diff --git a/src/brchess.c b/src/brchess.c index ca53be3..4766875 100644 --- a/src/brchess.c +++ b/src/brchess.c @@ -1,11 +1,11 @@ -/* bodichess.c - main loop. +/* brchess.c - main loop. * * Copyright (C) 2021 Bruno Raoult ("br") * Licensed under the GNU General Public License v3.0 or later. * Some rights reserved. See COPYING. * * You should have received a copy of the GNU General Public License along with this - * program. If not, see . + * program. If not, see . * * SPDX-License-Identifier: GPL-3.0-or-later * @@ -27,11 +27,11 @@ #include "debug.h" #include "fen.h" #include "eval.h" -#include "bodichess.h" +#include "brchess.h" typedef struct { char *name; /* User printable name */ - int (*func)(pos_t *, char *); /* function doing the job */ + int (*func)(pos_t *, char *); /* function doing the job */ char *doc; /* function doc */ } COMMAND; @@ -78,7 +78,7 @@ COMMAND commands[] = { static int done=0; -int bodichess(pos_t *pos) +int brchess(pos_t *pos) { char *buffer, *s; @@ -372,10 +372,10 @@ int do_help(__attribute__((unused)) pos_t *pos, return 0; } -#ifdef BIN_bodichess +#ifdef BIN_brchess /** main() * options: - int bodichess(pos_t *pos) + int brchess(pos_t *pos) * */ static int usage(char *prg) @@ -413,6 +413,6 @@ int main(int ac, char **av) if (optind < ac) return usage(*av); - return bodichess(pos); + return brchess(pos); } -#endif /* BIN_bodichess */ +#endif /* BIN_brchess */ diff --git a/src/brchess.h b/src/brchess.h index f32bddf..4309270 100644 --- a/src/brchess.h +++ b/src/brchess.h @@ -5,17 +5,17 @@ * Some rights reserved. See COPYING. * * You should have received a copy of the GNU General Public License along with this - * program. If not, see . + * program. If not, see . * * SPDX-License-Identifier: GPL-3.0-or-later * */ -#ifndef BODICHESS_H -#define BODICHESS_H +#ifndef BRCHESS_H +#define BRCHESS_H #include "position.h" -int bodichess(pos_t *pos); +int brchess(pos_t *pos); -#endif /* BODICHESS_H */ +#endif /* BRCHESS_H */