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