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