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