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)
146 ### ==========================================================================
147 ### Section 3. Low-level configuration
148 ### ==========================================================================
150 ### 3.1 Selecting compiler (default = gcc)
152 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
153 DEPENDFLAGS += -std=c++11
154 LDFLAGS += $(EXTRALDFLAGS)
163 CXXFLAGS += -pedantic -Wextra
167 CXXFLAGS += -m$(bits)
171 CXXFLAGS += -m$(bits)
175 ifneq ($(KERNEL),Darwin)
176 LDFLAGS += -Wl,--no-as-needed
183 ifeq ($(KERNEL),Linux)
185 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
186 CXX=x86_64-w64-mingw32-c++
188 CXX=x86_64-w64-mingw32-c++-posix
191 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
192 CXX=i686-w64-mingw32-c++
194 CXX=i686-w64-mingw32-c++-posix
201 CXXFLAGS += -Wextra -Wshadow
208 CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
214 CXXFLAGS += -pedantic -Wextra -Wshadow
216 ifneq ($(KERNEL),Darwin)
217 ifneq ($(KERNEL),OpenBSD)
224 CXXFLAGS += -m$(bits)
228 CXXFLAGS += -m$(bits)
234 profile_make = icc-profile-make
235 profile_use = icc-profile-use
238 profile_make = clang-profile-make
239 profile_use = clang-profile-use
241 profile_make = gcc-profile-make
242 profile_use = gcc-profile-use
246 ifeq ($(KERNEL),Darwin)
247 CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
248 LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
251 ### Travis CI script uses COMPILER to overwrite CXX
256 ### Allow overwriting CXX from command line
261 ### On mingw use Windows threads, otherwise POSIX
262 ifneq ($(comp),mingw)
263 # On Android Bionic's C library comes with its own pthread implementation bundled in
264 ifneq ($(OS),Android)
265 # Haiku has pthreads in its libroot, so only link it in on other platforms
266 ifneq ($(KERNEL),Haiku)
279 ### 3.2.2 Debugging with undefined behavior sanitizers
280 ifneq ($(sanitize),no)
281 CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
282 LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
286 ifeq ($(optimize),yes)
291 ifeq ($(OS), Android)
292 CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
296 ifeq ($(comp),$(filter $(comp),gcc clang icc))
297 ifeq ($(KERNEL),Darwin)
298 CXXFLAGS += -mdynamic-no-pic
305 CXXFLAGS += -DIS_64BIT
309 ifeq ($(prefetch),yes)
315 CXXFLAGS += -DNO_PREFETCH
321 CXXFLAGS += -DUSE_POPCNT
322 else ifeq ($(comp),icc)
323 CXXFLAGS += -msse3 -DUSE_POPCNT
325 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
331 CXXFLAGS += -DUSE_PEXT
332 ifeq ($(comp),$(filter $(comp),gcc clang mingw))
337 ### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
338 ### This is a mix of compile and link time options because the lto link phase
339 ### needs access to the optimization flags.
340 ifeq ($(optimize),yes)
342 ifeq ($(comp),$(filter $(comp),gcc clang))
344 LDFLAGS += $(CXXFLAGS)
348 ifeq ($(KERNEL),Linux)
350 LDFLAGS += $(CXXFLAGS)
356 ### 3.9 Android 5 can only run position independent executables. Note that this
357 ### breaks Android 4.0 and earlier.
358 ifeq ($(OS), Android)
360 LDFLAGS += -fPIE -pie
364 ### ==========================================================================
365 ### Section 4. Public targets
366 ### ==========================================================================
370 @echo "To compile stockfish, type: "
372 @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
374 @echo "Supported targets:"
376 @echo "build > Standard build"
377 @echo "profile-build > PGO build"
378 @echo "strip > Strip executable"
379 @echo "install > Install executable"
380 @echo "clean > Clean up"
382 @echo "Supported archs:"
384 @echo "x86-64 > x86 64-bit"
385 @echo "x86-64-modern > x86 64-bit with popcnt support"
386 @echo "x86-64-bmi2 > x86 64-bit with pext support"
387 @echo "x86-32 > x86 32-bit with SSE support"
388 @echo "x86-32-old > x86 32-bit fall back for old hardware"
389 @echo "ppc-64 > PPC 64-bit"
390 @echo "ppc-32 > PPC 32-bit"
391 @echo "armv7 > ARMv7 32-bit"
392 @echo "general-64 > unspecified 64-bit"
393 @echo "general-32 > unspecified 32-bit"
395 @echo "Supported compilers:"
397 @echo "gcc > Gnu compiler (default)"
398 @echo "mingw > Gnu compiler with MinGW under Windows"
399 @echo "clang > LLVM Clang compiler"
400 @echo "icc > Intel compiler"
402 @echo "Simple examples. If you don't know what to do, you likely want to run: "
404 @echo "make build ARCH=x86-64 (This is for 64-bit systems)"
405 @echo "make build ARCH=x86-32 (This is for 32-bit systems)"
407 @echo "Advanced examples, for experienced users: "
409 @echo "make build ARCH=x86-64 COMP=clang"
410 @echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
414 .PHONY: help build profile-build strip install clean objclean profileclean help \
415 config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
416 clang-profile-use clang-profile-make
419 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
421 profile-build: config-sanity objclean profileclean
423 @echo "Step 1/4. Building instrumented executable ..."
424 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
426 @echo "Step 2/4. Running benchmark for pgo-build ..."
427 $(PGOBENCH) > /dev/null
429 @echo "Step 3/4. Building optimized executable ..."
430 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
431 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
433 @echo "Step 4/4. Deleting profile data ..."
434 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
440 -mkdir -p -m 755 $(BINDIR)
442 -strip $(BINDIR)/$(EXE)
445 clean: objclean profileclean
446 @rm -f .depend *~ core
448 # clean binaries and objects
450 @rm -f $(EXE) *.o ./syzygy/*.o
452 # clean auxiliary profiling files
455 @rm -f bench.txt *.gcda ./syzygy/*.gcda *.gcno ./syzygy/*.gcno
456 @rm -f stockfish.profdata *.profraw
461 ### ==========================================================================
462 ### Section 5. Private targets
463 ### ==========================================================================
465 all: $(EXE) client .depend
470 @echo "debug: '$(debug)'"
471 @echo "sanitize: '$(sanitize)'"
472 @echo "optimize: '$(optimize)'"
473 @echo "arch: '$(arch)'"
474 @echo "bits: '$(bits)'"
475 @echo "kernel: '$(KERNEL)'"
477 @echo "prefetch: '$(prefetch)'"
478 @echo "popcnt: '$(popcnt)'"
479 @echo "sse: '$(sse)'"
480 @echo "pext: '$(pext)'"
484 @echo "CXXFLAGS: $(CXXFLAGS)"
485 @echo "LDFLAGS: $(LDFLAGS)"
487 @echo "Testing config sanity. If this fails, try 'make help' ..."
489 @test "$(debug)" = "yes" || test "$(debug)" = "no"
490 @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
491 @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
492 @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
493 test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
494 @test "$(bits)" = "32" || test "$(bits)" = "64"
495 @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
496 @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
497 @test "$(sse)" = "yes" || test "$(sse)" = "no"
498 @test "$(pext)" = "yes" || test "$(pext)" = "no"
499 @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
502 $(CXX) -o $@ $(OBJS) $(LDFLAGS)
505 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
506 EXTRACXXFLAGS='-fprofile-instr-generate ' \
507 EXTRALDFLAGS=' -fprofile-instr-generate' \
511 llvm-profdata merge -output=stockfish.profdata *.profraw
512 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
513 EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
514 EXTRALDFLAGS='-fprofile-use ' \
518 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
519 EXTRACXXFLAGS='-fprofile-generate' \
520 EXTRALDFLAGS='-lgcov' \
524 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
525 EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
526 EXTRALDFLAGS='-lgcov' \
531 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
532 EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
536 $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
537 EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
544 GRPC_CPP_PLUGIN = grpc_cpp_plugin
545 GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
547 %.grpc.pb.h %.grpc.pb.cc: %.proto
548 $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
554 %.pb.h %.pb.cc: %.proto
555 $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
557 #LDFLAGS += -Wl,-Bstatic -Wl,-\( -lprotobuf -lgrpc++_unsecure -lgrpc_unsecure -lgrpc -lz -Wl,-\) -Wl,-Bdynamic -ldl
558 LDFLAGS += /usr/lib/x86_64-linux-gnu/libprotobuf.a /usr/lib/x86_64-linux-gnu/libgrpc++_unsecure.a /usr/lib/x86_64-linux-gnu/libgrpc_unsecure.a /usr/lib/x86_64-linux-gnu/libgrpc.a /usr/lib/x86_64-linux-gnu/libcares.a -ldl -lz
559 #LDFLAGS += /usr/lib/x86_64-linux-gnu/libprotobuf.a /usr/lib/libgrpc++_unsecure.a /usr/lib/libgrpc_unsecure.a /usr/lib/libgrpc.a /usr/lib/x86_64-linux-gnu/libcares.a -ldl -lz
562 $(CXX) -o $@ $(CLIOBJS) $(LDFLAGS)
567 -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) $(OBJS:.o=.cc) > $@ 2> /dev/null