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