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