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-2019 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 ### ==========================================================================
31 ### Installation dir definitions
33 BINDIR = $(PREFIX)/bin
35 ### Built-in benchmark for pgo-builds
36 PGOBENCH = ./$(EXE) bench
39 OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
40 material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
41 search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o \
42 hashprobe.grpc.pb.o hashprobe.pb.o
43 CLIOBJS = client.o hashprobe.grpc.pb.o hashprobe.pb.o uci.o
45 ### Establish the operating system name
46 KERNEL = $(shell uname -s)
47 ifeq ($(KERNEL),Linux)
48 OS = $(shell uname -o)
51 ### ==========================================================================
52 ### Section 2. High-level Configuration
53 ### ==========================================================================
55 # flag --- Comp switch --- Description
56 # ----------------------------------------------------------------------------
58 # debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
59 # sanitize = undefined/thread/no (-fsanitize )
60 # --- ( undefined ) --- enable undefined behavior checks
61 # --- ( thread ) --- enable threading error checks
62 # optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
63 # arch = (name) --- (-arch) --- Target architecture
64 # bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
65 # prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
66 # popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
67 # sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
68 # pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
70 # Note that Makefile is space sensitive, so when adding new architectures
71 # or modifying existing flags, you have to make sure there are no extra spaces
72 # at the end of the line for flag values.
74 ### 2.1. General and architecture defaults
84 ### 2.2 Architecture specific
86 ifeq ($(ARCH),general-32)
90 ifeq ($(ARCH),x86-32-old)
100 ifeq ($(ARCH),general-64)
105 ifeq ($(ARCH),x86-64)
112 ifeq ($(ARCH),x86-64-modern)
120 ifeq ($(ARCH),x86-64-bmi2)
134 ifeq ($(ARCH),ppc-32)
138 ifeq ($(ARCH),ppc-64)
144 ### ==========================================================================
145 ### Section 3. Low-level configuration
146 ### ==========================================================================
148 ### 3.1 Selecting compiler (default = gcc)
150 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
151 DEPENDFLAGS += -std=c++11
152 LDFLAGS += $(EXTRALDFLAGS)
161 CXXFLAGS += -pedantic -Wextra
165 CXXFLAGS += -m$(bits)
169 CXXFLAGS += -m$(bits)
173 ifneq ($(KERNEL),Darwin)
174 LDFLAGS += -Wl,--no-as-needed
181 ifeq ($(KERNEL),Linux)
183 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
184 CXX=x86_64-w64-mingw32-c++
186 CXX=x86_64-w64-mingw32-c++-posix
189 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
190 CXX=i686-w64-mingw32-c++
192 CXX=i686-w64-mingw32-c++-posix
199 CXXFLAGS += -Wextra -Wshadow
206 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
212 CXXFLAGS += -pedantic -Wextra -Wshadow
214 ifneq ($(KERNEL),Darwin)
215 ifneq ($(KERNEL),OpenBSD)
222 CXXFLAGS += -m$(bits)
226 CXXFLAGS += -m$(bits)
232 profile_make = icc-profile-make
233 profile_use = icc-profile-use
236 profile_make = clang-profile-make
237 profile_use = clang-profile-use
239 profile_make = gcc-profile-make
240 profile_use = gcc-profile-use
244 ifeq ($(KERNEL),Darwin)
245 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
246 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
249 ### Travis CI script uses COMPILER to overwrite CXX
254 ### Allow overwriting CXX from command line
259 ### On mingw use Windows threads, otherwise POSIX
260 ifneq ($(comp),mingw)
261 # On Android Bionic's C library comes with its own pthread implementation bundled in
262 ifneq ($(OS),Android)
263 # Haiku has pthreads in its libroot, so only link it in on other platforms
264 ifneq ($(KERNEL),Haiku)
277 ### 3.2.2 Debugging with undefined behavior sanitizers
278 ifneq ($(sanitize),no)
279 CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
280 LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
284 ifeq ($(optimize),yes)
289 ifeq ($(OS), Android)
290 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
294 ifeq ($(comp),$(filter $(comp),gcc clang icc))
295 ifeq ($(KERNEL),Darwin)
296 CXXFLAGS += -mdynamic-no-pic
303 CXXFLAGS += -DIS_64BIT
307 ifeq ($(prefetch),yes)
313 CXXFLAGS += -DNO_PREFETCH
319 CXXFLAGS += -msse3 -DUSE_POPCNT
321 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
327 CXXFLAGS += -DUSE_PEXT
328 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
333 ### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
334 ### This is a mix of compile and link time options because the lto link phase
335 ### needs access to the optimization flags.
336 ifeq ($(optimize),yes)
338 ifeq ($(comp),$(filter $(comp),gcc clang))
340 LDFLAGS += $(CXXFLAGS)
344 ifeq ($(KERNEL),Linux)
346 LDFLAGS += $(CXXFLAGS)
352 ### 3.9 Android 5 can only run position independent executables. Note that this
353 ### breaks Android 4.0 and earlier.
354 ifeq ($(OS), Android)
356 LDFLAGS += -fPIE -pie
360 ### ==========================================================================
361 ### Section 4. Public targets
362 ### ==========================================================================
366 @echo "To compile stockfish, type: "
368 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
370 @echo "Supported targets:"
372 @echo "build > Standard build"
373 @echo "profile-build > PGO build"
374 @echo "strip > Strip executable"
375 @echo "install > Install executable"
376 @echo "clean > Clean up"
378 @echo "Supported archs:"
380 @echo "x86-64 > x86 64-bit"
381 @echo "x86-64-modern > x86 64-bit with popcnt support"
382 @echo "x86-64-bmi2 > x86 64-bit with pext support"
383 @echo "x86-32 > x86 32-bit with SSE support"
384 @echo "x86-32-old > x86 32-bit fall back for old hardware"
385 @echo "ppc-64 > PPC 64-bit"
386 @echo "ppc-32 > PPC 32-bit"
387 @echo "armv7 > ARMv7 32-bit"
388 @echo "general-64 > unspecified 64-bit"
389 @echo "general-32 > unspecified 32-bit"
391 @echo "Supported compilers:"
393 @echo "gcc > Gnu compiler (default)"
394 @echo "mingw > Gnu compiler with MinGW under Windows"
395 @echo "clang > LLVM Clang compiler"
396 @echo "icc > Intel compiler"
398 @echo "Simple examples. If you don't know what to do, you likely want to run: "
400 @echo "make build ARCH=x86-64 (This is for 64-bit systems)"
401 @echo "make build ARCH=x86-32 (This is for 32-bit systems)"
403 @echo "Advanced examples, for experienced users: "
405 @echo "make build ARCH=x86-64 COMP=clang"
406 @echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
410 .PHONY: help build profile-build strip install clean objclean profileclean help \
411 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
412 clang-profile-use clang-profile-make
415 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
417 profile-build: config-sanity objclean profileclean
419 @echo "Step 1/4. Building instrumented executable ..."
420 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
422 @echo "Step 2/4. Running benchmark for pgo-build ..."
423 $(PGOBENCH) > /dev/null
425 @echo "Step 3/4. Building optimized executable ..."
426 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
427 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
429 @echo "Step 4/4. Deleting profile data ..."
430 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
436 -mkdir -p -m 755 $(BINDIR)
438 -strip $(BINDIR)/$(EXE)
441 clean: objclean profileclean
442 @rm -f .depend *~ core
444 # clean binaries and objects
446 @rm -f $(EXE) *.o ./syzygy/*.o
448 # clean auxiliary profiling files
451 @rm -f bench.txt *.gcda ./syzygy/*.gcda *.gcno ./syzygy/*.gcno
452 @rm -f stockfish.profdata *.profraw
457 ### ==========================================================================
458 ### Section 5. Private targets
459 ### ==========================================================================
461 all: $(EXE) client .depend
466 @echo "debug: '$(debug)'"
467 @echo "sanitize: '$(sanitize)'"
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 "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "no"
487 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
488 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
489 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
490 @test "$(bits)" = "32" || test "$(bits)" = "64"
491 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
492 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
493 @test "$(sse)" = "yes" || test "$(sse)" = "no"
494 @test "$(pext)" = "yes" || test "$(pext)" = "no"
495 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
498 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
501 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
502 EXTRACXXFLAGS='-fprofile-instr-generate ' \
503 EXTRALDFLAGS=' -fprofile-instr-generate' \
507 llvm-profdata merge -output=stockfish.profdata *.profraw
508 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
509 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
510 EXTRALDFLAGS='-fprofile-use ' \
514 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
515 EXTRACXXFLAGS='-fprofile-generate' \
516 EXTRALDFLAGS='-lgcov' \
520 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
521 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
522 EXTRALDFLAGS='-lgcov' \
527 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
528 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
532 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
533 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
540 GRPC_CPP_PLUGIN = grpc_cpp_plugin
541 GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
543 %.grpc.pb.h %.grpc.pb.cc: %.proto
544 $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
550 %.pb.h %.pb.cc: %.proto
551 $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
553 #LDFLAGS += -Wl,-Bstatic -Wl,-\( -lprotobuf -lgrpc++_unsecure -lgrpc_unsecure -lgrpc -lz -Wl,-\) -Wl,-Bdynamic -ldl
554 LDFLAGS += /usr/local/lib/libprotobuf.a /usr/local/lib/libgrpc++_unsecure.a /usr/local/lib/libgrpc_unsecure.a /usr/local/lib/libgrpc.a -ldl -lz
557 $(CXX) -o $@ $(CLIOBJS) $(LDFLAGS)
562 -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) $(OBJS:.o=.cc) > $@ 2> /dev/null