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