]> git.sesse.net Git - stockfish/blob - src/Makefile
e3466ea1bb8791979a6430d27cd35f1b15c50315
[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 ($(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 endif
486
487 ### 3.4 Bits
488 ifeq ($(bits),64)
489         CXXFLAGS += -DIS_64BIT
490 endif
491
492 ### 3.5 prefetch
493 ifeq ($(prefetch),yes)
494         ifeq ($(sse),yes)
495                 CXXFLAGS += -msse
496         endif
497 else
498         CXXFLAGS += -DNO_PREFETCH
499 endif
500
501 ### 3.6 popcnt
502 ifeq ($(popcnt),yes)
503         ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8 arm64))
504                 CXXFLAGS += -DUSE_POPCNT
505         else ifeq ($(comp),icc)
506                 CXXFLAGS += -msse3 -DUSE_POPCNT
507         else
508                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
509         endif
510 endif
511
512
513 ifeq ($(avx2),yes)
514         CXXFLAGS += -DUSE_AVX2
515         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
516                 CXXFLAGS += -mavx2
517         endif
518 endif
519
520 ifeq ($(avx512),yes)
521         CXXFLAGS += -DUSE_AVX512
522         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
523                 CXXFLAGS += -mavx512f -mavx512bw
524         endif
525 endif
526
527 ifeq ($(vnni256),yes)
528         CXXFLAGS += -DUSE_VNNI
529         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
530                 CXXFLAGS += -mavx512f -mavx512bw -mavx512vnni -mavx512dq -mavx512vl -mprefer-vector-width=256
531         endif
532 endif
533
534 ifeq ($(vnni512),yes)
535         CXXFLAGS += -DUSE_VNNI
536         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
537                 CXXFLAGS += -mavx512vnni -mavx512dq -mavx512vl
538         endif
539 endif
540
541 ifeq ($(sse41),yes)
542         CXXFLAGS += -DUSE_SSE41
543         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
544                 CXXFLAGS += -msse4.1
545         endif
546 endif
547
548 ifeq ($(ssse3),yes)
549         CXXFLAGS += -DUSE_SSSE3
550         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
551                 CXXFLAGS += -mssse3
552         endif
553 endif
554
555 ifeq ($(sse2),yes)
556         CXXFLAGS += -DUSE_SSE2
557         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
558                 CXXFLAGS += -msse2
559         endif
560 endif
561
562 ifeq ($(mmx),yes)
563         CXXFLAGS += -DUSE_MMX
564         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
565                 CXXFLAGS += -mmmx
566         endif
567 endif
568
569 ifeq ($(neon),yes)
570         CXXFLAGS += -DUSE_NEON
571         ifeq ($(KERNEL),Linux)
572         ifneq ($(COMP),ndk)
573         ifneq ($(arch),armv8)
574                 CXXFLAGS += -mfpu=neon
575         endif
576         endif
577         endif
578 endif
579
580 ### 3.7 pext
581 ifeq ($(pext),yes)
582         CXXFLAGS += -DUSE_PEXT
583         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
584                 CXXFLAGS += -mbmi2
585         endif
586 endif
587
588 ### 3.8 Link Time Optimization
589 ### This is a mix of compile and link time options because the lto link phase
590 ### needs access to the optimization flags.
591 ifeq ($(optimize),yes)
592 ifeq ($(debug), no)
593         ifeq ($(comp),clang)
594                 CXXFLAGS += -flto=thin
595                 ifneq ($(findstring MINGW,$(KERNEL)),)
596                         CXXFLAGS += -fuse-ld=lld
597                 else ifneq ($(findstring MSYS,$(KERNEL)),)
598                         CXXFLAGS += -fuse-ld=lld
599                 endif
600                 LDFLAGS += $(CXXFLAGS)
601
602 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
603 # GCC on some systems.
604         else ifeq ($(comp),gcc)
605         ifeq ($(gccisclang),)
606                 CXXFLAGS += -flto
607                 LDFLAGS += $(CXXFLAGS) -flto=jobserver
608                 ifneq ($(findstring MINGW,$(KERNEL)),)
609                         LDFLAGS += -save-temps
610                 else ifneq ($(findstring MSYS,$(KERNEL)),)
611                         LDFLAGS += -save-temps
612                 endif
613         else
614                 CXXFLAGS += -flto=thin
615                 LDFLAGS += $(CXXFLAGS)
616         endif
617
618 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
619 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are known to work, older might not.
620 # So, only enable it for a cross from Linux by default.
621         else ifeq ($(comp),mingw)
622         ifeq ($(KERNEL),Linux)
623         ifneq ($(arch),i386)
624                 CXXFLAGS += -flto
625                 LDFLAGS += $(CXXFLAGS) -flto=jobserver
626         endif
627         endif
628         endif
629 endif
630 endif
631
632 ### 3.9 Android 5 can only run position independent executables. Note that this
633 ### breaks Android 4.0 and earlier.
634 ifeq ($(OS), Android)
635         CXXFLAGS += -fPIE
636         LDFLAGS += -fPIE -pie
637 endif
638
639 ### ==========================================================================
640 ### Section 4. Public Targets
641 ### ==========================================================================
642
643
644 help:
645         @echo ""
646         @echo "To compile stockfish, type: "
647         @echo ""
648         @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
649         @echo ""
650         @echo "Supported targets:"
651         @echo ""
652         @echo "help                    > Display architecture details"
653         @echo "build                   > Standard build"
654         @echo "net                     > Download the default nnue net"
655         @echo "profile-build           > Faster build (with profile-guided optimization)"
656         @echo "strip                   > Strip executable"
657         @echo "install                 > Install executable"
658         @echo "clean                   > Clean up"
659         @echo ""
660         @echo "Supported archs:"
661         @echo ""
662         @echo "x86-64-vnni512          > x86 64-bit with vnni support 512bit wide"
663         @echo "x86-64-vnni256          > x86 64-bit with vnni support 256bit wide"
664         @echo "x86-64-avx512           > x86 64-bit with avx512 support"
665         @echo "x86-64-bmi2             > x86 64-bit with bmi2 support"
666         @echo "x86-64-avx2             > x86 64-bit with avx2 support"
667         @echo "x86-64-sse41-popcnt     > x86 64-bit with sse41 and popcnt support"
668         @echo "x86-64-modern           > common modern CPU, currently x86-64-sse41-popcnt"
669         @echo "x86-64-ssse3            > x86 64-bit with ssse3 support"
670         @echo "x86-64-sse3-popcnt      > x86 64-bit with sse3 and popcnt support"
671         @echo "x86-64                  > x86 64-bit generic (with sse2 support)"
672         @echo "x86-32-sse41-popcnt     > x86 32-bit with sse41 and popcnt support"
673         @echo "x86-32-sse2             > x86 32-bit with sse2 support"
674         @echo "x86-32                  > x86 32-bit generic (with mmx and sse support)"
675         @echo "ppc-64                  > PPC 64-bit"
676         @echo "ppc-32                  > PPC 32-bit"
677         @echo "armv7                   > ARMv7 32-bit"
678         @echo "armv7-neon              > ARMv7 32-bit with popcnt and neon"
679         @echo "armv8                   > ARMv8 64-bit with popcnt and neon"
680         @echo "apple-silicon           > Apple silicon ARM64"
681         @echo "general-64              > unspecified 64-bit"
682         @echo "general-32              > unspecified 32-bit"
683         @echo ""
684         @echo "Supported compilers:"
685         @echo ""
686         @echo "gcc                     > Gnu compiler (default)"
687         @echo "mingw                   > Gnu compiler with MinGW under Windows"
688         @echo "clang                   > LLVM Clang compiler"
689         @echo "icc                     > Intel compiler"
690         @echo "ndk                     > Google NDK to cross-compile for Android"
691         @echo ""
692         @echo "Simple examples. If you don't know what to do, you likely want to run: "
693         @echo ""
694         @echo "make -j build ARCH=x86-64  (A portable, slow compile for 64-bit systems)"
695         @echo "make -j build ARCH=x86-32  (A portable, slow compile for 32-bit systems)"
696         @echo ""
697         @echo "Advanced examples, for experienced users looking for performance: "
698         @echo ""
699         @echo "make    help  ARCH=x86-64-bmi2"
700         @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-9.0"
701         @echo "make -j build ARCH=x86-64-ssse3 COMP=clang"
702         @echo ""
703         @echo "-------------------------------"
704 ifeq ($(SUPPORTED_ARCH)$(help_skip_sanity), true)
705         @echo "The selected architecture $(ARCH) will enable the following configuration: "
706         @$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
707 else
708         @echo "Specify a supported architecture with the ARCH option for more details"
709         @echo ""
710 endif
711
712
713 .PHONY: help build profile-build strip install clean net objclean profileclean \
714         config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
715         clang-profile-use clang-profile-make
716
717 build: net config-sanity
718         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
719
720 profile-build: net config-sanity objclean profileclean
721         @echo ""
722         @echo "Step 1/4. Building instrumented executable ..."
723         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
724         @echo ""
725         @echo "Step 2/4. Running benchmark for pgo-build ..."
726         $(PGOBENCH) > /dev/null
727         @echo ""
728         @echo "Step 3/4. Building optimized executable ..."
729         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
730         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
731         @echo ""
732         @echo "Step 4/4. Deleting profile data ..."
733         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
734
735 strip:
736         $(STRIP) $(EXE)
737
738 install:
739         -mkdir -p -m 755 $(BINDIR)
740         -cp $(EXE) $(BINDIR)
741         -strip $(BINDIR)/$(EXE)
742
743 # clean all
744 clean: objclean profileclean
745         @rm -f .depend *~ core
746
747 # evaluation network (nnue)
748 net:
749         $(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
750         @echo "Default net: $(nnuenet)"
751         $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
752         $(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))
753         @if test -f "$(nnuenet)"; then \
754             echo "Already available."; \
755          else \
756             if [ "x$(curl_or_wget)" = "x" ]; then \
757                echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \
758             else \
759                echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet);\
760             fi; \
761         fi;
762         $(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))
763         @if [ "x$(shasum_command)" != "x" ]; then \
764             if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
765                 echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; \
766             fi \
767          else \
768             echo "shasum / sha256sum not found, skipping net validation"; \
769         fi
770
771 # clean binaries and objects
772 objclean:
773         @rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
774
775 # clean auxiliary profiling files
776 profileclean:
777         @rm -rf profdir
778         @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda *.s
779         @rm -f stockfish.profdata *.profraw
780
781 default:
782         help
783
784 ### ==========================================================================
785 ### Section 5. Private Targets
786 ### ==========================================================================
787
788 all: $(EXE) client .depend
789
790 config-sanity: net
791         @echo ""
792         @echo "Config:"
793         @echo "debug: '$(debug)'"
794         @echo "sanitize: '$(sanitize)'"
795         @echo "optimize: '$(optimize)'"
796         @echo "arch: '$(arch)'"
797         @echo "bits: '$(bits)'"
798         @echo "kernel: '$(KERNEL)'"
799         @echo "os: '$(OS)'"
800         @echo "prefetch: '$(prefetch)'"
801         @echo "popcnt: '$(popcnt)'"
802         @echo "pext: '$(pext)'"
803         @echo "sse: '$(sse)'"
804         @echo "mmx: '$(mmx)'"
805         @echo "sse2: '$(sse2)'"
806         @echo "ssse3: '$(ssse3)'"
807         @echo "sse41: '$(sse41)'"
808         @echo "avx2: '$(avx2)'"
809         @echo "avx512: '$(avx512)'"
810         @echo "vnni256: '$(vnni256)'"
811         @echo "vnni512: '$(vnni512)'"
812         @echo "neon: '$(neon)'"
813         @echo ""
814         @echo "Flags:"
815         @echo "CXX: $(CXX)"
816         @echo "CXXFLAGS: $(CXXFLAGS)"
817         @echo "LDFLAGS: $(LDFLAGS)"
818         @echo ""
819         @echo "Testing config sanity. If this fails, try 'make help' ..."
820         @echo ""
821         @test "$(debug)" = "yes" || test "$(debug)" = "no"
822         @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
823         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
824         @test "$(SUPPORTED_ARCH)" = "true"
825         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
826          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
827          test "$(arch)" = "armv7" || test "$(arch)" = "armv8" || test "$(arch)" = "arm64"
828         @test "$(bits)" = "32" || test "$(bits)" = "64"
829         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
830         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
831         @test "$(pext)" = "yes" || test "$(pext)" = "no"
832         @test "$(sse)" = "yes" || test "$(sse)" = "no"
833         @test "$(mmx)" = "yes" || test "$(mmx)" = "no"
834         @test "$(sse2)" = "yes" || test "$(sse2)" = "no"
835         @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
836         @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
837         @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
838         @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
839         @test "$(vnni256)" = "yes" || test "$(vnni256)" = "no"
840         @test "$(vnni512)" = "yes" || test "$(vnni512)" = "no"
841         @test "$(neon)" = "yes" || test "$(neon)" = "no"
842         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang" \
843         || test "$(comp)" = "armv7a-linux-androideabi16-clang"  || test "$(comp)" = "aarch64-linux-android21-clang"
844
845 $(EXE): $(OBJS)
846         +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
847
848 clang-profile-make:
849         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
850         EXTRACXXFLAGS='-fprofile-instr-generate ' \
851         EXTRALDFLAGS=' -fprofile-instr-generate' \
852         all
853
854 clang-profile-use:
855         $(XCRUN) llvm-profdata merge -output=stockfish.profdata *.profraw
856         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
857         EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
858         EXTRALDFLAGS='-fprofile-use ' \
859         all
860
861 gcc-profile-make:
862         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
863         EXTRACXXFLAGS='-fprofile-generate' \
864         EXTRALDFLAGS='-lgcov' \
865         all
866
867 gcc-profile-use:
868         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
869         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
870         EXTRALDFLAGS='-lgcov' \
871         all
872
873 icc-profile-make:
874         @mkdir -p profdir
875         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
876         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
877         all
878
879 icc-profile-use:
880         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
881         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
882         all
883
884 ### GRPC
885
886 PROTOS_PATH = .
887 PROTOC = protoc
888 GRPC_CPP_PLUGIN = grpc_cpp_plugin
889 GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
890
891 %.grpc.pb.h %.grpc.pb.cc: %.proto
892         $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
893
894 # oh my
895 %.cpp: %.cc
896         cp $< $@
897
898 %.pb.h %.pb.cc: %.proto
899         $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
900
901 #LDFLAGS += -Wl,-Bstatic -Wl,-\( -lprotobuf -lgrpc++_unsecure -lgrpc_unsecure -lgrpc -lz -Wl,-\) -Wl,-Bdynamic -ldl
902 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
903 #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
904
905 client: $(CLIOBJS)
906         $(CXX) -o $@ $(CLIOBJS) $(LDFLAGS)
907
908 # Other stuff
909
910 .depend:
911         -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
912
913 -include .depend