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 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
71 # ssse3 = yes/no --- -mssse3 --- Use Intel Supplemental Streaming SIMD Extensions 3
72 # sse41 = yes/no --- -msse4.1 --- Use Intel Streaming SIMD Extensions 4.1
73 # avx2 = yes/no --- -mavx2 --- Use Intel Advanced Vector Extensions 2
74 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
75 # avx512 = yes/no --- -mavx512bw --- Use Intel Advanced Vector Extensions 512
76 # vnni = yes/no --- -mavx512vnni --- Use Intel Vector Neural Network Instructions 512
77 # neon = yes/no --- -DUSE_NEON --- Use ARM SIMD architecture
79 # Note that Makefile is space sensitive, so when adding new architectures
80 # or modifying existing flags, you have to make sure there are no extra spaces
81 # at the end of the line for flag values.
83 ### 2.1. General and architecture defaults
107 ### 2.2 Architecture specific
109 ifeq ($(ARCH),general-32)
114 ifeq ($(ARCH),x86-32-old)
119 ifeq ($(ARCH),x86-32)
127 ifeq ($(ARCH),general-64)
131 ifeq ($(ARCH),x86-64)
137 ifeq ($(ARCH),x86-64-sse3-popcnt)
144 ifeq ($(ARCH),x86-64-ssse3)
151 ifeq ($(ARCH),$(filter $(ARCH),x86-64-sse41-popcnt x86-64-modern))
160 ifeq ($(ARCH),x86-64-avx2)
170 ifeq ($(ARCH),x86-64-bmi2)
181 ifeq ($(ARCH),x86-64-avx512)
193 ifeq ($(ARCH),x86-64-vnni)
212 ifeq ($(ARCH),armv7-neon)
227 ifeq ($(ARCH),apple-silicon)
234 ifeq ($(ARCH),ppc-32)
239 ifeq ($(ARCH),ppc-64)
245 ### ==========================================================================
246 ### Section 3. Low-level Configuration
247 ### ==========================================================================
249 ### 3.1 Selecting compiler (default = gcc)
250 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
251 DEPENDFLAGS += -std=c++17
252 LDFLAGS += $(EXTRALDFLAGS)
261 CXXFLAGS += -pedantic -Wextra -Wshadow
263 ifeq ($(arch),$(filter $(arch),armv7 armv8-a))
265 CXXFLAGS += -m$(bits)
269 CXXFLAGS += -m$(bits)
273 ifeq ($(arch),$(filter $(arch),armv7))
277 ifneq ($(KERNEL),Darwin)
278 LDFLAGS += -Wl,--no-as-needed
281 gccversion = $(shell $(CXX) --version)
282 gccisclang = $(findstring clang,$(gccversion))
288 ifeq ($(KERNEL),Linux)
290 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
291 CXX=x86_64-w64-mingw32-c++
293 CXX=x86_64-w64-mingw32-c++-posix
296 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
297 CXX=i686-w64-mingw32-c++
299 CXX=i686-w64-mingw32-c++-posix
306 CXXFLAGS += -Wextra -Wshadow
313 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
319 CXXFLAGS += -pedantic -Wextra -Wshadow
321 ifneq ($(KERNEL),Darwin)
322 ifneq ($(KERNEL),OpenBSD)
327 ifeq ($(arch),$(filter $(arch),armv7 armv8))
329 CXXFLAGS += -m$(bits)
333 CXXFLAGS += -m$(bits)
339 profile_make = icc-profile-make
340 profile_use = icc-profile-use
343 profile_make = clang-profile-make
344 profile_use = clang-profile-use
346 profile_make = gcc-profile-make
347 profile_use = gcc-profile-use
351 ifeq ($(KERNEL),Darwin)
352 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.14
353 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.14
356 # To cross-compile for Android, NDK version r21 or later is recommended.
357 # In earlier NDK versions, you'll need to pass -fno-addrsig if using GNU binutils.
358 # Currently we don't know how to make PGO builds with the NDK yet.
360 CXXFLAGS += -stdlib=libc++ -fPIE
362 comp=armv7a-linux-androideabi16-clang
363 CXX=armv7a-linux-androideabi16-clang++
364 CXXFLAGS += -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=neon
365 STRIP=arm-linux-androideabi-strip
367 ifeq ($(arch),armv8-a)
368 comp=aarch64-linux-android21-clang
369 CXX=aarch64-linux-android21-clang++
370 STRIP=aarch64-linux-android-strip
372 LDFLAGS += -static-libstdc++ -pie -lm -latomic
375 ### Travis CI script uses COMPILER to overwrite CXX
380 ### Allow overwriting CXX from command line
385 ### On mingw use Windows threads, otherwise POSIX
386 ifneq ($(comp),mingw)
387 # On Android Bionic's C library comes with its own pthread implementation bundled in
388 ifneq ($(OS),Android)
389 # Haiku has pthreads in its libroot, so only link it in on other platforms
390 ifneq ($(KERNEL),Haiku)
405 ### 3.2.2 Debugging with undefined behavior sanitizers
406 ifneq ($(sanitize),no)
407 CXXFLAGS += -g3 -fsanitize=$(sanitize)
408 LDFLAGS += -fsanitize=$(sanitize)
412 ifeq ($(optimize),yes)
417 ifeq ($(OS), Android)
418 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
422 ifeq ($(comp),$(filter $(comp),gcc clang icc))
423 ifeq ($(KERNEL),Darwin)
424 CXXFLAGS += -mdynamic-no-pic
431 CXXFLAGS += -DIS_64BIT
435 ifeq ($(prefetch),yes)
440 CXXFLAGS += -DNO_PREFETCH
445 ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8-a arm64))
446 CXXFLAGS += -DUSE_POPCNT
447 else ifeq ($(comp),icc)
448 CXXFLAGS += -msse3 -DUSE_POPCNT
450 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
456 CXXFLAGS += -DUSE_AVX2
457 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
463 CXXFLAGS += -DUSE_AVX512
464 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
465 CXXFLAGS += -mavx512f -mavx512bw
470 CXXFLAGS += -DUSE_VNNI
471 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
472 CXXFLAGS += -mavx512vnni -mavx512dq -mavx512vl
477 CXXFLAGS += -DUSE_SSE41
478 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
484 CXXFLAGS += -DUSE_SSSE3
485 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
491 CXXFLAGS += -DUSE_MMX
492 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
498 CXXFLAGS += -DUSE_NEON
499 ifeq ($(KERNEL),Linux)
501 CXXFLAGS += -mfpu=neon
506 ifeq ($(arch),x86_64)
507 CXXFLAGS += -msse2 -DUSE_SSE2
512 CXXFLAGS += -DUSE_PEXT
513 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
518 ### 3.8 Link Time Optimization
519 ### This is a mix of compile and link time options because the lto link phase
520 ### needs access to the optimization flags.
521 ifeq ($(optimize),yes)
524 CXXFLAGS += -flto=thin
525 LDFLAGS += $(CXXFLAGS)
526 else ifeq ($(comp),clang)
527 CXXFLAGS += -flto=thin
528 LDFLAGS += $(CXXFLAGS)
530 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
531 # GCC on some systems.
532 else ifeq ($(comp),gcc)
533 ifeq ($(gccisclang),)
535 LDFLAGS += $(CXXFLAGS) -flto=jobserver
536 ifneq ($(findstring MINGW,$(KERNEL)),)
537 LDFLAGS += -save-temps
538 else ifneq ($(findstring MSYS,$(KERNEL)),)
539 LDFLAGS += -save-temps
542 CXXFLAGS += -flto=thin
543 LDFLAGS += $(CXXFLAGS)
546 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
547 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are known to work, older might not.
548 # So, only enable it for a cross from Linux by default.
549 else ifeq ($(comp),mingw)
550 ifeq ($(KERNEL),Linux)
552 LDFLAGS += $(CXXFLAGS) -flto=jobserver
558 ### 3.9 Android 5 can only run position independent executables. Note that this
559 ### breaks Android 4.0 and earlier.
560 ifeq ($(OS), Android)
562 LDFLAGS += -fPIE -pie
565 ### ==========================================================================
566 ### Section 4. Public Targets
567 ### ==========================================================================
571 @echo "To compile stockfish, type: "
573 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
575 @echo "Supported targets:"
577 @echo "help > Display architecture details"
578 @echo "build > Standard build"
579 @echo "net > Download the default nnue net"
580 @echo "profile-build > Faster build (with profile-guided optimization)"
581 @echo "strip > Strip executable"
582 @echo "install > Install executable"
583 @echo "clean > Clean up"
585 @echo "Supported archs:"
587 @echo "x86-64-vnni > x86 64-bit with vnni support"
588 @echo "x86-64-avx512 > x86 64-bit with avx512 support"
589 @echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
590 @echo "x86-64-avx2 > x86 64-bit with avx2 support"
591 @echo "x86-64-sse41-popcnt > x86 64-bit with sse41 and popcnt support"
592 @echo "x86-64-modern > common modern CPU, currently x86-64-sse41-popcnt"
593 @echo "x86-64-ssse3 > x86 64-bit with ssse3 support"
594 @echo "x86-64-sse3-popcnt > x86 64-bit with sse3 and popcnt support"
595 @echo "x86-64 > x86 64-bit generic"
596 @echo "x86-32 > x86 32-bit (also enables MMX and SSE)"
597 @echo "x86-32-old > x86 32-bit fall back for old hardware"
598 @echo "ppc-64 > PPC 64-bit"
599 @echo "ppc-32 > PPC 32-bit"
600 @echo "armv7 > ARMv7 32-bit"
601 @echo "armv7-neon" > ARMv7 32-bit with popcnt and neon"
602 @echo "armv8 > ARMv8 64-bit with popcnt and neon"
603 @echo "apple-silicon > Apple silicon ARM64"
604 @echo "general-64 > unspecified 64-bit"
605 @echo "general-32 > unspecified 32-bit"
607 @echo "Supported compilers:"
609 @echo "gcc > Gnu compiler (default)"
610 @echo "mingw > Gnu compiler with MinGW under Windows"
611 @echo "clang > LLVM Clang compiler"
612 @echo "icc > Intel compiler"
613 @echo "ndk > Google NDK to cross-compile for Android"
615 @echo "Simple examples. If you don't know what to do, you likely want to run: "
617 @echo "make -j build ARCH=x86-64 (A portable, slow compile for 64-bit systems)"
618 @echo "make -j build ARCH=x86-32 (A portable, slow compile for 32-bit systems)"
620 @echo "Advanced examples, for experienced users looking for performance: "
622 @echo "make help ARCH=x86-64-bmi2"
623 @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-9.0"
624 @echo "make -j build ARCH=x86-64-ssse3 COMP=clang"
626 ifneq ($(empty_arch), yes)
627 @echo "-------------------------------\n"
628 @echo "The selected architecture $(ARCH) will enable the following configuration: "
629 @$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
633 .PHONY: help build profile-build strip install clean net objclean profileclean \
634 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
635 clang-profile-use clang-profile-make
638 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
640 profile-build: net config-sanity objclean profileclean
642 @echo "Step 1/4. Building instrumented executable ..."
643 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
645 @echo "Step 2/4. Running benchmark for pgo-build ..."
646 $(PGOBENCH) > /dev/null
648 @echo "Step 3/4. Building optimized executable ..."
649 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
650 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
652 @echo "Step 4/4. Deleting profile data ..."
653 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
659 -mkdir -p -m 755 $(BINDIR)
661 -strip $(BINDIR)/$(EXE)
664 clean: objclean profileclean
665 @rm -f .depend *~ core
668 $(eval nnuenet := $(shell grep EvalFile ucioption.cpp | grep Option | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
669 @echo "Default net: $(nnuenet)"
670 $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
671 $(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))
672 @if test -f "$(nnuenet)"; then echo "Already available."; else echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet); fi
673 $(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))
674 @if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; fi
676 # clean binaries and objects
678 @rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
680 # clean auxiliary profiling files
683 @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda *.s
684 @rm -f stockfish.profdata *.profraw
689 ### ==========================================================================
690 ### Section 5. Private Targets
691 ### ==========================================================================
698 @echo "debug: '$(debug)'"
699 @echo "sanitize: '$(sanitize)'"
700 @echo "optimize: '$(optimize)'"
701 @echo "arch: '$(arch)'"
702 @echo "bits: '$(bits)'"
703 @echo "kernel: '$(KERNEL)'"
705 @echo "prefetch: '$(prefetch)'"
706 @echo "popcnt: '$(popcnt)'"
707 @echo "sse: '$(sse)'"
708 @echo "ssse3: '$(ssse3)'"
709 @echo "sse41: '$(sse41)'"
710 @echo "avx2: '$(avx2)'"
711 @echo "pext: '$(pext)'"
712 @echo "avx512: '$(avx512)'"
713 @echo "vnni: '$(vnni)'"
714 @echo "neon: '$(neon)'"
718 @echo "CXXFLAGS: $(CXXFLAGS)"
719 @echo "LDFLAGS: $(LDFLAGS)"
721 @echo "Testing config sanity. If this fails, try 'make help' ..."
723 @test "$(debug)" = "yes" || test "$(debug)" = "no"
724 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
725 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
726 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
727 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
728 test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a" || test "$(arch)" = "arm64"
729 @test "$(bits)" = "32" || test "$(bits)" = "64"
730 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
731 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
732 @test "$(sse)" = "yes" || test "$(sse)" = "no"
733 @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
734 @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
735 @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
736 @test "$(pext)" = "yes" || test "$(pext)" = "no"
737 @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
738 @test "$(vnni)" = "yes" || test "$(vnni)" = "no"
739 @test "$(neon)" = "yes" || test "$(neon)" = "no"
740 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang" \
741 || test "$(comp)" = "armv7a-linux-androideabi16-clang" || test "$(comp)" = "aarch64-linux-android21-clang"
744 +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
747 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
748 EXTRACXXFLAGS='-fprofile-instr-generate ' \
749 EXTRALDFLAGS=' -fprofile-instr-generate' \
753 llvm-profdata merge -output=stockfish.profdata *.profraw
754 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
755 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
756 EXTRALDFLAGS='-fprofile-use ' \
760 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
761 EXTRACXXFLAGS='-fprofile-generate' \
762 EXTRALDFLAGS='-lgcov' \
766 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
767 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
768 EXTRALDFLAGS='-lgcov' \
773 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
774 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
778 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
779 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
783 -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null