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