]> git.sesse.net Git - stockfish/blob - src/Makefile
Merge remote-tracking branch 'upstream/master'
[stockfish] / src / Makefile
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
5 #
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.
10 #
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.
15 #
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/>.
18
19
20 ### ==========================================================================
21 ### Section 1. General Configuration
22 ### ==========================================================================
23
24 ### Executable name
25 ifeq ($(COMP),mingw)
26 EXE = stockfish.exe
27 else
28 EXE = stockfish
29 endif
30
31 ### Installation dir definitions
32 PREFIX = /usr/local
33 BINDIR = $(PREFIX)/bin
34
35 ### Built-in benchmark for pgo-builds
36 PGOBENCH = ./$(EXE) bench
37
38 ### Object files
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
44
45 ### Establish the operating system name
46 KERNEL = $(shell uname -s)
47 ifeq ($(KERNEL),Linux)
48         OS = $(shell uname -o)
49 endif
50
51 ### ==========================================================================
52 ### Section 2. High-level Configuration
53 ### ==========================================================================
54 #
55 # flag                --- Comp switch --- Description
56 # ----------------------------------------------------------------------------
57 #
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
69 #
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.
73
74 ### 2.1. General and architecture defaults
75 optimize = yes
76 debug = no
77 sanitize = no
78 bits = 32
79 prefetch = no
80 popcnt = no
81 sse = no
82 pext = no
83
84 ### 2.2 Architecture specific
85
86 ifeq ($(ARCH),general-32)
87         arch = any
88 endif
89
90 ifeq ($(ARCH),x86-32-old)
91         arch = i386
92 endif
93
94 ifeq ($(ARCH),x86-32)
95         arch = i386
96         prefetch = yes
97         sse = yes
98 endif
99
100 ifeq ($(ARCH),general-64)
101         arch = any
102         bits = 64
103 endif
104
105 ifeq ($(ARCH),x86-64)
106         arch = x86_64
107         bits = 64
108         prefetch = yes
109         sse = yes
110 endif
111
112 ifeq ($(ARCH),x86-64-modern)
113         arch = x86_64
114         bits = 64
115         prefetch = yes
116         popcnt = yes
117         sse = yes
118 endif
119
120 ifeq ($(ARCH),x86-64-bmi2)
121         arch = x86_64
122         bits = 64
123         prefetch = yes
124         popcnt = yes
125         sse = yes
126         pext = yes
127 endif
128
129 ifeq ($(ARCH),armv7)
130         arch = armv7
131         prefetch = yes
132 endif
133
134 ifeq ($(ARCH),ppc-32)
135         arch = ppc
136 endif
137
138 ifeq ($(ARCH),ppc-64)
139         arch = ppc64
140         bits = 64
141         popcnt = yes
142         prefetch = yes
143 endif
144
145
146 ### ==========================================================================
147 ### Section 3. Low-level configuration
148 ### ==========================================================================
149
150 ### 3.1 Selecting compiler (default = gcc)
151
152 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
153 DEPENDFLAGS += -std=c++11
154 LDFLAGS += $(EXTRALDFLAGS)
155
156 ifeq ($(COMP),)
157         COMP=gcc
158 endif
159
160 ifeq ($(COMP),gcc)
161         comp=gcc
162         CXX=g++
163         CXXFLAGS += -pedantic -Wextra
164
165         ifeq ($(ARCH),armv7)
166                 ifeq ($(OS),Android)
167                         CXXFLAGS += -m$(bits)
168                         LDFLAGS += -m$(bits)
169                 endif
170         else
171                 CXXFLAGS += -m$(bits)
172                 LDFLAGS += -m$(bits)
173         endif
174
175         ifneq ($(KERNEL),Darwin)
176            LDFLAGS += -Wl,--no-as-needed
177         endif
178 endif
179
180 ifeq ($(COMP),mingw)
181         comp=mingw
182
183         ifeq ($(KERNEL),Linux)
184                 ifeq ($(bits),64)
185                         ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
186                                 CXX=x86_64-w64-mingw32-c++
187                         else
188                                 CXX=x86_64-w64-mingw32-c++-posix
189                         endif
190                 else
191                         ifeq ($(shell which i686-w64-mingw32-c++-posix),)
192                                 CXX=i686-w64-mingw32-c++
193                         else
194                                 CXX=i686-w64-mingw32-c++-posix
195                         endif
196                 endif
197         else
198                 CXX=g++
199         endif
200
201         CXXFLAGS += -Wextra -Wshadow
202         LDFLAGS += -static
203 endif
204
205 ifeq ($(COMP),icc)
206         comp=icc
207         CXX=icpc
208         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
209 endif
210
211 ifeq ($(COMP),clang)
212         comp=clang
213         CXX=clang++
214         CXXFLAGS += -pedantic -Wextra -Wshadow
215
216         ifneq ($(KERNEL),Darwin)
217         ifneq ($(KERNEL),OpenBSD)
218                 LDFLAGS += -latomic
219         endif
220         endif
221
222         ifeq ($(ARCH),armv7)
223                 ifeq ($(OS),Android)
224                         CXXFLAGS += -m$(bits)
225                         LDFLAGS += -m$(bits)
226                 endif
227         else
228                 CXXFLAGS += -m$(bits)
229                 LDFLAGS += -m$(bits)
230         endif
231 endif
232
233 ifeq ($(comp),icc)
234         profile_make = icc-profile-make
235         profile_use = icc-profile-use
236 else
237 ifeq ($(comp),clang)
238         profile_make = clang-profile-make
239         profile_use = clang-profile-use
240 else
241         profile_make = gcc-profile-make
242         profile_use = gcc-profile-use
243 endif
244 endif
245
246 ifeq ($(KERNEL),Darwin)
247         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
248         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
249 endif
250
251 ### Travis CI script uses COMPILER to overwrite CXX
252 ifdef COMPILER
253         COMPCXX=$(COMPILER)
254 endif
255
256 ### Allow overwriting CXX from command line
257 ifdef COMPCXX
258         CXX=$(COMPCXX)
259 endif
260
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)
267                         LDFLAGS += -lpthread
268                 endif
269         endif
270 endif
271
272 ### 3.2.1 Debugging
273 ifeq ($(debug),no)
274         CXXFLAGS += -DNDEBUG
275 else
276         CXXFLAGS += -g
277 endif
278
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
283 endif
284
285 ### 3.3 Optimization
286 ifeq ($(optimize),yes)
287
288         CXXFLAGS += -O3 -g
289
290         ifeq ($(comp),gcc)
291                 ifeq ($(OS), Android)
292                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
293                 endif
294         endif
295         
296         ifeq ($(comp),$(filter $(comp),gcc clang icc))
297                 ifeq ($(KERNEL),Darwin)
298                         CXXFLAGS += -mdynamic-no-pic
299                 endif
300         endif
301 endif
302
303 ### 3.4 Bits
304 ifeq ($(bits),64)
305         CXXFLAGS += -DIS_64BIT
306 endif
307
308 ### 3.5 prefetch
309 ifeq ($(prefetch),yes)
310         ifeq ($(sse),yes)
311                 CXXFLAGS += -msse
312                 DEPENDFLAGS += -msse
313         endif
314 else
315         CXXFLAGS += -DNO_PREFETCH
316 endif
317
318 ### 3.6 popcnt
319 ifeq ($(popcnt),yes)
320         ifeq ($(arch),ppc64)
321                 CXXFLAGS += -DUSE_POPCNT
322         else ifeq ($(comp),icc)
323                 CXXFLAGS += -msse3 -DUSE_POPCNT
324         else
325                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
326         endif
327 endif
328
329 ### 3.7 pext
330 ifeq ($(pext),yes)
331         CXXFLAGS += -DUSE_PEXT
332         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
333                 CXXFLAGS += -mbmi2
334         endif
335 endif
336
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)
341 ifeq ($(debug), no)
342         ifeq ($(comp),$(filter $(comp),gcc clang))
343                 CXXFLAGS += -flto
344                 LDFLAGS += $(CXXFLAGS)
345         endif
346
347         ifeq ($(comp),mingw)
348         ifeq ($(KERNEL),Linux)
349                 CXXFLAGS += -flto
350                 LDFLAGS += $(CXXFLAGS)
351         endif
352         endif
353 endif
354 endif
355
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)
359         CXXFLAGS += -fPIE
360         LDFLAGS += -fPIE -pie
361 endif
362
363
364 ### ==========================================================================
365 ### Section 4. Public targets
366 ### ==========================================================================
367
368 help:
369         @echo ""
370         @echo "To compile stockfish, type: "
371         @echo ""
372         @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
373         @echo ""
374         @echo "Supported targets:"
375         @echo ""
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"
381         @echo ""
382         @echo "Supported archs:"
383         @echo ""
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"
394         @echo ""
395         @echo "Supported compilers:"
396         @echo ""
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"
401         @echo ""
402         @echo "Simple examples. If you don't know what to do, you likely want to run: "
403         @echo ""
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)"
406         @echo ""
407         @echo "Advanced examples, for experienced users: "
408         @echo ""
409         @echo "make build ARCH=x86-64 COMP=clang"
410         @echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
411         @echo ""
412
413
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
417
418 build: config-sanity
419         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
420
421 profile-build: config-sanity objclean profileclean
422         @echo ""
423         @echo "Step 1/4. Building instrumented executable ..."
424         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
425         @echo ""
426         @echo "Step 2/4. Running benchmark for pgo-build ..."
427         $(PGOBENCH) > /dev/null
428         @echo ""
429         @echo "Step 3/4. Building optimized executable ..."
430         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
431         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
432         @echo ""
433         @echo "Step 4/4. Deleting profile data ..."
434         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
435
436 strip:
437         strip $(EXE)
438
439 install:
440         -mkdir -p -m 755 $(BINDIR)
441         -cp $(EXE) $(BINDIR)
442         -strip $(BINDIR)/$(EXE)
443
444 #clean all
445 clean: objclean profileclean
446         @rm -f .depend *~ core
447
448 # clean binaries and objects
449 objclean:
450         @rm -f $(EXE) *.o ./syzygy/*.o
451
452 # clean auxiliary profiling files
453 profileclean:
454         @rm -rf profdir
455         @rm -f bench.txt *.gcda ./syzygy/*.gcda *.gcno ./syzygy/*.gcno
456         @rm -f stockfish.profdata *.profraw
457
458 default:
459         help
460
461 ### ==========================================================================
462 ### Section 5. Private targets
463 ### ==========================================================================
464
465 all: $(EXE) client .depend
466
467 config-sanity:
468         @echo ""
469         @echo "Config:"
470         @echo "debug: '$(debug)'"
471         @echo "sanitize: '$(sanitize)'"
472         @echo "optimize: '$(optimize)'"
473         @echo "arch: '$(arch)'"
474         @echo "bits: '$(bits)'"
475         @echo "kernel: '$(KERNEL)'"
476         @echo "os: '$(OS)'"
477         @echo "prefetch: '$(prefetch)'"
478         @echo "popcnt: '$(popcnt)'"
479         @echo "sse: '$(sse)'"
480         @echo "pext: '$(pext)'"
481         @echo ""
482         @echo "Flags:"
483         @echo "CXX: $(CXX)"
484         @echo "CXXFLAGS: $(CXXFLAGS)"
485         @echo "LDFLAGS: $(LDFLAGS)"
486         @echo ""
487         @echo "Testing config sanity. If this fails, try 'make help' ..."
488         @echo ""
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"
500
501 $(EXE): $(OBJS)
502         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
503
504 clang-profile-make:
505         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
506         EXTRACXXFLAGS='-fprofile-instr-generate ' \
507         EXTRALDFLAGS=' -fprofile-instr-generate' \
508         all
509
510 clang-profile-use:
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 ' \
515         all
516
517 gcc-profile-make:
518         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
519         EXTRACXXFLAGS='-fprofile-generate' \
520         EXTRALDFLAGS='-lgcov' \
521         all
522
523 gcc-profile-use:
524         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
525         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
526         EXTRALDFLAGS='-lgcov' \
527         all
528
529 icc-profile-make:
530         @mkdir -p profdir
531         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
532         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
533         all
534
535 icc-profile-use:
536         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
537         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
538         all
539
540 ### GRPC
541
542 PROTOS_PATH = .
543 PROTOC = protoc
544 GRPC_CPP_PLUGIN = grpc_cpp_plugin
545 GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
546
547 %.grpc.pb.h %.grpc.pb.cc: %.proto
548         $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
549
550 # oh my
551 %.cpp: %.cc
552         cp $< $@
553
554 %.pb.h %.pb.cc: %.proto
555         $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
556
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
560
561 client: $(CLIOBJS)
562         $(CXX) -o $@ $(CLIOBJS) $(LDFLAGS)
563
564 # Other stuff
565
566 .depend:
567         -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) $(OBJS:.o=.cc) > $@ 2> /dev/null
568
569 -include .depend
570