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