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 # neon = yes/no --- -DUSE_NEON --- Use ARM SIMD architecture
78 # Note that Makefile is space sensitive, so when adding new architectures
79 # or modifying existing flags, you have to make sure there are no extra spaces
80 # at the end of the line for flag values.
82 ### 2.1. General and architecture defaults
99 ### 2.2 Architecture specific
100 ifeq ($(ARCH),general-32)
105 ifeq ($(ARCH),x86-32-old)
110 ifeq ($(ARCH),x86-32)
118 ifeq ($(ARCH),general-64)
122 ifeq ($(ARCH),x86-64)
128 ifeq ($(ARCH),x86-64-sse3-popcnt)
135 ifeq ($(ARCH),x86-64-ssse3)
142 ifeq ($(ARCH),x86-64-modern)
151 ifeq ($(ARCH),x86-64-sse41-popcnt)
160 ifeq ($(ARCH),x86-64-avx2)
170 ifeq ($(ARCH),x86-64-bmi2)
181 ifeq ($(ARCH),x86-64-avx512)
206 ifeq ($(ARCH),apple-silicon)
213 ifeq ($(ARCH),ppc-32)
218 ifeq ($(ARCH),ppc-64)
224 ### ==========================================================================
225 ### Section 3. Low-level Configuration
226 ### ==========================================================================
228 ### 3.1 Selecting compiler (default = gcc)
229 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
230 DEPENDFLAGS += -std=c++17
231 LDFLAGS += $(EXTRALDFLAGS)
240 CXXFLAGS += -pedantic -Wextra -Wshadow
242 ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
244 CXXFLAGS += -m$(bits)
248 CXXFLAGS += -m$(bits)
252 ifneq ($(KERNEL),Darwin)
253 LDFLAGS += -Wl,--no-as-needed
256 gccversion = $(shell $(CXX) --version)
257 gccisclang = $(findstring clang,$(gccversion))
263 ifeq ($(KERNEL),Linux)
265 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
266 CXX=x86_64-w64-mingw32-c++
268 CXX=x86_64-w64-mingw32-c++-posix
271 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
272 CXX=i686-w64-mingw32-c++
274 CXX=i686-w64-mingw32-c++-posix
281 CXXFLAGS += -Wextra -Wshadow
288 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
294 CXXFLAGS += -pedantic -Wextra -Wshadow
296 ifneq ($(KERNEL),Darwin)
297 ifneq ($(KERNEL),OpenBSD)
302 ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
304 CXXFLAGS += -m$(bits)
308 CXXFLAGS += -m$(bits)
314 profile_make = icc-profile-make
315 profile_use = icc-profile-use
318 profile_make = clang-profile-make
319 profile_use = clang-profile-use
321 profile_make = gcc-profile-make
322 profile_use = gcc-profile-use
326 ifeq ($(KERNEL),Darwin)
327 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.14
328 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.14
331 ### Travis CI script uses COMPILER to overwrite CXX
336 ### Allow overwriting CXX from command line
341 ### On mingw use Windows threads, otherwise POSIX
342 ifneq ($(comp),mingw)
343 # On Android Bionic's C library comes with its own pthread implementation bundled in
344 ifneq ($(OS),Android)
345 # Haiku has pthreads in its libroot, so only link it in on other platforms
346 ifneq ($(KERNEL),Haiku)
359 ### 3.2.2 Debugging with undefined behavior sanitizers
360 ifneq ($(sanitize),no)
361 CXXFLAGS += -g3 -fsanitize=$(sanitize)
362 LDFLAGS += -fsanitize=$(sanitize)
366 ifeq ($(optimize),yes)
371 ifeq ($(OS), Android)
372 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
376 ifeq ($(comp),$(filter $(comp),gcc clang icc))
377 ifeq ($(KERNEL),Darwin)
378 CXXFLAGS += -mdynamic-no-pic
385 CXXFLAGS += -DIS_64BIT
389 ifeq ($(prefetch),yes)
395 CXXFLAGS += -DNO_PREFETCH
400 ifeq ($(arch),$(filter $(arch),ppc64 armv8-a arm64))
401 CXXFLAGS += -DUSE_POPCNT
402 else ifeq ($(comp),icc)
403 CXXFLAGS += -msse3 -DUSE_POPCNT
405 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
410 CXXFLAGS += -DUSE_AVX2
411 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
417 CXXFLAGS += -DUSE_AVX512
418 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
419 CXXFLAGS += -mavx512bw
424 CXXFLAGS += -DUSE_SSE41
425 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
431 CXXFLAGS += -DUSE_SSSE3
432 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
438 CXXFLAGS += -DUSE_MMX
439 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
445 CXXFLAGS += -DUSE_NEON
448 ifeq ($(arch),x86_64)
449 CXXFLAGS += -DUSE_SSE2
454 CXXFLAGS += -DUSE_PEXT
455 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
460 ### 3.8 Link Time Optimization
461 ### This is a mix of compile and link time options because the lto link phase
462 ### needs access to the optimization flags.
463 ifeq ($(optimize),yes)
466 CXXFLAGS += -flto=thin
467 LDFLAGS += $(CXXFLAGS)
469 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
470 # GCC on some systems.
471 else ifeq ($(comp),gcc)
472 ifeq ($(gccisclang),)
474 LDFLAGS += $(CXXFLAGS) -flto=jobserver
476 CXXFLAGS += -flto=thin
477 LDFLAGS += $(CXXFLAGS)
480 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
481 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are know to work, older might not.
482 # So, only enable it for a cross from Linux by default.
483 else ifeq ($(comp),mingw)
484 ifeq ($(KERNEL),Linux)
486 LDFLAGS += $(CXXFLAGS) -flto=jobserver
492 ### 3.9 Android 5 can only run position independent executables. Note that this
493 ### breaks Android 4.0 and earlier.
494 ifeq ($(OS), Android)
496 LDFLAGS += -fPIE -pie
499 ### ==========================================================================
500 ### Section 4. Public Targets
501 ### ==========================================================================
505 @echo "To compile stockfish, type: "
507 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
509 @echo "Supported targets:"
511 @echo "build > Standard build"
512 @echo "profile-build > Standard build with PGO"
513 @echo "strip > Strip executable"
514 @echo "install > Install executable"
515 @echo "clean > Clean up"
516 @echo "net > Download the default nnue net"
518 @echo "Supported archs:"
520 @echo "x86-64-avx512 > x86 64-bit with avx512 support"
521 @echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
522 @echo "x86-64-avx2 > x86 64-bit with avx2 support"
523 @echo "x86-64-sse41-popcnt > x86 64-bit with sse41 and popcnt support"
524 @echo "x86-64-modern > the same as previous (x86-64-sse41-popcnt)"
525 @echo "x86-64-ssse3 > x86 64-bit with ssse3 support"
526 @echo "x86-64-sse3-popcnt > x86 64-bit with sse3 and popcnt support"
527 @echo "x86-64 > x86 64-bit generic"
528 @echo "x86-32 > x86 32-bit (also enables MMX and SSE)"
529 @echo "x86-32-old > x86 32-bit fall back for old hardware"
530 @echo "ppc-64 > PPC 64-bit"
531 @echo "ppc-32 > PPC 32-bit"
532 @echo "armv7 > ARMv7 32-bit"
533 @echo "armv8 > ARMv8 64-bit"
534 @echo "apple-silicon > Apple silicon ARM64"
535 @echo "general-64 > unspecified 64-bit"
536 @echo "general-32 > unspecified 32-bit"
538 @echo "Supported compilers:"
540 @echo "gcc > Gnu compiler (default)"
541 @echo "mingw > Gnu compiler with MinGW under Windows"
542 @echo "clang > LLVM Clang compiler"
543 @echo "icc > Intel compiler"
545 @echo "Simple examples. If you don't know what to do, you likely want to run: "
547 @echo "make -j build ARCH=x86-64 (This is for 64-bit systems)"
548 @echo "make -j build ARCH=x86-32 (This is for 32-bit systems)"
550 @echo "Advanced examples, for experienced users: "
552 @echo "make -j build ARCH=x86-64-modern COMP=clang"
553 @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-4.8"
555 @echo "The selected architecture $(ARCH) enables the following configuration: "
557 @$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
560 .PHONY: help build profile-build strip install clean net objclean profileclean \
561 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
562 clang-profile-use clang-profile-make
565 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
567 profile-build: config-sanity objclean profileclean
569 @echo "Step 1/4. Building instrumented executable ..."
570 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
572 @echo "Step 2/4. Running benchmark for pgo-build ..."
573 $(PGOBENCH) > /dev/null
575 @echo "Step 3/4. Building optimized executable ..."
576 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
577 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
579 @echo "Step 4/4. Deleting profile data ..."
580 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
586 -mkdir -p -m 755 $(BINDIR)
588 -strip $(BINDIR)/$(EXE)
591 clean: objclean profileclean
592 @rm -f .depend *~ core
595 $(eval nnuenet := $(shell grep EvalFile ucioption.cpp | grep Option | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
596 @echo "Default net: $(nnuenet)"
597 $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
598 $(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))
599 @if test -f "$(nnuenet)"; then echo "Already available."; else echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet); fi
601 # clean binaries and objects
603 @rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
605 # clean auxiliary profiling files
608 @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda
609 @rm -f stockfish.profdata *.profraw
614 ### ==========================================================================
615 ### Section 5. Private Targets
616 ### ==========================================================================
623 @echo "debug: '$(debug)'"
624 @echo "sanitize: '$(sanitize)'"
625 @echo "optimize: '$(optimize)'"
626 @echo "arch: '$(arch)'"
627 @echo "bits: '$(bits)'"
628 @echo "kernel: '$(KERNEL)'"
630 @echo "prefetch: '$(prefetch)'"
631 @echo "popcnt: '$(popcnt)'"
632 @echo "sse: '$(sse)'"
633 @echo "ssse3: '$(ssse3)'"
634 @echo "sse41: '$(sse41)'"
635 @echo "avx2: '$(avx2)'"
636 @echo "pext: '$(pext)'"
637 @echo "avx512: '$(avx512)'"
638 @echo "neon: '$(neon)'"
642 @echo "CXXFLAGS: $(CXXFLAGS)"
643 @echo "LDFLAGS: $(LDFLAGS)"
645 @echo "Testing config sanity. If this fails, try 'make help' ..."
647 @test "$(debug)" = "yes" || test "$(debug)" = "no"
648 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
649 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
650 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
651 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
652 test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a" || test "$(arch)" = "arm64"
653 @test "$(bits)" = "32" || test "$(bits)" = "64"
654 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
655 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
656 @test "$(sse)" = "yes" || test "$(sse)" = "no"
657 @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
658 @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
659 @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
660 @test "$(pext)" = "yes" || test "$(pext)" = "no"
661 @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
662 @test "$(neon)" = "yes" || test "$(neon)" = "no"
663 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
666 +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
669 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
670 EXTRACXXFLAGS='-fprofile-instr-generate ' \
671 EXTRALDFLAGS=' -fprofile-instr-generate' \
675 llvm-profdata merge -output=stockfish.profdata *.profraw
676 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
677 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
678 EXTRALDFLAGS='-fprofile-use ' \
682 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
683 EXTRACXXFLAGS='-fprofile-generate' \
684 EXTRALDFLAGS='-lgcov' \
688 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
689 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
690 EXTRALDFLAGS='-lgcov' \
695 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
696 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
700 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
701 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
705 -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null