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