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