]> git.sesse.net Git - stockfish/blob - src/Makefile
Simplify pawns King Safety calculation
[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 += -stdlib=libc++
198                 DEPENDFLAGS += -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         COMPCXX=$(COMPILER)
222 endif
223
224 ### Allow overwriting CXX from command line
225 ifdef COMPCXX
226         CXX=$(COMPCXX)
227 endif
228
229 ### On mingw use Windows threads, otherwise POSIX
230 ifneq ($(comp),mingw)
231         # On Android Bionic's C library comes with its own pthread implementation bundled in
232         ifneq ($(arch),armv7)
233                 # Haiku has pthreads in its libroot, so only link it in on other platforms
234                 ifneq ($(UNAME),Haiku)
235                         LDFLAGS += -lpthread
236                 endif
237         endif
238 endif
239
240 ### 3.4 Debugging
241 ifeq ($(debug),no)
242         CXXFLAGS += -DNDEBUG
243 else
244         CXXFLAGS += -g
245 endif
246
247 ### 3.5 Optimization
248 ifeq ($(optimize),yes)
249
250         ifeq ($(comp),gcc)
251                 CXXFLAGS += -O3
252
253                 ifeq ($(UNAME),Darwin)
254                         ifeq ($(arch),i386)
255                                 CXXFLAGS += -mdynamic-no-pic
256                         endif
257                         ifeq ($(arch),x86_64)
258                                 CXXFLAGS += -mdynamic-no-pic
259                         endif
260                 endif
261
262                 ifeq ($(arch),armv7)
263                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
264                 endif
265         endif
266
267         ifeq ($(comp),mingw)
268                 CXXFLAGS += -O3
269         endif
270
271         ifeq ($(comp),icc)
272                 ifeq ($(UNAME),Darwin)
273                         CXXFLAGS += -fast -mdynamic-no-pic
274                 else
275                         CXXFLAGS += -fast
276                 endif
277         endif
278
279         ifeq ($(comp),clang)
280                 CXXFLAGS += -O3
281
282                 ifeq ($(UNAME),Darwin)
283                         ifeq ($(pext),no)
284                                 CXXFLAGS += -flto
285                                 LDFLAGS += $(CXXFLAGS)
286                         endif
287                         ifeq ($(arch),i386)
288                                 CXXFLAGS += -mdynamic-no-pic
289                         endif
290                         ifeq ($(arch),x86_64)
291                                 CXXFLAGS += -mdynamic-no-pic
292                         endif
293                 endif
294         endif
295 endif
296
297 ### 3.6. Bits
298 ifeq ($(bits),64)
299         CXXFLAGS += -DIS_64BIT
300 endif
301
302 ### 3.7 prefetch
303 ifeq ($(prefetch),yes)
304         ifeq ($(sse),yes)
305                 CXXFLAGS += -msse
306                 DEPENDFLAGS += -msse
307         endif
308 else
309         CXXFLAGS += -DNO_PREFETCH
310 endif
311
312 ### 3.8 bsfq
313 ifeq ($(bsfq),yes)
314         CXXFLAGS += -DUSE_BSFQ
315 endif
316
317 ### 3.9 popcnt
318 ifeq ($(popcnt),yes)
319         ifeq ($(comp),icc)
320                 CXXFLAGS += -msse3 -DUSE_POPCNT
321         else
322                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
323         endif
324 endif
325
326 ### 3.10 pext
327 ifeq ($(pext),yes)
328         CXXFLAGS += -DUSE_PEXT
329         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
330                 CXXFLAGS += -mbmi -mbmi2
331         endif
332 endif
333
334 ### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
335 ### This is a mix of compile and link time options because the lto link phase
336 ### needs access to the optimization flags.
337 ifeq ($(comp),gcc)
338         ifeq ($(optimize),yes)
339         ifeq ($(debug),no)
340                 CXXFLAGS += -flto
341                 LDFLAGS += $(CXXFLAGS)
342         endif
343         endif
344 endif
345
346 ifeq ($(comp),mingw)
347         ifeq ($(UNAME),Linux)
348         ifeq ($(optimize),yes)
349         ifeq ($(debug),no)
350                 CXXFLAGS += -flto
351                 LDFLAGS += $(CXXFLAGS)
352         endif
353         endif
354         endif
355 endif
356
357 ### 3.12 Android 5 can only run position independent executables. Note that this
358 ### breaks Android 4.0 and earlier.
359 ifeq ($(arch),armv7)
360         CXXFLAGS += -fPIE
361         LDFLAGS += -fPIE -pie
362 endif
363
364
365 ### ==========================================================================
366 ### Section 4. Public targets
367 ### ==========================================================================
368
369 help:
370         @echo ""
371         @echo "To compile stockfish, type: "
372         @echo ""
373         @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
374         @echo ""
375         @echo "Supported targets:"
376         @echo ""
377         @echo "build                   > Standard build"
378         @echo "profile-build           > PGO build"
379         @echo "strip                   > Strip executable"
380         @echo "install                 > Install executable"
381         @echo "clean                   > Clean up"
382         @echo ""
383         @echo "Supported archs:"
384         @echo ""
385         @echo "x86-64                  > x86 64-bit"
386         @echo "x86-64-modern           > x86 64-bit with popcnt support"
387         @echo "x86-64-bmi2             > x86 64-bit with pext support"
388         @echo "x86-32                  > x86 32-bit with SSE support"
389         @echo "x86-32-old              > x86 32-bit fall back for old hardware"
390         @echo "ppc-64                  > PPC 64-bit"
391         @echo "ppc-32                  > PPC 32-bit"
392         @echo "armv7                   > ARMv7 32-bit"
393         @echo "general-64              > unspecified 64-bit"
394         @echo "general-32              > unspecified 32-bit"
395         @echo ""
396         @echo "Supported compilers:"
397         @echo ""
398         @echo "gcc                     > Gnu compiler (default)"
399         @echo "mingw                   > Gnu compiler with MinGW under Windows"
400         @echo "clang                   > LLVM Clang compiler"
401         @echo "icc                     > Intel compiler"
402         @echo ""
403         @echo "Simple examples. If you don't know what to do, you likely want to run: "
404         @echo ""
405         @echo "make build ARCH=x86-64    (This is for 64-bit systems)"
406         @echo "make build ARCH=x86-32    (This is for 32-bit systems)"
407         @echo ""
408         @echo "Advanced examples, for experienced users: "
409         @echo ""
410         @echo "make build ARCH=x86-64 COMP=clang"
411         @echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
412         @echo ""
413
414
415 .PHONY: build profile-build
416 build:
417         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
418         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
419
420 profile-build:
421         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
422         @echo ""
423         @echo "Step 0/4. Preparing for profile build."
424         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
425         @echo ""
426         @echo "Step 1/4. Building executable for benchmark ..."
427         @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
428         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
429         @echo ""
430         @echo "Step 2/4. Running benchmark for pgo-build ..."
431         $(PGOBENCH) > /dev/null
432         @echo ""
433         @echo "Step 3/4. Building final executable ..."
434         @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
435         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
436         @echo ""
437         @echo "Step 4/4. Deleting profile data ..."
438         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
439
440 strip:
441         strip $(EXE)
442
443 install:
444         -mkdir -p -m 755 $(BINDIR)
445         -cp $(EXE) $(BINDIR)
446         -strip $(BINDIR)/$(EXE)
447
448 clean:
449         $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
450
451 default:
452         help
453
454 ### ==========================================================================
455 ### Section 5. Private targets
456 ### ==========================================================================
457
458 all: $(EXE) .depend
459
460 config-sanity:
461         @echo ""
462         @echo "Config:"
463         @echo "debug: '$(debug)'"
464         @echo "optimize: '$(optimize)'"
465         @echo "arch: '$(arch)'"
466         @echo "bits: '$(bits)'"
467         @echo "prefetch: '$(prefetch)'"
468         @echo "bsfq: '$(bsfq)'"
469         @echo "popcnt: '$(popcnt)'"
470         @echo "sse: '$(sse)'"
471         @echo "pext: '$(pext)'"
472         @echo ""
473         @echo "Flags:"
474         @echo "CXX: $(CXX)"
475         @echo "CXXFLAGS: $(CXXFLAGS)"
476         @echo "LDFLAGS: $(LDFLAGS)"
477         @echo ""
478         @echo "Testing config sanity. If this fails, try 'make help' ..."
479         @echo ""
480         @test "$(debug)" = "yes" || test "$(debug)" = "no"
481         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
482         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
483          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
484         @test "$(bits)" = "32" || test "$(bits)" = "64"
485         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
486         @test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
487         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
488         @test "$(sse)" = "yes" || test "$(sse)" = "no"
489         @test "$(pext)" = "yes" || test "$(pext)" = "no"
490         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
491
492 $(EXE): $(OBJS)
493         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
494
495 gcc-profile-prepare:
496         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
497
498 gcc-profile-make:
499         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
500         EXTRACXXFLAGS='-fprofile-generate' \
501         EXTRALDFLAGS='-lgcov' \
502         all
503
504 gcc-profile-use:
505         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
506         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
507         EXTRALDFLAGS='-lgcov' \
508         all
509
510 gcc-profile-clean:
511         @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
512
513 icc-profile-prepare:
514         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
515         @mkdir profdir
516
517 icc-profile-make:
518         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
519         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
520         all
521
522 icc-profile-use:
523         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
524         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
525         all
526
527 icc-profile-clean:
528         @rm -rf profdir bench.txt
529
530 .depend:
531         -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
532
533 -include .depend
534