]> git.sesse.net Git - stockfish/blob - src/Makefile
Simplify away non-normal moves in SEE
[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 KERNEL = $(shell uname -s)
26 ifeq ($(KERNEL),Linux)
27         OS = $(shell uname -o)
28 endif
29
30 ### Executable name
31 EXE = stockfish
32
33 ### Installation dir definitions
34 PREFIX = /usr/local
35 BINDIR = $(PREFIX)/bin
36
37 ### Built-in benchmark for pgo-builds
38 PGOBENCH = ./$(EXE) bench
39
40 ### Object files
41 OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
42         material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
43         search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
44
45 ### ==========================================================================
46 ### Section 2. High-level Configuration
47 ### ==========================================================================
48 #
49 # flag                --- Comp switch --- Description
50 # ----------------------------------------------------------------------------
51 #
52 # debug = yes/no      --- -DNDEBUG         --- Enable/Disable debug mode
53 # sanitize = undefined/thread/no (-fsanitize )
54 #                     --- ( undefined )    --- enable undefined behavior checks
55 #                     --- ( thread    )    --- enable threading error  checks
56 # optimize = yes/no   --- (-O3/-fast etc.) --- Enable/Disable optimizations
57 # arch = (name)       --- (-arch)          --- Target architecture
58 # bits = 64/32        --- -DIS_64BIT       --- 64-/32-bit operating system
59 # prefetch = yes/no   --- -DUSE_PREFETCH   --- Use prefetch asm-instruction
60 # popcnt = yes/no     --- -DUSE_POPCNT     --- Use popcnt asm-instruction
61 # sse = yes/no        --- -msse            --- Use Intel Streaming SIMD Extensions
62 # pext = yes/no       --- -DUSE_PEXT       --- Use pext x86_64 asm-instruction
63 #
64 # Note that Makefile is space sensitive, so when adding new architectures
65 # or modifying existing flags, you have to make sure there are no extra spaces
66 # at the end of the line for flag values.
67
68 ### 2.1. General and architecture defaults
69 optimize = yes
70 debug = no
71 sanitize = no
72 bits = 32
73 prefetch = no
74 popcnt = no
75 sse = no
76 pext = no
77
78 ### 2.2 Architecture specific
79
80 ifeq ($(ARCH),general-32)
81         arch = any
82 endif
83
84 ifeq ($(ARCH),x86-32-old)
85         arch = i386
86 endif
87
88 ifeq ($(ARCH),x86-32)
89         arch = i386
90         prefetch = yes
91         sse = yes
92 endif
93
94 ifeq ($(ARCH),general-64)
95         arch = any
96         bits = 64
97 endif
98
99 ifeq ($(ARCH),x86-64)
100         arch = x86_64
101         bits = 64
102         prefetch = yes
103         sse = yes
104 endif
105
106 ifeq ($(ARCH),x86-64-modern)
107         arch = x86_64
108         bits = 64
109         prefetch = yes
110         popcnt = yes
111         sse = yes
112 endif
113
114 ifeq ($(ARCH),x86-64-bmi2)
115         arch = x86_64
116         bits = 64
117         prefetch = yes
118         popcnt = yes
119         sse = yes
120         pext = yes
121 endif
122
123 ifeq ($(ARCH),armv7)
124         arch = armv7
125         prefetch = 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
157         ifeq ($(ARCH),armv7)
158                 ifeq ($(OS),Android)
159                         CXXFLAGS += -m$(bits)
160                         LDFLAGS += -m$(bits)
161                 endif
162         else
163                 CXXFLAGS += -m$(bits)
164                 LDFLAGS += -m$(bits)
165         endif
166
167         ifneq ($(KERNEL),Darwin)
168            LDFLAGS += -Wl,--no-as-needed
169         endif
170 endif
171
172 ifeq ($(COMP),mingw)
173         comp=mingw
174
175         ifeq ($(KERNEL),Linux)
176                 ifeq ($(bits),64)
177                         ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
178                                 CXX=x86_64-w64-mingw32-c++
179                         else
180                                 CXX=x86_64-w64-mingw32-c++-posix
181                         endif
182                 else
183                         ifeq ($(shell which i686-w64-mingw32-c++-posix),)
184                                 CXX=i686-w64-mingw32-c++
185                         else
186                                 CXX=i686-w64-mingw32-c++-posix
187                         endif
188                 endif
189         else
190                 CXX=g++
191         endif
192
193         CXXFLAGS += -Wextra -Wshadow
194         LDFLAGS += -static
195 endif
196
197 ifeq ($(COMP),icc)
198         comp=icc
199         CXX=icpc
200         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
201 endif
202
203 ifeq ($(COMP),clang)
204         comp=clang
205         CXX=clang++
206         CXXFLAGS += -pedantic -Wextra -Wshadow
207 ifneq ($(KERNEL),Darwin)
208         LDFLAGS += -latomic
209 endif
210
211         ifeq ($(ARCH),armv7)
212                 ifeq ($(OS),Android)
213                         CXXFLAGS += -m$(bits)
214                         LDFLAGS += -m$(bits)
215                 endif
216         else
217                 CXXFLAGS += -m$(bits)
218                 LDFLAGS += -m$(bits)
219         endif
220 endif
221
222 ifeq ($(comp),icc)
223         profile_make = icc-profile-make
224         profile_use = icc-profile-use
225 else
226 ifeq ($(comp),clang)
227         profile_make = clang-profile-make
228         profile_use = clang-profile-use
229 else
230         profile_make = gcc-profile-make
231         profile_use = gcc-profile-use
232 endif
233 endif
234
235 ifeq ($(KERNEL),Darwin)
236         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
237         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
238 endif
239
240 ### Travis CI script uses COMPILER to overwrite CXX
241 ifdef COMPILER
242         COMPCXX=$(COMPILER)
243 endif
244
245 ### Allow overwriting CXX from command line
246 ifdef COMPCXX
247         CXX=$(COMPCXX)
248 endif
249
250 ### On mingw use Windows threads, otherwise POSIX
251 ifneq ($(comp),mingw)
252         # On Android Bionic's C library comes with its own pthread implementation bundled in
253         ifneq ($(OS),Android)
254                 # Haiku has pthreads in its libroot, so only link it in on other platforms
255                 ifneq ($(KERNEL),Haiku)
256                         LDFLAGS += -lpthread
257                 endif
258         endif
259 endif
260
261 ### 3.2.1 Debugging
262 ifeq ($(debug),no)
263         CXXFLAGS += -DNDEBUG
264 else
265         CXXFLAGS += -g
266 endif
267
268 ### 3.2.2 Debugging with undefined behavior sanitizers
269 ifneq ($(sanitize),no)
270         CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
271         LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
272 endif
273
274 ### 3.3 Optimization
275 ifeq ($(optimize),yes)
276
277         CXXFLAGS += -O3
278
279         ifeq ($(comp),gcc)
280
281                 ifeq ($(KERNEL),Darwin)
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
290                 ifeq ($(OS), Android)
291                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
292                 endif
293         endif
294
295         ifeq ($(comp),icc)
296                 ifeq ($(KERNEL),Darwin)
297                         CXXFLAGS += -mdynamic-no-pic
298                 endif
299         endif
300
301         ifeq ($(comp),clang)
302                 ifeq ($(KERNEL),Darwin)
303                                 CXXFLAGS += -flto
304                                 LDFLAGS += $(CXXFLAGS)
305                         ifeq ($(arch),i386)
306                                 CXXFLAGS += -mdynamic-no-pic
307                         endif
308                         ifeq ($(arch),x86_64)
309                                 CXXFLAGS += -mdynamic-no-pic
310                         endif
311                 endif
312         endif
313 endif
314
315 ### 3.4 Bits
316 ifeq ($(bits),64)
317         CXXFLAGS += -DIS_64BIT
318 endif
319
320 ### 3.5 prefetch
321 ifeq ($(prefetch),yes)
322         ifeq ($(sse),yes)
323                 CXXFLAGS += -msse
324                 DEPENDFLAGS += -msse
325         endif
326 else
327         CXXFLAGS += -DNO_PREFETCH
328 endif
329
330 ### 3.6 popcnt
331 ifeq ($(popcnt),yes)
332         ifeq ($(comp),icc)
333                 CXXFLAGS += -msse3 -DUSE_POPCNT
334         else
335                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
336         endif
337 endif
338
339 ### 3.7 pext
340 ifeq ($(pext),yes)
341         CXXFLAGS += -DUSE_PEXT
342         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
343                 CXXFLAGS += -mbmi2
344         endif
345 endif
346
347 ### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
348 ### This is a mix of compile and link time options because the lto link phase
349 ### needs access to the optimization flags.
350 ifeq ($(comp),gcc)
351         ifeq ($(optimize),yes)
352         ifeq ($(debug),no)
353                 CXXFLAGS += -flto
354                 LDFLAGS += $(CXXFLAGS)
355         endif
356         endif
357 endif
358
359 ifeq ($(comp),mingw)
360         ifeq ($(KERNEL),Linux)
361         ifeq ($(optimize),yes)
362         ifeq ($(debug),no)
363                 CXXFLAGS += -flto
364                 LDFLAGS += $(CXXFLAGS)
365         endif
366         endif
367         endif
368 endif
369
370 ### 3.9 Android 5 can only run position independent executables. Note that this
371 ### breaks Android 4.0 and earlier.
372 ifeq ($(OS), Android)
373         CXXFLAGS += -fPIE
374         LDFLAGS += -fPIE -pie
375 endif
376
377
378 ### ==========================================================================
379 ### Section 4. Public targets
380 ### ==========================================================================
381
382 help:
383         @echo ""
384         @echo "To compile stockfish, type: "
385         @echo ""
386         @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
387         @echo ""
388         @echo "Supported targets:"
389         @echo ""
390         @echo "build                   > Standard build"
391         @echo "profile-build           > PGO build"
392         @echo "strip                   > Strip executable"
393         @echo "install                 > Install executable"
394         @echo "clean                   > Clean up"
395         @echo ""
396         @echo "Supported archs:"
397         @echo ""
398         @echo "x86-64                  > x86 64-bit"
399         @echo "x86-64-modern           > x86 64-bit with popcnt support"
400         @echo "x86-64-bmi2             > x86 64-bit with pext support"
401         @echo "x86-32                  > x86 32-bit with SSE support"
402         @echo "x86-32-old              > x86 32-bit fall back for old hardware"
403         @echo "ppc-64                  > PPC 64-bit"
404         @echo "ppc-32                  > PPC 32-bit"
405         @echo "armv7                   > ARMv7 32-bit"
406         @echo "general-64              > unspecified 64-bit"
407         @echo "general-32              > unspecified 32-bit"
408         @echo ""
409         @echo "Supported compilers:"
410         @echo ""
411         @echo "gcc                     > Gnu compiler (default)"
412         @echo "mingw                   > Gnu compiler with MinGW under Windows"
413         @echo "clang                   > LLVM Clang compiler"
414         @echo "icc                     > Intel compiler"
415         @echo ""
416         @echo "Simple examples. If you don't know what to do, you likely want to run: "
417         @echo ""
418         @echo "make build ARCH=x86-64    (This is for 64-bit systems)"
419         @echo "make build ARCH=x86-32    (This is for 32-bit systems)"
420         @echo ""
421         @echo "Advanced examples, for experienced users: "
422         @echo ""
423         @echo "make build ARCH=x86-64 COMP=clang"
424         @echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
425         @echo ""
426
427
428 .PHONY: help build profile-build strip install clean objclean profileclean help \
429         config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
430         clang-profile-use clang-profile-make
431
432 build: config-sanity
433         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
434
435 profile-build: config-sanity objclean profileclean
436         @echo ""
437         @echo "Step 1/4. Building instrumented executable ..."
438         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
439         @echo ""
440         @echo "Step 2/4. Running benchmark for pgo-build ..."
441         $(PGOBENCH) > /dev/null
442         @echo ""
443         @echo "Step 3/4. Building optimized executable ..."
444         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
445         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
446         @echo ""
447         @echo "Step 4/4. Deleting profile data ..."
448         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
449
450 strip:
451         strip $(EXE)
452
453 install:
454         -mkdir -p -m 755 $(BINDIR)
455         -cp $(EXE) $(BINDIR)
456         -strip $(BINDIR)/$(EXE)
457
458 #clean all
459 clean: objclean profileclean
460         @rm -f .depend *~ core
461
462 # clean binaries and objects
463 objclean:
464         @rm -f $(EXE) $(EXE).exe *.o ./syzygy/*.o
465
466 # clean auxiliary profiling files
467 profileclean:
468         @rm -rf profdir
469         @rm -f bench.txt *.gcda ./syzygy/*.gcda *.gcno ./syzygy/*.gcno
470         @rm -f stockfish.profdata *.profraw
471
472 default:
473         help
474
475 ### ==========================================================================
476 ### Section 5. Private targets
477 ### ==========================================================================
478
479 all: $(EXE) .depend
480
481 config-sanity:
482         @echo ""
483         @echo "Config:"
484         @echo "debug: '$(debug)'"
485         @echo "sanitize: '$(sanitize)'"
486         @echo "optimize: '$(optimize)'"
487         @echo "arch: '$(arch)'"
488         @echo "bits: '$(bits)'"
489         @echo "kernel: '$(KERNEL)'"
490         @echo "os: '$(OS)'"
491         @echo "prefetch: '$(prefetch)'"
492         @echo "popcnt: '$(popcnt)'"
493         @echo "sse: '$(sse)'"
494         @echo "pext: '$(pext)'"
495         @echo ""
496         @echo "Flags:"
497         @echo "CXX: $(CXX)"
498         @echo "CXXFLAGS: $(CXXFLAGS)"
499         @echo "LDFLAGS: $(LDFLAGS)"
500         @echo ""
501         @echo "Testing config sanity. If this fails, try 'make help' ..."
502         @echo ""
503         @test "$(debug)" = "yes" || test "$(debug)" = "no"
504         @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "no"
505         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
506         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
507          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
508         @test "$(bits)" = "32" || test "$(bits)" = "64"
509         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
510         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
511         @test "$(sse)" = "yes" || test "$(sse)" = "no"
512         @test "$(pext)" = "yes" || test "$(pext)" = "no"
513         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
514
515 $(EXE): $(OBJS)
516         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
517
518 clang-profile-make:
519         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
520         EXTRACXXFLAGS='-fprofile-instr-generate ' \
521         EXTRALDFLAGS=' -fprofile-instr-generate' \
522         all
523
524 clang-profile-use:
525         llvm-profdata merge -output=stockfish.profdata *.profraw
526         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
527         EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
528         EXTRALDFLAGS='-fprofile-use ' \
529         all
530
531 gcc-profile-make:
532         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
533         EXTRACXXFLAGS='-fprofile-generate' \
534         EXTRALDFLAGS='-lgcov' \
535         all
536
537 gcc-profile-use:
538         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
539         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
540         EXTRALDFLAGS='-lgcov' \
541         all
542
543 icc-profile-make:
544         @mkdir -p profdir
545         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
546         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
547         all
548
549 icc-profile-use:
550         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
551         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
552         all
553
554 .depend:
555         -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
556
557 -include .depend
558