]> git.sesse.net Git - stockfish/blob - src/Makefile
Support ARCH=armv8 in Makefile (#2355)
[stockfish] / src / Makefile
1 # Stockfish, a UCI chess playing engine derived from Glaurung 2.1
2 # Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
3 # Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
4 # Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
5 #
6 # Stockfish is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Stockfish is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 ### ==========================================================================
21 ### Section 1. General Configuration
22 ### ==========================================================================
23
24 ### Executable name
25 ifeq ($(COMP),mingw)
26 EXE = stockfish.exe
27 else
28 EXE = stockfish
29 endif
30
31 ### Installation dir definitions
32 PREFIX = /usr/local
33 BINDIR = $(PREFIX)/bin
34
35 ### Built-in benchmark for pgo-builds
36 PGOBENCH = ./$(EXE) bench
37
38 ### Source and object files
39 SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
40         material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
41         search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp
42
43 OBJS = $(notdir $(SRCS:.cpp=.o))
44
45 VPATH = syzygy
46
47 ### Establish the operating system name
48 KERNEL = $(shell uname -s)
49 ifeq ($(KERNEL),Linux)
50         OS = $(shell uname -o)
51 endif
52
53 ### ==========================================================================
54 ### Section 2. High-level Configuration
55 ### ==========================================================================
56 #
57 # flag                --- Comp switch --- Description
58 # ----------------------------------------------------------------------------
59 #
60 # debug = yes/no      --- -DNDEBUG         --- Enable/Disable debug mode
61 # sanitize = undefined/thread/no (-fsanitize )
62 #                     --- ( undefined )    --- enable undefined behavior checks
63 #                     --- ( thread    )    --- enable threading error  checks
64 # optimize = yes/no   --- (-O3/-fast etc.) --- Enable/Disable optimizations
65 # arch = (name)       --- (-arch)          --- Target architecture
66 # bits = 64/32        --- -DIS_64BIT       --- 64-/32-bit operating system
67 # prefetch = yes/no   --- -DUSE_PREFETCH   --- Use prefetch asm-instruction
68 # popcnt = yes/no     --- -DUSE_POPCNT     --- Use popcnt asm-instruction
69 # sse = yes/no        --- -msse            --- Use Intel Streaming SIMD Extensions
70 # pext = yes/no       --- -DUSE_PEXT       --- Use pext x86_64 asm-instruction
71 #
72 # Note that Makefile is space sensitive, so when adding new architectures
73 # or modifying existing flags, you have to make sure there are no extra spaces
74 # at the end of the line for flag values.
75
76 ### 2.1. General and architecture defaults
77 optimize = yes
78 debug = no
79 sanitize = no
80 bits = 32
81 prefetch = no
82 popcnt = no
83 sse = no
84 pext = no
85
86 ### 2.2 Architecture specific
87
88 ifeq ($(ARCH),general-32)
89         arch = any
90 endif
91
92 ifeq ($(ARCH),x86-32-old)
93         arch = i386
94 endif
95
96 ifeq ($(ARCH),x86-32)
97         arch = i386
98         prefetch = yes
99         sse = yes
100 endif
101
102 ifeq ($(ARCH),general-64)
103         arch = any
104         bits = 64
105 endif
106
107 ifeq ($(ARCH),x86-64)
108         arch = x86_64
109         bits = 64
110         prefetch = yes
111         sse = yes
112 endif
113
114 ifeq ($(ARCH),x86-64-modern)
115         arch = x86_64
116         bits = 64
117         prefetch = yes
118         popcnt = yes
119         sse = yes
120 endif
121
122 ifeq ($(ARCH),x86-64-bmi2)
123         arch = x86_64
124         bits = 64
125         prefetch = yes
126         popcnt = yes
127         sse = yes
128         pext = yes
129 endif
130
131 ifeq ($(ARCH),armv7)
132         arch = armv7
133         prefetch = yes
134 endif
135
136 ifeq ($(ARCH),armv8)
137         arch = armv8-a
138         bits = 64
139         prefetch = yes
140 endif
141
142 ifeq ($(ARCH),ppc-32)
143         arch = ppc
144 endif
145
146 ifeq ($(ARCH),ppc-64)
147         arch = ppc64
148         bits = 64
149         popcnt = yes
150         prefetch = yes
151 endif
152
153
154 ### ==========================================================================
155 ### Section 3. Low-level configuration
156 ### ==========================================================================
157
158 ### 3.1 Selecting compiler (default = gcc)
159
160 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
161 DEPENDFLAGS += -std=c++11
162 LDFLAGS += $(EXTRALDFLAGS)
163
164 ifeq ($(COMP),)
165         COMP=gcc
166 endif
167
168 ifeq ($(COMP),gcc)
169         comp=gcc
170         CXX=g++
171         CXXFLAGS += -pedantic -Wextra -Wshadow
172
173         ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
174                 ifeq ($(OS),Android)
175                         CXXFLAGS += -m$(bits)
176                         LDFLAGS += -m$(bits)
177                 endif
178         else
179                 CXXFLAGS += -m$(bits)
180                 LDFLAGS += -m$(bits)
181         endif
182
183         ifneq ($(KERNEL),Darwin)
184            LDFLAGS += -Wl,--no-as-needed
185         endif
186 endif
187
188 ifeq ($(COMP),mingw)
189         comp=mingw
190
191         ifeq ($(KERNEL),Linux)
192                 ifeq ($(bits),64)
193                         ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
194                                 CXX=x86_64-w64-mingw32-c++
195                         else
196                                 CXX=x86_64-w64-mingw32-c++-posix
197                         endif
198                 else
199                         ifeq ($(shell which i686-w64-mingw32-c++-posix),)
200                                 CXX=i686-w64-mingw32-c++
201                         else
202                                 CXX=i686-w64-mingw32-c++-posix
203                         endif
204                 endif
205         else
206                 CXX=g++
207         endif
208
209         CXXFLAGS += -Wextra -Wshadow
210         LDFLAGS += -static
211 endif
212
213 ifeq ($(COMP),icc)
214         comp=icc
215         CXX=icpc
216         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
217 endif
218
219 ifeq ($(COMP),clang)
220         comp=clang
221         CXX=clang++
222         CXXFLAGS += -pedantic -Wextra -Wshadow
223
224         ifneq ($(KERNEL),Darwin)
225         ifneq ($(KERNEL),OpenBSD)
226                 LDFLAGS += -latomic
227         endif
228         endif
229
230         ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
231                 ifeq ($(OS),Android)
232                         CXXFLAGS += -m$(bits)
233                         LDFLAGS += -m$(bits)
234                 endif
235         else
236                 CXXFLAGS += -m$(bits)
237                 LDFLAGS += -m$(bits)
238         endif
239 endif
240
241 ifeq ($(comp),icc)
242         profile_make = icc-profile-make
243         profile_use = icc-profile-use
244 else
245 ifeq ($(comp),clang)
246         profile_make = clang-profile-make
247         profile_use = clang-profile-use
248 else
249         profile_make = gcc-profile-make
250         profile_use = gcc-profile-use
251 endif
252 endif
253
254 ifeq ($(KERNEL),Darwin)
255         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
256         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
257 endif
258
259 ### Travis CI script uses COMPILER to overwrite CXX
260 ifdef COMPILER
261         COMPCXX=$(COMPILER)
262 endif
263
264 ### Allow overwriting CXX from command line
265 ifdef COMPCXX
266         CXX=$(COMPCXX)
267 endif
268
269 ### On mingw use Windows threads, otherwise POSIX
270 ifneq ($(comp),mingw)
271         # On Android Bionic's C library comes with its own pthread implementation bundled in
272         ifneq ($(OS),Android)
273                 # Haiku has pthreads in its libroot, so only link it in on other platforms
274                 ifneq ($(KERNEL),Haiku)
275                         LDFLAGS += -lpthread
276                 endif
277         endif
278 endif
279
280 ### 3.2.1 Debugging
281 ifeq ($(debug),no)
282         CXXFLAGS += -DNDEBUG
283 else
284         CXXFLAGS += -g
285 endif
286
287 ### 3.2.2 Debugging with undefined behavior sanitizers
288 ifneq ($(sanitize),no)
289         CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
290         LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
291 endif
292
293 ### 3.3 Optimization
294 ifeq ($(optimize),yes)
295
296         CXXFLAGS += -O3
297
298         ifeq ($(comp),gcc)
299                 ifeq ($(OS), Android)
300                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
301                 endif
302         endif
303
304         ifeq ($(comp),$(filter $(comp),gcc clang icc))
305                 ifeq ($(KERNEL),Darwin)
306                         CXXFLAGS += -mdynamic-no-pic
307                 endif
308         endif
309 endif
310
311 ### 3.4 Bits
312 ifeq ($(bits),64)
313         CXXFLAGS += -DIS_64BIT
314 endif
315
316 ### 3.5 prefetch
317 ifeq ($(prefetch),yes)
318         ifeq ($(sse),yes)
319                 CXXFLAGS += -msse
320                 DEPENDFLAGS += -msse
321         endif
322 else
323         CXXFLAGS += -DNO_PREFETCH
324 endif
325
326 ### 3.6 popcnt
327 ifeq ($(popcnt),yes)
328         ifeq ($(arch),ppc64)
329                 CXXFLAGS += -DUSE_POPCNT
330         else ifeq ($(comp),icc)
331                 CXXFLAGS += -msse3 -DUSE_POPCNT
332         else
333                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
334         endif
335 endif
336
337 ### 3.7 pext
338 ifeq ($(pext),yes)
339         CXXFLAGS += -DUSE_PEXT
340         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
341                 CXXFLAGS += -msse4 -mbmi2
342         endif
343 endif
344
345 ### 3.8 Link Time Optimization
346 ### This is a mix of compile and link time options because the lto link phase
347 ### needs access to the optimization flags.
348 ifeq ($(optimize),yes)
349 ifeq ($(debug), no)
350         ifeq ($(comp),$(filter $(comp),gcc clang))
351                 CXXFLAGS += -flto
352                 LDFLAGS += $(CXXFLAGS)
353         endif
354
355         ifeq ($(comp),mingw)
356         ifeq ($(KERNEL),Linux)
357                 CXXFLAGS += -flto
358                 LDFLAGS += $(CXXFLAGS)
359         endif
360         endif
361 endif
362 endif
363
364 ### 3.9 Android 5 can only run position independent executables. Note that this
365 ### breaks Android 4.0 and earlier.
366 ifeq ($(OS), Android)
367         CXXFLAGS += -fPIE
368         LDFLAGS += -fPIE -pie
369 endif
370
371
372 ### ==========================================================================
373 ### Section 4. Public targets
374 ### ==========================================================================
375
376 help:
377         @echo ""
378         @echo "To compile stockfish, type: "
379         @echo ""
380         @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
381         @echo ""
382         @echo "Supported targets:"
383         @echo ""
384         @echo "build                   > Standard build"
385         @echo "profile-build           > PGO build"
386         @echo "strip                   > Strip executable"
387         @echo "install                 > Install executable"
388         @echo "clean                   > Clean up"
389         @echo ""
390         @echo "Supported archs:"
391         @echo ""
392         @echo "x86-64-bmi2             > x86 64-bit with pext support (also enables SSE4)"
393         @echo "x86-64-modern           > x86 64-bit with popcnt support (also enables SSE3)"
394         @echo "x86-64                  > x86 64-bit generic"
395         @echo "x86-32                  > x86 32-bit (also enables SSE)"
396         @echo "x86-32-old              > x86 32-bit fall back for old hardware"
397         @echo "ppc-64                  > PPC 64-bit"
398         @echo "ppc-32                  > PPC 32-bit"
399         @echo "armv7                   > ARMv7 32-bit"
400         @echo "armv8                   > ARMv8 64-bit"
401         @echo "general-64              > unspecified 64-bit"
402         @echo "general-32              > unspecified 32-bit"
403         @echo ""
404         @echo "Supported compilers:"
405         @echo ""
406         @echo "gcc                     > Gnu compiler (default)"
407         @echo "mingw                   > Gnu compiler with MinGW under Windows"
408         @echo "clang                   > LLVM Clang compiler"
409         @echo "icc                     > Intel compiler"
410         @echo ""
411         @echo "Simple examples. If you don't know what to do, you likely want to run: "
412         @echo ""
413         @echo "make build ARCH=x86-64    (This is for 64-bit systems)"
414         @echo "make build ARCH=x86-32    (This is for 32-bit systems)"
415         @echo ""
416         @echo "Advanced examples, for experienced users: "
417         @echo ""
418         @echo "make build ARCH=x86-64 COMP=clang"
419         @echo "make profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-4.8"
420         @echo ""
421
422
423 .PHONY: help build profile-build strip install clean objclean profileclean \
424         config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
425         clang-profile-use clang-profile-make
426
427 build: config-sanity
428         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
429
430 profile-build: config-sanity objclean profileclean
431         @echo ""
432         @echo "Step 1/4. Building instrumented executable ..."
433         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
434         @echo ""
435         @echo "Step 2/4. Running benchmark for pgo-build ..."
436         $(PGOBENCH) > /dev/null
437         @echo ""
438         @echo "Step 3/4. Building optimized executable ..."
439         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
440         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
441         @echo ""
442         @echo "Step 4/4. Deleting profile data ..."
443         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
444
445 strip:
446         strip $(EXE)
447
448 install:
449         -mkdir -p -m 755 $(BINDIR)
450         -cp $(EXE) $(BINDIR)
451         -strip $(BINDIR)/$(EXE)
452
453 #clean all
454 clean: objclean profileclean
455         @rm -f .depend *~ core
456
457 # clean binaries and objects
458 objclean:
459         @rm -f $(EXE) *.o ./syzygy/*.o
460
461 # clean auxiliary profiling files
462 profileclean:
463         @rm -rf profdir
464         @rm -f bench.txt *.gcda *.gcno
465         @rm -f stockfish.profdata *.profraw
466
467 default:
468         help
469
470 ### ==========================================================================
471 ### Section 5. Private targets
472 ### ==========================================================================
473
474 all: $(EXE) .depend
475
476 config-sanity:
477         @echo ""
478         @echo "Config:"
479         @echo "debug: '$(debug)'"
480         @echo "sanitize: '$(sanitize)'"
481         @echo "optimize: '$(optimize)'"
482         @echo "arch: '$(arch)'"
483         @echo "bits: '$(bits)'"
484         @echo "kernel: '$(KERNEL)'"
485         @echo "os: '$(OS)'"
486         @echo "prefetch: '$(prefetch)'"
487         @echo "popcnt: '$(popcnt)'"
488         @echo "sse: '$(sse)'"
489         @echo "pext: '$(pext)'"
490         @echo ""
491         @echo "Flags:"
492         @echo "CXX: $(CXX)"
493         @echo "CXXFLAGS: $(CXXFLAGS)"
494         @echo "LDFLAGS: $(LDFLAGS)"
495         @echo ""
496         @echo "Testing config sanity. If this fails, try 'make help' ..."
497         @echo ""
498         @test "$(debug)" = "yes" || test "$(debug)" = "no"
499         @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
500         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
501         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
502          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
503          test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a"
504         @test "$(bits)" = "32" || test "$(bits)" = "64"
505         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
506         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
507         @test "$(sse)" = "yes" || test "$(sse)" = "no"
508         @test "$(pext)" = "yes" || test "$(pext)" = "no"
509         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
510
511 $(EXE): $(OBJS)
512         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
513
514 clang-profile-make:
515         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
516         EXTRACXXFLAGS='-fprofile-instr-generate ' \
517         EXTRALDFLAGS=' -fprofile-instr-generate' \
518         all
519
520 clang-profile-use:
521         llvm-profdata merge -output=stockfish.profdata *.profraw
522         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
523         EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
524         EXTRALDFLAGS='-fprofile-use ' \
525         all
526
527 gcc-profile-make:
528         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
529         EXTRACXXFLAGS='-fprofile-generate' \
530         EXTRALDFLAGS='-lgcov' \
531         all
532
533 gcc-profile-use:
534         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
535         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
536         EXTRALDFLAGS='-lgcov' \
537         all
538
539 icc-profile-make:
540         @mkdir -p profdir
541         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
542         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
543         all
544
545 icc-profile-use:
546         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
547         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
548         all
549
550 .depend:
551         -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
552
553 -include .depend
554