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