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