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