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 CXXFLAGS += -Wextra -Wshadow
170 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
176 CXXFLAGS += -pedantic -Wextra -Wshadow
177 ifeq ($(UNAME),Darwin)
178 CXXFLAGS += -std=c++0x -stdlib=libc++
179 DEPENDFLAGS += -std=c++0x -stdlib=libc++
184 profile_prepare = icc-profile-prepare
185 profile_make = icc-profile-make
186 profile_use = icc-profile-use
187 profile_clean = icc-profile-clean
189 profile_prepare = gcc-profile-prepare
190 profile_make = gcc-profile-make
191 profile_use = gcc-profile-use
192 profile_clean = gcc-profile-clean
195 ifeq ($(UNAME),Darwin)
196 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
197 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
200 ### On mingw use Windows threads, otherwise POSIX
201 ifneq ($(comp),mingw)
202 # On Android Bionic's C library comes with its own pthread implementation bundled in
203 ifneq ($(arch),armv7)
204 # Haiku has pthreads in its libroot, so only link it in on other platforms
205 ifneq ($(UNAME),Haiku)
219 ifeq ($(optimize),yes)
224 ifeq ($(UNAME),Darwin)
226 CXXFLAGS += -mdynamic-no-pic
228 ifeq ($(arch),x86_64)
229 CXXFLAGS += -mdynamic-no-pic
234 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
243 ifeq ($(UNAME),Darwin)
244 CXXFLAGS += -fast -mdynamic-no-pic
253 ifeq ($(UNAME),Darwin)
256 LDFLAGS += $(CXXFLAGS)
259 CXXFLAGS += -mdynamic-no-pic
261 ifeq ($(arch),x86_64)
262 CXXFLAGS += -mdynamic-no-pic
270 CXXFLAGS += -DIS_64BIT
274 ifeq ($(prefetch),yes)
280 CXXFLAGS += -DNO_PREFETCH
285 CXXFLAGS += -DUSE_BSFQ
291 CXXFLAGS += -msse3 -DUSE_POPCNT
293 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
299 CXXFLAGS += -DUSE_PEXT
300 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
301 CXXFLAGS += -mbmi -mbmi2
305 ### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw.
306 ### This is a mix of compile and link time options because the lto link phase
307 ### needs access to the optimization flags.
309 ifeq ($(optimize),yes)
312 LDFLAGS += $(CXXFLAGS)
317 ### 3.12 Android 5 can only run position independent executables. Note that this
318 ### breaks Android 4.0 and earlier.
321 LDFLAGS += -fPIE -pie
325 ### ==========================================================================
326 ### Section 4. Public targets
327 ### ==========================================================================
331 @echo "To compile stockfish, type: "
333 @echo "make target ARCH=arch [COMP=comp]"
335 @echo "Supported targets:"
337 @echo "build > Standard build"
338 @echo "profile-build > PGO build"
339 @echo "strip > Strip executable"
340 @echo "install > Install executable"
341 @echo "clean > Clean up"
343 @echo "Supported archs:"
345 @echo "x86-64 > x86 64-bit"
346 @echo "x86-64-modern > x86 64-bit with popcnt support"
347 @echo "x86-64-bmi2 > x86 64-bit with pext support"
348 @echo "x86-32 > x86 32-bit with SSE support"
349 @echo "x86-32-old > x86 32-bit fall back for old hardware"
350 @echo "ppc-64 > PPC 64-bit"
351 @echo "ppc-32 > PPC 32-bit"
352 @echo "armv7 > ARMv7 32-bit"
353 @echo "general-64 > unspecified 64-bit"
354 @echo "general-32 > unspecified 32-bit"
356 @echo "Supported compilers:"
358 @echo "gcc > Gnu compiler (default)"
359 @echo "mingw > Gnu compiler with MinGW under Windows"
360 @echo "clang > LLVM Clang compiler"
361 @echo "icc > Intel compiler"
363 @echo "Examples. If you don't know what to do, you likely want to run: "
365 @echo "make build ARCH=x86-64 (This is for 64-bit systems)"
366 @echo "make build ARCH=x86-32 (This is for 32-bit systems)"
369 .PHONY: build profile-build
371 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
372 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
375 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
377 @echo "Step 0/4. Preparing for profile build."
378 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
380 @echo "Step 1/4. Building executable for benchmark ..."
381 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
382 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
384 @echo "Step 2/4. Running benchmark for pgo-build ..."
385 @$(PGOBENCH) > /dev/null
387 @echo "Step 3/4. Building final executable ..."
388 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
389 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
391 @echo "Step 4/4. Deleting profile data ..."
392 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
398 -mkdir -p -m 755 $(BINDIR)
400 -strip $(BINDIR)/$(EXE)
403 $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
408 ### ==========================================================================
409 ### Section 5. Private targets
410 ### ==========================================================================
417 @echo "debug: '$(debug)'"
418 @echo "optimize: '$(optimize)'"
419 @echo "arch: '$(arch)'"
420 @echo "bits: '$(bits)'"
421 @echo "prefetch: '$(prefetch)'"
422 @echo "bsfq: '$(bsfq)'"
423 @echo "popcnt: '$(popcnt)'"
424 @echo "sse: '$(sse)'"
425 @echo "pext: '$(pext)'"
429 @echo "CXXFLAGS: $(CXXFLAGS)"
430 @echo "LDFLAGS: $(LDFLAGS)"
432 @echo "Testing config sanity. If this fails, try 'make help' ..."
434 @test "$(debug)" = "yes" || test "$(debug)" = "no"
435 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
436 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
437 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
438 @test "$(bits)" = "32" || test "$(bits)" = "64"
439 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
440 @test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
441 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
442 @test "$(sse)" = "yes" || test "$(sse)" = "no"
443 @test "$(pext)" = "yes" || test "$(pext)" = "no"
444 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
447 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
450 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
453 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
454 EXTRACXXFLAGS='-fprofile-generate' \
455 EXTRALDFLAGS='-lgcov' \
459 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
460 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
461 EXTRALDFLAGS='-lgcov' \
465 @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
468 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
472 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
473 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
477 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
478 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
482 @rm -rf profdir bench.txt
485 -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null