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