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
106 ### 2.2 Architecture specific
108 ifeq ($(ARCH),general-32)
113 ifeq ($(ARCH),x86-32-old)
118 ifeq ($(ARCH),x86-32)
126 ifeq ($(ARCH),general-64)
130 ifeq ($(ARCH),x86-64)
136 ifeq ($(ARCH),x86-64-sse3-popcnt)
143 ifeq ($(ARCH),x86-64-ssse3)
150 ifeq ($(ARCH),$(filter $(ARCH),x86-64-sse41-popcnt x86-64-modern))
159 ifeq ($(ARCH),x86-64-avx2)
169 ifeq ($(ARCH),x86-64-bmi2)
180 ifeq ($(ARCH),x86-64-avx512)
192 ifeq ($(ARCH),x86-64-vnni)
218 ifeq ($(ARCH),apple-silicon)
225 ifeq ($(ARCH),ppc-32)
230 ifeq ($(ARCH),ppc-64)
236 ### ==========================================================================
237 ### Section 3. Low-level Configuration
238 ### ==========================================================================
240 ### 3.1 Selecting compiler (default = gcc)
241 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
242 DEPENDFLAGS += -std=c++17
243 LDFLAGS += $(EXTRALDFLAGS)
252 CXXFLAGS += -pedantic -Wextra -Wshadow
254 ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
256 CXXFLAGS += -m$(bits)
260 CXXFLAGS += -m$(bits)
264 ifneq ($(KERNEL),Darwin)
265 LDFLAGS += -Wl,--no-as-needed
268 gccversion = $(shell $(CXX) --version)
269 gccisclang = $(findstring clang,$(gccversion))
275 ifeq ($(KERNEL),Linux)
277 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
278 CXX=x86_64-w64-mingw32-c++
280 CXX=x86_64-w64-mingw32-c++-posix
283 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
284 CXX=i686-w64-mingw32-c++
286 CXX=i686-w64-mingw32-c++-posix
293 CXXFLAGS += -Wextra -Wshadow
300 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
306 CXXFLAGS += -pedantic -Wextra -Wshadow
308 ifneq ($(KERNEL),Darwin)
309 ifneq ($(KERNEL),OpenBSD)
314 ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
316 CXXFLAGS += -m$(bits)
320 CXXFLAGS += -m$(bits)
326 profile_make = icc-profile-make
327 profile_use = icc-profile-use
330 profile_make = clang-profile-make
331 profile_use = clang-profile-use
333 profile_make = gcc-profile-make
334 profile_use = gcc-profile-use
338 ifeq ($(KERNEL),Darwin)
339 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.14
340 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.14
343 ### Travis CI script uses COMPILER to overwrite CXX
348 ### Allow overwriting CXX from command line
353 ### On mingw use Windows threads, otherwise POSIX
354 ifneq ($(comp),mingw)
355 # On Android Bionic's C library comes with its own pthread implementation bundled in
356 ifneq ($(OS),Android)
357 # Haiku has pthreads in its libroot, so only link it in on other platforms
358 ifneq ($(KERNEL),Haiku)
371 ### 3.2.2 Debugging with undefined behavior sanitizers
372 ifneq ($(sanitize),no)
373 CXXFLAGS += -g3 -fsanitize=$(sanitize)
374 LDFLAGS += -fsanitize=$(sanitize)
378 ifeq ($(optimize),yes)
383 ifeq ($(OS), Android)
384 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
388 ifeq ($(comp),$(filter $(comp),gcc clang icc))
389 ifeq ($(KERNEL),Darwin)
390 CXXFLAGS += -mdynamic-no-pic
397 CXXFLAGS += -DIS_64BIT
401 ifeq ($(prefetch),yes)
407 CXXFLAGS += -DNO_PREFETCH
412 ifeq ($(arch),$(filter $(arch),ppc64 armv8-a arm64))
413 CXXFLAGS += -DUSE_POPCNT
414 else ifeq ($(comp),icc)
415 CXXFLAGS += -msse3 -DUSE_POPCNT
417 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
422 CXXFLAGS += -DUSE_AVX2
423 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
429 CXXFLAGS += -DUSE_AVX512
430 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
431 CXXFLAGS += -mavx512f -mavx512bw
436 CXXFLAGS += -DUSE_VNNI
437 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
438 CXXFLAGS += -mavx512vnni -mavx512dq -mavx512vl
443 CXXFLAGS += -DUSE_SSE41
444 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
450 CXXFLAGS += -DUSE_SSSE3
451 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
457 CXXFLAGS += -DUSE_MMX
458 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
464 CXXFLAGS += -DUSE_NEON
467 ifeq ($(arch),x86_64)
468 CXXFLAGS += -msse2 -DUSE_SSE2
473 CXXFLAGS += -DUSE_PEXT
474 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
479 ### 3.8 Link Time Optimization
480 ### This is a mix of compile and link time options because the lto link phase
481 ### needs access to the optimization flags.
482 ifeq ($(optimize),yes)
485 CXXFLAGS += -flto=thin
486 LDFLAGS += $(CXXFLAGS)
488 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
489 # GCC on some systems.
490 else ifeq ($(comp),gcc)
491 ifeq ($(gccisclang),)
493 LDFLAGS += $(CXXFLAGS) -flto=jobserver
494 ifneq ($(findstring MINGW,$(KERNEL)),)
495 LDFLAGS += -save-temps
496 else ifneq ($(findstring MSYS,$(KERNEL)),)
497 LDFLAGS += -save-temps
500 CXXFLAGS += -flto=thin
501 LDFLAGS += $(CXXFLAGS)
504 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
505 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are know to work, older might not.
506 # So, only enable it for a cross from Linux by default.
507 else ifeq ($(comp),mingw)
508 ifeq ($(KERNEL),Linux)
510 LDFLAGS += $(CXXFLAGS) -flto=jobserver
516 ### 3.9 Android 5 can only run position independent executables. Note that this
517 ### breaks Android 4.0 and earlier.
518 ifeq ($(OS), Android)
520 LDFLAGS += -fPIE -pie
523 ### ==========================================================================
524 ### Section 4. Public Targets
525 ### ==========================================================================
529 @echo "To compile stockfish, type: "
531 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
533 @echo "Supported targets:"
535 @echo "help > Display architecture details"
536 @echo "build > Standard build"
537 @echo "net > Download the default nnue net"
538 @echo "profile-build > Faster build (with profile-guided optimization)"
539 @echo "strip > Strip executable"
540 @echo "install > Install executable"
541 @echo "clean > Clean up"
543 @echo "Supported archs:"
545 @echo "x86-64-vnni > x86 64-bit with vnni support"
546 @echo "x86-64-avx512 > x86 64-bit with avx512 support"
547 @echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
548 @echo "x86-64-avx2 > x86 64-bit with avx2 support"
549 @echo "x86-64-sse41-popcnt > x86 64-bit with sse41 and popcnt support"
550 @echo "x86-64-modern > common modern CPU, currently x86-64-sse41-popcnt"
551 @echo "x86-64-ssse3 > x86 64-bit with ssse3 support"
552 @echo "x86-64-sse3-popcnt > x86 64-bit with sse3 and popcnt support"
553 @echo "x86-64 > x86 64-bit generic"
554 @echo "x86-32 > x86 32-bit (also enables MMX and SSE)"
555 @echo "x86-32-old > x86 32-bit fall back for old hardware"
556 @echo "ppc-64 > PPC 64-bit"
557 @echo "ppc-32 > PPC 32-bit"
558 @echo "armv7 > ARMv7 32-bit"
559 @echo "armv8 > ARMv8 64-bit"
560 @echo "apple-silicon > Apple silicon ARM64"
561 @echo "general-64 > unspecified 64-bit"
562 @echo "general-32 > unspecified 32-bit"
564 @echo "Supported compilers:"
566 @echo "gcc > Gnu compiler (default)"
567 @echo "mingw > Gnu compiler with MinGW under Windows"
568 @echo "clang > LLVM Clang compiler"
569 @echo "icc > Intel compiler"
571 @echo "Simple examples. If you don't know what to do, you likely want to run: "
573 @echo "make -j build ARCH=x86-64 (A portable, slow compile for 64-bit systems)"
574 @echo "make -j build ARCH=x86-32 (A portable, slow compile for 32-bit systems)"
576 @echo "Advanced examples, for experienced users looking for performance: "
578 @echo "make help ARCH=x86-64-bmi2"
579 @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-9.0"
580 @echo "make -j build ARCH=x86-64-ssse3 COMP=clang"
582 ifneq ($(empty_arch), yes)
583 @echo "-------------------------------\n"
584 @echo "The selected architecture $(ARCH) will enable the following configuration: "
585 @$(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: net 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 -skL"; 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
629 $(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))
630 @if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; fi
632 # clean binaries and objects
634 @rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
636 # clean auxiliary profiling files
639 @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda *.s
640 @rm -f stockfish.profdata *.profraw
645 ### ==========================================================================
646 ### Section 5. Private Targets
647 ### ==========================================================================
654 @echo "debug: '$(debug)'"
655 @echo "sanitize: '$(sanitize)'"
656 @echo "optimize: '$(optimize)'"
657 @echo "arch: '$(arch)'"
658 @echo "bits: '$(bits)'"
659 @echo "kernel: '$(KERNEL)'"
661 @echo "prefetch: '$(prefetch)'"
662 @echo "popcnt: '$(popcnt)'"
663 @echo "sse: '$(sse)'"
664 @echo "ssse3: '$(ssse3)'"
665 @echo "sse41: '$(sse41)'"
666 @echo "avx2: '$(avx2)'"
667 @echo "pext: '$(pext)'"
668 @echo "avx512: '$(avx512)'"
669 @echo "vnni: '$(vnni)'"
670 @echo "neon: '$(neon)'"
674 @echo "CXXFLAGS: $(CXXFLAGS)"
675 @echo "LDFLAGS: $(LDFLAGS)"
677 @echo "Testing config sanity. If this fails, try 'make help' ..."
679 @test "$(debug)" = "yes" || test "$(debug)" = "no"
680 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
681 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
682 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
683 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
684 test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a" || test "$(arch)" = "arm64"
685 @test "$(bits)" = "32" || test "$(bits)" = "64"
686 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
687 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
688 @test "$(sse)" = "yes" || test "$(sse)" = "no"
689 @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
690 @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
691 @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
692 @test "$(pext)" = "yes" || test "$(pext)" = "no"
693 @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
694 @test "$(vnni)" = "yes" || test "$(vnni)" = "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' \
738 -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null