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