]> 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-2021 The Stockfish developers (see AUTHORS file)
3 #
4 # Stockfish is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # Stockfish is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18 ### ==========================================================================
19 ### Section 1. General Configuration
20 ### ==========================================================================
21
22 ### Executable name
23 ifeq ($(COMP),mingw)
24 EXE = stockfish.exe
25 else
26 EXE = stockfish
27 endif
28
29 ### Installation dir definitions
30 PREFIX = /usr/local
31 BINDIR = $(PREFIX)/bin
32
33 ### Built-in benchmark for pgo-builds
34 PGOBENCH = ./$(EXE) bench
35
36 ### Source and object files
37 SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
38         material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
39         search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
40         nnue/evaluate_nnue.cpp nnue/features/half_kp.cpp \
41         hashprobe.grpc.pb.cc hashprobe.pb.cc
42 CLISRCS = client.cpp hashprobe.grpc.pb.cc hashprobe.pb.cc uci.cpp
43
44 OBJS = $(notdir $(SRCS:.cpp=.o))
45 CLIOBJS = $(notdir $(CLISRCS:.cpp=.o))
46
47 VPATH = syzygy:nnue:nnue/features
48
49 ### Establish the operating system name
50 KERNEL = $(shell uname -s)
51 ifeq ($(KERNEL),Linux)
52         OS = $(shell uname -o)
53 endif
54
55 ### ==========================================================================
56 ### Section 2. High-level Configuration
57 ### ==========================================================================
58 #
59 # flag                --- Comp switch      --- Description
60 # ----------------------------------------------------------------------------
61 #
62 # debug = yes/no      --- -DNDEBUG         --- Enable/Disable debug mode
63 # sanitize = undefined/thread/no (-fsanitize )
64 #                     --- ( undefined )    --- enable undefined behavior checks
65 #                     --- ( thread    )    --- enable threading error  checks
66 # optimize = yes/no   --- (-O3/-fast etc.) --- Enable/Disable optimizations
67 # arch = (name)       --- (-arch)          --- Target architecture
68 # bits = 64/32        --- -DIS_64BIT       --- 64-/32-bit operating system
69 # prefetch = yes/no   --- -DUSE_PREFETCH   --- Use prefetch asm-instruction
70 # popcnt = yes/no     --- -DUSE_POPCNT     --- Use popcnt asm-instruction
71 # pext = yes/no       --- -DUSE_PEXT       --- Use pext x86_64 asm-instruction
72 # sse = yes/no        --- -msse            --- Use Intel Streaming SIMD Extensions
73 # mmx = yes/no        --- -mmmx            --- Use Intel MMX instructions
74 # sse2 = yes/no       --- -msse2           --- Use Intel Streaming SIMD Extensions 2
75 # ssse3 = yes/no      --- -mssse3          --- Use Intel Supplemental Streaming SIMD Extensions 3
76 # sse41 = yes/no      --- -msse4.1         --- Use Intel Streaming SIMD Extensions 4.1
77 # avx2 = yes/no       --- -mavx2           --- Use Intel Advanced Vector Extensions 2
78 # avx512 = yes/no     --- -mavx512bw       --- Use Intel Advanced Vector Extensions 512
79 # vnni256 = yes/no    --- -mavx512vnni     --- Use Intel Vector Neural Network Instructions 256
80 # vnni512 = yes/no    --- -mavx512vnni     --- Use Intel Vector Neural Network Instructions 512
81 # neon = yes/no       --- -DUSE_NEON       --- Use ARM SIMD architecture
82 #
83 # Note that Makefile is space sensitive, so when adding new architectures
84 # or modifying existing flags, you have to make sure there are no extra spaces
85 # at the end of the line for flag values.
86
87 ### 2.1. General and architecture defaults
88
89 ifeq ($(ARCH),)
90    ARCH = x86-64-modern
91    help_skip_sanity = yes
92 endif
93 # explicitly check for the list of supported architectures (as listed with make help),
94 # the user can override with `make ARCH=x86-32-vnni256 SUPPORTED_ARCH=true`
95 ifeq ($(ARCH), $(filter $(ARCH), \
96                  x86-64-vnni512 x86-64-vnni256 x86-64-avx512 x86-64-bmi2 x86-64-avx2 \
97                  x86-64-sse41-popcnt x86-64-modern x86-64-ssse3 x86-64-sse3-popcnt \
98                  x86-64 x86-32-sse41-popcnt x86-32-sse2 x86-32 ppc-64 ppc-32 \
99                  armv7 armv7-neon armv8 apple-silicon general-64 general-32))
100    SUPPORTED_ARCH=true
101 else
102    SUPPORTED_ARCH=false
103 endif
104
105 optimize = yes
106 debug = no
107 sanitize = no
108 bits = 64
109 prefetch = no
110 popcnt = no
111 pext = no
112 sse = no
113 mmx = no
114 sse2 = no
115 ssse3 = no
116 sse41 = no
117 avx2 = no
118 avx512 = no
119 vnni256 = no
120 vnni512 = no
121 neon = no
122 STRIP = strip
123
124 ### 2.2 Architecture specific
125
126 ifeq ($(findstring x86,$(ARCH)),x86)
127
128 # x86-32/64
129
130 ifeq ($(findstring x86-32,$(ARCH)),x86-32)
131         arch = i386
132         bits = 32
133         sse = yes
134         mmx = yes
135 else
136         arch = x86_64
137         sse = yes
138         sse2 = yes
139 endif
140
141 ifeq ($(findstring -sse,$(ARCH)),-sse)
142         sse = yes
143 endif
144
145 ifeq ($(findstring -popcnt,$(ARCH)),-popcnt)
146         popcnt = yes
147 endif
148
149 ifeq ($(findstring -mmx,$(ARCH)),-mmx)
150         mmx = yes
151 endif
152
153 ifeq ($(findstring -sse2,$(ARCH)),-sse2)
154         sse = yes
155         sse2 = yes
156 endif
157
158 ifeq ($(findstring -ssse3,$(ARCH)),-ssse3)
159         sse = yes
160         sse2 = yes
161         ssse3 = yes
162 endif
163
164 ifeq ($(findstring -sse41,$(ARCH)),-sse41)
165         sse = yes
166         sse2 = yes
167         ssse3 = yes
168         sse41 = yes
169 endif
170
171 ifeq ($(findstring -modern,$(ARCH)),-modern)
172         popcnt = yes
173         sse = yes
174         sse2 = yes
175         ssse3 = yes
176         sse41 = yes
177 endif
178
179 ifeq ($(findstring -avx2,$(ARCH)),-avx2)
180         popcnt = yes
181         sse = yes
182         sse2 = yes
183         ssse3 = yes
184         sse41 = yes
185         avx2 = yes
186 endif
187
188 ifeq ($(findstring -bmi2,$(ARCH)),-bmi2)
189         popcnt = yes
190         sse = yes
191         sse2 = yes
192         ssse3 = yes
193         sse41 = yes
194         avx2 = yes
195         pext = yes
196 endif
197
198 ifeq ($(findstring -avx512,$(ARCH)),-avx512)
199         popcnt = yes
200         sse = yes
201         sse2 = yes
202         ssse3 = yes
203         sse41 = yes
204         avx2 = yes
205         pext = yes
206         avx512 = yes
207 endif
208
209 ifeq ($(findstring -vnni256,$(ARCH)),-vnni256)
210         popcnt = yes
211         sse = yes
212         sse2 = yes
213         ssse3 = yes
214         sse41 = yes
215         avx2 = yes
216         pext = yes
217         vnni256 = yes
218 endif
219
220 ifeq ($(findstring -vnni512,$(ARCH)),-vnni512)
221         popcnt = yes
222         sse = yes
223         sse2 = yes
224         ssse3 = yes
225         sse41 = yes
226         avx2 = yes
227         pext = yes
228         avx512 = yes
229         vnni512 = yes
230 endif
231
232 ifeq ($(sse),yes)
233         prefetch = yes
234 endif
235
236 # 64-bit pext is not available on x86-32
237 ifeq ($(bits),32)
238         pext = no
239 endif
240
241 else
242
243 # all other architectures
244
245 ifeq ($(ARCH),general-32)
246         arch = any
247         bits = 32
248 endif
249
250 ifeq ($(ARCH),general-64)
251         arch = any
252 endif
253
254 ifeq ($(ARCH),armv7)
255         arch = armv7
256         prefetch = yes
257         bits = 32
258 endif
259
260 ifeq ($(ARCH),armv7-neon)
261         arch = armv7
262         prefetch = yes
263         popcnt = yes
264         neon = yes
265         bits = 32
266 endif
267
268 ifeq ($(ARCH),armv8)
269         arch = armv8
270         prefetch = yes
271         popcnt = yes
272         neon = yes
273 endif
274
275 ifeq ($(ARCH),apple-silicon)
276         arch = arm64
277         prefetch = yes
278         popcnt = yes
279         neon = yes
280 endif
281
282 ifeq ($(ARCH),ppc-32)
283         arch = ppc
284         bits = 32
285 endif
286
287 ifeq ($(ARCH),ppc-64)
288         arch = ppc64
289         popcnt = yes
290         prefetch = yes
291 endif
292
293 endif
294
295 ### ==========================================================================
296 ### Section 3. Low-level Configuration
297 ### ==========================================================================
298
299 ### 3.1 Selecting compiler (default = gcc)
300 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
301 DEPENDFLAGS += -std=c++17
302 LDFLAGS += $(EXTRALDFLAGS)
303
304 ifeq ($(COMP),)
305         COMP=gcc
306 endif
307
308 ifeq ($(COMP),gcc)
309         comp=gcc
310         CXX=g++
311         CXXFLAGS += -pedantic -Wextra
312
313         ifeq ($(arch),$(filter $(arch),armv7 armv8))
314                 ifeq ($(OS),Android)
315                         CXXFLAGS += -m$(bits)
316                         LDFLAGS += -m$(bits)
317                 endif
318         else
319                 CXXFLAGS += -m$(bits)
320                 LDFLAGS += -m$(bits)
321         endif
322
323         ifeq ($(arch),$(filter $(arch),armv7))
324                 LDFLAGS += -latomic
325         endif
326
327         ifneq ($(KERNEL),Darwin)
328            LDFLAGS += -Wl,--no-as-needed
329         endif
330 endif
331
332 ifeq ($(COMP),mingw)
333         comp=mingw
334
335         ifeq ($(KERNEL),Linux)
336                 ifeq ($(bits),64)
337                         ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
338                                 CXX=x86_64-w64-mingw32-c++
339                         else
340                                 CXX=x86_64-w64-mingw32-c++-posix
341                         endif
342                 else
343                         ifeq ($(shell which i686-w64-mingw32-c++-posix),)
344                                 CXX=i686-w64-mingw32-c++
345                         else
346                                 CXX=i686-w64-mingw32-c++-posix
347                         endif
348                 endif
349         else
350                 CXX=g++
351         endif
352
353         CXXFLAGS += -Wextra -Wshadow
354         LDFLAGS += -static
355 endif
356
357 ifeq ($(COMP),icc)
358         comp=icc
359         CXX=icpc
360         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
361 endif
362
363 ifeq ($(COMP),clang)
364         comp=clang
365         CXX=clang++
366         CXXFLAGS += -pedantic -Wextra -Wshadow
367
368         ifneq ($(KERNEL),Darwin)
369         ifneq ($(KERNEL),OpenBSD)
370         ifneq ($(KERNEL),FreeBSD)
371                 LDFLAGS += -latomic
372         endif
373         endif
374         endif
375
376         ifeq ($(arch),$(filter $(arch),armv7 armv8))
377                 ifeq ($(OS),Android)
378                         CXXFLAGS += -m$(bits)
379                         LDFLAGS += -m$(bits)
380                 endif
381         else
382                 CXXFLAGS += -m$(bits)
383                 LDFLAGS += -m$(bits)
384         endif
385 endif
386
387 ifeq ($(KERNEL),Darwin)
388         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.14
389         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.14
390         XCRUN = xcrun
391 endif
392
393 # To cross-compile for Android, NDK version r21 or later is recommended.
394 # In earlier NDK versions, you'll need to pass -fno-addrsig if using GNU binutils.
395 # Currently we don't know how to make PGO builds with the NDK yet.
396 ifeq ($(COMP),ndk)
397         CXXFLAGS += -stdlib=libc++ -fPIE
398         comp=clang
399         ifeq ($(arch),armv7)
400                 CXX=armv7a-linux-androideabi16-clang++
401                 CXXFLAGS += -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=neon
402                 STRIP=arm-linux-androideabi-strip
403         endif
404         ifeq ($(arch),armv8)
405                 CXX=aarch64-linux-android21-clang++
406                 STRIP=aarch64-linux-android-strip
407         endif
408         LDFLAGS += -static-libstdc++ -pie -lm -latomic
409 endif
410
411 ifeq ($(comp),icc)
412         profile_make = icc-profile-make
413         profile_use = icc-profile-use
414 else ifeq ($(comp),clang)
415         profile_make = clang-profile-make
416         profile_use = clang-profile-use
417 else
418         profile_make = gcc-profile-make
419         profile_use = gcc-profile-use
420 endif
421
422 ### Travis CI script uses COMPILER to overwrite CXX
423 ifdef COMPILER
424         COMPCXX=$(COMPILER)
425 endif
426
427 ### Allow overwriting CXX from command line
428 ifdef COMPCXX
429         CXX=$(COMPCXX)
430 endif
431
432 ### Sometimes gcc is really clang
433 ifeq ($(COMP),gcc)
434         gccversion = $(shell $(CXX) --version)
435         gccisclang = $(findstring clang,$(gccversion))
436         ifneq ($(gccisclang),)
437                 profile_make = clang-profile-make
438                 profile_use = clang-profile-use
439         endif
440 endif
441
442 ### On mingw use Windows threads, otherwise POSIX
443 ifneq ($(comp),mingw)
444         CXXFLAGS += -DUSE_PTHREADS
445         # On Android Bionic's C library comes with its own pthread implementation bundled in
446         ifneq ($(OS),Android)
447                 # Haiku has pthreads in its libroot, so only link it in on other platforms
448                 ifneq ($(KERNEL),Haiku)
449                         ifneq ($(COMP),ndk)
450                                 LDFLAGS += -lpthread
451                         endif
452                 endif
453         endif
454 endif
455
456 ### 3.2.1 Debugging
457 ifeq ($(debug),no)
458         CXXFLAGS += -DNDEBUG
459 else
460         CXXFLAGS += -g
461 endif
462
463 ### 3.2.2 Debugging with undefined behavior sanitizers
464 ifneq ($(sanitize),no)
465         CXXFLAGS += -g3 -fsanitize=$(sanitize)
466         LDFLAGS += -fsanitize=$(sanitize)
467 endif
468
469 ### 3.3 Optimization
470 ifeq ($(optimize),yes)
471
472         CXXFLAGS += -O3 -g
473
474         ifeq ($(comp),gcc)
475                 ifeq ($(OS), Android)
476                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
477                 endif
478         endif
479
480         ifeq ($(comp),$(filter $(comp),gcc clang icc))
481                 ifeq ($(KERNEL),Darwin)
482                         CXXFLAGS += -mdynamic-no-pic
483                 endif
484         endif
485
486         ifeq ($(comp),clang)
487                 CXXFLAGS += -fexperimental-new-pass-manager
488         endif
489 endif
490
491 ### 3.4 Bits
492 ifeq ($(bits),64)
493         CXXFLAGS += -DIS_64BIT
494 endif
495
496 ### 3.5 prefetch
497 ifeq ($(prefetch),yes)
498         ifeq ($(sse),yes)
499                 CXXFLAGS += -msse
500         endif
501 else
502         CXXFLAGS += -DNO_PREFETCH
503 endif
504
505 ### 3.6 popcnt
506 ifeq ($(popcnt),yes)
507         ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8 arm64))
508                 CXXFLAGS += -DUSE_POPCNT
509         else ifeq ($(comp),icc)
510                 CXXFLAGS += -msse3 -DUSE_POPCNT
511         else
512                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
513         endif
514 endif
515
516
517 ifeq ($(avx2),yes)
518         CXXFLAGS += -DUSE_AVX2
519         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
520                 CXXFLAGS += -mavx2
521         endif
522 endif
523
524 ifeq ($(avx512),yes)
525         CXXFLAGS += -DUSE_AVX512
526         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
527                 CXXFLAGS += -mavx512f -mavx512bw
528         endif
529 endif
530
531 ifeq ($(vnni256),yes)
532         CXXFLAGS += -DUSE_VNNI
533         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
534                 CXXFLAGS += -mavx512f -mavx512bw -mavx512vnni -mavx512dq -mavx512vl -mprefer-vector-width=256
535         endif
536 endif
537
538 ifeq ($(vnni512),yes)
539         CXXFLAGS += -DUSE_VNNI
540         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
541                 CXXFLAGS += -mavx512vnni -mavx512dq -mavx512vl
542         endif
543 endif
544
545 ifeq ($(sse41),yes)
546         CXXFLAGS += -DUSE_SSE41
547         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
548                 CXXFLAGS += -msse4.1
549         endif
550 endif
551
552 ifeq ($(ssse3),yes)
553         CXXFLAGS += -DUSE_SSSE3
554         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
555                 CXXFLAGS += -mssse3
556         endif
557 endif
558
559 ifeq ($(sse2),yes)
560         CXXFLAGS += -DUSE_SSE2
561         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
562                 CXXFLAGS += -msse2
563         endif
564 endif
565
566 ifeq ($(mmx),yes)
567         CXXFLAGS += -DUSE_MMX
568         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
569                 CXXFLAGS += -mmmx
570         endif
571 endif
572
573 ifeq ($(neon),yes)
574         CXXFLAGS += -DUSE_NEON
575         ifeq ($(KERNEL),Linux)
576         ifneq ($(COMP),ndk)
577         ifneq ($(arch),armv8)
578                 CXXFLAGS += -mfpu=neon
579         endif
580         endif
581         endif
582 endif
583
584 ### 3.7 pext
585 ifeq ($(pext),yes)
586         CXXFLAGS += -DUSE_PEXT
587         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
588                 CXXFLAGS += -mbmi2
589         endif
590 endif
591
592 ### 3.8 Link Time Optimization
593 ### This is a mix of compile and link time options because the lto link phase
594 ### needs access to the optimization flags.
595 ifeq ($(optimize),yes)
596 ifeq ($(debug), no)
597         ifeq ($(comp),clang)
598                 CXXFLAGS += -flto
599                 ifneq ($(findstring MINGW,$(KERNEL)),)
600                         CXXFLAGS += -fuse-ld=lld
601                 else ifneq ($(findstring MSYS,$(KERNEL)),)
602                         CXXFLAGS += -fuse-ld=lld
603                 endif
604                 LDFLAGS += $(CXXFLAGS)
605
606 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
607 # GCC on some systems.
608         else ifeq ($(comp),gcc)
609         ifeq ($(gccisclang),)
610                 CXXFLAGS += -flto
611                 LDFLAGS += $(CXXFLAGS) -flto=jobserver
612                 ifneq ($(findstring MINGW,$(KERNEL)),)
613                         LDFLAGS += -save-temps
614                 else ifneq ($(findstring MSYS,$(KERNEL)),)
615                         LDFLAGS += -save-temps
616                 endif
617         else
618                 CXXFLAGS += -flto
619                 LDFLAGS += $(CXXFLAGS)
620         endif
621
622 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
623 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are known to work, older might not.
624 # So, only enable it for a cross from Linux by default.
625         else ifeq ($(comp),mingw)
626         ifeq ($(KERNEL),Linux)
627         ifneq ($(arch),i386)
628                 CXXFLAGS += -flto
629                 LDFLAGS += $(CXXFLAGS) -flto=jobserver
630         endif
631         endif
632         endif
633 endif
634 endif
635
636 ### 3.9 Android 5 can only run position independent executables. Note that this
637 ### breaks Android 4.0 and earlier.
638 ifeq ($(OS), Android)
639         CXXFLAGS += -fPIE
640         LDFLAGS += -fPIE -pie
641 endif
642
643 ### ==========================================================================
644 ### Section 4. Public Targets
645 ### ==========================================================================
646
647
648 help:
649         @echo ""
650         @echo "To compile stockfish, type: "
651         @echo ""
652         @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
653         @echo ""
654         @echo "Supported targets:"
655         @echo ""
656         @echo "help                    > Display architecture details"
657         @echo "build                   > Standard build"
658         @echo "net                     > Download the default nnue net"
659         @echo "profile-build           > Faster build (with profile-guided optimization)"
660         @echo "strip                   > Strip executable"
661         @echo "install                 > Install executable"
662         @echo "clean                   > Clean up"
663         @echo ""
664         @echo "Supported archs:"
665         @echo ""
666         @echo "x86-64-vnni512          > x86 64-bit with vnni support 512bit wide"
667         @echo "x86-64-vnni256          > x86 64-bit with vnni support 256bit wide"
668         @echo "x86-64-avx512           > x86 64-bit with avx512 support"
669         @echo "x86-64-bmi2             > x86 64-bit with bmi2 support"
670         @echo "x86-64-avx2             > x86 64-bit with avx2 support"
671         @echo "x86-64-sse41-popcnt     > x86 64-bit with sse41 and popcnt support"
672         @echo "x86-64-modern           > common modern CPU, currently x86-64-sse41-popcnt"
673         @echo "x86-64-ssse3            > x86 64-bit with ssse3 support"
674         @echo "x86-64-sse3-popcnt      > x86 64-bit with sse3 and popcnt support"
675         @echo "x86-64                  > x86 64-bit generic (with sse2 support)"
676         @echo "x86-32-sse41-popcnt     > x86 32-bit with sse41 and popcnt support"
677         @echo "x86-32-sse2             > x86 32-bit with sse2 support"
678         @echo "x86-32                  > x86 32-bit generic (with mmx and sse support)"
679         @echo "ppc-64                  > PPC 64-bit"
680         @echo "ppc-32                  > PPC 32-bit"
681         @echo "armv7                   > ARMv7 32-bit"
682         @echo "armv7-neon              > ARMv7 32-bit with popcnt and neon"
683         @echo "armv8                   > ARMv8 64-bit with popcnt and neon"
684         @echo "apple-silicon           > Apple silicon ARM64"
685         @echo "general-64              > unspecified 64-bit"
686         @echo "general-32              > unspecified 32-bit"
687         @echo ""
688         @echo "Supported compilers:"
689         @echo ""
690         @echo "gcc                     > Gnu compiler (default)"
691         @echo "mingw                   > Gnu compiler with MinGW under Windows"
692         @echo "clang                   > LLVM Clang compiler"
693         @echo "icc                     > Intel compiler"
694         @echo "ndk                     > Google NDK to cross-compile for Android"
695         @echo ""
696         @echo "Simple examples. If you don't know what to do, you likely want to run: "
697         @echo ""
698         @echo "make -j build ARCH=x86-64  (A portable, slow compile for 64-bit systems)"
699         @echo "make -j build ARCH=x86-32  (A portable, slow compile for 32-bit systems)"
700         @echo ""
701         @echo "Advanced examples, for experienced users looking for performance: "
702         @echo ""
703         @echo "make    help  ARCH=x86-64-bmi2"
704         @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-9.0"
705         @echo "make -j build ARCH=x86-64-ssse3 COMP=clang"
706         @echo ""
707         @echo "-------------------------------"
708 ifeq ($(SUPPORTED_ARCH)$(help_skip_sanity), true)
709         @echo "The selected architecture $(ARCH) will enable the following configuration: "
710         @$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
711 else
712         @echo "Specify a supported architecture with the ARCH option for more details"
713         @echo ""
714 endif
715
716
717 .PHONY: help build profile-build strip install clean net objclean profileclean \
718         config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
719         clang-profile-use clang-profile-make
720
721 build: net config-sanity
722         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
723
724 profile-build: net config-sanity objclean profileclean
725         @echo ""
726         @echo "Step 1/4. Building instrumented executable ..."
727         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
728         @echo ""
729         @echo "Step 2/4. Running benchmark for pgo-build ..."
730         $(PGOBENCH) > /dev/null
731         @echo ""
732         @echo "Step 3/4. Building optimized executable ..."
733         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
734         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
735         @echo ""
736         @echo "Step 4/4. Deleting profile data ..."
737         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
738
739 strip:
740         $(STRIP) $(EXE)
741
742 install:
743         -mkdir -p -m 755 $(BINDIR)
744         -cp $(EXE) $(BINDIR)
745         -strip $(BINDIR)/$(EXE)
746
747 # clean all
748 clean: objclean profileclean
749         @rm -f .depend *~ core
750
751 # evaluation network (nnue)
752 net:
753         $(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
754         @echo "Default net: $(nnuenet)"
755         $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
756         $(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -skL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi))
757         @if test -f "$(nnuenet)"; then \
758             echo "Already available."; \
759          else \
760             if [ "x$(curl_or_wget)" = "x" ]; then \
761                echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \
762             else \
763                echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet);\
764             fi; \
765         fi;
766         $(eval shasum_command := $(shell if hash shasum 2>/dev/null; then echo "shasum -a 256 "; elif hash sha256sum 2>/dev/null; then echo "sha256sum "; fi))
767         @if [ "x$(shasum_command)" != "x" ]; then \
768             if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
769                 echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; \
770             fi \
771          else \
772             echo "shasum / sha256sum not found, skipping net validation"; \
773         fi
774
775 # clean binaries and objects
776 objclean:
777         @rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
778
779 # clean auxiliary profiling files
780 profileclean:
781         @rm -rf profdir
782         @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda *.s
783         @rm -f stockfish.profdata *.profraw
784
785 default:
786         help
787
788 ### ==========================================================================
789 ### Section 5. Private Targets
790 ### ==========================================================================
791
792 all: $(EXE) client .depend
793
794 config-sanity: net
795         @echo ""
796         @echo "Config:"
797         @echo "debug: '$(debug)'"
798         @echo "sanitize: '$(sanitize)'"
799         @echo "optimize: '$(optimize)'"
800         @echo "arch: '$(arch)'"
801         @echo "bits: '$(bits)'"
802         @echo "kernel: '$(KERNEL)'"
803         @echo "os: '$(OS)'"
804         @echo "prefetch: '$(prefetch)'"
805         @echo "popcnt: '$(popcnt)'"
806         @echo "pext: '$(pext)'"
807         @echo "sse: '$(sse)'"
808         @echo "mmx: '$(mmx)'"
809         @echo "sse2: '$(sse2)'"
810         @echo "ssse3: '$(ssse3)'"
811         @echo "sse41: '$(sse41)'"
812         @echo "avx2: '$(avx2)'"
813         @echo "avx512: '$(avx512)'"
814         @echo "vnni256: '$(vnni256)'"
815         @echo "vnni512: '$(vnni512)'"
816         @echo "neon: '$(neon)'"
817         @echo ""
818         @echo "Flags:"
819         @echo "CXX: $(CXX)"
820         @echo "CXXFLAGS: $(CXXFLAGS)"
821         @echo "LDFLAGS: $(LDFLAGS)"
822         @echo ""
823         @echo "Testing config sanity. If this fails, try 'make help' ..."
824         @echo ""
825         @test "$(debug)" = "yes" || test "$(debug)" = "no"
826         @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
827         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
828         @test "$(SUPPORTED_ARCH)" = "true"
829         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
830          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
831          test "$(arch)" = "armv7" || test "$(arch)" = "armv8" || test "$(arch)" = "arm64"
832         @test "$(bits)" = "32" || test "$(bits)" = "64"
833         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
834         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
835         @test "$(pext)" = "yes" || test "$(pext)" = "no"
836         @test "$(sse)" = "yes" || test "$(sse)" = "no"
837         @test "$(mmx)" = "yes" || test "$(mmx)" = "no"
838         @test "$(sse2)" = "yes" || test "$(sse2)" = "no"
839         @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
840         @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
841         @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
842         @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
843         @test "$(vnni256)" = "yes" || test "$(vnni256)" = "no"
844         @test "$(vnni512)" = "yes" || test "$(vnni512)" = "no"
845         @test "$(neon)" = "yes" || test "$(neon)" = "no"
846         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang" \
847         || test "$(comp)" = "armv7a-linux-androideabi16-clang"  || test "$(comp)" = "aarch64-linux-android21-clang"
848
849 $(EXE): $(OBJS)
850         +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
851
852 clang-profile-make:
853         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
854         EXTRACXXFLAGS='-fprofile-instr-generate ' \
855         EXTRALDFLAGS=' -fprofile-instr-generate' \
856         all
857
858 clang-profile-use:
859         $(XCRUN) llvm-profdata merge -output=stockfish.profdata *.profraw
860         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
861         EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
862         EXTRALDFLAGS='-fprofile-use ' \
863         all
864
865 gcc-profile-make:
866         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
867         EXTRACXXFLAGS='-fprofile-generate' \
868         EXTRALDFLAGS='-lgcov' \
869         all
870
871 gcc-profile-use:
872         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
873         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
874         EXTRALDFLAGS='-lgcov' \
875         all
876
877 icc-profile-make:
878         @mkdir -p profdir
879         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
880         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
881         all
882
883 icc-profile-use:
884         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
885         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
886         all
887
888 ### GRPC
889
890 PROTOS_PATH = .
891 PROTOC = protoc
892 GRPC_CPP_PLUGIN = grpc_cpp_plugin
893 GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
894
895 %.grpc.pb.h %.grpc.pb.cc: %.proto
896         $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
897
898 # oh my
899 %.cpp: %.cc
900         cp $< $@
901
902 %.pb.h %.pb.cc: %.proto
903         $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
904
905 #LDFLAGS += -Wl,-Bstatic -Wl,-\( -lprotobuf -lgrpc++_unsecure -lgrpc_unsecure -lgrpc -lz -Wl,-\) -Wl,-Bdynamic -ldl
906 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
907 #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
908
909 client: $(CLIOBJS)
910         $(CXX) -o $@ $(CLIOBJS) $(LDFLAGS)
911
912 # Other stuff
913
914 .depend:
915         -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
916
917 -include .depend