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