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