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 UNAME = $(shell uname)
30 ### Installation dir definitions
32 BINDIR = $(PREFIX)/bin
34 ### Built-in benchmark for pgo-builds
35 PGOBENCH = ./$(EXE) bench 16 1 1000 default time
38 OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
39 material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
40 search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
42 ### ==========================================================================
43 ### Section 2. High-level Configuration
44 ### ==========================================================================
46 # flag --- Comp switch --- Description
47 # ----------------------------------------------------------------------------
49 # debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
50 # optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
51 # arch = (name) --- (-arch) --- Target architecture
52 # bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
53 # prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
54 # popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
55 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
56 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
58 # Note that Makefile is space sensitive, so when adding new architectures
59 # or modifying existing flags, you have to make sure there are no extra spaces
60 # at the end of the line for flag values.
62 ### 2.1. General and architecture defaults
71 ### 2.2 Architecture specific
73 ifeq ($(ARCH),general-32)
77 ifeq ($(ARCH),x86-32-old)
87 ifeq ($(ARCH),general-64)
99 ifeq ($(ARCH),x86-64-modern)
107 ifeq ($(ARCH),x86-64-bmi2)
121 ifeq ($(ARCH),ppc-32)
125 ifeq ($(ARCH),ppc-64)
131 ### ==========================================================================
132 ### Section 3. Low-level configuration
133 ### ==========================================================================
135 ### 3.1 Selecting compiler (default = gcc)
137 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS)
138 DEPENDFLAGS += -std=c++11
139 LDFLAGS += $(EXTRALDFLAGS)
148 CXXFLAGS += -pedantic -Wextra -Wshadow -m$(bits)
149 ifneq ($(UNAME),Darwin)
150 LDFLAGS += -Wl,--no-as-needed
157 ifeq ($(UNAME),Linux)
159 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
160 CXX=x86_64-w64-mingw32-c++
162 CXX=x86_64-w64-mingw32-c++-posix
165 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
166 CXX=i686-w64-mingw32-c++
168 CXX=i686-w64-mingw32-c++-posix
175 CXXFLAGS += -Wextra -Wshadow
182 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
188 CXXFLAGS += -pedantic -Wextra -Wshadow -m$(bits)
190 ifeq ($(UNAME),Darwin)
191 CXXFLAGS += -stdlib=libc++
192 DEPENDFLAGS += -stdlib=libc++
197 profile_prepare = icc-profile-prepare
198 profile_make = icc-profile-make
199 profile_use = icc-profile-use
200 profile_clean = icc-profile-clean
202 profile_prepare = gcc-profile-prepare
203 profile_make = gcc-profile-make
204 profile_use = gcc-profile-use
205 profile_clean = gcc-profile-clean
208 ifeq ($(UNAME),Darwin)
209 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
210 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
213 ### Travis CI script uses COMPILER to overwrite CXX
218 ### Allow overwriting CXX from command line
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
309 CXXFLAGS += -msse3 -DUSE_POPCNT
311 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
317 CXXFLAGS += -DUSE_PEXT
318 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
319 CXXFLAGS += -mbmi -mbmi2
323 ### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
324 ### This is a mix of compile and link time options because the lto link phase
325 ### needs access to the optimization flags.
327 ifeq ($(optimize),yes)
330 LDFLAGS += $(CXXFLAGS)
336 ifeq ($(UNAME),Linux)
337 ifeq ($(optimize),yes)
340 LDFLAGS += $(CXXFLAGS)
346 ### 3.12 Android 5 can only run position independent executables. Note that this
347 ### breaks Android 4.0 and earlier.
350 LDFLAGS += -fPIE -pie
354 ### ==========================================================================
355 ### Section 4. Public targets
356 ### ==========================================================================
360 @echo "To compile stockfish, type: "
362 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
364 @echo "Supported targets:"
366 @echo "build > Standard build"
367 @echo "profile-build > PGO build"
368 @echo "strip > Strip executable"
369 @echo "install > Install executable"
370 @echo "clean > Clean up"
372 @echo "Supported archs:"
374 @echo "x86-64 > x86 64-bit"
375 @echo "x86-64-modern > x86 64-bit with popcnt support"
376 @echo "x86-64-bmi2 > x86 64-bit with pext support"
377 @echo "x86-32 > x86 32-bit with SSE support"
378 @echo "x86-32-old > x86 32-bit fall back for old hardware"
379 @echo "ppc-64 > PPC 64-bit"
380 @echo "ppc-32 > PPC 32-bit"
381 @echo "armv7 > ARMv7 32-bit"
382 @echo "general-64 > unspecified 64-bit"
383 @echo "general-32 > unspecified 32-bit"
385 @echo "Supported compilers:"
387 @echo "gcc > Gnu compiler (default)"
388 @echo "mingw > Gnu compiler with MinGW under Windows"
389 @echo "clang > LLVM Clang compiler"
390 @echo "icc > Intel compiler"
392 @echo "Simple examples. If you don't know what to do, you likely want to run: "
394 @echo "make build ARCH=x86-64 (This is for 64-bit systems)"
395 @echo "make build ARCH=x86-32 (This is for 32-bit systems)"
397 @echo "Advanced examples, for experienced users: "
399 @echo "make build ARCH=x86-64 COMP=clang"
400 @echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
404 .PHONY: build profile-build
406 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
407 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
410 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
412 @echo "Step 0/4. Preparing for profile build."
413 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
415 @echo "Step 1/4. Building executable for benchmark ..."
416 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
417 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
419 @echo "Step 2/4. Running benchmark for pgo-build ..."
420 $(PGOBENCH) > /dev/null
422 @echo "Step 3/4. Building final executable ..."
423 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
424 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
426 @echo "Step 4/4. Deleting profile data ..."
427 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
433 -mkdir -p -m 755 $(BINDIR)
435 -strip $(BINDIR)/$(EXE)
438 $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
443 ### ==========================================================================
444 ### Section 5. Private targets
445 ### ==========================================================================
452 @echo "debug: '$(debug)'"
453 @echo "optimize: '$(optimize)'"
454 @echo "arch: '$(arch)'"
455 @echo "bits: '$(bits)'"
456 @echo "prefetch: '$(prefetch)'"
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 "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
475 @test "$(sse)" = "yes" || test "$(sse)" = "no"
476 @test "$(pext)" = "yes" || test "$(pext)" = "no"
477 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
480 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
483 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
486 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
487 EXTRACXXFLAGS='-fprofile-generate' \
488 EXTRALDFLAGS='-lgcov' \
492 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
493 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
494 EXTRALDFLAGS='-lgcov' \
498 @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
501 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
505 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
506 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
510 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
511 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
515 @rm -rf profdir bench.txt
518 -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null