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