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