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 \
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 $(EXTRACXXFLAGS)
144 LDFLAGS += $(EXTRALDFLAGS)
153 CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
159 CXXFLAGS += -Wextra -Wshadow
160 LDFLAGS += -static-libstdc++ -static-libgcc
166 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
172 CXXFLAGS += -pedantic -Wno-long-long -Wextra -Wshadow
176 profile_prepare = icc-profile-prepare
177 profile_make = icc-profile-make
178 profile_use = icc-profile-use
179 profile_clean = icc-profile-clean
181 profile_prepare = gcc-profile-prepare
182 profile_make = gcc-profile-make
183 profile_use = gcc-profile-use
184 profile_clean = gcc-profile-clean
187 ifeq ($(UNAME),Darwin)
188 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.6
189 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.6
192 ### On mingw use Windows threads, otherwise POSIX
193 ifneq ($(comp),mingw)
194 # On Android Bionic's C library comes with its own pthread implementation bundled in
195 ifneq ($(arch),armv7)
196 # Haiku has pthreads in its libroot, so only link it in on other platforms
197 ifneq ($(UNAME),Haiku)
211 ifeq ($(optimize),yes)
216 ifeq ($(UNAME),Darwin)
218 CXXFLAGS += -mdynamic-no-pic
220 ifeq ($(arch),x86_64)
221 CXXFLAGS += -mdynamic-no-pic
226 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
235 ifeq ($(UNAME),Darwin)
236 CXXFLAGS += -fast -mdynamic-no-pic
245 ifeq ($(UNAME),Darwin)
248 LDFLAGS += $(CXXFLAGS)
251 CXXFLAGS += -mdynamic-no-pic
253 ifeq ($(arch),x86_64)
254 CXXFLAGS += -mdynamic-no-pic
262 CXXFLAGS += -DIS_64BIT
266 ifeq ($(prefetch),yes)
272 CXXFLAGS += -DNO_PREFETCH
277 CXXFLAGS += -DUSE_BSFQ
283 CXXFLAGS += -msse3 -DUSE_POPCNT
285 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
291 CXXFLAGS += -DUSE_PEXT
292 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
293 CXXFLAGS += -mbmi -mbmi2
297 ### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw.
298 ### This is a mix of compile and link time options because the lto link phase
299 ### needs access to the optimization flags.
301 ifeq ($(optimize),yes)
303 GCC_MAJOR := `$(CXX) -dumpversion | cut -f1 -d.`
304 GCC_MINOR := `$(CXX) -dumpversion | cut -f2 -d.`
305 ifeq (1,$(shell expr \( $(GCC_MAJOR) \> 4 \) \| \( $(GCC_MAJOR) \= 4 \& $(GCC_MINOR) \>= 5 \)))
307 LDFLAGS += $(CXXFLAGS)
313 ### 3.12 Android 5 can only run position independent executables. Note that this
314 ### breaks Android 4.0 and earlier.
317 LDFLAGS += -fPIE -pie
321 ### ==========================================================================
322 ### Section 4. Public targets
323 ### ==========================================================================
327 @echo "To compile stockfish, type: "
329 @echo "make target ARCH=arch [COMP=comp]"
331 @echo "Supported targets:"
333 @echo "build > Standard build"
334 @echo "profile-build > PGO build"
335 @echo "strip > Strip executable"
336 @echo "install > Install executable"
337 @echo "clean > Clean up"
339 @echo "Supported archs:"
341 @echo "x86-64 > x86 64-bit"
342 @echo "x86-64-modern > x86 64-bit with popcnt support"
343 @echo "x86-64-bmi2 > x86 64-bit with pext support"
344 @echo "x86-32 > x86 32-bit with SSE support"
345 @echo "x86-32-old > x86 32-bit fall back for old hardware"
346 @echo "ppc-64 > PPC 64-bit"
347 @echo "ppc-32 > PPC 32-bit"
348 @echo "armv7 > ARMv7 32-bit"
349 @echo "general-64 > unspecified 64-bit"
350 @echo "general-32 > unspecified 32-bit"
352 @echo "Supported compilers:"
354 @echo "gcc > Gnu compiler (default)"
355 @echo "mingw > Gnu compiler with MinGW under Windows"
356 @echo "clang > LLVM Clang compiler"
357 @echo "icc > Intel compiler"
359 @echo "Examples. If you don't know what to do, you likely want to run: "
361 @echo "make build ARCH=x86-64 (This is for 64-bit systems)"
362 @echo "make build ARCH=x86-32 (This is for 32-bit systems)"
365 .PHONY: build profile-build
367 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
368 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
371 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
373 @echo "Step 0/4. Preparing for profile build."
374 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
376 @echo "Step 1/4. Building executable for benchmark ..."
377 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
378 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
380 @echo "Step 2/4. Running benchmark for pgo-build ..."
381 @$(PGOBENCH) > /dev/null
383 @echo "Step 3/4. Building final executable ..."
384 @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
385 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
387 @echo "Step 4/4. Deleting profile data ..."
388 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
394 -mkdir -p -m 755 $(BINDIR)
396 -strip $(BINDIR)/$(EXE)
399 $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
404 ### ==========================================================================
405 ### Section 5. Private targets
406 ### ==========================================================================
413 @echo "debug: '$(debug)'"
414 @echo "optimize: '$(optimize)'"
415 @echo "arch: '$(arch)'"
416 @echo "bits: '$(bits)'"
417 @echo "prefetch: '$(prefetch)'"
418 @echo "bsfq: '$(bsfq)'"
419 @echo "popcnt: '$(popcnt)'"
420 @echo "sse: '$(sse)'"
421 @echo "pext: '$(pext)'"
425 @echo "CXXFLAGS: $(CXXFLAGS)"
426 @echo "LDFLAGS: $(LDFLAGS)"
428 @echo "Testing config sanity. If this fails, try 'make help' ..."
430 @test "$(debug)" = "yes" || test "$(debug)" = "no"
431 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
432 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
433 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
434 @test "$(bits)" = "32" || test "$(bits)" = "64"
435 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
436 @test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
437 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
438 @test "$(sse)" = "yes" || test "$(sse)" = "no"
439 @test "$(pext)" = "yes" || test "$(pext)" = "no"
440 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
443 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
446 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
449 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
450 EXTRACXXFLAGS='-fprofile-arcs' \
451 EXTRALDFLAGS='-lgcov' \
455 # Deleting corrupt ucioption.gc* profile files is necessary to avoid an
456 # "internal compiler error" for gcc versions 4.7.x
458 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
459 EXTRACXXFLAGS='-fbranch-probabilities' \
460 EXTRALDFLAGS='-lgcov' \
464 @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
467 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
471 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
472 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
476 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
477 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
481 @rm -rf profdir bench.txt
484 -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null