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