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