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