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