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