]> git.sesse.net Git - stockfish/blob - src/Makefile
75bbbd6c40b19609e6c36c93357ea877c6b96399
[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-2014 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 1 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 # os = (name)         ---                  --- Target operating system
52 # bits = 64/32        --- -DIS_64BIT       --- 64-/32-bit operating system
53 # prefetch = yes/no   --- -DUSE_PREFETCH   --- Use prefetch x86 asm-instruction
54 # bsfq = yes/no       --- -DUSE_BSFQ       --- Use bsfq x86_64 asm-instruction (only
55 #                                              with GCC and ICC 64-bit)
56 # popcnt = yes/no     --- -DUSE_POPCNT     --- Use popcnt x86_64 asm-instruction
57 # sse = yes/no        --- -msse            --- Use Intel Streaming SIMD Extensions
58 # pext = yes/no       --- -DUSE_PEXT       --- Use pext x86_64 asm-instruction
59 #
60 # Note that Makefile is space sensitive, so when adding new architectures
61 # or modifying existing flags, you have to make sure there are no extra spaces
62 # at the end of the line for flag values.
63
64 ### 2.1. General and architecture defaults
65 optimize = yes
66 debug = no
67 os = any
68 bits = 32
69 prefetch = no
70 bsfq = no
71 popcnt = no
72 sse = no
73 pext = no
74
75 ### 2.2 Architecture specific
76
77 ifeq ($(ARCH),general-32)
78         arch = any
79 endif
80
81 ifeq ($(ARCH),x86-32-old)
82         arch = i386
83 endif
84
85 ifeq ($(ARCH),x86-32)
86         arch = i386
87         prefetch = yes
88         sse = yes
89 endif
90
91 ifeq ($(ARCH),general-64)
92         arch = any
93         bits = 64
94 endif
95
96 ifeq ($(ARCH),x86-64)
97         arch = x86_64
98         bits = 64
99         prefetch = yes
100         bsfq = yes
101         sse = yes
102 endif
103
104 ifeq ($(ARCH),x86-64-modern)
105         arch = x86_64
106         bits = 64
107         prefetch = yes
108         bsfq = yes
109         popcnt = yes
110         sse = yes
111 endif
112
113 ifeq ($(ARCH),x86-64-bmi2)
114         arch = x86_64
115         bits = 64
116         prefetch = yes
117         bsfq = yes
118         popcnt = yes
119         sse = yes
120         pext = yes
121 endif
122
123 ifeq ($(ARCH),armv7)
124         arch = armv7
125         prefetch = yes
126         bsfq = yes
127 endif
128
129 ifeq ($(ARCH),ppc-32)
130         arch = ppc
131 endif
132
133 ifeq ($(ARCH),ppc-64)
134         arch = ppc64
135         bits = 64
136 endif
137
138
139 ### ==========================================================================
140 ### Section 3. Low-level configuration
141 ### ==========================================================================
142
143 ### 3.1 Selecting compiler (default = gcc)
144
145 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
146 LDFLAGS += $(EXTRALDFLAGS)
147
148 ifeq ($(COMP),)
149         COMP=gcc
150 endif
151
152 ifeq ($(COMP),gcc)
153         comp=gcc
154         CXX=g++
155         CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
156 endif
157
158 ifeq ($(COMP),mingw)
159         comp=mingw
160         CXX=g++
161         CXXFLAGS += -Wextra -Wshadow
162         LDFLAGS += -static-libstdc++ -static-libgcc
163 endif
164
165 ifeq ($(COMP),icc)
166         comp=icc
167         CXX=icpc
168         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
169 endif
170
171 ifeq ($(COMP),clang)
172         comp=clang
173         CXX=clang++
174         CXXFLAGS += -pedantic -Wno-long-long -Wextra -Wshadow
175 endif
176
177 ifeq ($(comp),icc)
178         profile_prepare = icc-profile-prepare
179         profile_make = icc-profile-make
180         profile_use = icc-profile-use
181         profile_clean = icc-profile-clean
182 else
183         profile_prepare = gcc-profile-prepare
184         profile_make = gcc-profile-make
185         profile_use = gcc-profile-use
186         profile_clean = gcc-profile-clean
187 endif
188
189 ifeq ($(UNAME),Darwin)
190         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.6
191         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.6
192 endif
193
194 ### On mingw use Windows threads, otherwise POSIX
195 ifneq ($(comp),mingw)
196         # On Android Bionic's C library comes with its own pthread implementation bundled in
197         ifneq ($(arch),armv7)
198                 # Haiku has pthreads in its libroot, so only link it in on other platforms
199                 ifneq ($(UNAME),Haiku)
200                         LDFLAGS += -lpthread
201                 endif
202         endif
203 endif
204
205 ### 3.4 Debugging
206 ifeq ($(debug),no)
207         CXXFLAGS += -DNDEBUG
208 else
209         CXXFLAGS += -g
210 endif
211
212 ### 3.5 Optimization
213 ifeq ($(optimize),yes)
214
215         ifeq ($(comp),gcc)
216                 CXXFLAGS += -O3
217
218                 ifeq ($(UNAME),Darwin)
219                         ifeq ($(arch),i386)
220                                 CXXFLAGS += -mdynamic-no-pic
221                         endif
222                         ifeq ($(arch),x86_64)
223                                 CXXFLAGS += -mdynamic-no-pic
224                         endif
225                 endif
226
227                 ifeq ($(arch),armv7)
228                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
229                 endif
230         endif
231
232         ifeq ($(comp),mingw)
233                 CXXFLAGS += -O3
234         endif
235
236         ifeq ($(comp),icc)
237                 ifeq ($(UNAME),Darwin)
238                         CXXFLAGS += -fast -mdynamic-no-pic
239                 else
240                         CXXFLAGS += -fast
241                 endif
242         endif
243
244         ifeq ($(comp),clang)
245                 CXXFLAGS += -O3
246
247                 ifeq ($(UNAME),Darwin)
248                         ifeq ($(pext),no)
249                                 CXXFLAGS += -flto
250                                 LDFLAGS += $(CXXFLAGS)
251                         endif
252                         ifeq ($(arch),i386)
253                                 CXXFLAGS += -mdynamic-no-pic
254                         endif
255                         ifeq ($(arch),x86_64)
256                                 CXXFLAGS += -mdynamic-no-pic
257                         endif
258                 endif
259         endif
260 endif
261
262 ### 3.6. Bits
263 ifeq ($(bits),64)
264         CXXFLAGS += -DIS_64BIT
265 endif
266
267 ### 3.7 prefetch
268 ifeq ($(prefetch),yes)
269         ifeq ($(sse),yes)
270                 CXXFLAGS += -msse
271                 DEPENDFLAGS += -msse
272         endif
273 else
274         CXXFLAGS += -DNO_PREFETCH
275 endif
276
277 ### 3.8 bsfq
278 ifeq ($(bsfq),yes)
279         CXXFLAGS += -DUSE_BSFQ
280 endif
281
282 ### 3.9 popcnt
283 ifeq ($(popcnt),yes)
284         ifeq ($(comp),icc)
285                 CXXFLAGS += -msse3 -DUSE_POPCNT
286         else
287                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
288         endif
289 endif
290
291 ### 3.10 pext
292 ifeq ($(pext),yes)
293         CXXFLAGS += -DUSE_PEXT
294         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
295                 CXXFLAGS += -mbmi -mbmi2
296         endif
297 endif
298
299 ### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw.
300 ### This is a mix of compile and link time options because the lto link phase
301 ### needs access to the optimization flags.
302 ifeq ($(comp),gcc)
303         ifeq ($(optimize),yes)
304         ifeq ($(debug),no)
305                 GCC_MAJOR := `$(CXX) -dumpversion | cut -f1 -d.`
306                 GCC_MINOR := `$(CXX) -dumpversion | cut -f2 -d.`
307                 ifeq (1,$(shell expr \( $(GCC_MAJOR) \> 4 \) \| \( $(GCC_MAJOR) \= 4 \& $(GCC_MINOR) \>= 5 \)))
308                         CXXFLAGS += -flto
309                         LDFLAGS += $(CXXFLAGS)
310                 endif
311         endif
312         endif
313 endif
314
315 ### ==========================================================================
316 ### Section 4. Public targets
317 ### ==========================================================================
318
319 help:
320         @echo ""
321         @echo "To compile stockfish, type: "
322         @echo ""
323         @echo "make target ARCH=arch [COMP=comp]"
324         @echo ""
325         @echo "Supported targets:"
326         @echo ""
327         @echo "build                   > Standard build"
328         @echo "profile-build           > PGO build"
329         @echo "strip                   > Strip executable"
330         @echo "install                 > Install executable"
331         @echo "clean                   > Clean up"
332         @echo ""
333         @echo "Supported archs:"
334         @echo ""
335         @echo "x86-64                  > x86 64-bit"
336         @echo "x86-64-modern           > x86 64-bit with popcnt support"
337         @echo "x86-64-bmi2             > x86 64-bit with pext support"
338         @echo "x86-32                  > x86 32-bit with SSE support"
339         @echo "x86-32-old              > x86 32-bit fall back for old hardware"
340         @echo "ppc-64                  > PPC 64-bit"
341         @echo "ppc-32                  > PPC 32-bit"
342         @echo "armv7                   > ARMv7 32-bit"
343         @echo "general-64              > unspecified 64-bit"
344         @echo "general-32              > unspecified 32-bit"
345         @echo ""
346         @echo "Supported compilers:"
347         @echo ""
348         @echo "gcc                     > Gnu compiler (default)"
349         @echo "mingw                   > Gnu compiler with MinGW under Windows"
350         @echo "clang                   > LLVM Clang compiler"
351         @echo "icc                     > Intel compiler"
352         @echo ""
353         @echo "Examples. If you don't know what to do, you likely want to run: "
354         @echo ""
355         @echo "make build ARCH=x86-64    (This is for 64-bit systems)"
356         @echo "make build ARCH=x86-32    (This is for 32-bit systems)"
357         @echo ""
358
359 .PHONY: build profile-build
360 build:
361         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
362         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
363
364 profile-build:
365         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
366         @echo ""
367         @echo "Step 0/4. Preparing for profile build."
368         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
369         @echo ""
370         @echo "Step 1/4. Building executable for benchmark ..."
371         @touch *.cpp *.h
372         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
373         @echo ""
374         @echo "Step 2/4. Running benchmark for pgo-build ..."
375         @$(PGOBENCH) > /dev/null
376         @echo ""
377         @echo "Step 3/4. Building final executable ..."
378         @touch *.cpp
379         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
380         @echo ""
381         @echo "Step 4/4. Deleting profile data ..."
382         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
383
384 strip:
385         strip $(EXE)
386
387 install:
388         -mkdir -p -m 755 $(BINDIR)
389         -cp $(EXE) $(BINDIR)
390         -strip $(BINDIR)/$(EXE)
391
392 clean:
393         $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
394
395 default:
396         help
397
398 ### ==========================================================================
399 ### Section 5. Private targets
400 ### ==========================================================================
401
402 all: $(EXE) .depend
403
404 config-sanity:
405         @echo ""
406         @echo "Config:"
407         @echo "debug: '$(debug)'"
408         @echo "optimize: '$(optimize)'"
409         @echo "arch: '$(arch)'"
410         @echo "os: '$(os)'"
411         @echo "bits: '$(bits)'"
412         @echo "prefetch: '$(prefetch)'"
413         @echo "bsfq: '$(bsfq)'"
414         @echo "popcnt: '$(popcnt)'"
415         @echo "sse: '$(sse)'"
416         @echo "pext: '$(pext)'"
417         @echo ""
418         @echo "Flags:"
419         @echo "CXX: $(CXX)"
420         @echo "CXXFLAGS: $(CXXFLAGS)"
421         @echo "LDFLAGS: $(LDFLAGS)"
422         @echo ""
423         @echo "Testing config sanity. If this fails, try 'make help' ..."
424         @echo ""
425         @test "$(debug)" = "yes" || test "$(debug)" = "no"
426         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
427         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
428          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
429         @test "$(os)" = "any"
430         @test "$(bits)" = "32" || test "$(bits)" = "64"
431         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
432         @test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
433         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
434         @test "$(sse)" = "yes" || test "$(sse)" = "no"
435         @test "$(pext)" = "yes" || test "$(pext)" = "no"
436         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
437
438 $(EXE): $(OBJS)
439         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
440
441 gcc-profile-prepare:
442         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
443
444 gcc-profile-make:
445         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
446         EXTRACXXFLAGS='-fprofile-generate' \
447         EXTRALDFLAGS='-lgcov' \
448         all
449
450 gcc-profile-use:
451         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
452         EXTRACXXFLAGS='-fprofile-use' \
453         EXTRALDFLAGS='-lgcov' \
454         all
455
456 gcc-profile-clean:
457         @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
458
459 icc-profile-prepare:
460         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
461         @mkdir profdir
462
463 icc-profile-make:
464         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
465         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
466         all
467
468 icc-profile-use:
469         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
470         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
471         all
472
473 icc-profile-clean:
474         @rm -rf profdir bench.txt
475
476 .depend:
477         -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
478
479 -include .depend
480