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