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 # optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
54 # arch = (name) --- (-arch) --- Target architecture
55 # bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
56 # prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
57 # popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
58 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
59 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
61 # Note that Makefile is space sensitive, so when adding new architectures
62 # or modifying existing flags, you have to make sure there are no extra spaces
63 # at the end of the line for flag values.
65 ### 2.1. General and architecture defaults
74 ### 2.2 Architecture specific
76 ifeq ($(ARCH),general-32)
80 ifeq ($(ARCH),x86-32-old)
90 ifeq ($(ARCH),general-64)
102 ifeq ($(ARCH),x86-64-modern)
110 ifeq ($(ARCH),x86-64-bmi2)
124 ifeq ($(ARCH),ppc-32)
128 ifeq ($(ARCH),ppc-64)
134 ### ==========================================================================
135 ### Section 3. Low-level configuration
136 ### ==========================================================================
138 ### 3.1 Selecting compiler (default = gcc)
140 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS)
141 DEPENDFLAGS += -std=c++11
142 LDFLAGS += $(EXTRALDFLAGS)
151 CXXFLAGS += -pedantic -Wextra -Wshadow
155 CXXFLAGS += -m$(bits)
158 CXXFLAGS += -m$(bits)
161 ifneq ($(KERNEL),Darwin)
162 LDFLAGS += -Wl,--no-as-needed
169 ifeq ($(KERNEL),Linux)
171 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
172 CXX=x86_64-w64-mingw32-c++
174 CXX=x86_64-w64-mingw32-c++-posix
177 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
178 CXX=i686-w64-mingw32-c++
180 CXX=i686-w64-mingw32-c++-posix
187 CXXFLAGS += -Wextra -Wshadow
194 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
200 CXXFLAGS += -pedantic -Wextra -Wshadow
204 CXXFLAGS += -m$(bits)
208 CXXFLAGS += -m$(bits)
212 ifeq ($(KERNEL),Darwin)
213 CXXFLAGS += -stdlib=libc++
214 DEPENDFLAGS += -stdlib=libc++
219 profile_prepare = icc-profile-prepare
220 profile_make = icc-profile-make
221 profile_use = icc-profile-use
222 profile_clean = icc-profile-clean
224 profile_prepare = gcc-profile-prepare
225 profile_make = gcc-profile-make
226 profile_use = gcc-profile-use
227 profile_clean = gcc-profile-clean
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)
264 ifeq ($(optimize),yes)
270 ifeq ($(KERNEL),Darwin)
272 CXXFLAGS += -mdynamic-no-pic
274 ifeq ($(arch),x86_64)
275 CXXFLAGS += -mdynamic-no-pic
279 ifeq ($(OS), Android)
280 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
285 ifeq ($(KERNEL),Darwin)
286 CXXFLAGS += -mdynamic-no-pic
291 ifeq ($(KERNEL),Darwin)
294 LDFLAGS += $(CXXFLAGS)
297 CXXFLAGS += -mdynamic-no-pic
299 ifeq ($(arch),x86_64)
300 CXXFLAGS += -mdynamic-no-pic
308 CXXFLAGS += -DIS_64BIT
312 ifeq ($(prefetch),yes)
318 CXXFLAGS += -DNO_PREFETCH
324 CXXFLAGS += -msse3 -DUSE_POPCNT
326 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
332 CXXFLAGS += -DUSE_PEXT
333 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
338 ### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
339 ### This is a mix of compile and link time options because the lto link phase
340 ### needs access to the optimization flags.
342 ifeq ($(optimize),yes)
345 LDFLAGS += $(CXXFLAGS)
351 ifeq ($(KERNEL),Linux)
352 ifeq ($(optimize),yes)
355 LDFLAGS += $(CXXFLAGS)
361 ### 3.9 Android 5 can only run position independent executables. Note that this
362 ### breaks Android 4.0 and earlier.
363 ifeq ($(OS), Android)
365 LDFLAGS += -fPIE -pie
369 ### ==========================================================================
370 ### Section 4. Public targets
371 ### ==========================================================================
375 @echo "To compile stockfish, type: "
377 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
379 @echo "Supported targets:"
381 @echo "build > Standard build"
382 @echo "profile-build > PGO build"
383 @echo "strip > Strip executable"
384 @echo "install > Install executable"
385 @echo "clean > Clean up"
387 @echo "Supported archs:"
389 @echo "x86-64 > x86 64-bit"
390 @echo "x86-64-modern > x86 64-bit with popcnt support"
391 @echo "x86-64-bmi2 > x86 64-bit with pext support"
392 @echo "x86-32 > x86 32-bit with SSE support"
393 @echo "x86-32-old > x86 32-bit fall back for old hardware"
394 @echo "ppc-64 > PPC 64-bit"
395 @echo "ppc-32 > PPC 32-bit"
396 @echo "armv7 > ARMv7 32-bit"
397 @echo "general-64 > unspecified 64-bit"
398 @echo "general-32 > unspecified 32-bit"
400 @echo "Supported compilers:"
402 @echo "gcc > Gnu compiler (default)"
403 @echo "mingw > Gnu compiler with MinGW under Windows"
404 @echo "clang > LLVM Clang compiler"
405 @echo "icc > Intel compiler"
407 @echo "Simple examples. If you don't know what to do, you likely want to run: "
409 @echo "make build ARCH=x86-64 (This is for 64-bit systems)"
410 @echo "make build ARCH=x86-32 (This is for 32-bit systems)"
412 @echo "Advanced examples, for experienced users: "
414 @echo "make build ARCH=x86-64 COMP=clang"
415 @echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
419 .PHONY: build profile-build
421 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
422 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
425 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
427 @echo "Step 0/4. Preparing for profile build."
428 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
430 @echo "Step 1/4. Building executable for benchmark ..."
431 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
432 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
434 @echo "Step 2/4. Running benchmark for pgo-build ..."
435 $(PGOBENCH) > /dev/null
437 @echo "Step 3/4. Building final executable ..."
438 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
439 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
441 @echo "Step 4/4. Deleting profile data ..."
442 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
448 -mkdir -p -m 755 $(BINDIR)
450 -strip $(BINDIR)/$(EXE)
453 $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
458 ### ==========================================================================
459 ### Section 5. Private targets
460 ### ==========================================================================
467 @echo "debug: '$(debug)'"
468 @echo "optimize: '$(optimize)'"
469 @echo "arch: '$(arch)'"
470 @echo "bits: '$(bits)'"
471 @echo "kernel: '$(KERNEL)'"
473 @echo "prefetch: '$(prefetch)'"
474 @echo "popcnt: '$(popcnt)'"
475 @echo "sse: '$(sse)'"
476 @echo "pext: '$(pext)'"
480 @echo "CXXFLAGS: $(CXXFLAGS)"
481 @echo "LDFLAGS: $(LDFLAGS)"
483 @echo "Testing config sanity. If this fails, try 'make help' ..."
485 @test "$(debug)" = "yes" || test "$(debug)" = "no"
486 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
487 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
488 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
489 @test "$(bits)" = "32" || test "$(bits)" = "64"
490 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
491 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
492 @test "$(sse)" = "yes" || test "$(sse)" = "no"
493 @test "$(pext)" = "yes" || test "$(pext)" = "no"
494 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
497 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
500 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
503 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
504 EXTRACXXFLAGS='-fprofile-generate' \
505 EXTRALDFLAGS='-lgcov' \
509 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
510 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
511 EXTRALDFLAGS='-lgcov' \
515 @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
518 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
522 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
523 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
527 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
528 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
532 @rm -rf profdir bench.txt
535 -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null