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