1 # Stockfish, a UCI chess playing engine derived from Glaurung 2.1
2 # Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
3 # Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
4 # Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6 # Stockfish is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # Stockfish is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ### ==========================================================================
21 ### Section 1. General Configuration
22 ### ==========================================================================
31 ### Installation dir definitions
33 BINDIR = $(PREFIX)/bin
35 ### Built-in benchmark for pgo-builds
36 PGOBENCH = ./$(EXE) bench
38 ### Source and object files
39 SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
40 material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
41 search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
42 nnue/evaluate_nnue.cpp nnue/features/half_kp.cpp \
43 hashprobe.grpc.pb.cc hashprobe.pb.cc
44 CLISRCS = client.cpp hashprobe.grpc.pb.cc hashprobe.pb.cc uci.cpp
46 OBJS = $(notdir $(SRCS:.cpp=.o))
47 CLIOBJS = $(notdir $(CLISRCS:.cpp=.o))
49 VPATH = syzygy:nnue:nnue/features
51 ### Establish the operating system name
52 KERNEL = $(shell uname -s)
53 ifeq ($(KERNEL),Linux)
54 OS = $(shell uname -o)
57 ### ==========================================================================
58 ### Section 2. High-level Configuration
59 ### ==========================================================================
61 # flag --- Comp switch --- Description
62 # ----------------------------------------------------------------------------
64 # debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
65 # sanitize = undefined/thread/no (-fsanitize )
66 # --- ( undefined ) --- enable undefined behavior checks
67 # --- ( thread ) --- enable threading error checks
68 # optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
69 # arch = (name) --- (-arch) --- Target architecture
70 # bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
71 # prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
72 # popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
73 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
74 # sse3 = yes/no --- -msse3 --- Use Intel Streaming SIMD Extensions 3
75 # ssse3 = yes/no --- -mssse3 --- Use Intel Supplemental Streaming SIMD Extensions 3
76 # sse41 = yes/no --- -msse4.1 --- Use Intel Streaming SIMD Extensions 4.1
77 # sse42 = yes/no --- -msse4.2 --- Use Intel Streaming SIMD Extensions 4.2
78 # avx2 = yes/no --- -mavx2 --- Use Intel Advanced Vector Extensions 2
79 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
80 # avx512 = yes/no --- -mavx512bw --- Use Intel Advanced Vector Extensions 512
81 # neon = yes/no --- -DUSE_NEON --- Use ARM SIMD architecture
83 # Note that Makefile is space sensitive, so when adding new architectures
84 # or modifying existing flags, you have to make sure there are no extra spaces
85 # at the end of the line for flag values.
87 ### 2.1. General and architecture defaults
105 ### 2.2 Architecture specific
106 ifeq ($(ARCH),general-32)
111 ifeq ($(ARCH),x86-32-old)
116 ifeq ($(ARCH),x86-32)
123 ifeq ($(ARCH),general-64)
127 ifeq ($(ARCH),x86-64)
133 ifeq ($(ARCH),x86-64-sse3)
140 ifeq ($(ARCH),x86-64-sse3-popcnt)
148 ifeq ($(ARCH),x86-64-ssse3)
156 ifeq ($(ARCH),x86-64-sse41)
166 ifeq ($(ARCH),x86-64-modern)
176 ifeq ($(ARCH),x86-64-sse42)
187 ifeq ($(ARCH),x86-64-avx2)
199 ifeq ($(ARCH),x86-64-bmi2)
212 ifeq ($(ARCH),x86-64-avx512)
239 ifeq ($(ARCH),apple-silicon)
246 ifeq ($(ARCH),ppc-32)
251 ifeq ($(ARCH),ppc-64)
257 ### ==========================================================================
258 ### Section 3. Low-level Configuration
259 ### ==========================================================================
261 ### 3.1 Selecting compiler (default = gcc)
262 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
263 DEPENDFLAGS += -std=c++17
264 LDFLAGS += $(EXTRALDFLAGS)
273 CXXFLAGS += -pedantic -Wextra
275 ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
277 CXXFLAGS += -m$(bits)
281 CXXFLAGS += -m$(bits)
285 ifneq ($(KERNEL),Darwin)
286 LDFLAGS += -Wl,--no-as-needed
293 ifeq ($(KERNEL),Linux)
295 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
296 CXX=x86_64-w64-mingw32-c++
298 CXX=x86_64-w64-mingw32-c++-posix
301 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
302 CXX=i686-w64-mingw32-c++
304 CXX=i686-w64-mingw32-c++-posix
311 CXXFLAGS += -Wextra -Wshadow
318 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
324 CXXFLAGS += -pedantic -Wextra -Wshadow
326 ifneq ($(KERNEL),Darwin)
327 ifneq ($(KERNEL),OpenBSD)
332 ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
334 CXXFLAGS += -m$(bits)
338 CXXFLAGS += -m$(bits)
344 profile_make = icc-profile-make
345 profile_use = icc-profile-use
348 profile_make = clang-profile-make
349 profile_use = clang-profile-use
351 profile_make = gcc-profile-make
352 profile_use = gcc-profile-use
356 ifeq ($(KERNEL),Darwin)
357 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.15
358 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.15
361 ### Travis CI script uses COMPILER to overwrite CXX
366 ### Allow overwriting CXX from command line
371 ### On mingw use Windows threads, otherwise POSIX
372 ifneq ($(comp),mingw)
373 # On Android Bionic's C library comes with its own pthread implementation bundled in
374 ifneq ($(OS),Android)
375 # Haiku has pthreads in its libroot, so only link it in on other platforms
376 ifneq ($(KERNEL),Haiku)
389 ### 3.2.2 Debugging with undefined behavior sanitizers
390 ifneq ($(sanitize),no)
391 CXXFLAGS += -g3 -fsanitize=$(sanitize)
392 LDFLAGS += -fsanitize=$(sanitize)
396 ifeq ($(optimize),yes)
401 ifeq ($(OS), Android)
402 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
406 ifeq ($(comp),$(filter $(comp),gcc clang icc))
407 ifeq ($(KERNEL),Darwin)
408 CXXFLAGS += -mdynamic-no-pic
415 CXXFLAGS += -DIS_64BIT
419 ifeq ($(prefetch),yes)
425 CXXFLAGS += -DNO_PREFETCH
430 ifeq ($(arch),$(filter $(arch),ppc64 armv8-a arm64))
431 CXXFLAGS += -DUSE_POPCNT
432 else ifeq ($(comp),icc)
433 CXXFLAGS += -msse3 -DUSE_POPCNT
435 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
440 CXXFLAGS += -DUSE_AVX2
441 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
447 CXXFLAGS += -DUSE_AVX512
448 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
449 CXXFLAGS += -mavx512bw
454 CXXFLAGS += -DUSE_SSE42
455 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
461 CXXFLAGS += -DUSE_SSE41
462 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
468 CXXFLAGS += -DUSE_SSSE3
469 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
475 CXXFLAGS += -DUSE_SSE3
476 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
482 CXXFLAGS += -DUSE_NEON
485 ifeq ($(arch),x86_64)
486 CXXFLAGS += -DUSE_SSE2
491 CXXFLAGS += -DUSE_PEXT
492 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
497 ### 3.8 Link Time Optimization
498 ### This is a mix of compile and link time options because the lto link phase
499 ### needs access to the optimization flags.
500 ifeq ($(optimize),yes)
502 ifeq ($(comp),$(filter $(comp),gcc clang))
504 LDFLAGS += $(CXXFLAGS)
507 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
508 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are know to work, older might not.
509 # So, only enable it for a cross from Linux by default.
511 ifeq ($(KERNEL),Linux)
513 LDFLAGS += $(CXXFLAGS)
519 ### 3.9 Android 5 can only run position independent executables. Note that this
520 ### breaks Android 4.0 and earlier.
521 ifeq ($(OS), Android)
523 LDFLAGS += -fPIE -pie
526 ### ==========================================================================
527 ### Section 4. Public Targets
528 ### ==========================================================================
532 @echo "To compile stockfish, type: "
534 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
536 @echo "Supported targets:"
538 @echo "build > Standard build"
539 @echo "profile-build > Standard build with PGO"
540 @echo "strip > Strip executable"
541 @echo "install > Install executable"
542 @echo "clean > Clean up"
543 @echo "net > Download the default nnue net"
545 @echo "Supported archs:"
547 @echo "x86-64-avx512 > x86 64-bit with avx512 support"
548 @echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
549 @echo "x86-64-avx2 > x86 64-bit with avx2 support"
550 @echo "x86-64-sse42 > x86 64-bit with sse42 support"
551 @echo "x86-64-modern > x86 64-bit with sse41 support (x86-64-sse41)"
552 @echo "x86-64-sse41 > x86 64-bit with sse41 support"
553 @echo "x86-64-ssse3 > x86 64-bit with ssse3 support"
554 @echo "x86-64-sse3-popcnt > x86 64-bit with sse3 and popcnt support"
555 @echo "x86-64-sse3 > x86 64-bit with sse3 support"
556 @echo "x86-64 > x86 64-bit generic"
557 @echo "x86-32 > x86 32-bit (also enables SSE)"
558 @echo "x86-32-old > x86 32-bit fall back for old hardware"
559 @echo "ppc-64 > PPC 64-bit"
560 @echo "ppc-32 > PPC 32-bit"
561 @echo "armv7 > ARMv7 32-bit"
562 @echo "armv8 > ARMv8 64-bit"
563 @echo "apple-silicon > Apple silicon ARM64"
564 @echo "general-64 > unspecified 64-bit"
565 @echo "general-32 > unspecified 32-bit"
567 @echo "Supported compilers:"
569 @echo "gcc > Gnu compiler (default)"
570 @echo "mingw > Gnu compiler with MinGW under Windows"
571 @echo "clang > LLVM Clang compiler"
572 @echo "icc > Intel compiler"
574 @echo "Simple examples. If you don't know what to do, you likely want to run: "
576 @echo "make -j build ARCH=x86-64 (This is for 64-bit systems)"
577 @echo "make -j build ARCH=x86-32 (This is for 32-bit systems)"
579 @echo "Advanced examples, for experienced users: "
581 @echo "make -j build ARCH=x86-64-modern COMP=clang"
582 @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-4.8"
584 @echo "The selected architecture $(ARCH) enables the following configuration: "
586 @$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
589 .PHONY: help build profile-build strip install clean net objclean profileclean \
590 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
591 clang-profile-use clang-profile-make
594 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
596 profile-build: config-sanity objclean profileclean
598 @echo "Step 1/4. Building instrumented executable ..."
599 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
601 @echo "Step 2/4. Running benchmark for pgo-build ..."
602 $(PGOBENCH) > /dev/null
604 @echo "Step 3/4. Building optimized executable ..."
605 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
606 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
608 @echo "Step 4/4. Deleting profile data ..."
609 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
615 -mkdir -p -m 755 $(BINDIR)
617 -strip $(BINDIR)/$(EXE)
620 clean: objclean profileclean
621 @rm -f .depend *~ core
624 $(eval nnuenet := $(shell grep EvalFile ucioption.cpp | grep Option | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
625 @echo "Default net: $(nnuenet)"
626 $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
627 $(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -sL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi))
628 @if test -f "$(nnuenet)"; then echo "Already available."; else echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet); fi
630 # clean binaries and objects
632 @rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
634 # clean auxiliary profiling files
637 @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda
638 @rm -f stockfish.profdata *.profraw
643 ### ==========================================================================
644 ### Section 5. Private Targets
645 ### ==========================================================================
647 all: $(EXE) client .depend
652 @echo "debug: '$(debug)'"
653 @echo "sanitize: '$(sanitize)'"
654 @echo "optimize: '$(optimize)'"
655 @echo "arch: '$(arch)'"
656 @echo "bits: '$(bits)'"
657 @echo "kernel: '$(KERNEL)'"
659 @echo "prefetch: '$(prefetch)'"
660 @echo "popcnt: '$(popcnt)'"
661 @echo "sse: '$(sse)'"
662 @echo "sse3: '$(sse3)'"
663 @echo "ssse3: '$(ssse3)'"
664 @echo "sse41: '$(sse41)'"
665 @echo "sse42: '$(sse42)'"
666 @echo "avx2: '$(avx2)'"
667 @echo "pext: '$(pext)'"
668 @echo "avx512: '$(avx512)'"
669 @echo "neon: '$(neon)'"
673 @echo "CXXFLAGS: $(CXXFLAGS)"
674 @echo "LDFLAGS: $(LDFLAGS)"
676 @echo "Testing config sanity. If this fails, try 'make help' ..."
678 @test "$(debug)" = "yes" || test "$(debug)" = "no"
679 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
680 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
681 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
682 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
683 test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a" || test "$(arch)" = "arm64"
684 @test "$(bits)" = "32" || test "$(bits)" = "64"
685 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
686 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
687 @test "$(sse)" = "yes" || test "$(sse)" = "no"
688 @test "$(sse3)" = "yes" || test "$(sse3)" = "no"
689 @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
690 @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
691 @test "$(sse42)" = "yes" || test "$(sse42)" = "no"
692 @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
693 @test "$(pext)" = "yes" || test "$(pext)" = "no"
694 @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
695 @test "$(neon)" = "yes" || test "$(neon)" = "no"
696 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
699 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
702 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
703 EXTRACXXFLAGS='-fprofile-instr-generate ' \
704 EXTRALDFLAGS=' -fprofile-instr-generate' \
708 llvm-profdata merge -output=stockfish.profdata *.profraw
709 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
710 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
711 EXTRALDFLAGS='-fprofile-use ' \
715 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
716 EXTRACXXFLAGS='-fprofile-generate' \
717 EXTRALDFLAGS='-lgcov' \
721 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
722 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
723 EXTRALDFLAGS='-lgcov' \
728 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
729 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
733 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
734 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
741 GRPC_CPP_PLUGIN = grpc_cpp_plugin
742 GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
744 %.grpc.pb.h %.grpc.pb.cc: %.proto
745 $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
751 %.pb.h %.pb.cc: %.proto
752 $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
754 #LDFLAGS += -Wl,-Bstatic -Wl,-\( -lprotobuf -lgrpc++_unsecure -lgrpc_unsecure -lgrpc -lz -Wl,-\) -Wl,-Bdynamic -ldl
755 LDFLAGS += /usr/lib/x86_64-linux-gnu/libprotobuf.a /usr/lib/x86_64-linux-gnu/libgrpc++_unsecure.a /usr/lib/x86_64-linux-gnu/libgrpc_unsecure.a /usr/lib/x86_64-linux-gnu/libgrpc.a /usr/lib/x86_64-linux-gnu/libcares.a -ldl -lz
756 #LDFLAGS += /usr/lib/x86_64-linux-gnu/libprotobuf.a /usr/lib/libgrpc++_unsecure.a /usr/lib/libgrpc_unsecure.a /usr/lib/libgrpc.a /usr/lib/x86_64-linux-gnu/libcares.a -ldl -lz
759 $(CXX) -o $@ $(CLIOBJS) $(LDFLAGS)
764 -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null