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