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