]> git.sesse.net Git - stockfish/blob - src/Makefile
Revert -flto=auto on mingw
[stockfish] / src / Makefile
1 # Stockfish, a UCI chess playing engine derived from Glaurung 2.1
2 # Copyright (C) 2004-2022 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 ### Establish the operating system name
23 KERNEL = $(shell uname -s)
24 ifeq ($(KERNEL),Linux)
25         OS = $(shell uname -o)
26 endif
27
28 ### Target Windows OS
29 ifeq ($(OS),Windows_NT)
30         target_windows = yes
31 else ifeq ($(COMP),mingw)
32         target_windows = yes
33         ifeq ($(WINE_PATH),)
34                 WINE_PATH = $(shell which wine)
35         endif
36 endif
37
38 ### Executable name
39 ifeq ($(target_windows),yes)
40         EXE = stockfish.exe
41 else
42         EXE = stockfish
43 endif
44
45 ### Installation dir definitions
46 PREFIX = /usr/local
47 BINDIR = $(PREFIX)/bin
48
49 ### Built-in benchmark for pgo-builds
50 ifeq ($(SDE_PATH),)
51         PGOBENCH = $(WINE_PATH) ./$(EXE) bench
52 else
53         PGOBENCH = $(SDE_PATH) -- $(WINE_PATH) ./$(EXE) bench
54 endif
55
56 ### Source and object files
57 SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
58         material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
59         search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
60         nnue/evaluate_nnue.cpp nnue/features/half_ka_v2_hm.cpp
61
62 OBJS = $(notdir $(SRCS:.cpp=.o))
63
64 VPATH = syzygy:nnue:nnue/features
65
66 ### ==========================================================================
67 ### Section 2. High-level Configuration
68 ### ==========================================================================
69 #
70 # flag                --- Comp switch      --- Description
71 # ----------------------------------------------------------------------------
72 #
73 # debug = yes/no      --- -DNDEBUG         --- Enable/Disable debug mode
74 # sanitize = none/<sanitizer> ... (-fsanitize )
75 #                     --- ( undefined )    --- enable undefined behavior checks
76 #                     --- ( thread    )    --- enable threading error checks
77 #                     --- ( address   )    --- enable memory access checks
78 #                     --- ...etc...        --- see compiler documentation for supported sanitizers
79 # optimize = yes/no   --- (-O3/-fast etc.) --- Enable/Disable optimizations
80 # arch = (name)       --- (-arch)          --- Target architecture
81 # bits = 64/32        --- -DIS_64BIT       --- 64-/32-bit operating system
82 # prefetch = yes/no   --- -DUSE_PREFETCH   --- Use prefetch asm-instruction
83 # popcnt = yes/no     --- -DUSE_POPCNT     --- Use popcnt asm-instruction
84 # pext = yes/no       --- -DUSE_PEXT       --- Use pext x86_64 asm-instruction
85 # sse = yes/no        --- -msse            --- Use Intel Streaming SIMD Extensions
86 # mmx = yes/no        --- -mmmx            --- Use Intel MMX instructions
87 # sse2 = yes/no       --- -msse2           --- Use Intel Streaming SIMD Extensions 2
88 # ssse3 = yes/no      --- -mssse3          --- Use Intel Supplemental Streaming SIMD Extensions 3
89 # sse41 = yes/no      --- -msse4.1         --- Use Intel Streaming SIMD Extensions 4.1
90 # avx2 = yes/no       --- -mavx2           --- Use Intel Advanced Vector Extensions 2
91 # avxvnni = yes/no    --- -mavxvnni        --- Use Intel Vector Neural Network Instructions AVX
92 # avx512 = yes/no     --- -mavx512bw       --- Use Intel Advanced Vector Extensions 512
93 # vnni256 = yes/no    --- -mavx512vnni     --- Use Intel Vector Neural Network Instructions 256
94 # vnni512 = yes/no    --- -mavx512vnni     --- Use Intel Vector Neural Network Instructions 512
95 # neon = yes/no       --- -DUSE_NEON       --- Use ARM SIMD architecture
96 #
97 # Note that Makefile is space sensitive, so when adding new architectures
98 # or modifying existing flags, you have to make sure there are no extra spaces
99 # at the end of the line for flag values.
100 #
101 # Example of use for these flags:
102 # make build ARCH=x86-64-avx512 debug=yes sanitize="address undefined"
103
104
105 ### 2.1. General and architecture defaults
106
107 ifeq ($(ARCH),)
108    ARCH = x86-64-modern
109    help_skip_sanity = yes
110 endif
111 # explicitly check for the list of supported architectures (as listed with make help),
112 # the user can override with `make ARCH=x86-32-vnni256 SUPPORTED_ARCH=true`
113 ifeq ($(ARCH), $(filter $(ARCH), \
114                  x86-64-vnni512 x86-64-vnni256 x86-64-avx512 x86-64-avxvnni x86-64-bmi2 \
115                  x86-64-avx2 x86-64-sse41-popcnt x86-64-modern x86-64-ssse3 x86-64-sse3-popcnt \
116                  x86-64 x86-32-sse41-popcnt x86-32-sse2 x86-32 ppc-64 ppc-32 e2k \
117                  armv7 armv7-neon armv8 apple-silicon general-64 general-32))
118    SUPPORTED_ARCH=true
119 else
120    SUPPORTED_ARCH=false
121 endif
122
123 optimize = yes
124 debug = no
125 sanitize = none
126 bits = 64
127 prefetch = no
128 popcnt = no
129 pext = no
130 sse = no
131 mmx = no
132 sse2 = no
133 ssse3 = no
134 sse41 = no
135 avx2 = no
136 avxvnni = no
137 avx512 = no
138 vnni256 = no
139 vnni512 = no
140 neon = no
141 arm_version = 0
142 STRIP = strip
143
144 ### 2.2 Architecture specific
145
146 ifeq ($(findstring x86,$(ARCH)),x86)
147
148 # x86-32/64
149
150 ifeq ($(findstring x86-32,$(ARCH)),x86-32)
151         arch = i386
152         bits = 32
153         sse = yes
154         mmx = yes
155 else
156         arch = x86_64
157         sse = yes
158         sse2 = yes
159 endif
160
161 ifeq ($(findstring -sse,$(ARCH)),-sse)
162         sse = yes
163 endif
164
165 ifeq ($(findstring -popcnt,$(ARCH)),-popcnt)
166         popcnt = yes
167 endif
168
169 ifeq ($(findstring -mmx,$(ARCH)),-mmx)
170         mmx = yes
171 endif
172
173 ifeq ($(findstring -sse2,$(ARCH)),-sse2)
174         sse = yes
175         sse2 = yes
176 endif
177
178 ifeq ($(findstring -ssse3,$(ARCH)),-ssse3)
179         sse = yes
180         sse2 = yes
181         ssse3 = yes
182 endif
183
184 ifeq ($(findstring -sse41,$(ARCH)),-sse41)
185         sse = yes
186         sse2 = yes
187         ssse3 = yes
188         sse41 = yes
189 endif
190
191 ifeq ($(findstring -modern,$(ARCH)),-modern)
192         popcnt = yes
193         sse = yes
194         sse2 = yes
195         ssse3 = yes
196         sse41 = yes
197 endif
198
199 ifeq ($(findstring -avx2,$(ARCH)),-avx2)
200         popcnt = yes
201         sse = yes
202         sse2 = yes
203         ssse3 = yes
204         sse41 = yes
205         avx2 = yes
206 endif
207
208 ifeq ($(findstring -avxvnni,$(ARCH)),-avxvnni)
209         popcnt = yes
210         sse = yes
211         sse2 = yes
212         ssse3 = yes
213         sse41 = yes
214         avx2 = yes
215         avxvnni = yes
216         pext = yes
217 endif
218
219 ifeq ($(findstring -bmi2,$(ARCH)),-bmi2)
220         popcnt = yes
221         sse = yes
222         sse2 = yes
223         ssse3 = yes
224         sse41 = yes
225         avx2 = yes
226         pext = yes
227 endif
228
229 ifeq ($(findstring -avx512,$(ARCH)),-avx512)
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 endif
239
240 ifeq ($(findstring -vnni256,$(ARCH)),-vnni256)
241         popcnt = yes
242         sse = yes
243         sse2 = yes
244         ssse3 = yes
245         sse41 = yes
246         avx2 = yes
247         pext = yes
248         vnni256 = yes
249 endif
250
251 ifeq ($(findstring -vnni512,$(ARCH)),-vnni512)
252         popcnt = yes
253         sse = yes
254         sse2 = yes
255         ssse3 = yes
256         sse41 = yes
257         avx2 = yes
258         pext = yes
259         avx512 = yes
260         vnni512 = yes
261 endif
262
263 ifeq ($(sse),yes)
264         prefetch = yes
265 endif
266
267 # 64-bit pext is not available on x86-32
268 ifeq ($(bits),32)
269         pext = no
270 endif
271
272 else
273
274 # all other architectures
275
276 ifeq ($(ARCH),general-32)
277         arch = any
278         bits = 32
279 endif
280
281 ifeq ($(ARCH),general-64)
282         arch = any
283 endif
284
285 ifeq ($(ARCH),armv7)
286         arch = armv7
287         prefetch = yes
288         bits = 32
289         arm_version = 7
290 endif
291
292 ifeq ($(ARCH),armv7-neon)
293         arch = armv7
294         prefetch = yes
295         popcnt = yes
296         neon = yes
297         bits = 32
298         arm_version = 7
299 endif
300
301 ifeq ($(ARCH),armv8)
302         arch = armv8
303         prefetch = yes
304         popcnt = yes
305         neon = yes
306         arm_version = 8
307 endif
308
309 ifeq ($(ARCH),apple-silicon)
310         arch = arm64
311         prefetch = yes
312         popcnt = yes
313         neon = yes
314         arm_version = 8
315 endif
316
317 ifeq ($(ARCH),ppc-32)
318         arch = ppc
319         bits = 32
320 endif
321
322 ifeq ($(ARCH),ppc-64)
323         arch = ppc64
324         popcnt = yes
325         prefetch = yes
326 endif
327
328 ifeq ($(findstring e2k,$(ARCH)),e2k)
329         arch = e2k
330         mmx = yes
331         bits = 64
332         sse = yes
333         sse2 = yes
334         ssse3 = yes
335         sse41 = yes
336         popcnt = yes
337 endif
338
339 endif
340
341 ### ==========================================================================
342 ### Section 3. Low-level Configuration
343 ### ==========================================================================
344
345 ### 3.1 Selecting compiler (default = gcc)
346 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
347 DEPENDFLAGS += -std=c++17
348 LDFLAGS += $(EXTRALDFLAGS)
349
350 ifeq ($(COMP),)
351         COMP=gcc
352 endif
353
354 ifeq ($(COMP),gcc)
355         comp=gcc
356         CXX=g++
357         CXXFLAGS += -pedantic -Wextra -Wshadow
358
359         ifeq ($(arch),$(filter $(arch),armv7 armv8))
360                 ifeq ($(OS),Android)
361                         CXXFLAGS += -m$(bits)
362                         LDFLAGS += -m$(bits)
363                 endif
364         else
365                 CXXFLAGS += -m$(bits)
366                 LDFLAGS += -m$(bits)
367         endif
368
369         ifeq ($(arch),$(filter $(arch),armv7))
370                 LDFLAGS += -latomic
371         endif
372
373         ifneq ($(KERNEL),Darwin)
374            LDFLAGS += -Wl,--no-as-needed
375         endif
376 endif
377
378 ifeq ($(target_windows),yes)
379         LDFLAGS += -static
380 endif
381
382 ifeq ($(COMP),mingw)
383         comp=mingw
384
385         ifeq ($(bits),64)
386                 ifeq ($(shell which x86_64-w64-mingw32-c++-posix 2> /dev/null),)
387                         CXX=x86_64-w64-mingw32-c++
388                 else
389                         CXX=x86_64-w64-mingw32-c++-posix
390                 endif
391         else
392                 ifeq ($(shell which i686-w64-mingw32-c++-posix 2> /dev/null),)
393                         CXX=i686-w64-mingw32-c++
394                 else
395                         CXX=i686-w64-mingw32-c++-posix
396                 endif
397         endif
398         CXXFLAGS += -pedantic -Wextra -Wshadow
399 endif
400
401 ifeq ($(COMP),icc)
402         comp=icc
403         CXX=icpc
404         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
405 endif
406
407 ifeq ($(COMP),clang)
408         comp=clang
409         CXX=clang++
410         ifeq ($(target_windows),yes)
411                 CXX=x86_64-w64-mingw32-clang++
412         endif
413
414         CXXFLAGS += -pedantic -Wextra -Wshadow
415
416         ifeq ($(filter $(KERNEL),Darwin OpenBSD FreeBSD),)
417         ifeq ($(target_windows),)
418         ifneq ($(RTLIB),compiler-rt)
419                 LDFLAGS += -latomic
420         endif
421         endif
422         endif
423
424         ifeq ($(arch),$(filter $(arch),armv7 armv8))
425                 ifeq ($(OS),Android)
426                         CXXFLAGS += -m$(bits)
427                         LDFLAGS += -m$(bits)
428                 endif
429         else
430                 CXXFLAGS += -m$(bits)
431                 LDFLAGS += -m$(bits)
432         endif
433 endif
434
435 ifeq ($(KERNEL),Darwin)
436         CXXFLAGS += -mmacosx-version-min=10.14
437         LDFLAGS += -mmacosx-version-min=10.14
438         ifneq ($(arch),any)
439                 CXXFLAGS += -arch $(arch)
440                 LDFLAGS += -arch $(arch)
441         endif
442         XCRUN = xcrun
443 endif
444
445 # To cross-compile for Android, NDK version r21 or later is recommended.
446 # In earlier NDK versions, you'll need to pass -fno-addrsig if using GNU binutils.
447 # Currently we don't know how to make PGO builds with the NDK yet.
448 ifeq ($(COMP),ndk)
449         CXXFLAGS += -stdlib=libc++ -fPIE
450         comp=clang
451         ifeq ($(arch),armv7)
452                 CXX=armv7a-linux-androideabi16-clang++
453                 CXXFLAGS += -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=neon
454                 STRIP=arm-linux-androideabi-strip
455         endif
456         ifeq ($(arch),armv8)
457                 CXX=aarch64-linux-android21-clang++
458                 STRIP=aarch64-linux-android-strip
459         endif
460         LDFLAGS += -static-libstdc++ -pie -lm -latomic
461 endif
462
463 ifeq ($(comp),icc)
464         profile_make = icc-profile-make
465         profile_use = icc-profile-use
466 else ifeq ($(comp),clang)
467         profile_make = clang-profile-make
468         profile_use = clang-profile-use
469 else
470         profile_make = gcc-profile-make
471         profile_use = gcc-profile-use
472         ifeq ($(KERNEL),Darwin)
473                 EXTRAPROFILEFLAGS = -fvisibility=hidden
474         endif
475 endif
476
477 ### Travis CI script uses COMPILER to overwrite CXX
478 ifdef COMPILER
479         COMPCXX=$(COMPILER)
480 endif
481
482 ### Allow overwriting CXX from command line
483 ifdef COMPCXX
484         CXX=$(COMPCXX)
485 endif
486
487 ### Sometimes gcc is really clang
488 ifeq ($(COMP),gcc)
489         gccversion = $(shell $(CXX) --version)
490         gccisclang = $(findstring clang,$(gccversion))
491         ifneq ($(gccisclang),)
492                 profile_make = clang-profile-make
493                 profile_use = clang-profile-use
494         endif
495 endif
496
497 ### On mingw use Windows threads, otherwise POSIX
498 ifneq ($(comp),mingw)
499         CXXFLAGS += -DUSE_PTHREADS
500         # On Android Bionic's C library comes with its own pthread implementation bundled in
501         ifneq ($(OS),Android)
502                 # Haiku has pthreads in its libroot, so only link it in on other platforms
503                 ifneq ($(KERNEL),Haiku)
504                         ifneq ($(COMP),ndk)
505                                 LDFLAGS += -lpthread
506                         endif
507                 endif
508         endif
509 endif
510
511 ### 3.2.1 Debugging
512 ifeq ($(debug),no)
513         CXXFLAGS += -DNDEBUG
514 else
515         CXXFLAGS += -g
516 endif
517
518 ### 3.2.2 Debugging with undefined behavior sanitizers
519 ifneq ($(sanitize),none)
520         CXXFLAGS += -g3 $(addprefix -fsanitize=,$(sanitize))
521         LDFLAGS += $(addprefix -fsanitize=,$(sanitize))
522 endif
523
524 ### 3.3 Optimization
525 ifeq ($(optimize),yes)
526
527         CXXFLAGS += -O3
528
529         ifeq ($(comp),gcc)
530                 ifeq ($(OS), Android)
531                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
532                 endif
533         endif
534
535     ifeq ($(KERNEL),Darwin)
536         ifeq ($(comp),$(filter $(comp),clang icc))
537             CXXFLAGS += -mdynamic-no-pic
538         endif
539
540         ifeq ($(comp),gcc)
541             ifneq ($(arch),arm64)
542                 CXXFLAGS += -mdynamic-no-pic
543             endif
544         endif
545     endif
546
547         ifeq ($(comp),clang)
548                 CXXFLAGS += -fexperimental-new-pass-manager
549         endif
550 endif
551
552 ### 3.4 Bits
553 ifeq ($(bits),64)
554         CXXFLAGS += -DIS_64BIT
555 endif
556
557 ### 3.5 prefetch and popcount
558 ifeq ($(prefetch),yes)
559         ifeq ($(sse),yes)
560                 CXXFLAGS += -msse
561         endif
562 else
563         CXXFLAGS += -DNO_PREFETCH
564 endif
565
566 ifeq ($(popcnt),yes)
567         ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8 arm64))
568                 CXXFLAGS += -DUSE_POPCNT
569         else ifeq ($(comp),icc)
570                 CXXFLAGS += -msse3 -DUSE_POPCNT
571         else
572                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
573         endif
574 endif
575
576 ### 3.6 SIMD architectures
577 ifeq ($(avx2),yes)
578         CXXFLAGS += -DUSE_AVX2
579         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
580                 CXXFLAGS += -mavx2
581         endif
582 endif
583
584 ifeq ($(avxvnni),yes)
585         CXXFLAGS += -DUSE_VNNI -DUSE_AVXVNNI
586         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
587                 CXXFLAGS += -mavxvnni
588         endif
589 endif
590
591 ifeq ($(avx512),yes)
592         CXXFLAGS += -DUSE_AVX512
593         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
594                 CXXFLAGS += -mavx512f -mavx512bw
595         endif
596 endif
597
598 ifeq ($(vnni256),yes)
599         CXXFLAGS += -DUSE_VNNI
600         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
601                 CXXFLAGS += -mavx512f -mavx512bw -mavx512vnni -mavx512dq -mavx512vl -mprefer-vector-width=256
602         endif
603 endif
604
605 ifeq ($(vnni512),yes)
606         CXXFLAGS += -DUSE_VNNI
607         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
608                 CXXFLAGS += -mavx512vnni -mavx512dq -mavx512vl
609         endif
610 endif
611
612 ifeq ($(sse41),yes)
613         CXXFLAGS += -DUSE_SSE41
614         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
615                 CXXFLAGS += -msse4.1
616         endif
617 endif
618
619 ifeq ($(ssse3),yes)
620         CXXFLAGS += -DUSE_SSSE3
621         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
622                 CXXFLAGS += -mssse3
623         endif
624 endif
625
626 ifeq ($(sse2),yes)
627         CXXFLAGS += -DUSE_SSE2
628         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
629                 CXXFLAGS += -msse2
630         endif
631 endif
632
633 ifeq ($(mmx),yes)
634         CXXFLAGS += -DUSE_MMX
635         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
636                 CXXFLAGS += -mmmx
637         endif
638 endif
639
640 ifeq ($(neon),yes)
641         CXXFLAGS += -DUSE_NEON=$(arm_version)
642         ifeq ($(KERNEL),Linux)
643         ifneq ($(COMP),ndk)
644         ifneq ($(arch),armv8)
645                 CXXFLAGS += -mfpu=neon
646         endif
647         endif
648         endif
649 endif
650
651 ### 3.7 pext
652 ifeq ($(pext),yes)
653         CXXFLAGS += -DUSE_PEXT
654         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
655                 CXXFLAGS += -mbmi2
656         endif
657 endif
658
659 ### 3.8 Link Time Optimization
660 ### This is a mix of compile and link time options because the lto link phase
661 ### needs access to the optimization flags.
662 ifeq ($(optimize),yes)
663 ifeq ($(debug), no)
664         ifeq ($(comp),clang)
665                 CXXFLAGS += -flto
666                 ifeq ($(target_windows),yes)
667                         CXXFLAGS += -fuse-ld=lld
668                 endif
669                 LDFLAGS += $(CXXFLAGS)
670
671 # GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
672 # GCC on some systems.
673         else ifeq ($(comp),gcc)
674         ifeq ($(gccisclang),)
675                 CXXFLAGS += -flto
676                 LDFLAGS += $(CXXFLAGS) -flto=jobserver
677         else
678                 CXXFLAGS += -flto
679                 LDFLAGS += $(CXXFLAGS)
680         endif
681
682 # To use LTO and static linking on Windows,
683 # the tool chain requires gcc version 10.1 or later.
684         else ifeq ($(comp),mingw)
685         ifneq ($(arch),i386)
686                 CXXFLAGS += -flto
687                 LDFLAGS += $(CXXFLAGS) -save-temps
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.*args*
844         @rm -f stockfish.*lt*
845         @rm -f stockfish.res
846         @rm -f ./-lstdc++.res
847
848 default:
849         help
850
851 ### ==========================================================================
852 ### Section 5. Private Targets
853 ### ==========================================================================
854
855 all: $(EXE) .depend
856
857 config-sanity: net
858         @echo ""
859         @echo "Config:"
860         @echo "debug: '$(debug)'"
861         @echo "sanitize: '$(sanitize)'"
862         @echo "optimize: '$(optimize)'"
863         @echo "arch: '$(arch)'"
864         @echo "bits: '$(bits)'"
865         @echo "kernel: '$(KERNEL)'"
866         @echo "os: '$(OS)'"
867         @echo "prefetch: '$(prefetch)'"
868         @echo "popcnt: '$(popcnt)'"
869         @echo "pext: '$(pext)'"
870         @echo "sse: '$(sse)'"
871         @echo "mmx: '$(mmx)'"
872         @echo "sse2: '$(sse2)'"
873         @echo "ssse3: '$(ssse3)'"
874         @echo "sse41: '$(sse41)'"
875         @echo "avx2: '$(avx2)'"
876         @echo "avxvnni: '$(avxvnni)'"
877         @echo "avx512: '$(avx512)'"
878         @echo "vnni256: '$(vnni256)'"
879         @echo "vnni512: '$(vnni512)'"
880         @echo "neon: '$(neon)'"
881         @echo "arm_version: '$(arm_version)'"
882         @echo ""
883         @echo "Flags:"
884         @echo "CXX: $(CXX)"
885         @echo "CXXFLAGS: $(CXXFLAGS)"
886         @echo "LDFLAGS: $(LDFLAGS)"
887         @echo ""
888         @echo "Testing config sanity. If this fails, try 'make help' ..."
889         @echo ""
890         @test "$(debug)" = "yes" || test "$(debug)" = "no"
891         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
892         @test "$(SUPPORTED_ARCH)" = "true"
893         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
894          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "e2k" || \
895          test "$(arch)" = "armv7" || test "$(arch)" = "armv8" || test "$(arch)" = "arm64"
896         @test "$(bits)" = "32" || test "$(bits)" = "64"
897         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
898         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
899         @test "$(pext)" = "yes" || test "$(pext)" = "no"
900         @test "$(sse)" = "yes" || test "$(sse)" = "no"
901         @test "$(mmx)" = "yes" || test "$(mmx)" = "no"
902         @test "$(sse2)" = "yes" || test "$(sse2)" = "no"
903         @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
904         @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
905         @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
906         @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
907         @test "$(vnni256)" = "yes" || test "$(vnni256)" = "no"
908         @test "$(vnni512)" = "yes" || test "$(vnni512)" = "no"
909         @test "$(neon)" = "yes" || test "$(neon)" = "no"
910         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang" \
911         || test "$(comp)" = "armv7a-linux-androideabi16-clang"  || test "$(comp)" = "aarch64-linux-android21-clang"
912
913 $(EXE): $(OBJS)
914         +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
915
916 clang-profile-make:
917         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
918         EXTRACXXFLAGS='-fprofile-instr-generate ' \
919         EXTRALDFLAGS=' -fprofile-instr-generate' \
920         all
921
922 clang-profile-use:
923         $(XCRUN) llvm-profdata merge -output=stockfish.profdata *.profraw
924         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
925         EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
926         EXTRALDFLAGS='-fprofile-use ' \
927         all
928
929 gcc-profile-make:
930         @mkdir -p profdir
931         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
932         EXTRACXXFLAGS='-fprofile-generate=profdir' \
933         EXTRACXXFLAGS+=$(EXTRAPROFILEFLAGS) \
934         EXTRALDFLAGS='-lgcov' \
935         all
936
937 gcc-profile-use:
938         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
939         EXTRACXXFLAGS='-fprofile-use=profdir -fno-peel-loops -fno-tracer' \
940         EXTRACXXFLAGS+=$(EXTRAPROFILEFLAGS) \
941         EXTRALDFLAGS='-lgcov' \
942         all
943
944 icc-profile-make:
945         @mkdir -p profdir
946         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
947         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
948         all
949
950 icc-profile-use:
951         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
952         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
953         all
954
955 .depend: $(SRCS)
956         -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
957
958 -include .depend