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