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