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-2018 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
39 OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
40 material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
41 search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
43 ### Establish the operating system name
44 KERNEL = $(shell uname -s)
45 ifeq ($(KERNEL),Linux)
46 OS = $(shell uname -o)
49 ### ==========================================================================
50 ### Section 2. High-level Configuration
51 ### ==========================================================================
53 # flag --- Comp switch --- Description
54 # ----------------------------------------------------------------------------
56 # debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
57 # sanitize = undefined/thread/no (-fsanitize )
58 # --- ( undefined ) --- enable undefined behavior checks
59 # --- ( thread ) --- enable threading error checks
60 # optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
61 # arch = (name) --- (-arch) --- Target architecture
62 # bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
63 # prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
64 # popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
65 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
66 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
68 # Note that Makefile is space sensitive, so when adding new architectures
69 # or modifying existing flags, you have to make sure there are no extra spaces
70 # at the end of the line for flag values.
72 ### 2.1. General and architecture defaults
82 ### 2.2 Architecture specific
84 ifeq ($(ARCH),general-32)
88 ifeq ($(ARCH),x86-32-old)
98 ifeq ($(ARCH),general-64)
103 ifeq ($(ARCH),x86-64)
110 ifeq ($(ARCH),x86-64-modern)
118 ifeq ($(ARCH),x86-64-bmi2)
132 ifeq ($(ARCH),ppc-32)
136 ifeq ($(ARCH),ppc-64)
142 ### ==========================================================================
143 ### Section 3. Low-level configuration
144 ### ==========================================================================
146 ### 3.1 Selecting compiler (default = gcc)
148 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
149 DEPENDFLAGS += -std=c++11
150 LDFLAGS += $(EXTRALDFLAGS)
159 CXXFLAGS += -pedantic -Wextra -Wshadow
163 CXXFLAGS += -m$(bits)
167 CXXFLAGS += -m$(bits)
171 ifneq ($(KERNEL),Darwin)
172 LDFLAGS += -Wl,--no-as-needed
179 ifeq ($(KERNEL),Linux)
181 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
182 CXX=x86_64-w64-mingw32-c++
184 CXX=x86_64-w64-mingw32-c++-posix
187 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
188 CXX=i686-w64-mingw32-c++
190 CXX=i686-w64-mingw32-c++-posix
197 CXXFLAGS += -Wextra -Wshadow
204 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
210 CXXFLAGS += -pedantic -Wextra -Wshadow
212 ifneq ($(KERNEL),Darwin)
213 ifneq ($(KERNEL),OpenBSD)
220 CXXFLAGS += -m$(bits)
224 CXXFLAGS += -m$(bits)
230 profile_make = icc-profile-make
231 profile_use = icc-profile-use
234 profile_make = clang-profile-make
235 profile_use = clang-profile-use
237 profile_make = gcc-profile-make
238 profile_use = gcc-profile-use
242 ifeq ($(KERNEL),Darwin)
243 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
244 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
247 ### Travis CI script uses COMPILER to overwrite CXX
252 ### Allow overwriting CXX from command line
257 ### On mingw use Windows threads, otherwise POSIX
258 ifneq ($(comp),mingw)
259 # On Android Bionic's C library comes with its own pthread implementation bundled in
260 ifneq ($(OS),Android)
261 # Haiku has pthreads in its libroot, so only link it in on other platforms
262 ifneq ($(KERNEL),Haiku)
275 ### 3.2.2 Debugging with undefined behavior sanitizers
276 ifneq ($(sanitize),no)
277 CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
278 LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
282 ifeq ($(optimize),yes)
287 ifeq ($(OS), Android)
288 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
292 ifeq ($(comp),$(filter $(comp),gcc clang icc))
293 ifeq ($(KERNEL),Darwin)
294 CXXFLAGS += -mdynamic-no-pic
301 CXXFLAGS += -DIS_64BIT
305 ifeq ($(prefetch),yes)
311 CXXFLAGS += -DNO_PREFETCH
317 CXXFLAGS += -msse3 -DUSE_POPCNT
319 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
325 CXXFLAGS += -DUSE_PEXT
326 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
331 ### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
332 ### This is a mix of compile and link time options because the lto link phase
333 ### needs access to the optimization flags.
334 ifeq ($(optimize),yes)
336 ifeq ($(comp),$(filter $(comp),gcc clang))
338 LDFLAGS += $(CXXFLAGS)
342 ifeq ($(KERNEL),Linux)
344 LDFLAGS += $(CXXFLAGS)
350 ### 3.9 Android 5 can only run position independent executables. Note that this
351 ### breaks Android 4.0 and earlier.
352 ifeq ($(OS), Android)
354 LDFLAGS += -fPIE -pie
358 ### ==========================================================================
359 ### Section 4. Public targets
360 ### ==========================================================================
364 @echo "To compile stockfish, type: "
366 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
368 @echo "Supported targets:"
370 @echo "build > Standard build"
371 @echo "profile-build > PGO build"
372 @echo "strip > Strip executable"
373 @echo "install > Install executable"
374 @echo "clean > Clean up"
376 @echo "Supported archs:"
378 @echo "x86-64 > x86 64-bit"
379 @echo "x86-64-modern > x86 64-bit with popcnt support"
380 @echo "x86-64-bmi2 > x86 64-bit with pext support"
381 @echo "x86-32 > x86 32-bit with SSE support"
382 @echo "x86-32-old > x86 32-bit fall back for old hardware"
383 @echo "ppc-64 > PPC 64-bit"
384 @echo "ppc-32 > PPC 32-bit"
385 @echo "armv7 > ARMv7 32-bit"
386 @echo "general-64 > unspecified 64-bit"
387 @echo "general-32 > unspecified 32-bit"
389 @echo "Supported compilers:"
391 @echo "gcc > Gnu compiler (default)"
392 @echo "mingw > Gnu compiler with MinGW under Windows"
393 @echo "clang > LLVM Clang compiler"
394 @echo "icc > Intel compiler"
396 @echo "Simple examples. If you don't know what to do, you likely want to run: "
398 @echo "make build ARCH=x86-64 (This is for 64-bit systems)"
399 @echo "make build ARCH=x86-32 (This is for 32-bit systems)"
401 @echo "Advanced examples, for experienced users: "
403 @echo "make build ARCH=x86-64 COMP=clang"
404 @echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
408 .PHONY: help build profile-build strip install clean objclean profileclean help \
409 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
410 clang-profile-use clang-profile-make
413 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
415 profile-build: config-sanity objclean profileclean
417 @echo "Step 1/4. Building instrumented executable ..."
418 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
420 @echo "Step 2/4. Running benchmark for pgo-build ..."
421 $(PGOBENCH) > /dev/null
423 @echo "Step 3/4. Building optimized executable ..."
424 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
425 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
427 @echo "Step 4/4. Deleting profile data ..."
428 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
434 -mkdir -p -m 755 $(BINDIR)
436 -strip $(BINDIR)/$(EXE)
439 clean: objclean profileclean
440 @rm -f .depend *~ core
442 # clean binaries and objects
444 @rm -f $(EXE) *.o ./syzygy/*.o
446 # clean auxiliary profiling files
449 @rm -f bench.txt *.gcda ./syzygy/*.gcda *.gcno ./syzygy/*.gcno
450 @rm -f stockfish.profdata *.profraw
455 ### ==========================================================================
456 ### Section 5. Private targets
457 ### ==========================================================================
464 @echo "debug: '$(debug)'"
465 @echo "sanitize: '$(sanitize)'"
466 @echo "optimize: '$(optimize)'"
467 @echo "arch: '$(arch)'"
468 @echo "bits: '$(bits)'"
469 @echo "kernel: '$(KERNEL)'"
471 @echo "prefetch: '$(prefetch)'"
472 @echo "popcnt: '$(popcnt)'"
473 @echo "sse: '$(sse)'"
474 @echo "pext: '$(pext)'"
478 @echo "CXXFLAGS: $(CXXFLAGS)"
479 @echo "LDFLAGS: $(LDFLAGS)"
481 @echo "Testing config sanity. If this fails, try 'make help' ..."
483 @test "$(debug)" = "yes" || test "$(debug)" = "no"
484 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "no"
485 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
486 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
487 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
488 @test "$(bits)" = "32" || test "$(bits)" = "64"
489 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
490 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
491 @test "$(sse)" = "yes" || test "$(sse)" = "no"
492 @test "$(pext)" = "yes" || test "$(pext)" = "no"
493 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
496 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
499 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
500 EXTRACXXFLAGS='-fprofile-instr-generate ' \
501 EXTRALDFLAGS=' -fprofile-instr-generate' \
505 llvm-profdata merge -output=stockfish.profdata *.profraw
506 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
507 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
508 EXTRALDFLAGS='-fprofile-use ' \
512 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
513 EXTRACXXFLAGS='-fprofile-generate' \
514 EXTRALDFLAGS='-lgcov' \
518 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
519 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
520 EXTRALDFLAGS='-lgcov' \
525 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
526 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
530 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
531 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
535 -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null