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