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 # sse3 = yes/no --- -msse3 --- Use Intel Streaming SIMD Extensions 3
72 # ssse3 = yes/no --- -mssse3 --- Use Intel Supplemental Streaming SIMD Extensions 3
73 # sse41 = yes/no --- -msse4.1 --- Use Intel Streaming SIMD Extensions 4.1
74 # sse42 = yes/no --- -msse4.2 --- Use Intel Streaming SIMD Extensions 4.2
75 # avx2 = yes/no --- -mavx2 --- Use Intel Advanced Vector Extensions 2
76 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
77 # avx512 = yes/no --- -mavx512bw --- Use Intel Advanced Vector Extensions 512
78 # neon = yes/no --- -DUSE_NEON --- Use ARM SIMD architecture
80 # Note that Makefile is space sensitive, so when adding new architectures
81 # or modifying existing flags, you have to make sure there are no extra spaces
82 # at the end of the line for flag values.
84 ### 2.1. General and architecture defaults
102 ### 2.2 Architecture specific
103 ifeq ($(ARCH),general-32)
108 ifeq ($(ARCH),x86-32-old)
113 ifeq ($(ARCH),x86-32)
120 ifeq ($(ARCH),general-64)
124 ifeq ($(ARCH),x86-64)
130 ifeq ($(ARCH),x86-64-sse3)
137 ifeq ($(ARCH),x86-64-sse3-popcnt)
145 ifeq ($(ARCH),x86-64-ssse3)
153 ifeq ($(ARCH),x86-64-sse41)
163 ifeq ($(ARCH),x86-64-modern)
173 ifeq ($(ARCH),x86-64-sse42)
184 ifeq ($(ARCH),x86-64-avx2)
196 ifeq ($(ARCH),x86-64-bmi2)
209 ifeq ($(ARCH),x86-64-avx512)
236 ifeq ($(ARCH),apple-silicon)
243 ifeq ($(ARCH),ppc-32)
248 ifeq ($(ARCH),ppc-64)
254 ### ==========================================================================
255 ### Section 3. Low-level Configuration
256 ### ==========================================================================
258 ### 3.1 Selecting compiler (default = gcc)
259 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
260 DEPENDFLAGS += -std=c++17
261 LDFLAGS += $(EXTRALDFLAGS)
270 CXXFLAGS += -pedantic -Wextra -Wshadow
272 ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
274 CXXFLAGS += -m$(bits)
278 CXXFLAGS += -m$(bits)
282 ifneq ($(KERNEL),Darwin)
283 LDFLAGS += -Wl,--no-as-needed
286 gccversion = $(shell $(CXX) --version)
287 gccisclang = $(findstring clang,$(gccversion))
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)
503 CXXFLAGS += -flto=thin
504 LDFLAGS += $(CXXFLAGS)
506 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
507 # GCC on some systems.
508 else ifeq ($(comp),gcc)
509 ifeq ($(gccisclang),)
511 LDFLAGS += $(CXXFLAGS) -flto=jobserver
513 CXXFLAGS += -flto=thin
514 LDFLAGS += $(CXXFLAGS)
517 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
518 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are know to work, older might not.
519 # So, only enable it for a cross from Linux by default.
520 else ifeq ($(comp),mingw)
521 ifeq ($(KERNEL),Linux)
523 LDFLAGS += $(CXXFLAGS) -flto=jobserver
529 ### 3.9 Android 5 can only run position independent executables. Note that this
530 ### breaks Android 4.0 and earlier.
531 ifeq ($(OS), Android)
533 LDFLAGS += -fPIE -pie
536 ### ==========================================================================
537 ### Section 4. Public Targets
538 ### ==========================================================================
542 @echo "To compile stockfish, type: "
544 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
546 @echo "Supported targets:"
548 @echo "build > Standard build"
549 @echo "profile-build > Standard build with PGO"
550 @echo "strip > Strip executable"
551 @echo "install > Install executable"
552 @echo "clean > Clean up"
553 @echo "net > Download the default nnue net"
555 @echo "Supported archs:"
557 @echo "x86-64-avx512 > x86 64-bit with avx512 support"
558 @echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
559 @echo "x86-64-avx2 > x86 64-bit with avx2 support"
560 @echo "x86-64-sse42 > x86 64-bit with sse42 support"
561 @echo "x86-64-modern > x86 64-bit with sse41 support (x86-64-sse41)"
562 @echo "x86-64-sse41 > x86 64-bit with sse41 support"
563 @echo "x86-64-ssse3 > x86 64-bit with ssse3 support"
564 @echo "x86-64-sse3-popcnt > x86 64-bit with sse3 and popcnt support"
565 @echo "x86-64-sse3 > x86 64-bit with sse3 support"
566 @echo "x86-64 > x86 64-bit generic"
567 @echo "x86-32 > x86 32-bit (also enables SSE)"
568 @echo "x86-32-old > x86 32-bit fall back for old hardware"
569 @echo "ppc-64 > PPC 64-bit"
570 @echo "ppc-32 > PPC 32-bit"
571 @echo "armv7 > ARMv7 32-bit"
572 @echo "armv8 > ARMv8 64-bit"
573 @echo "apple-silicon > Apple silicon ARM64"
574 @echo "general-64 > unspecified 64-bit"
575 @echo "general-32 > unspecified 32-bit"
577 @echo "Supported compilers:"
579 @echo "gcc > Gnu compiler (default)"
580 @echo "mingw > Gnu compiler with MinGW under Windows"
581 @echo "clang > LLVM Clang compiler"
582 @echo "icc > Intel compiler"
584 @echo "Simple examples. If you don't know what to do, you likely want to run: "
586 @echo "make -j build ARCH=x86-64 (This is for 64-bit systems)"
587 @echo "make -j build ARCH=x86-32 (This is for 32-bit systems)"
589 @echo "Advanced examples, for experienced users: "
591 @echo "make -j build ARCH=x86-64-modern COMP=clang"
592 @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-4.8"
594 @echo "The selected architecture $(ARCH) enables the following configuration: "
596 @$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
599 .PHONY: help build profile-build strip install clean net objclean profileclean \
600 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
601 clang-profile-use clang-profile-make
604 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
606 profile-build: config-sanity objclean profileclean
608 @echo "Step 1/4. Building instrumented executable ..."
609 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
611 @echo "Step 2/4. Running benchmark for pgo-build ..."
612 $(PGOBENCH) > /dev/null
614 @echo "Step 3/4. Building optimized executable ..."
615 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
616 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
618 @echo "Step 4/4. Deleting profile data ..."
619 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
625 -mkdir -p -m 755 $(BINDIR)
627 -strip $(BINDIR)/$(EXE)
630 clean: objclean profileclean
631 @rm -f .depend *~ core
634 $(eval nnuenet := $(shell grep EvalFile ucioption.cpp | grep Option | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
635 @echo "Default net: $(nnuenet)"
636 $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
637 $(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))
638 @if test -f "$(nnuenet)"; then echo "Already available."; else echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet); fi
640 # clean binaries and objects
642 @rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
644 # clean auxiliary profiling files
647 @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda
648 @rm -f stockfish.profdata *.profraw
653 ### ==========================================================================
654 ### Section 5. Private Targets
655 ### ==========================================================================
662 @echo "debug: '$(debug)'"
663 @echo "sanitize: '$(sanitize)'"
664 @echo "optimize: '$(optimize)'"
665 @echo "arch: '$(arch)'"
666 @echo "bits: '$(bits)'"
667 @echo "kernel: '$(KERNEL)'"
669 @echo "prefetch: '$(prefetch)'"
670 @echo "popcnt: '$(popcnt)'"
671 @echo "sse: '$(sse)'"
672 @echo "sse3: '$(sse3)'"
673 @echo "ssse3: '$(ssse3)'"
674 @echo "sse41: '$(sse41)'"
675 @echo "sse42: '$(sse42)'"
676 @echo "avx2: '$(avx2)'"
677 @echo "pext: '$(pext)'"
678 @echo "avx512: '$(avx512)'"
679 @echo "neon: '$(neon)'"
683 @echo "CXXFLAGS: $(CXXFLAGS)"
684 @echo "LDFLAGS: $(LDFLAGS)"
686 @echo "Testing config sanity. If this fails, try 'make help' ..."
688 @test "$(debug)" = "yes" || test "$(debug)" = "no"
689 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
690 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
691 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
692 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
693 test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a" || test "$(arch)" = "arm64"
694 @test "$(bits)" = "32" || test "$(bits)" = "64"
695 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
696 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
697 @test "$(sse)" = "yes" || test "$(sse)" = "no"
698 @test "$(sse3)" = "yes" || test "$(sse3)" = "no"
699 @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
700 @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
701 @test "$(sse42)" = "yes" || test "$(sse42)" = "no"
702 @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
703 @test "$(pext)" = "yes" || test "$(pext)" = "no"
704 @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
705 @test "$(neon)" = "yes" || test "$(neon)" = "no"
706 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
709 +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
712 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
713 EXTRACXXFLAGS='-fprofile-instr-generate ' \
714 EXTRALDFLAGS=' -fprofile-instr-generate' \
718 llvm-profdata merge -output=stockfish.profdata *.profraw
719 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
720 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
721 EXTRALDFLAGS='-fprofile-use ' \
725 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
726 EXTRACXXFLAGS='-fprofile-generate' \
727 EXTRALDFLAGS='-lgcov' \
731 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
732 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
733 EXTRALDFLAGS='-lgcov' \
738 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
739 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
743 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
744 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
748 -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null