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