]> git.sesse.net Git - stockfish/blob - src/Makefile
Merge remote-tracking branch 'upstream/master' into HEAD
[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 ### Source and object files
39 SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
40         material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
41         search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
42         hashprobe.grpc.pb.cc hashprobe.pb.cc
43 CLISRCS = client.cpp hashprobe.grpc.pb.cc hashprobe.pb.cc uci.cpp
44
45 OBJS = $(notdir $(SRCS:.cpp=.o))
46 CLIOBJS = $(notdir $(CLISRCS:.cpp=.o))
47
48 VPATH = syzygy
49
50 ### Establish the operating system name
51 KERNEL = $(shell uname -s)
52 ifeq ($(KERNEL),Linux)
53         OS = $(shell uname -o)
54 endif
55
56 ### ==========================================================================
57 ### Section 2. High-level Configuration
58 ### ==========================================================================
59 #
60 # flag                --- Comp switch      --- Description
61 # ----------------------------------------------------------------------------
62 #
63 # debug = yes/no      --- -DNDEBUG         --- Enable/Disable debug mode
64 # sanitize = undefined/thread/no (-fsanitize )
65 #                     --- ( undefined )    --- enable undefined behavior checks
66 #                     --- ( thread    )    --- enable threading error  checks
67 # optimize = yes/no   --- (-O3/-fast etc.) --- Enable/Disable optimizations
68 # arch = (name)       --- (-arch)          --- Target architecture
69 # bits = 64/32        --- -DIS_64BIT       --- 64-/32-bit operating system
70 # prefetch = yes/no   --- -DUSE_PREFETCH   --- Use prefetch asm-instruction
71 # popcnt = yes/no     --- -DUSE_POPCNT     --- Use popcnt asm-instruction
72 # sse = yes/no        --- -msse            --- Use Intel Streaming SIMD Extensions
73 # pext = yes/no       --- -DUSE_PEXT       --- Use pext x86_64 asm-instruction
74 #
75 # Note that Makefile is space sensitive, so when adding new architectures
76 # or modifying existing flags, you have to make sure there are no extra spaces
77 # at the end of the line for flag values.
78
79 ### 2.1. General and architecture defaults
80 optimize = yes
81 debug = no
82 sanitize = no
83 bits = 64
84 prefetch = no
85 popcnt = no
86 sse = no
87 pext = no
88
89 ### 2.2 Architecture specific
90 ifeq ($(ARCH),general-32)
91         arch = any
92         bits = 32
93 endif
94
95 ifeq ($(ARCH),x86-32-old)
96         arch = i386
97         bits = 32
98 endif
99
100 ifeq ($(ARCH),x86-32)
101         arch = i386
102         bits = 32
103         prefetch = yes
104         sse = yes
105 endif
106
107 ifeq ($(ARCH),general-64)
108         arch = any
109 endif
110
111 ifeq ($(ARCH),x86-64)
112         arch = x86_64
113         prefetch = yes
114         sse = yes
115 endif
116
117 ifeq ($(ARCH),x86-64-modern)
118         arch = x86_64
119         prefetch = yes
120         popcnt = yes
121         sse = yes
122 endif
123
124 ifeq ($(ARCH),x86-64-bmi2)
125         arch = x86_64
126         prefetch = yes
127         popcnt = yes
128         sse = yes
129         pext = yes
130 endif
131
132 ifeq ($(ARCH),armv7)
133         arch = armv7
134         prefetch = yes
135         bits = 32
136 endif
137
138 ifeq ($(ARCH),armv8)
139         arch = armv8-a
140         prefetch = yes
141         popcnt = yes
142 endif
143
144 ifeq ($(ARCH),ppc-32)
145         arch = ppc
146         bits = 32
147 endif
148
149 ifeq ($(ARCH),ppc-64)
150         arch = ppc64
151         popcnt = yes
152         prefetch = yes
153 endif
154
155 ### ==========================================================================
156 ### Section 3. Low-level Configuration
157 ### ==========================================================================
158
159 ### 3.1 Selecting compiler (default = gcc)
160 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
161 DEPENDFLAGS += -std=c++11
162 LDFLAGS += $(EXTRALDFLAGS)
163
164 ifeq ($(COMP),)
165         COMP=gcc
166 endif
167
168 ifeq ($(COMP),gcc)
169         comp=gcc
170         CXX=g++
171         CXXFLAGS += -pedantic -Wextra
172
173         ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
174                 ifeq ($(OS),Android)
175                         CXXFLAGS += -m$(bits)
176                         LDFLAGS += -m$(bits)
177                 endif
178         else
179                 CXXFLAGS += -m$(bits)
180                 LDFLAGS += -m$(bits)
181         endif
182
183         ifneq ($(KERNEL),Darwin)
184            LDFLAGS += -Wl,--no-as-needed
185         endif
186 endif
187
188 ifeq ($(COMP),mingw)
189         comp=mingw
190
191         ifeq ($(KERNEL),Linux)
192                 ifeq ($(bits),64)
193                         ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
194                                 CXX=x86_64-w64-mingw32-c++
195                         else
196                                 CXX=x86_64-w64-mingw32-c++-posix
197                         endif
198                 else
199                         ifeq ($(shell which i686-w64-mingw32-c++-posix),)
200                                 CXX=i686-w64-mingw32-c++
201                         else
202                                 CXX=i686-w64-mingw32-c++-posix
203                         endif
204                 endif
205         else
206                 CXX=g++
207         endif
208
209         CXXFLAGS += -Wextra -Wshadow
210         LDFLAGS += -static
211 endif
212
213 ifeq ($(COMP),icc)
214         comp=icc
215         CXX=icpc
216         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
217 endif
218
219 ifeq ($(COMP),clang)
220         comp=clang
221         CXX=clang++
222         CXXFLAGS += -pedantic -Wextra -Wshadow
223
224         ifneq ($(KERNEL),Darwin)
225         ifneq ($(KERNEL),OpenBSD)
226                 LDFLAGS += -latomic
227         endif
228         endif
229
230         ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
231                 ifeq ($(OS),Android)
232                         CXXFLAGS += -m$(bits)
233                         LDFLAGS += -m$(bits)
234                 endif
235         else
236                 CXXFLAGS += -m$(bits)
237                 LDFLAGS += -m$(bits)
238         endif
239 endif
240
241 ifeq ($(comp),icc)
242         profile_make = icc-profile-make
243         profile_use = icc-profile-use
244 else
245 ifeq ($(comp),clang)
246         profile_make = clang-profile-make
247         profile_use = clang-profile-use
248 else
249         profile_make = gcc-profile-make
250         profile_use = gcc-profile-use
251 endif
252 endif
253
254 ifeq ($(KERNEL),Darwin)
255         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
256         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
257 endif
258
259 ### Travis CI script uses COMPILER to overwrite CXX
260 ifdef COMPILER
261         COMPCXX=$(COMPILER)
262 endif
263
264 ### Allow overwriting CXX from command line
265 ifdef COMPCXX
266         CXX=$(COMPCXX)
267 endif
268
269 ### On mingw use Windows threads, otherwise POSIX
270 ifneq ($(comp),mingw)
271         # On Android Bionic's C library comes with its own pthread implementation bundled in
272         ifneq ($(OS),Android)
273                 # Haiku has pthreads in its libroot, so only link it in on other platforms
274                 ifneq ($(KERNEL),Haiku)
275                         LDFLAGS += -lpthread
276                 endif
277         endif
278 endif
279
280 ### 3.2.1 Debugging
281 ifeq ($(debug),no)
282         CXXFLAGS += -DNDEBUG
283 else
284         CXXFLAGS += -g
285 endif
286
287 ### 3.2.2 Debugging with undefined behavior sanitizers
288 ifneq ($(sanitize),no)
289         CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
290         LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
291 endif
292
293 ### 3.3 Optimization
294 ifeq ($(optimize),yes)
295
296         CXXFLAGS += -O3 -g
297
298         ifeq ($(comp),gcc)
299                 ifeq ($(OS), Android)
300                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
301                 endif
302         endif
303
304         ifeq ($(comp),$(filter $(comp),gcc clang icc))
305                 ifeq ($(KERNEL),Darwin)
306                         CXXFLAGS += -mdynamic-no-pic
307                 endif
308         endif
309 endif
310
311 ### 3.4 Bits
312 ifeq ($(bits),64)
313         CXXFLAGS += -DIS_64BIT
314 endif
315
316 ### 3.5 prefetch
317 ifeq ($(prefetch),yes)
318         ifeq ($(sse),yes)
319                 CXXFLAGS += -msse
320                 DEPENDFLAGS += -msse
321         endif
322 else
323         CXXFLAGS += -DNO_PREFETCH
324 endif
325
326 ### 3.6 popcnt
327 ifeq ($(popcnt),yes)
328         ifeq ($(arch),$(filter $(arch),ppc64 armv8-a))
329                 CXXFLAGS += -DUSE_POPCNT
330         else ifeq ($(comp),icc)
331                 CXXFLAGS += -msse3 -DUSE_POPCNT
332         else
333                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
334         endif
335 endif
336
337 ### 3.7 pext
338 ifeq ($(pext),yes)
339         CXXFLAGS += -DUSE_PEXT
340         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
341                 CXXFLAGS += -msse4 -mbmi2
342         endif
343 endif
344
345 ### 3.8 Link Time Optimization
346 ### This is a mix of compile and link time options because the lto link phase
347 ### needs access to the optimization flags.
348 ifeq ($(optimize),yes)
349 ifeq ($(debug), no)
350         ifeq ($(comp),$(filter $(comp),gcc clang))
351                 CXXFLAGS += -flto
352                 LDFLAGS += $(CXXFLAGS)
353         endif
354
355 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
356 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are know to work, older might not.
357 # So, only enable it for a cross from Linux by default.
358         ifeq ($(comp),mingw)
359         ifeq ($(KERNEL),Linux)
360                 CXXFLAGS += -flto
361                 LDFLAGS += $(CXXFLAGS)
362         endif
363         endif
364 endif
365 endif
366
367 ### 3.9 Android 5 can only run position independent executables. Note that this
368 ### breaks Android 4.0 and earlier.
369 ifeq ($(OS), Android)
370         CXXFLAGS += -fPIE
371         LDFLAGS += -fPIE -pie
372 endif
373
374 ### ==========================================================================
375 ### Section 4. Public Targets
376 ### ==========================================================================
377
378 help:
379         @echo ""
380         @echo "To compile stockfish, type: "
381         @echo ""
382         @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
383         @echo ""
384         @echo "Supported targets:"
385         @echo ""
386         @echo "build                   > Standard build"
387         @echo "profile-build           > PGO build"
388         @echo "strip                   > Strip executable"
389         @echo "install                 > Install executable"
390         @echo "clean                   > Clean up"
391         @echo ""
392         @echo "Supported archs:"
393         @echo ""
394         @echo "x86-64-bmi2             > x86 64-bit with pext support (also enables SSE4)"
395         @echo "x86-64-modern           > x86 64-bit with popcnt support (also enables SSE3)"
396         @echo "x86-64                  > x86 64-bit generic"
397         @echo "x86-32                  > x86 32-bit (also enables SSE)"
398         @echo "x86-32-old              > x86 32-bit fall back for old hardware"
399         @echo "ppc-64                  > PPC 64-bit"
400         @echo "ppc-32                  > PPC 32-bit"
401         @echo "armv7                   > ARMv7 32-bit"
402         @echo "armv8                   > ARMv8 64-bit"
403         @echo "general-64              > unspecified 64-bit"
404         @echo "general-32              > unspecified 32-bit"
405         @echo ""
406         @echo "Supported compilers:"
407         @echo ""
408         @echo "gcc                     > Gnu compiler (default)"
409         @echo "mingw                   > Gnu compiler with MinGW under Windows"
410         @echo "clang                   > LLVM Clang compiler"
411         @echo "icc                     > Intel compiler"
412         @echo ""
413         @echo "Simple examples. If you don't know what to do, you likely want to run: "
414         @echo ""
415         @echo "make build ARCH=x86-64    (This is for 64-bit systems)"
416         @echo "make build ARCH=x86-32    (This is for 32-bit systems)"
417         @echo ""
418         @echo "Advanced examples, for experienced users: "
419         @echo ""
420         @echo "make build ARCH=x86-64 COMP=clang"
421         @echo "make profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-4.8"
422         @echo ""
423
424
425 .PHONY: help build profile-build strip install clean objclean profileclean \
426         config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
427         clang-profile-use clang-profile-make
428
429 build: config-sanity
430         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
431
432 profile-build: config-sanity objclean profileclean
433         @echo ""
434         @echo "Step 1/4. Building instrumented executable ..."
435         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
436         @echo ""
437         @echo "Step 2/4. Running benchmark for pgo-build ..."
438         $(PGOBENCH) > /dev/null
439         @echo ""
440         @echo "Step 3/4. Building optimized executable ..."
441         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
442         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
443         @echo ""
444         @echo "Step 4/4. Deleting profile data ..."
445         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
446
447 strip:
448         strip $(EXE)
449
450 install:
451         -mkdir -p -m 755 $(BINDIR)
452         -cp $(EXE) $(BINDIR)
453         -strip $(BINDIR)/$(EXE)
454
455 #clean all
456 clean: objclean profileclean
457         @rm -f .depend *~ core
458
459 # clean binaries and objects
460 objclean:
461         @rm -f $(EXE) *.o ./syzygy/*.o
462
463 # clean auxiliary profiling files
464 profileclean:
465         @rm -rf profdir
466         @rm -f bench.txt *.gcda *.gcno
467         @rm -f stockfish.profdata *.profraw
468
469 default:
470         help
471
472 ### ==========================================================================
473 ### Section 5. Private Targets
474 ### ==========================================================================
475
476 all: $(EXE) client .depend
477
478 config-sanity:
479         @echo ""
480         @echo "Config:"
481         @echo "debug: '$(debug)'"
482         @echo "sanitize: '$(sanitize)'"
483         @echo "optimize: '$(optimize)'"
484         @echo "arch: '$(arch)'"
485         @echo "bits: '$(bits)'"
486         @echo "kernel: '$(KERNEL)'"
487         @echo "os: '$(OS)'"
488         @echo "prefetch: '$(prefetch)'"
489         @echo "popcnt: '$(popcnt)'"
490         @echo "sse: '$(sse)'"
491         @echo "pext: '$(pext)'"
492         @echo ""
493         @echo "Flags:"
494         @echo "CXX: $(CXX)"
495         @echo "CXXFLAGS: $(CXXFLAGS)"
496         @echo "LDFLAGS: $(LDFLAGS)"
497         @echo ""
498         @echo "Testing config sanity. If this fails, try 'make help' ..."
499         @echo ""
500         @test "$(debug)" = "yes" || test "$(debug)" = "no"
501         @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
502         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
503         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
504          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
505          test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a"
506         @test "$(bits)" = "32" || test "$(bits)" = "64"
507         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
508         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
509         @test "$(sse)" = "yes" || test "$(sse)" = "no"
510         @test "$(pext)" = "yes" || test "$(pext)" = "no"
511         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
512
513 $(EXE): $(OBJS)
514         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
515
516 clang-profile-make:
517         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
518         EXTRACXXFLAGS='-fprofile-instr-generate ' \
519         EXTRALDFLAGS=' -fprofile-instr-generate' \
520         all
521
522 clang-profile-use:
523         llvm-profdata merge -output=stockfish.profdata *.profraw
524         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
525         EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
526         EXTRALDFLAGS='-fprofile-use ' \
527         all
528
529 gcc-profile-make:
530         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
531         EXTRACXXFLAGS='-fprofile-generate' \
532         EXTRALDFLAGS='-lgcov' \
533         all
534
535 gcc-profile-use:
536         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
537         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
538         EXTRALDFLAGS='-lgcov' \
539         all
540
541 icc-profile-make:
542         @mkdir -p profdir
543         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
544         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
545         all
546
547 icc-profile-use:
548         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
549         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
550         all
551
552 ### GRPC
553
554 PROTOS_PATH = .
555 PROTOC = protoc
556 GRPC_CPP_PLUGIN = grpc_cpp_plugin
557 GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
558
559 %.grpc.pb.h %.grpc.pb.cc: %.proto
560         $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
561
562 # oh my
563 %.cpp: %.cc
564         cp $< $@
565
566 %.pb.h %.pb.cc: %.proto
567         $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
568
569 #LDFLAGS += -Wl,-Bstatic -Wl,-\( -lprotobuf -lgrpc++_unsecure -lgrpc_unsecure -lgrpc -lz -Wl,-\) -Wl,-Bdynamic -ldl
570 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
571 #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
572
573 client: $(CLIOBJS)
574         $(CXX) -o $@ $(CLIOBJS) $(LDFLAGS)
575
576 # Other stuff
577
578 .depend:
579         -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
580
581 -include .depend