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 CXXFLAGS += -DUSE_PTHREADS
421 # On Android Bionic's C library comes with its own pthread implementation bundled in
422 ifneq ($(OS),Android)
423 # Haiku has pthreads in its libroot, so only link it in on other platforms
424 ifneq ($(KERNEL),Haiku)
439 ### 3.2.2 Debugging with undefined behavior sanitizers
440 ifneq ($(sanitize),no)
441 CXXFLAGS += -g3 -fsanitize=$(sanitize)
442 LDFLAGS += -fsanitize=$(sanitize)
446 ifeq ($(optimize),yes)
451 ifeq ($(OS), Android)
452 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
456 ifeq ($(comp),$(filter $(comp),gcc clang icc))
457 ifeq ($(KERNEL),Darwin)
458 CXXFLAGS += -mdynamic-no-pic
465 CXXFLAGS += -DIS_64BIT
469 ifeq ($(prefetch),yes)
474 CXXFLAGS += -DNO_PREFETCH
479 ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8-a arm64))
480 CXXFLAGS += -DUSE_POPCNT
481 else ifeq ($(comp),icc)
482 CXXFLAGS += -msse3 -DUSE_POPCNT
484 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
490 CXXFLAGS += -DUSE_AVX2
491 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
497 CXXFLAGS += -DUSE_AVX512
498 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
499 CXXFLAGS += -mavx512f -mavx512bw
504 CXXFLAGS += -DUSE_VNNI
505 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
506 CXXFLAGS += -mavx512vnni -mavx512dq -mavx512vl
511 CXXFLAGS += -DUSE_SSE41
512 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
518 CXXFLAGS += -DUSE_SSSE3
519 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
525 CXXFLAGS += -DUSE_SSE2
526 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
532 CXXFLAGS += -DUSE_MMX
533 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
539 CXXFLAGS += -DUSE_NEON
540 ifeq ($(KERNEL),Linux)
542 CXXFLAGS += -mfpu=neon
549 CXXFLAGS += -DUSE_PEXT
550 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
555 ### 3.8 Link Time Optimization
556 ### This is a mix of compile and link time options because the lto link phase
557 ### needs access to the optimization flags.
558 ifeq ($(optimize),yes)
561 CXXFLAGS += -flto=thin
562 LDFLAGS += $(CXXFLAGS)
563 else ifeq ($(comp),clang)
564 CXXFLAGS += -flto=thin
565 LDFLAGS += $(CXXFLAGS)
567 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
568 # GCC on some systems.
569 else ifeq ($(comp),gcc)
570 ifeq ($(gccisclang),)
572 LDFLAGS += $(CXXFLAGS) -flto=jobserver
573 ifneq ($(findstring MINGW,$(KERNEL)),)
574 LDFLAGS += -save-temps
575 else ifneq ($(findstring MSYS,$(KERNEL)),)
576 LDFLAGS += -save-temps
579 CXXFLAGS += -flto=thin
580 LDFLAGS += $(CXXFLAGS)
583 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
584 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are known to work, older might not.
585 # So, only enable it for a cross from Linux by default.
586 else ifeq ($(comp),mingw)
587 ifeq ($(KERNEL),Linux)
589 LDFLAGS += $(CXXFLAGS) -flto=jobserver
595 ### 3.9 Android 5 can only run position independent executables. Note that this
596 ### breaks Android 4.0 and earlier.
597 ifeq ($(OS), Android)
599 LDFLAGS += -fPIE -pie
602 ### ==========================================================================
603 ### Section 4. Public Targets
604 ### ==========================================================================
608 @echo "To compile stockfish, type: "
610 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
612 @echo "Supported targets:"
614 @echo "help > Display architecture details"
615 @echo "build > Standard build"
616 @echo "net > Download the default nnue net"
617 @echo "profile-build > Faster build (with profile-guided optimization)"
618 @echo "strip > Strip executable"
619 @echo "install > Install executable"
620 @echo "clean > Clean up"
622 @echo "Supported archs:"
624 @echo "x86-64-vnni > x86 64-bit with vnni support"
625 @echo "x86-64-avx512 > x86 64-bit with avx512 support"
626 @echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
627 @echo "x86-64-avx2 > x86 64-bit with avx2 support"
628 @echo "x86-64-sse41-popcnt > x86 64-bit with sse41 and popcnt support"
629 @echo "x86-64-modern > common modern CPU, currently x86-64-sse41-popcnt"
630 @echo "x86-64-ssse3 > x86 64-bit with ssse3 support"
631 @echo "x86-64-sse3-popcnt > x86 64-bit with sse3 and popcnt support"
632 @echo "x86-64 > x86 64-bit generic (with sse2 support)"
633 @echo "x86-32-sse41-popcnt > x86 32-bit with sse41 and popcnt support"
634 @echo "x86-32-sse2 > x86 32-bit with sse2 support"
635 @echo "x86-32 > x86 32-bit generic (with mmx and sse support)"
636 @echo "ppc-64 > PPC 64-bit"
637 @echo "ppc-32 > PPC 32-bit"
638 @echo "armv7 > ARMv7 32-bit"
639 @echo "armv7-neon > ARMv7 32-bit with popcnt and neon"
640 @echo "armv8 > ARMv8 64-bit with popcnt and neon"
641 @echo "apple-silicon > Apple silicon ARM64"
642 @echo "general-64 > unspecified 64-bit"
643 @echo "general-32 > unspecified 32-bit"
645 @echo "Supported compilers:"
647 @echo "gcc > Gnu compiler (default)"
648 @echo "mingw > Gnu compiler with MinGW under Windows"
649 @echo "clang > LLVM Clang compiler"
650 @echo "icc > Intel compiler"
651 @echo "ndk > Google NDK to cross-compile for Android"
653 @echo "Simple examples. If you don't know what to do, you likely want to run: "
655 @echo "make -j build ARCH=x86-64 (A portable, slow compile for 64-bit systems)"
656 @echo "make -j build ARCH=x86-32 (A portable, slow compile for 32-bit systems)"
658 @echo "Advanced examples, for experienced users looking for performance: "
660 @echo "make help ARCH=x86-64-bmi2"
661 @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-9.0"
662 @echo "make -j build ARCH=x86-64-ssse3 COMP=clang"
664 ifneq ($(empty_arch), yes)
665 @echo "-------------------------------"
666 @echo "The selected architecture $(ARCH) will enable the following configuration: "
667 @$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
671 .PHONY: help build profile-build strip install clean net objclean profileclean \
672 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
673 clang-profile-use clang-profile-make
676 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
678 profile-build: net config-sanity objclean profileclean
680 @echo "Step 1/4. Building instrumented executable ..."
681 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
683 @echo "Step 2/4. Running benchmark for pgo-build ..."
684 $(PGOBENCH) > /dev/null
686 @echo "Step 3/4. Building optimized executable ..."
687 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
688 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
690 @echo "Step 4/4. Deleting profile data ..."
691 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
697 -mkdir -p -m 755 $(BINDIR)
699 -strip $(BINDIR)/$(EXE)
702 clean: objclean profileclean
703 @rm -f .depend *~ core
706 $(eval nnuenet := $(shell grep EvalFile ucioption.cpp | grep Option | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
707 @echo "Default net: $(nnuenet)"
708 $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
709 $(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))
710 @if test -f "$(nnuenet)"; then \
711 echo "Already available."; \
713 if [ "x$(curl_or_wget)" = "x" ]; then \
714 echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \
716 echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet);\
719 $(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))
720 @if [ "x$(shasum_command)" != "x" ]; then \
721 if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
722 echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; \
725 echo "shasum / sha256sum not found, skipping net validation"; \
729 # clean binaries and objects
731 @rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
733 # clean auxiliary profiling files
736 @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda *.s
737 @rm -f stockfish.profdata *.profraw
742 ### ==========================================================================
743 ### Section 5. Private Targets
744 ### ==========================================================================
751 @echo "debug: '$(debug)'"
752 @echo "sanitize: '$(sanitize)'"
753 @echo "optimize: '$(optimize)'"
754 @echo "arch: '$(arch)'"
755 @echo "bits: '$(bits)'"
756 @echo "kernel: '$(KERNEL)'"
758 @echo "prefetch: '$(prefetch)'"
759 @echo "popcnt: '$(popcnt)'"
760 @echo "pext: '$(pext)'"
761 @echo "sse: '$(sse)'"
762 @echo "mmx: '$(mmx)'"
763 @echo "sse2: '$(sse2)'"
764 @echo "ssse3: '$(ssse3)'"
765 @echo "sse41: '$(sse41)'"
766 @echo "avx2: '$(avx2)'"
767 @echo "avx512: '$(avx512)'"
768 @echo "vnni: '$(vnni)'"
769 @echo "neon: '$(neon)'"
773 @echo "CXXFLAGS: $(CXXFLAGS)"
774 @echo "LDFLAGS: $(LDFLAGS)"
776 @echo "Testing config sanity. If this fails, try 'make help' ..."
778 @test "$(debug)" = "yes" || test "$(debug)" = "no"
779 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
780 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
781 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
782 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
783 test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a" || test "$(arch)" = "arm64"
784 @test "$(bits)" = "32" || test "$(bits)" = "64"
785 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
786 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
787 @test "$(pext)" = "yes" || test "$(pext)" = "no"
788 @test "$(sse)" = "yes" || test "$(sse)" = "no"
789 @test "$(mmx)" = "yes" || test "$(mmx)" = "no"
790 @test "$(sse2)" = "yes" || test "$(sse2)" = "no"
791 @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
792 @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
793 @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
794 @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
795 @test "$(vnni)" = "yes" || test "$(vnni)" = "no"
796 @test "$(neon)" = "yes" || test "$(neon)" = "no"
797 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang" \
798 || test "$(comp)" = "armv7a-linux-androideabi16-clang" || test "$(comp)" = "aarch64-linux-android21-clang"
801 +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
804 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
805 EXTRACXXFLAGS='-fprofile-instr-generate ' \
806 EXTRALDFLAGS=' -fprofile-instr-generate' \
810 $(XCRUN) llvm-profdata merge -output=stockfish.profdata *.profraw
811 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
812 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
813 EXTRALDFLAGS='-fprofile-use ' \
817 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
818 EXTRACXXFLAGS='-fprofile-generate' \
819 EXTRALDFLAGS='-lgcov' \
823 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
824 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
825 EXTRALDFLAGS='-lgcov' \
830 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
831 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
835 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
836 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
840 -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null