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