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
43 OBJS = $(notdir $(SRCS:.cpp=.o))
47 ### Establish the operating system name
48 KERNEL = $(shell uname -s)
49 ifeq ($(KERNEL),Linux)
50 OS = $(shell uname -o)
53 ### ==========================================================================
54 ### Section 2. High-level Configuration
55 ### ==========================================================================
57 # flag --- Comp switch --- Description
58 # ----------------------------------------------------------------------------
60 # debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
61 # sanitize = undefined/thread/no (-fsanitize )
62 # --- ( undefined ) --- enable undefined behavior checks
63 # --- ( thread ) --- enable threading error checks
64 # optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
65 # arch = (name) --- (-arch) --- Target architecture
66 # bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
67 # prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
68 # popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
69 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
70 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
72 # Note that Makefile is space sensitive, so when adding new architectures
73 # or modifying existing flags, you have to make sure there are no extra spaces
74 # at the end of the line for flag values.
76 ### 2.1. General and architecture defaults
86 ### 2.2 Architecture specific
88 ifeq ($(ARCH),general-32)
92 ifeq ($(ARCH),x86-32-old)
102 ifeq ($(ARCH),general-64)
107 ifeq ($(ARCH),x86-64)
114 ifeq ($(ARCH),x86-64-modern)
122 ifeq ($(ARCH),x86-64-bmi2)
142 ifeq ($(ARCH),ppc-32)
146 ifeq ($(ARCH),ppc-64)
154 ### ==========================================================================
155 ### Section 3. Low-level configuration
156 ### ==========================================================================
158 ### 3.1 Selecting compiler (default = gcc)
160 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
161 DEPENDFLAGS += -std=c++11
162 LDFLAGS += $(EXTRALDFLAGS)
171 CXXFLAGS += -pedantic -Wextra -Wshadow
173 ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
175 CXXFLAGS += -m$(bits)
179 CXXFLAGS += -m$(bits)
183 ifneq ($(KERNEL),Darwin)
184 LDFLAGS += -Wl,--no-as-needed
191 ifeq ($(KERNEL),Linux)
193 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
194 CXX=x86_64-w64-mingw32-c++
196 CXX=x86_64-w64-mingw32-c++-posix
199 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
200 CXX=i686-w64-mingw32-c++
202 CXX=i686-w64-mingw32-c++-posix
209 CXXFLAGS += -Wextra -Wshadow
216 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
222 CXXFLAGS += -pedantic -Wextra -Wshadow
224 ifneq ($(KERNEL),Darwin)
225 ifneq ($(KERNEL),OpenBSD)
230 ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
232 CXXFLAGS += -m$(bits)
236 CXXFLAGS += -m$(bits)
242 profile_make = icc-profile-make
243 profile_use = icc-profile-use
246 profile_make = clang-profile-make
247 profile_use = clang-profile-use
249 profile_make = gcc-profile-make
250 profile_use = gcc-profile-use
254 ifeq ($(KERNEL),Darwin)
255 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
256 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
259 ### Travis CI script uses COMPILER to overwrite CXX
264 ### Allow overwriting CXX from command line
269 ### On mingw use Windows threads, otherwise POSIX
270 ifneq ($(comp),mingw)
271 # On Android Bionic's C library comes with its own pthread implementation bundled in
272 ifneq ($(OS),Android)
273 # Haiku has pthreads in its libroot, so only link it in on other platforms
274 ifneq ($(KERNEL),Haiku)
287 ### 3.2.2 Debugging with undefined behavior sanitizers
288 ifneq ($(sanitize),no)
289 CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
290 LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
294 ifeq ($(optimize),yes)
299 ifeq ($(OS), Android)
300 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
304 ifeq ($(comp),$(filter $(comp),gcc clang icc))
305 ifeq ($(KERNEL),Darwin)
306 CXXFLAGS += -mdynamic-no-pic
313 CXXFLAGS += -DIS_64BIT
317 ifeq ($(prefetch),yes)
323 CXXFLAGS += -DNO_PREFETCH
329 CXXFLAGS += -DUSE_POPCNT
330 else ifeq ($(comp),icc)
331 CXXFLAGS += -msse3 -DUSE_POPCNT
333 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
339 CXXFLAGS += -DUSE_PEXT
340 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
341 CXXFLAGS += -msse4 -mbmi2
345 ### 3.8 Link Time Optimization
346 ### This is a mix of compile and link time options because the lto link phase
347 ### needs access to the optimization flags.
348 ifeq ($(optimize),yes)
350 ifeq ($(comp),$(filter $(comp),gcc clang))
352 LDFLAGS += $(CXXFLAGS)
356 ifeq ($(KERNEL),Linux)
358 LDFLAGS += $(CXXFLAGS)
364 ### 3.9 Android 5 can only run position independent executables. Note that this
365 ### breaks Android 4.0 and earlier.
366 ifeq ($(OS), Android)
368 LDFLAGS += -fPIE -pie
372 ### ==========================================================================
373 ### Section 4. Public targets
374 ### ==========================================================================
378 @echo "To compile stockfish, type: "
380 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
382 @echo "Supported targets:"
384 @echo "build > Standard build"
385 @echo "profile-build > PGO build"
386 @echo "strip > Strip executable"
387 @echo "install > Install executable"
388 @echo "clean > Clean up"
390 @echo "Supported archs:"
392 @echo "x86-64-bmi2 > x86 64-bit with pext support (also enables SSE4)"
393 @echo "x86-64-modern > x86 64-bit with popcnt support (also enables SSE3)"
394 @echo "x86-64 > x86 64-bit generic"
395 @echo "x86-32 > x86 32-bit (also enables SSE)"
396 @echo "x86-32-old > x86 32-bit fall back for old hardware"
397 @echo "ppc-64 > PPC 64-bit"
398 @echo "ppc-32 > PPC 32-bit"
399 @echo "armv7 > ARMv7 32-bit"
400 @echo "armv8 > ARMv8 64-bit"
401 @echo "general-64 > unspecified 64-bit"
402 @echo "general-32 > unspecified 32-bit"
404 @echo "Supported compilers:"
406 @echo "gcc > Gnu compiler (default)"
407 @echo "mingw > Gnu compiler with MinGW under Windows"
408 @echo "clang > LLVM Clang compiler"
409 @echo "icc > Intel compiler"
411 @echo "Simple examples. If you don't know what to do, you likely want to run: "
413 @echo "make build ARCH=x86-64 (This is for 64-bit systems)"
414 @echo "make build ARCH=x86-32 (This is for 32-bit systems)"
416 @echo "Advanced examples, for experienced users: "
418 @echo "make build ARCH=x86-64 COMP=clang"
419 @echo "make profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-4.8"
423 .PHONY: help build profile-build strip install clean objclean profileclean \
424 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
425 clang-profile-use clang-profile-make
428 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
430 profile-build: config-sanity objclean profileclean
432 @echo "Step 1/4. Building instrumented executable ..."
433 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
435 @echo "Step 2/4. Running benchmark for pgo-build ..."
436 $(PGOBENCH) > /dev/null
438 @echo "Step 3/4. Building optimized executable ..."
439 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
440 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
442 @echo "Step 4/4. Deleting profile data ..."
443 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
449 -mkdir -p -m 755 $(BINDIR)
451 -strip $(BINDIR)/$(EXE)
454 clean: objclean profileclean
455 @rm -f .depend *~ core
457 # clean binaries and objects
459 @rm -f $(EXE) *.o ./syzygy/*.o
461 # clean auxiliary profiling files
464 @rm -f bench.txt *.gcda *.gcno
465 @rm -f stockfish.profdata *.profraw
470 ### ==========================================================================
471 ### Section 5. Private targets
472 ### ==========================================================================
479 @echo "debug: '$(debug)'"
480 @echo "sanitize: '$(sanitize)'"
481 @echo "optimize: '$(optimize)'"
482 @echo "arch: '$(arch)'"
483 @echo "bits: '$(bits)'"
484 @echo "kernel: '$(KERNEL)'"
486 @echo "prefetch: '$(prefetch)'"
487 @echo "popcnt: '$(popcnt)'"
488 @echo "sse: '$(sse)'"
489 @echo "pext: '$(pext)'"
493 @echo "CXXFLAGS: $(CXXFLAGS)"
494 @echo "LDFLAGS: $(LDFLAGS)"
496 @echo "Testing config sanity. If this fails, try 'make help' ..."
498 @test "$(debug)" = "yes" || test "$(debug)" = "no"
499 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
500 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
501 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
502 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
503 test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a"
504 @test "$(bits)" = "32" || test "$(bits)" = "64"
505 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
506 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
507 @test "$(sse)" = "yes" || test "$(sse)" = "no"
508 @test "$(pext)" = "yes" || test "$(pext)" = "no"
509 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
512 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
515 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
516 EXTRACXXFLAGS='-fprofile-instr-generate ' \
517 EXTRALDFLAGS=' -fprofile-instr-generate' \
521 llvm-profdata merge -output=stockfish.profdata *.profraw
522 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
523 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
524 EXTRALDFLAGS='-fprofile-use ' \
528 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
529 EXTRACXXFLAGS='-fprofile-generate' \
530 EXTRALDFLAGS='-lgcov' \
534 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
535 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
536 EXTRALDFLAGS='-lgcov' \
541 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
542 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
546 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
547 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
551 -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null