]> git.sesse.net Git - stockfish/blob - src/Makefile
Merge branch 'master' into clusterMergeMaster11
[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 cluster.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
44 OBJS = $(notdir $(SRCS:.cpp=.o))
45
46 VPATH = syzygy:nnue:nnue/features
47
48 ### Establish the operating system name
49 KERNEL = $(shell uname -s)
50 ifeq ($(KERNEL),Linux)
51         OS = $(shell uname -o)
52 endif
53
54 ### ==========================================================================
55 ### Section 2. High-level Configuration
56 ### ==========================================================================
57 #
58 # flag                --- Comp switch      --- Description
59 # ----------------------------------------------------------------------------
60 #
61 # debug = yes/no      --- -DNDEBUG         --- Enable/Disable debug mode
62 # sanitize = undefined/thread/no (-fsanitize )
63 #                     --- ( undefined )    --- enable undefined behavior checks
64 #                     --- ( thread    )    --- enable threading error  checks
65 # optimize = yes/no   --- (-O3/-fast etc.) --- Enable/Disable optimizations
66 # arch = (name)       --- (-arch)          --- Target architecture
67 # bits = 64/32        --- -DIS_64BIT       --- 64-/32-bit operating system
68 # prefetch = yes/no   --- -DUSE_PREFETCH   --- Use prefetch asm-instruction
69 # popcnt = yes/no     --- -DUSE_POPCNT     --- Use popcnt asm-instruction
70 # pext = yes/no       --- -DUSE_PEXT       --- Use pext x86_64 asm-instruction
71 # sse = yes/no        --- -msse            --- Use Intel Streaming SIMD Extensions
72 # mmx = yes/no        --- -mmmx            --- Use Intel MMX instructions
73 # sse2 = yes/no       --- -msse2           --- Use Intel Streaming SIMD Extensions 2
74 # ssse3 = yes/no      --- -mssse3          --- Use Intel Supplemental Streaming SIMD Extensions 3
75 # sse41 = yes/no      --- -msse4.1         --- Use Intel Streaming SIMD Extensions 4.1
76 # avx2 = yes/no       --- -mavx2           --- Use Intel Advanced Vector Extensions 2
77 # avx512 = yes/no     --- -mavx512bw       --- Use Intel Advanced Vector Extensions 512
78 # vnni256 = yes/no    --- -mavx512vnni     --- Use Intel Vector Neural Network Instructions 256
79 # vnni512 = yes/no    --- -mavx512vnni     --- Use Intel Vector Neural Network Instructions 512
80 # neon = yes/no       --- -DUSE_NEON       --- Use ARM SIMD architecture
81 # mpi = yes/no        --- -DUSE_MPI        --- Use Message Passing Interface
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 mpi = no
123 STRIP = strip
124
125 ### 2.2 Architecture specific
126
127 ifeq ($(findstring x86,$(ARCH)),x86)
128
129 # x86-32/64
130
131 ifeq ($(findstring x86-32,$(ARCH)),x86-32)
132         arch = i386
133         bits = 32
134         sse = yes
135         mmx = yes
136 else
137         arch = x86_64
138         sse = yes
139         sse2 = yes
140 endif
141
142 ifeq ($(findstring -sse,$(ARCH)),-sse)
143         sse = yes
144 endif
145
146 ifeq ($(findstring -popcnt,$(ARCH)),-popcnt)
147         popcnt = yes
148 endif
149
150 ifeq ($(findstring -mmx,$(ARCH)),-mmx)
151         mmx = yes
152 endif
153
154 ifeq ($(findstring -sse2,$(ARCH)),-sse2)
155         sse = yes
156         sse2 = yes
157 endif
158
159 ifeq ($(findstring -ssse3,$(ARCH)),-ssse3)
160         sse = yes
161         sse2 = yes
162         ssse3 = yes
163 endif
164
165 ifeq ($(findstring -sse41,$(ARCH)),-sse41)
166         sse = yes
167         sse2 = yes
168         ssse3 = yes
169         sse41 = yes
170 endif
171
172 ifeq ($(findstring -modern,$(ARCH)),-modern)
173         popcnt = yes
174         sse = yes
175         sse2 = yes
176         ssse3 = yes
177         sse41 = yes
178 endif
179
180 ifeq ($(findstring -avx2,$(ARCH)),-avx2)
181         popcnt = yes
182         sse = yes
183         sse2 = yes
184         ssse3 = yes
185         sse41 = yes
186         avx2 = yes
187 endif
188
189 ifeq ($(findstring -bmi2,$(ARCH)),-bmi2)
190         popcnt = yes
191         sse = yes
192         sse2 = yes
193         ssse3 = yes
194         sse41 = yes
195         avx2 = yes
196         pext = yes
197 endif
198
199 ifeq ($(findstring -avx512,$(ARCH)),-avx512)
200         popcnt = yes
201         sse = yes
202         sse2 = yes
203         ssse3 = yes
204         sse41 = yes
205         avx2 = yes
206         pext = yes
207         avx512 = yes
208 endif
209
210 ifeq ($(findstring -vnni256,$(ARCH)),-vnni256)
211         popcnt = yes
212         sse = yes
213         sse2 = yes
214         ssse3 = yes
215         sse41 = yes
216         avx2 = yes
217         pext = yes
218         vnni256 = yes
219 endif
220
221 ifeq ($(findstring -vnni512,$(ARCH)),-vnni512)
222         popcnt = yes
223         sse = yes
224         sse2 = yes
225         ssse3 = yes
226         sse41 = yes
227         avx2 = yes
228         pext = yes
229         avx512 = yes
230         vnni512 = yes
231 endif
232
233 ifeq ($(sse),yes)
234         prefetch = yes
235 endif
236
237 # 64-bit pext is not available on x86-32
238 ifeq ($(bits),32)
239         pext = no
240 endif
241
242 else
243
244 # all other architectures
245
246 ifeq ($(ARCH),general-32)
247         arch = any
248         bits = 32
249 endif
250
251 ifeq ($(ARCH),general-64)
252         arch = any
253 endif
254
255 ifeq ($(ARCH),armv7)
256         arch = armv7
257         prefetch = yes
258         bits = 32
259 endif
260
261 ifeq ($(ARCH),armv7-neon)
262         arch = armv7
263         prefetch = yes
264         popcnt = yes
265         neon = yes
266         bits = 32
267 endif
268
269 ifeq ($(ARCH),armv8)
270         arch = armv8
271         prefetch = yes
272         popcnt = yes
273         neon = yes
274 endif
275
276 ifeq ($(ARCH),apple-silicon)
277         arch = arm64
278         prefetch = yes
279         popcnt = yes
280         neon = yes
281 endif
282
283 ifeq ($(ARCH),ppc-32)
284         arch = ppc
285         bits = 32
286 endif
287
288 ifeq ($(ARCH),ppc-64)
289         arch = ppc64
290         popcnt = yes
291         prefetch = yes
292 endif
293
294 endif
295
296 ### ==========================================================================
297 ### Section 3. Low-level Configuration
298 ### ==========================================================================
299
300 ### 3.1 Selecting compiler (default = gcc)
301 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
302 DEPENDFLAGS += -std=c++17
303 LDFLAGS += $(EXTRALDFLAGS)
304
305 ifeq ($(COMP),)
306         COMP=gcc
307 endif
308
309 ifeq ($(COMP),gcc)
310         comp=gcc
311         CXX=g++
312         CXXFLAGS += -pedantic -Wextra -Wshadow
313
314         ifeq ($(arch),$(filter $(arch),armv7 armv8))
315                 ifeq ($(OS),Android)
316                         CXXFLAGS += -m$(bits)
317                         LDFLAGS += -m$(bits)
318                 endif
319         else
320                 CXXFLAGS += -m$(bits)
321                 LDFLAGS += -m$(bits)
322         endif
323
324         ifeq ($(arch),$(filter $(arch),armv7))
325                 LDFLAGS += -latomic
326         endif
327
328         ifneq ($(KERNEL),Darwin)
329            LDFLAGS += -Wl,--no-as-needed
330         endif
331 endif
332
333 ifeq ($(COMP),mingw)
334         comp=mingw
335
336         ifeq ($(KERNEL),Linux)
337                 ifeq ($(bits),64)
338                         ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
339                                 CXX=x86_64-w64-mingw32-c++
340                         else
341                                 CXX=x86_64-w64-mingw32-c++-posix
342                         endif
343                 else
344                         ifeq ($(shell which i686-w64-mingw32-c++-posix),)
345                                 CXX=i686-w64-mingw32-c++
346                         else
347                                 CXX=i686-w64-mingw32-c++-posix
348                         endif
349                 endif
350         else
351                 CXX=g++
352         endif
353
354         CXXFLAGS += -Wextra -Wshadow
355         LDFLAGS += -static
356 endif
357
358 ifeq ($(COMP),icc)
359         comp=icc
360         CXX=icpc
361         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
362 endif
363
364 ifeq ($(COMP),clang)
365         comp=clang
366         CXX=clang++
367         CXXFLAGS += -pedantic -Wextra -Wshadow
368
369         ifneq ($(KERNEL),Darwin)
370         ifneq ($(KERNEL),OpenBSD)
371                 LDFLAGS += -latomic
372         endif
373         endif
374
375         ifeq ($(arch),$(filter $(arch),armv7 armv8))
376                 ifeq ($(OS),Android)
377                         CXXFLAGS += -m$(bits)
378                         LDFLAGS += -m$(bits)
379                 endif
380         else
381                 CXXFLAGS += -m$(bits)
382                 LDFLAGS += -m$(bits)
383         endif
384 endif
385
386 ifeq ($(KERNEL),Darwin)
387         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.14
388         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.14
389         XCRUN = xcrun
390 endif
391
392 # To cross-compile for Android, NDK version r21 or later is recommended.
393 # In earlier NDK versions, you'll need to pass -fno-addrsig if using GNU binutils.
394 # Currently we don't know how to make PGO builds with the NDK yet.
395 ifeq ($(COMP),ndk)
396         CXXFLAGS += -stdlib=libc++ -fPIE
397         comp=clang
398         ifeq ($(arch),armv7)
399                 CXX=armv7a-linux-androideabi16-clang++
400                 CXXFLAGS += -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=neon
401                 STRIP=arm-linux-androideabi-strip
402         endif
403         ifeq ($(arch),armv8)
404                 CXX=aarch64-linux-android21-clang++
405                 STRIP=aarch64-linux-android-strip
406         endif
407         LDFLAGS += -static-libstdc++ -pie -lm -latomic
408 endif
409
410 ifeq ($(comp),icc)
411         profile_make = icc-profile-make
412         profile_use = icc-profile-use
413 else ifeq ($(comp),clang)
414         profile_make = clang-profile-make
415         profile_use = clang-profile-use
416 else
417         profile_make = gcc-profile-make
418         profile_use = gcc-profile-use
419 endif
420
421 ### Travis CI script uses COMPILER to overwrite CXX
422 ifdef COMPILER
423         COMPCXX=$(COMPILER)
424 endif
425
426 ### Allow overwriting CXX from command line
427 ifdef COMPCXX
428         CXX=$(COMPCXX)
429 endif
430
431 ### Sometimes gcc is really clang
432 ifeq ($(COMP),gcc)
433         gccversion = $(shell $(CXX) --version)
434         gccisclang = $(findstring clang,$(gccversion))
435         ifneq ($(gccisclang),)
436                 profile_make = clang-profile-make
437                 profile_use = clang-profile-use
438         endif
439 endif
440
441 ### On mingw use Windows threads, otherwise POSIX
442 ifneq ($(comp),mingw)
443         CXXFLAGS += -DUSE_PTHREADS
444         # On Android Bionic's C library comes with its own pthread implementation bundled in
445         ifneq ($(OS),Android)
446                 # Haiku has pthreads in its libroot, so only link it in on other platforms
447                 ifneq ($(KERNEL),Haiku)
448                         ifneq ($(COMP),ndk)
449                                 LDFLAGS += -lpthread
450                         endif
451                 endif
452         endif
453 endif
454
455 ### 3.2.1 Debugging
456 ifeq ($(debug),no)
457         CXXFLAGS += -DNDEBUG
458 else
459         CXXFLAGS += -g
460 endif
461
462 ### 3.2.2 Debugging with undefined behavior sanitizers
463 ifneq ($(sanitize),no)
464         CXXFLAGS += -g3 -fsanitize=$(sanitize)
465         LDFLAGS += -fsanitize=$(sanitize)
466 endif
467
468 ### 3.3 Optimization
469 ifeq ($(optimize),yes)
470
471         CXXFLAGS += -O3
472
473         ifeq ($(comp),gcc)
474                 ifeq ($(OS), Android)
475                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
476                 endif
477         endif
478
479         ifeq ($(comp),$(filter $(comp),gcc clang icc))
480                 ifeq ($(KERNEL),Darwin)
481                         CXXFLAGS += -mdynamic-no-pic
482                 endif
483         endif
484 endif
485
486 ### 3.4 Bits
487 ifeq ($(bits),64)
488         CXXFLAGS += -DIS_64BIT
489 endif
490
491 ### 3.5 prefetch
492 ifeq ($(prefetch),yes)
493         ifeq ($(sse),yes)
494                 CXXFLAGS += -msse
495         endif
496 else
497         CXXFLAGS += -DNO_PREFETCH
498 endif
499
500 ### 3.6 popcnt
501 ifeq ($(popcnt),yes)
502         ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8 arm64))
503                 CXXFLAGS += -DUSE_POPCNT
504         else ifeq ($(comp),icc)
505                 CXXFLAGS += -msse3 -DUSE_POPCNT
506         else
507                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
508         endif
509 endif
510
511
512 ifeq ($(avx2),yes)
513         CXXFLAGS += -DUSE_AVX2
514         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
515                 CXXFLAGS += -mavx2
516         endif
517 endif
518
519 ifeq ($(avx512),yes)
520         CXXFLAGS += -DUSE_AVX512
521         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
522                 CXXFLAGS += -mavx512f -mavx512bw
523         endif
524 endif
525
526 ifeq ($(vnni256),yes)
527         CXXFLAGS += -DUSE_VNNI
528         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
529                 CXXFLAGS += -mavx512f -mavx512bw -mavx512vnni -mavx512dq -mavx512vl -mprefer-vector-width=256
530         endif
531 endif
532
533 ifeq ($(vnni512),yes)
534         CXXFLAGS += -DUSE_VNNI
535         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
536                 CXXFLAGS += -mavx512vnni -mavx512dq -mavx512vl
537         endif
538 endif
539
540 ifeq ($(sse41),yes)
541         CXXFLAGS += -DUSE_SSE41
542         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
543                 CXXFLAGS += -msse4.1
544         endif
545 endif
546
547 ifeq ($(ssse3),yes)
548         CXXFLAGS += -DUSE_SSSE3
549         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
550                 CXXFLAGS += -mssse3
551         endif
552 endif
553
554 ifeq ($(sse2),yes)
555         CXXFLAGS += -DUSE_SSE2
556         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
557                 CXXFLAGS += -msse2
558         endif
559 endif
560
561 ifeq ($(mmx),yes)
562         CXXFLAGS += -DUSE_MMX
563         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
564                 CXXFLAGS += -mmmx
565         endif
566 endif
567
568 ifeq ($(neon),yes)
569         CXXFLAGS += -DUSE_NEON
570         ifeq ($(KERNEL),Linux)
571         ifneq ($(COMP),ndk)
572         ifneq ($(arch),armv8)
573                 CXXFLAGS += -mfpu=neon
574         endif
575         endif
576         endif
577 endif
578
579 ### 3.7 pext
580 ifeq ($(pext),yes)
581         CXXFLAGS += -DUSE_PEXT
582         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
583                 CXXFLAGS += -mbmi2
584         endif
585 endif
586
587 ### 3.8 Link Time Optimization
588 ### This is a mix of compile and link time options because the lto link phase
589 ### needs access to the optimization flags.
590 ifeq ($(optimize),yes)
591 ifeq ($(debug), no)
592         ifeq ($(comp),clang)
593                 CXXFLAGS += -flto=thin
594                 ifneq ($(findstring MINGW,$(KERNEL)),)
595                         CXXFLAGS += -fuse-ld=lld
596                 else ifneq ($(findstring MSYS,$(KERNEL)),)
597                         CXXFLAGS += -fuse-ld=lld
598                 endif
599                 LDFLAGS += $(CXXFLAGS)
600
601 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
602 # GCC on some systems.
603         else ifeq ($(comp),gcc)
604         ifeq ($(gccisclang),)
605                 CXXFLAGS += -flto
606                 LDFLAGS += $(CXXFLAGS) -flto=jobserver
607                 ifneq ($(findstring MINGW,$(KERNEL)),)
608                         LDFLAGS += -save-temps
609                 else ifneq ($(findstring MSYS,$(KERNEL)),)
610                         LDFLAGS += -save-temps
611                 endif
612         else
613                 CXXFLAGS += -flto=thin
614                 LDFLAGS += $(CXXFLAGS)
615         endif
616
617 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
618 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are known to work, older might not.
619 # So, only enable it for a cross from Linux by default.
620         else ifeq ($(comp),mingw)
621         ifeq ($(KERNEL),Linux)
622         ifneq ($(arch),i386)
623                 CXXFLAGS += -flto
624                 LDFLAGS += $(CXXFLAGS) -flto=jobserver
625         endif
626         endif
627         endif
628 endif
629 endif
630
631 ### 3.9 Android 5 can only run position independent executables. Note that this
632 ### breaks Android 4.0 and earlier.
633 ifeq ($(OS), Android)
634         CXXFLAGS += -fPIE
635         LDFLAGS += -fPIE -pie
636 endif
637
638 ### 3.10 MPI
639 ifneq (,$(findstring mpi, $(CXX)))
640         mpi = yes
641         CXXFLAGS += -DUSE_MPI -Wno-cast-qual -fexceptions
642         DEPENDFLAGS += -DUSE_MPI
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) .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 "mpi: '$(mpi)'"
820         @echo ""
821         @echo "Flags:"
822         @echo "CXX: $(CXX)"
823         @echo "CXXFLAGS: $(CXXFLAGS)"
824         @echo "LDFLAGS: $(LDFLAGS)"
825         @echo ""
826         @echo "Testing config sanity. If this fails, try 'make help' ..."
827         @echo ""
828         @test "$(debug)" = "yes" || test "$(debug)" = "no"
829         @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
830         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
831         @test "$(SUPPORTED_ARCH)" = "true"
832         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
833          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
834          test "$(arch)" = "armv7" || test "$(arch)" = "armv8" || test "$(arch)" = "arm64"
835         @test "$(bits)" = "32" || test "$(bits)" = "64"
836         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
837         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
838         @test "$(pext)" = "yes" || test "$(pext)" = "no"
839         @test "$(sse)" = "yes" || test "$(sse)" = "no"
840         @test "$(mmx)" = "yes" || test "$(mmx)" = "no"
841         @test "$(sse2)" = "yes" || test "$(sse2)" = "no"
842         @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
843         @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
844         @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
845         @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
846         @test "$(vnni256)" = "yes" || test "$(vnni256)" = "no"
847         @test "$(vnni512)" = "yes" || test "$(vnni512)" = "no"
848         @test "$(neon)" = "yes" || test "$(neon)" = "no"
849         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang" \
850         || test "$(comp)" = "armv7a-linux-androideabi16-clang"  || test "$(comp)" = "aarch64-linux-android21-clang"
851
852 $(EXE): $(OBJS)
853         +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
854
855 clang-profile-make:
856         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
857         EXTRACXXFLAGS='-fprofile-instr-generate ' \
858         EXTRALDFLAGS=' -fprofile-instr-generate' \
859         all
860
861 clang-profile-use:
862         $(XCRUN) llvm-profdata merge -output=stockfish.profdata *.profraw
863         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
864         EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
865         EXTRALDFLAGS='-fprofile-use ' \
866         all
867
868 gcc-profile-make:
869         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
870         EXTRACXXFLAGS='-fprofile-generate' \
871         EXTRALDFLAGS='-lgcov' \
872         all
873
874 gcc-profile-use:
875         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
876         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
877         EXTRALDFLAGS='-lgcov' \
878         all
879
880 icc-profile-make:
881         @mkdir -p profdir
882         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
883         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
884         all
885
886 icc-profile-use:
887         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
888         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
889         all
890
891 .depend:
892         -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
893
894 -include .depend