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