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