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