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