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