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
44 OBJS = $(notdir $(SRCS:.cpp=.o))
46 VPATH = syzygy:nnue:nnue/features
48 ### Establish the operating system name
49 KERNEL = $(shell uname -s)
50 ifeq ($(KERNEL),Linux)
51 OS = $(shell uname -o)
54 ### ==========================================================================
55 ### Section 2. High-level Configuration
56 ### ==========================================================================
58 # flag --- Comp switch --- Description
59 # ----------------------------------------------------------------------------
61 # debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
62 # sanitize = undefined/thread/no (-fsanitize )
63 # --- ( undefined ) --- enable undefined behavior checks
64 # --- ( thread ) --- enable threading error checks
65 # optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
66 # arch = (name) --- (-arch) --- Target architecture
67 # bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
68 # prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
69 # popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
70 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
71 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
72 # mmx = yes/no --- -mmmx --- Use Intel MMX instructions
73 # sse2 = yes/no --- -msse2 --- Use Intel Streaming SIMD Extensions 2
74 # ssse3 = yes/no --- -mssse3 --- Use Intel Supplemental Streaming SIMD Extensions 3
75 # sse41 = yes/no --- -msse4.1 --- Use Intel Streaming SIMD Extensions 4.1
76 # avx2 = yes/no --- -mavx2 --- Use Intel Advanced Vector Extensions 2
77 # avx512 = yes/no --- -mavx512bw --- Use Intel Advanced Vector Extensions 512
78 # vnni = yes/no --- -mavx512vnni --- Use Intel Vector Neural Network Instructions 512
79 # neon = yes/no --- -DUSE_NEON --- Use ARM SIMD architecture
81 # Note that Makefile is space sensitive, so when adding new architectures
82 # or modifying existing flags, you have to make sure there are no extra spaces
83 # at the end of the line for flag values.
85 ### 2.1. General and architecture defaults
110 ### 2.2 Architecture specific
112 ifeq ($(findstring x86,$(ARCH)),x86)
116 ifeq ($(findstring x86-32,$(ARCH)),x86-32)
127 ifeq ($(findstring -sse,$(ARCH)),-sse)
131 ifeq ($(findstring -popcnt,$(ARCH)),-popcnt)
135 ifeq ($(findstring -mmx,$(ARCH)),-mmx)
139 ifeq ($(findstring -sse2,$(ARCH)),-sse2)
144 ifeq ($(findstring -ssse3,$(ARCH)),-ssse3)
150 ifeq ($(findstring -sse41,$(ARCH)),-sse41)
157 ifeq ($(findstring -modern,$(ARCH)),-modern)
165 ifeq ($(findstring -avx2,$(ARCH)),-avx2)
174 ifeq ($(findstring -bmi2,$(ARCH)),-bmi2)
184 ifeq ($(findstring -avx512,$(ARCH)),-avx512)
195 ifeq ($(findstring -vnni,$(ARCH)),-vnni)
211 # 64-bit pext is not available on x86-32
218 # all other architectures
220 ifeq ($(ARCH),general-32)
225 ifeq ($(ARCH),general-64)
235 ifeq ($(ARCH),armv7-neon)
250 ifeq ($(ARCH),apple-silicon)
257 ifeq ($(ARCH),ppc-32)
262 ifeq ($(ARCH),ppc-64)
270 ### ==========================================================================
271 ### Section 3. Low-level Configuration
272 ### ==========================================================================
274 ### 3.1 Selecting compiler (default = gcc)
275 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
276 DEPENDFLAGS += -std=c++17
277 LDFLAGS += $(EXTRALDFLAGS)
286 CXXFLAGS += -pedantic -Wextra -Wshadow
288 ifeq ($(arch),$(filter $(arch),armv7 armv8-a))
290 CXXFLAGS += -m$(bits)
294 CXXFLAGS += -m$(bits)
298 ifeq ($(arch),$(filter $(arch),armv7))
302 ifneq ($(KERNEL),Darwin)
303 LDFLAGS += -Wl,--no-as-needed
310 ifeq ($(KERNEL),Linux)
312 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
313 CXX=x86_64-w64-mingw32-c++
315 CXX=x86_64-w64-mingw32-c++-posix
318 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
319 CXX=i686-w64-mingw32-c++
321 CXX=i686-w64-mingw32-c++-posix
328 CXXFLAGS += -Wextra -Wshadow
335 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
341 CXXFLAGS += -pedantic -Wextra -Wshadow
343 ifneq ($(KERNEL),Darwin)
344 ifneq ($(KERNEL),OpenBSD)
349 ifeq ($(arch),$(filter $(arch),armv7 armv8))
351 CXXFLAGS += -m$(bits)
355 CXXFLAGS += -m$(bits)
361 profile_make = icc-profile-make
362 profile_use = icc-profile-use
365 profile_make = clang-profile-make
366 profile_use = clang-profile-use
368 profile_make = gcc-profile-make
369 profile_use = gcc-profile-use
373 ifeq ($(KERNEL),Darwin)
374 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.14
375 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.14
379 # To cross-compile for Android, NDK version r21 or later is recommended.
380 # In earlier NDK versions, you'll need to pass -fno-addrsig if using GNU binutils.
381 # Currently we don't know how to make PGO builds with the NDK yet.
383 CXXFLAGS += -stdlib=libc++ -fPIE
385 comp=armv7a-linux-androideabi16-clang
386 CXX=armv7a-linux-androideabi16-clang++
387 CXXFLAGS += -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=neon
388 STRIP=arm-linux-androideabi-strip
390 ifeq ($(arch),armv8-a)
391 comp=aarch64-linux-android21-clang
392 CXX=aarch64-linux-android21-clang++
393 STRIP=aarch64-linux-android-strip
395 LDFLAGS += -static-libstdc++ -pie -lm -latomic
398 ### Travis CI script uses COMPILER to overwrite CXX
403 ### Allow overwriting CXX from command line
408 ### Sometimes gcc is really clang
410 gccversion = $(shell $(CXX) --version)
411 gccisclang = $(findstring clang,$(gccversion))
412 ifneq ($(gccisclang),)
413 profile_make = clang-profile-make
414 profile_use = clang-profile-use
418 ### On mingw use Windows threads, otherwise POSIX
419 ifneq ($(comp),mingw)
420 # On Android Bionic's C library comes with its own pthread implementation bundled in
421 ifneq ($(OS),Android)
422 # Haiku has pthreads in its libroot, so only link it in on other platforms
423 ifneq ($(KERNEL),Haiku)
438 ### 3.2.2 Debugging with undefined behavior sanitizers
439 ifneq ($(sanitize),no)
440 CXXFLAGS += -g3 -fsanitize=$(sanitize)
441 LDFLAGS += -fsanitize=$(sanitize)
445 ifeq ($(optimize),yes)
450 ifeq ($(OS), Android)
451 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
455 ifeq ($(comp),$(filter $(comp),gcc clang icc))
456 ifeq ($(KERNEL),Darwin)
457 CXXFLAGS += -mdynamic-no-pic
464 CXXFLAGS += -DIS_64BIT
468 ifeq ($(prefetch),yes)
473 CXXFLAGS += -DNO_PREFETCH
478 ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8-a arm64))
479 CXXFLAGS += -DUSE_POPCNT
480 else ifeq ($(comp),icc)
481 CXXFLAGS += -msse3 -DUSE_POPCNT
483 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
489 CXXFLAGS += -DUSE_AVX2
490 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
496 CXXFLAGS += -DUSE_AVX512
497 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
498 CXXFLAGS += -mavx512f -mavx512bw
503 CXXFLAGS += -DUSE_VNNI
504 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
505 CXXFLAGS += -mavx512vnni -mavx512dq -mavx512vl
510 CXXFLAGS += -DUSE_SSE41
511 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
517 CXXFLAGS += -DUSE_SSSE3
518 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
524 CXXFLAGS += -DUSE_SSE2
525 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
531 CXXFLAGS += -DUSE_MMX
532 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
538 CXXFLAGS += -DUSE_NEON
539 ifeq ($(KERNEL),Linux)
541 CXXFLAGS += -mfpu=neon
548 CXXFLAGS += -DUSE_PEXT
549 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
554 ### 3.8 Link Time Optimization
555 ### This is a mix of compile and link time options because the lto link phase
556 ### needs access to the optimization flags.
557 ifeq ($(optimize),yes)
560 CXXFLAGS += -flto=thin
561 LDFLAGS += $(CXXFLAGS)
562 else ifeq ($(comp),clang)
563 CXXFLAGS += -flto=thin
564 LDFLAGS += $(CXXFLAGS)
566 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
567 # GCC on some systems.
568 else ifeq ($(comp),gcc)
569 ifeq ($(gccisclang),)
571 LDFLAGS += $(CXXFLAGS) -flto=jobserver
572 ifneq ($(findstring MINGW,$(KERNEL)),)
573 LDFLAGS += -save-temps
574 else ifneq ($(findstring MSYS,$(KERNEL)),)
575 LDFLAGS += -save-temps
578 CXXFLAGS += -flto=thin
579 LDFLAGS += $(CXXFLAGS)
582 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
583 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are known to work, older might not.
584 # So, only enable it for a cross from Linux by default.
585 else ifeq ($(comp),mingw)
586 ifeq ($(KERNEL),Linux)
588 LDFLAGS += $(CXXFLAGS) -flto=jobserver
594 ### 3.9 Android 5 can only run position independent executables. Note that this
595 ### breaks Android 4.0 and earlier.
596 ifeq ($(OS), Android)
598 LDFLAGS += -fPIE -pie
601 ### ==========================================================================
602 ### Section 4. Public Targets
603 ### ==========================================================================
607 @echo "To compile stockfish, type: "
609 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
611 @echo "Supported targets:"
613 @echo "help > Display architecture details"
614 @echo "build > Standard build"
615 @echo "net > Download the default nnue net"
616 @echo "profile-build > Faster build (with profile-guided optimization)"
617 @echo "strip > Strip executable"
618 @echo "install > Install executable"
619 @echo "clean > Clean up"
621 @echo "Supported archs:"
623 @echo "x86-64-vnni > x86 64-bit with vnni support"
624 @echo "x86-64-avx512 > x86 64-bit with avx512 support"
625 @echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
626 @echo "x86-64-avx2 > x86 64-bit with avx2 support"
627 @echo "x86-64-sse41-popcnt > x86 64-bit with sse41 and popcnt support"
628 @echo "x86-64-modern > common modern CPU, currently x86-64-sse41-popcnt"
629 @echo "x86-64-ssse3 > x86 64-bit with ssse3 support"
630 @echo "x86-64-sse3-popcnt > x86 64-bit with sse3 and popcnt support"
631 @echo "x86-64 > x86 64-bit generic (with sse2 support)"
632 @echo "x86-32-sse41-popcnt > x86 32-bit with sse41 and popcnt support"
633 @echo "x86-32-sse2 > x86 32-bit with sse2 support"
634 @echo "x86-32 > x86 32-bit generic (with mmx and sse support)"
635 @echo "ppc-64 > PPC 64-bit"
636 @echo "ppc-32 > PPC 32-bit"
637 @echo "armv7 > ARMv7 32-bit"
638 @echo "armv7-neon > ARMv7 32-bit with popcnt and neon"
639 @echo "armv8 > ARMv8 64-bit with popcnt and neon"
640 @echo "apple-silicon > Apple silicon ARM64"
641 @echo "general-64 > unspecified 64-bit"
642 @echo "general-32 > unspecified 32-bit"
644 @echo "Supported compilers:"
646 @echo "gcc > Gnu compiler (default)"
647 @echo "mingw > Gnu compiler with MinGW under Windows"
648 @echo "clang > LLVM Clang compiler"
649 @echo "icc > Intel compiler"
650 @echo "ndk > Google NDK to cross-compile for Android"
652 @echo "Simple examples. If you don't know what to do, you likely want to run: "
654 @echo "make -j build ARCH=x86-64 (A portable, slow compile for 64-bit systems)"
655 @echo "make -j build ARCH=x86-32 (A portable, slow compile for 32-bit systems)"
657 @echo "Advanced examples, for experienced users looking for performance: "
659 @echo "make help ARCH=x86-64-bmi2"
660 @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-9.0"
661 @echo "make -j build ARCH=x86-64-ssse3 COMP=clang"
663 ifneq ($(empty_arch), yes)
664 @echo "-------------------------------"
665 @echo "The selected architecture $(ARCH) will enable the following configuration: "
666 @$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
670 .PHONY: help build profile-build strip install clean net objclean profileclean \
671 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
672 clang-profile-use clang-profile-make
675 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
677 profile-build: net config-sanity objclean profileclean
679 @echo "Step 1/4. Building instrumented executable ..."
680 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
682 @echo "Step 2/4. Running benchmark for pgo-build ..."
683 $(PGOBENCH) > /dev/null
685 @echo "Step 3/4. Building optimized executable ..."
686 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
687 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
689 @echo "Step 4/4. Deleting profile data ..."
690 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
696 -mkdir -p -m 755 $(BINDIR)
698 -strip $(BINDIR)/$(EXE)
701 clean: objclean profileclean
702 @rm -f .depend *~ core
705 $(eval nnuenet := $(shell grep EvalFile ucioption.cpp | grep Option | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
706 @echo "Default net: $(nnuenet)"
707 $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
708 $(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -skL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi))
709 @if test -f "$(nnuenet)"; then \
710 echo "Already available."; \
712 if [ "x$(curl_or_wget)" = "x" ]; then \
713 echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \
715 echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet);\
718 $(eval shasum_command := $(shell if hash shasum 2>/dev/null; then echo "shasum -a 256 "; elif hash sha256sum 2>/dev/null; then echo "sha256sum "; fi))
719 @if [ "x$(shasum_command)" != "x" ]; then \
720 if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
721 echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; \
724 echo "shasum / sha256sum not found, skipping net validation"; \
728 # clean binaries and objects
730 @rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
732 # clean auxiliary profiling files
735 @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda *.s
736 @rm -f stockfish.profdata *.profraw
741 ### ==========================================================================
742 ### Section 5. Private Targets
743 ### ==========================================================================
750 @echo "debug: '$(debug)'"
751 @echo "sanitize: '$(sanitize)'"
752 @echo "optimize: '$(optimize)'"
753 @echo "arch: '$(arch)'"
754 @echo "bits: '$(bits)'"
755 @echo "kernel: '$(KERNEL)'"
757 @echo "prefetch: '$(prefetch)'"
758 @echo "popcnt: '$(popcnt)'"
759 @echo "pext: '$(pext)'"
760 @echo "sse: '$(sse)'"
761 @echo "mmx: '$(mmx)'"
762 @echo "sse2: '$(sse2)'"
763 @echo "ssse3: '$(ssse3)'"
764 @echo "sse41: '$(sse41)'"
765 @echo "avx2: '$(avx2)'"
766 @echo "avx512: '$(avx512)'"
767 @echo "vnni: '$(vnni)'"
768 @echo "neon: '$(neon)'"
772 @echo "CXXFLAGS: $(CXXFLAGS)"
773 @echo "LDFLAGS: $(LDFLAGS)"
775 @echo "Testing config sanity. If this fails, try 'make help' ..."
777 @test "$(debug)" = "yes" || test "$(debug)" = "no"
778 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
779 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
780 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
781 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
782 test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a" || test "$(arch)" = "arm64"
783 @test "$(bits)" = "32" || test "$(bits)" = "64"
784 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
785 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
786 @test "$(pext)" = "yes" || test "$(pext)" = "no"
787 @test "$(sse)" = "yes" || test "$(sse)" = "no"
788 @test "$(mmx)" = "yes" || test "$(mmx)" = "no"
789 @test "$(sse2)" = "yes" || test "$(sse2)" = "no"
790 @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
791 @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
792 @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
793 @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
794 @test "$(vnni)" = "yes" || test "$(vnni)" = "no"
795 @test "$(neon)" = "yes" || test "$(neon)" = "no"
796 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang" \
797 || test "$(comp)" = "armv7a-linux-androideabi16-clang" || test "$(comp)" = "aarch64-linux-android21-clang"
800 +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
803 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
804 EXTRACXXFLAGS='-fprofile-instr-generate ' \
805 EXTRALDFLAGS=' -fprofile-instr-generate' \
809 $(XCRUN) llvm-profdata merge -output=stockfish.profdata *.profraw
810 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
811 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
812 EXTRALDFLAGS='-fprofile-use ' \
816 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
817 EXTRACXXFLAGS='-fprofile-generate' \
818 EXTRALDFLAGS='-lgcov' \
822 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
823 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
824 EXTRALDFLAGS='-lgcov' \
829 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
830 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
834 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
835 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
839 -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null