]> git.sesse.net Git - stockfish/blob - src/Makefile
Stockfish 6 Release Candidate 1
[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 \
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 $(EXTRACXXFLAGS)
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 += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
154 endif
155
156 ifeq ($(COMP),mingw)
157         comp=mingw
158         CXX=g++
159         CXXFLAGS += -Wextra -Wshadow
160         LDFLAGS += -static-libstdc++ -static-libgcc
161 endif
162
163 ifeq ($(COMP),icc)
164         comp=icc
165         CXX=icpc
166         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
167 endif
168
169 ifeq ($(COMP),clang)
170         comp=clang
171         CXX=clang++
172         CXXFLAGS += -pedantic -Wno-long-long -Wextra -Wshadow
173 endif
174
175 ifeq ($(comp),icc)
176         profile_prepare = icc-profile-prepare
177         profile_make = icc-profile-make
178         profile_use = icc-profile-use
179         profile_clean = icc-profile-clean
180 else
181         profile_prepare = gcc-profile-prepare
182         profile_make = gcc-profile-make
183         profile_use = gcc-profile-use
184         profile_clean = gcc-profile-clean
185 endif
186
187 ifeq ($(UNAME),Darwin)
188         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.6
189         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.6
190 endif
191
192 ### On mingw use Windows threads, otherwise POSIX
193 ifneq ($(comp),mingw)
194         # On Android Bionic's C library comes with its own pthread implementation bundled in
195         ifneq ($(arch),armv7)
196                 # Haiku has pthreads in its libroot, so only link it in on other platforms
197                 ifneq ($(UNAME),Haiku)
198                         LDFLAGS += -lpthread
199                 endif
200         endif
201 endif
202
203 ### 3.4 Debugging
204 ifeq ($(debug),no)
205         CXXFLAGS += -DNDEBUG
206 else
207         CXXFLAGS += -g
208 endif
209
210 ### 3.5 Optimization
211 ifeq ($(optimize),yes)
212
213         ifeq ($(comp),gcc)
214                 CXXFLAGS += -O3
215
216                 ifeq ($(UNAME),Darwin)
217                         ifeq ($(arch),i386)
218                                 CXXFLAGS += -mdynamic-no-pic
219                         endif
220                         ifeq ($(arch),x86_64)
221                                 CXXFLAGS += -mdynamic-no-pic
222                         endif
223                 endif
224
225                 ifeq ($(arch),armv7)
226                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
227                 endif
228         endif
229
230         ifeq ($(comp),mingw)
231                 CXXFLAGS += -O3
232         endif
233
234         ifeq ($(comp),icc)
235                 ifeq ($(UNAME),Darwin)
236                         CXXFLAGS += -fast -mdynamic-no-pic
237                 else
238                         CXXFLAGS += -fast
239                 endif
240         endif
241
242         ifeq ($(comp),clang)
243                 CXXFLAGS += -O3
244
245                 ifeq ($(UNAME),Darwin)
246                         ifeq ($(pext),no)
247                                 CXXFLAGS += -flto
248                                 LDFLAGS += $(CXXFLAGS)
249                         endif
250                         ifeq ($(arch),i386)
251                                 CXXFLAGS += -mdynamic-no-pic
252                         endif
253                         ifeq ($(arch),x86_64)
254                                 CXXFLAGS += -mdynamic-no-pic
255                         endif
256                 endif
257         endif
258 endif
259
260 ### 3.6. Bits
261 ifeq ($(bits),64)
262         CXXFLAGS += -DIS_64BIT
263 endif
264
265 ### 3.7 prefetch
266 ifeq ($(prefetch),yes)
267         ifeq ($(sse),yes)
268                 CXXFLAGS += -msse
269                 DEPENDFLAGS += -msse
270         endif
271 else
272         CXXFLAGS += -DNO_PREFETCH
273 endif
274
275 ### 3.8 bsfq
276 ifeq ($(bsfq),yes)
277         CXXFLAGS += -DUSE_BSFQ
278 endif
279
280 ### 3.9 popcnt
281 ifeq ($(popcnt),yes)
282         ifeq ($(comp),icc)
283                 CXXFLAGS += -msse3 -DUSE_POPCNT
284         else
285                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
286         endif
287 endif
288
289 ### 3.10 pext
290 ifeq ($(pext),yes)
291         CXXFLAGS += -DUSE_PEXT
292         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
293                 CXXFLAGS += -mbmi -mbmi2
294         endif
295 endif
296
297 ### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw.
298 ### This is a mix of compile and link time options because the lto link phase
299 ### needs access to the optimization flags.
300 ifeq ($(comp),gcc)
301         ifeq ($(optimize),yes)
302         ifeq ($(debug),no)
303                 GCC_MAJOR := `$(CXX) -dumpversion | cut -f1 -d.`
304                 GCC_MINOR := `$(CXX) -dumpversion | cut -f2 -d.`
305                 ifeq (1,$(shell expr \( $(GCC_MAJOR) \> 4 \) \| \( $(GCC_MAJOR) \= 4 \& $(GCC_MINOR) \>= 5 \)))
306                         CXXFLAGS += -flto
307                         LDFLAGS += $(CXXFLAGS)
308                 endif
309         endif
310         endif
311 endif
312
313 ### 3.12 Android 5 can only run position independent executables. Note that this
314 ### breaks Android 4.0 and earlier.
315 ifeq ($(arch),armv7)
316         CXXFLAGS += -fPIE
317         LDFLAGS += -fPIE -pie
318 endif
319
320
321 ### ==========================================================================
322 ### Section 4. Public targets
323 ### ==========================================================================
324
325 help:
326         @echo ""
327         @echo "To compile stockfish, type: "
328         @echo ""
329         @echo "make target ARCH=arch [COMP=comp]"
330         @echo ""
331         @echo "Supported targets:"
332         @echo ""
333         @echo "build                   > Standard build"
334         @echo "profile-build           > PGO build"
335         @echo "strip                   > Strip executable"
336         @echo "install                 > Install executable"
337         @echo "clean                   > Clean up"
338         @echo ""
339         @echo "Supported archs:"
340         @echo ""
341         @echo "x86-64                  > x86 64-bit"
342         @echo "x86-64-modern           > x86 64-bit with popcnt support"
343         @echo "x86-64-bmi2             > x86 64-bit with pext support"
344         @echo "x86-32                  > x86 32-bit with SSE support"
345         @echo "x86-32-old              > x86 32-bit fall back for old hardware"
346         @echo "ppc-64                  > PPC 64-bit"
347         @echo "ppc-32                  > PPC 32-bit"
348         @echo "armv7                   > ARMv7 32-bit"
349         @echo "general-64              > unspecified 64-bit"
350         @echo "general-32              > unspecified 32-bit"
351         @echo ""
352         @echo "Supported compilers:"
353         @echo ""
354         @echo "gcc                     > Gnu compiler (default)"
355         @echo "mingw                   > Gnu compiler with MinGW under Windows"
356         @echo "clang                   > LLVM Clang compiler"
357         @echo "icc                     > Intel compiler"
358         @echo ""
359         @echo "Examples. If you don't know what to do, you likely want to run: "
360         @echo ""
361         @echo "make build ARCH=x86-64    (This is for 64-bit systems)"
362         @echo "make build ARCH=x86-32    (This is for 32-bit systems)"
363         @echo ""
364
365 .PHONY: build profile-build
366 build:
367         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
368         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
369
370 profile-build:
371         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
372         @echo ""
373         @echo "Step 0/4. Preparing for profile build."
374         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
375         @echo ""
376         @echo "Step 1/4. Building executable for benchmark ..."
377         @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
378         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
379         @echo ""
380         @echo "Step 2/4. Running benchmark for pgo-build ..."
381         @$(PGOBENCH) > /dev/null
382         @echo ""
383         @echo "Step 3/4. Building final executable ..."
384         @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
385         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
386         @echo ""
387         @echo "Step 4/4. Deleting profile data ..."
388         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
389
390 strip:
391         strip $(EXE)
392
393 install:
394         -mkdir -p -m 755 $(BINDIR)
395         -cp $(EXE) $(BINDIR)
396         -strip $(BINDIR)/$(EXE)
397
398 clean:
399         $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
400
401 default:
402         help
403
404 ### ==========================================================================
405 ### Section 5. Private targets
406 ### ==========================================================================
407
408 all: $(EXE) .depend
409
410 config-sanity:
411         @echo ""
412         @echo "Config:"
413         @echo "debug: '$(debug)'"
414         @echo "optimize: '$(optimize)'"
415         @echo "arch: '$(arch)'"
416         @echo "bits: '$(bits)'"
417         @echo "prefetch: '$(prefetch)'"
418         @echo "bsfq: '$(bsfq)'"
419         @echo "popcnt: '$(popcnt)'"
420         @echo "sse: '$(sse)'"
421         @echo "pext: '$(pext)'"
422         @echo ""
423         @echo "Flags:"
424         @echo "CXX: $(CXX)"
425         @echo "CXXFLAGS: $(CXXFLAGS)"
426         @echo "LDFLAGS: $(LDFLAGS)"
427         @echo ""
428         @echo "Testing config sanity. If this fails, try 'make help' ..."
429         @echo ""
430         @test "$(debug)" = "yes" || test "$(debug)" = "no"
431         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
432         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
433          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
434         @test "$(bits)" = "32" || test "$(bits)" = "64"
435         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
436         @test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
437         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
438         @test "$(sse)" = "yes" || test "$(sse)" = "no"
439         @test "$(pext)" = "yes" || test "$(pext)" = "no"
440         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
441
442 $(EXE): $(OBJS)
443         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
444
445 gcc-profile-prepare:
446         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
447
448 gcc-profile-make:
449         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
450         EXTRACXXFLAGS='-fprofile-generate' \
451         EXTRALDFLAGS='-lgcov' \
452         all
453
454 gcc-profile-use:
455 # Deleting corrupt ucioption.gc* profile files is necessary to avoid an 
456 # "internal compiler error" for gcc versions 4.7.x
457         @rm -f ucioption.gc*
458         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
459         EXTRACXXFLAGS='-fprofile-use' \
460         EXTRALDFLAGS='-lgcov' \
461         all
462
463 gcc-profile-clean:
464         @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
465
466 icc-profile-prepare:
467         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
468         @mkdir profdir
469
470 icc-profile-make:
471         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
472         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
473         all
474
475 icc-profile-use:
476         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
477         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
478         all
479
480 icc-profile-clean:
481         @rm -rf profdir bench.txt
482
483 .depend:
484         -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
485
486 -include .depend
487