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