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