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