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