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
5 # Stockfish is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # Stockfish is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 ### ==========================================================================
20 ### Section 1. General Configuration
21 ### ==========================================================================
23 ### Establish the operating system name
24 UNAME = $(shell uname)
29 ### Installation dir definitions
31 BINDIR = $(PREFIX)/bin
33 ### Built-in benchmark for pgo-builds
34 PGOBENCH = ./$(EXE) bench 16 1 1000 default time
37 OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
38 material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
39 search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
41 ### ==========================================================================
42 ### Section 2. High-level Configuration
43 ### ==========================================================================
45 # flag --- Comp switch --- Description
46 # ----------------------------------------------------------------------------
48 # debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
49 # optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
50 # arch = (name) --- (-arch) --- Target architecture
51 # bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
52 # prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch x86 asm-instruction
53 # bsfq = yes/no --- -DUSE_BSFQ --- Use bsfq x86_64 asm-instruction (only
54 # with GCC and ICC 64-bit)
55 # popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt x86_64 asm-instruction
56 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
57 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
59 # Note that Makefile is space sensitive, so when adding new architectures
60 # or modifying existing flags, you have to make sure there are no extra spaces
61 # at the end of the line for flag values.
63 ### 2.1. General and architecture defaults
73 ### 2.2 Architecture specific
75 ifeq ($(ARCH),general-32)
79 ifeq ($(ARCH),x86-32-old)
89 ifeq ($(ARCH),general-64)
102 ifeq ($(ARCH),x86-64-modern)
111 ifeq ($(ARCH),x86-64-bmi2)
127 ifeq ($(ARCH),ppc-32)
131 ifeq ($(ARCH),ppc-64)
137 ### ==========================================================================
138 ### Section 3. Low-level configuration
139 ### ==========================================================================
141 ### 3.1 Selecting compiler (default = gcc)
143 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS)
144 DEPENDFLAGS += -std=c++11
145 LDFLAGS += $(EXTRALDFLAGS)
154 CXXFLAGS += -pedantic -Wextra -Wshadow
155 ifneq ($(UNAME),Darwin)
156 LDFLAGS += -Wl,--no-as-needed
163 ifeq ($(UNAME),Linux)
165 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
166 CXX=x86_64-w64-mingw32-c++
168 CXX=x86_64-w64-mingw32-c++-posix
171 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
172 CXX=i686-w64-mingw32-c++
174 CXX=i686-w64-mingw32-c++-posix
181 CXXFLAGS += -Wextra -Wshadow
188 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
194 CXXFLAGS += -pedantic -Wextra -Wshadow
195 ifeq ($(UNAME),Darwin)
196 CXXFLAGS += -std=c++0x -stdlib=libc++
197 DEPENDFLAGS += -std=c++0x -stdlib=libc++
202 profile_prepare = icc-profile-prepare
203 profile_make = icc-profile-make
204 profile_use = icc-profile-use
205 profile_clean = icc-profile-clean
207 profile_prepare = gcc-profile-prepare
208 profile_make = gcc-profile-make
209 profile_use = gcc-profile-use
210 profile_clean = gcc-profile-clean
213 ifeq ($(UNAME),Darwin)
214 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
215 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
218 ### Travis CI script uses COMPILER to overwrite CXX
223 ### On mingw use Windows threads, otherwise POSIX
224 ifneq ($(comp),mingw)
225 # On Android Bionic's C library comes with its own pthread implementation bundled in
226 ifneq ($(arch),armv7)
227 # Haiku has pthreads in its libroot, so only link it in on other platforms
228 ifneq ($(UNAME),Haiku)
242 ifeq ($(optimize),yes)
247 ifeq ($(UNAME),Darwin)
249 CXXFLAGS += -mdynamic-no-pic
251 ifeq ($(arch),x86_64)
252 CXXFLAGS += -mdynamic-no-pic
257 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
266 ifeq ($(UNAME),Darwin)
267 CXXFLAGS += -fast -mdynamic-no-pic
276 ifeq ($(UNAME),Darwin)
279 LDFLAGS += $(CXXFLAGS)
282 CXXFLAGS += -mdynamic-no-pic
284 ifeq ($(arch),x86_64)
285 CXXFLAGS += -mdynamic-no-pic
293 CXXFLAGS += -DIS_64BIT
297 ifeq ($(prefetch),yes)
303 CXXFLAGS += -DNO_PREFETCH
308 CXXFLAGS += -DUSE_BSFQ
314 CXXFLAGS += -msse3 -DUSE_POPCNT
316 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
322 CXXFLAGS += -DUSE_PEXT
323 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
324 CXXFLAGS += -mbmi -mbmi2
328 ### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
329 ### This is a mix of compile and link time options because the lto link phase
330 ### needs access to the optimization flags.
332 ifeq ($(optimize),yes)
335 LDFLAGS += $(CXXFLAGS)
341 ifeq ($(UNAME),Linux)
342 ifeq ($(optimize),yes)
345 LDFLAGS += $(CXXFLAGS)
351 ### 3.12 Android 5 can only run position independent executables. Note that this
352 ### breaks Android 4.0 and earlier.
355 LDFLAGS += -fPIE -pie
359 ### ==========================================================================
360 ### Section 4. Public targets
361 ### ==========================================================================
365 @echo "To compile stockfish, type: "
367 @echo "make target ARCH=arch [COMP=comp]"
369 @echo "Supported targets:"
371 @echo "build > Standard build"
372 @echo "profile-build > PGO build"
373 @echo "strip > Strip executable"
374 @echo "install > Install executable"
375 @echo "clean > Clean up"
377 @echo "Supported archs:"
379 @echo "x86-64 > x86 64-bit"
380 @echo "x86-64-modern > x86 64-bit with popcnt support"
381 @echo "x86-64-bmi2 > x86 64-bit with pext support"
382 @echo "x86-32 > x86 32-bit with SSE support"
383 @echo "x86-32-old > x86 32-bit fall back for old hardware"
384 @echo "ppc-64 > PPC 64-bit"
385 @echo "ppc-32 > PPC 32-bit"
386 @echo "armv7 > ARMv7 32-bit"
387 @echo "general-64 > unspecified 64-bit"
388 @echo "general-32 > unspecified 32-bit"
390 @echo "Supported compilers:"
392 @echo "gcc > Gnu compiler (default)"
393 @echo "mingw > Gnu compiler with MinGW under Windows"
394 @echo "clang > LLVM Clang compiler"
395 @echo "icc > Intel compiler"
397 @echo "Examples. If you don't know what to do, you likely want to run: "
399 @echo "make build ARCH=x86-64 (This is for 64-bit systems)"
400 @echo "make build ARCH=x86-32 (This is for 32-bit systems)"
403 .PHONY: build profile-build
405 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
406 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
409 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
411 @echo "Step 0/4. Preparing for profile build."
412 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
414 @echo "Step 1/4. Building executable for benchmark ..."
415 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
416 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
418 @echo "Step 2/4. Running benchmark for pgo-build ..."
419 $(PGOBENCH) > /dev/null
421 @echo "Step 3/4. Building final executable ..."
422 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
423 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
425 @echo "Step 4/4. Deleting profile data ..."
426 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
432 -mkdir -p -m 755 $(BINDIR)
434 -strip $(BINDIR)/$(EXE)
437 $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
442 ### ==========================================================================
443 ### Section 5. Private targets
444 ### ==========================================================================
451 @echo "debug: '$(debug)'"
452 @echo "optimize: '$(optimize)'"
453 @echo "arch: '$(arch)'"
454 @echo "bits: '$(bits)'"
455 @echo "prefetch: '$(prefetch)'"
456 @echo "bsfq: '$(bsfq)'"
457 @echo "popcnt: '$(popcnt)'"
458 @echo "sse: '$(sse)'"
459 @echo "pext: '$(pext)'"
463 @echo "CXXFLAGS: $(CXXFLAGS)"
464 @echo "LDFLAGS: $(LDFLAGS)"
466 @echo "Testing config sanity. If this fails, try 'make help' ..."
468 @test "$(debug)" = "yes" || test "$(debug)" = "no"
469 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
470 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
471 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
472 @test "$(bits)" = "32" || test "$(bits)" = "64"
473 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
474 @test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
475 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
476 @test "$(sse)" = "yes" || test "$(sse)" = "no"
477 @test "$(pext)" = "yes" || test "$(pext)" = "no"
478 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
481 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
484 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
487 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
488 EXTRACXXFLAGS='-fprofile-generate' \
489 EXTRALDFLAGS='-lgcov' \
493 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
494 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
495 EXTRALDFLAGS='-lgcov' \
499 @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
502 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
506 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
507 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
511 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
512 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
516 @rm -rf profdir bench.txt
519 -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null