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-2016 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 ### ==========================================================================
24 ### Establish the operating system name
25 KERNEL = $(shell uname -s)
26 ifeq ($(KERNEL),Linux)
27 OS = $(shell uname -o)
33 ### Installation dir definitions
35 BINDIR = $(PREFIX)/bin
37 ### Built-in benchmark for pgo-builds
38 PGOBENCH = ./$(EXE) bench
41 OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
42 material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
43 search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
45 ### ==========================================================================
46 ### Section 2. High-level Configuration
47 ### ==========================================================================
49 # flag --- Comp switch --- Description
50 # ----------------------------------------------------------------------------
52 # debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
53 # sanitize = yes/no --- (-fsanitize ) --- enable undefined behavior checks
54 # optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
55 # arch = (name) --- (-arch) --- Target architecture
56 # bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
57 # prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
58 # popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
59 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
60 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
62 # Note that Makefile is space sensitive, so when adding new architectures
63 # or modifying existing flags, you have to make sure there are no extra spaces
64 # at the end of the line for flag values.
66 ### 2.1. General and architecture defaults
76 ### 2.2 Architecture specific
78 ifeq ($(ARCH),general-32)
82 ifeq ($(ARCH),x86-32-old)
92 ifeq ($(ARCH),general-64)
104 ifeq ($(ARCH),x86-64-modern)
112 ifeq ($(ARCH),x86-64-bmi2)
126 ifeq ($(ARCH),ppc-32)
130 ifeq ($(ARCH),ppc-64)
136 ### ==========================================================================
137 ### Section 3. Low-level configuration
138 ### ==========================================================================
140 ### 3.1 Selecting compiler (default = gcc)
142 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS)
143 DEPENDFLAGS += -std=c++11
144 LDFLAGS += $(EXTRALDFLAGS)
153 CXXFLAGS += -pedantic -Wextra -Wshadow
157 CXXFLAGS += -m$(bits)
161 CXXFLAGS += -m$(bits)
165 ifneq ($(KERNEL),Darwin)
166 LDFLAGS += -Wl,--no-as-needed
173 ifeq ($(KERNEL),Linux)
175 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
176 CXX=x86_64-w64-mingw32-c++
178 CXX=x86_64-w64-mingw32-c++-posix
181 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
182 CXX=i686-w64-mingw32-c++
184 CXX=i686-w64-mingw32-c++-posix
191 CXXFLAGS += -Wextra -Wshadow
198 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
204 CXXFLAGS += -pedantic -Wextra -Wshadow
208 CXXFLAGS += -m$(bits)
212 CXXFLAGS += -m$(bits)
218 profile_make = icc-profile-make
219 profile_use = icc-profile-use
222 profile_make = clang-profile-make
223 profile_use = clang-profile-use
225 profile_make = gcc-profile-make
226 profile_use = gcc-profile-use
230 ifeq ($(KERNEL),Darwin)
231 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
232 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
235 ### Travis CI script uses COMPILER to overwrite CXX
240 ### Allow overwriting CXX from command line
245 ### On mingw use Windows threads, otherwise POSIX
246 ifneq ($(comp),mingw)
247 # On Android Bionic's C library comes with its own pthread implementation bundled in
248 ifneq ($(OS),Android)
249 # Haiku has pthreads in its libroot, so only link it in on other platforms
250 ifneq ($(KERNEL),Haiku)
263 ### 3.2.2 Debugging with undefined behavior sanitizers
264 ifeq ($(sanitize),yes)
265 CXXFLAGS += -g3 -fsanitize=undefined
266 LDFLAGS += -fsanitize=undefined
270 ifeq ($(optimize),yes)
276 ifeq ($(KERNEL),Darwin)
278 CXXFLAGS += -mdynamic-no-pic
280 ifeq ($(arch),x86_64)
281 CXXFLAGS += -mdynamic-no-pic
285 ifeq ($(OS), Android)
286 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
291 ifeq ($(KERNEL),Darwin)
292 CXXFLAGS += -mdynamic-no-pic
297 ifeq ($(KERNEL),Darwin)
299 LDFLAGS += $(CXXFLAGS)
301 CXXFLAGS += -mdynamic-no-pic
303 ifeq ($(arch),x86_64)
304 CXXFLAGS += -mdynamic-no-pic
312 CXXFLAGS += -DIS_64BIT
316 ifeq ($(prefetch),yes)
322 CXXFLAGS += -DNO_PREFETCH
328 CXXFLAGS += -msse3 -DUSE_POPCNT
330 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
336 CXXFLAGS += -DUSE_PEXT
337 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
342 ### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
343 ### This is a mix of compile and link time options because the lto link phase
344 ### needs access to the optimization flags.
346 ifeq ($(optimize),yes)
349 LDFLAGS += $(CXXFLAGS)
355 ifeq ($(KERNEL),Linux)
356 ifeq ($(optimize),yes)
359 LDFLAGS += $(CXXFLAGS)
365 ### 3.9 Android 5 can only run position independent executables. Note that this
366 ### breaks Android 4.0 and earlier.
367 ifeq ($(OS), Android)
369 LDFLAGS += -fPIE -pie
373 ### ==========================================================================
374 ### Section 4. Public targets
375 ### ==========================================================================
379 @echo "To compile stockfish, type: "
381 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
383 @echo "Supported targets:"
385 @echo "build > Standard build"
386 @echo "profile-build > PGO build"
387 @echo "strip > Strip executable"
388 @echo "install > Install executable"
389 @echo "clean > Clean up"
391 @echo "Supported archs:"
393 @echo "x86-64 > x86 64-bit"
394 @echo "x86-64-modern > x86 64-bit with popcnt support"
395 @echo "x86-64-bmi2 > x86 64-bit with pext support"
396 @echo "x86-32 > x86 32-bit with SSE support"
397 @echo "x86-32-old > x86 32-bit fall back for old hardware"
398 @echo "ppc-64 > PPC 64-bit"
399 @echo "ppc-32 > PPC 32-bit"
400 @echo "armv7 > ARMv7 32-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-modern COMP=gcc COMPCXX=g++-4.8"
423 .PHONY: help build profile-build strip install clean objclean profileclean help \
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) $(EXE).exe *.o ./syzygy/*.o
461 # clean auxiliary profiling files
464 @rm -f bench.txt *.gcda ./syzygy/*.gcda *.gcno ./syzygy/*.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)" = "yes" || 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" || test "$(arch)" = "armv7"
503 @test "$(bits)" = "32" || test "$(bits)" = "64"
504 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
505 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
506 @test "$(sse)" = "yes" || test "$(sse)" = "no"
507 @test "$(pext)" = "yes" || test "$(pext)" = "no"
508 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
511 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
514 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
515 EXTRACXXFLAGS='-fprofile-instr-generate ' \
516 EXTRALDFLAGS=' -fprofile-instr-generate' \
520 llvm-profdata merge -output=stockfish.profdata *.profraw
521 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
522 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
523 EXTRALDFLAGS='-fprofile-use ' \
527 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
528 EXTRACXXFLAGS='-fprofile-generate' \
529 EXTRALDFLAGS='-lgcov' \
533 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
534 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
535 EXTRALDFLAGS='-lgcov' \
540 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
541 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
545 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
546 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
550 -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null