]> git.sesse.net Git - stockfish/blob - src/Makefile
Disable spinlocks
[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 #
5 # Stockfish is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Stockfish is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 ### ==========================================================================
20 ### Section 1. General Configuration
21 ### ==========================================================================
22
23 ### Establish the operating system name
24 UNAME = $(shell uname)
25
26 ### Executable name
27 EXE = stockfish
28
29 ### Installation dir definitions
30 PREFIX = /usr/local
31 BINDIR = $(PREFIX)/bin
32
33 ### Built-in benchmark for pgo-builds
34 PGOBENCH = ./$(EXE) bench 16 1 1000 default time
35
36 ### Object files
37 OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
38         material.o misc.o movegen.o movepick.o pawns.o position.o \
39         search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
40
41 ### ==========================================================================
42 ### Section 2. High-level Configuration
43 ### ==========================================================================
44 #
45 # flag                --- Comp switch --- Description
46 # ----------------------------------------------------------------------------
47 #
48 # debug = yes/no      --- -DNDEBUG         --- Enable/Disable debug mode
49 # optimize = yes/no   --- (-O3/-fast etc.) --- Enable/Disable optimizations
50 # arch = (name)       --- (-arch)          --- Target architecture
51 # bits = 64/32        --- -DIS_64BIT       --- 64-/32-bit operating system
52 # prefetch = yes/no   --- -DUSE_PREFETCH   --- Use prefetch x86 asm-instruction
53 # bsfq = yes/no       --- -DUSE_BSFQ       --- Use bsfq x86_64 asm-instruction (only
54 #                                              with GCC and ICC 64-bit)
55 # popcnt = yes/no     --- -DUSE_POPCNT     --- Use popcnt x86_64 asm-instruction
56 # sse = yes/no        --- -msse            --- Use Intel Streaming SIMD Extensions
57 # pext = yes/no       --- -DUSE_PEXT       --- Use pext x86_64 asm-instruction
58 #
59 # Note that Makefile is space sensitive, so when adding new architectures
60 # or modifying existing flags, you have to make sure there are no extra spaces
61 # at the end of the line for flag values.
62
63 ### 2.1. General and architecture defaults
64 optimize = yes
65 debug = no
66 bits = 32
67 prefetch = no
68 bsfq = no
69 popcnt = no
70 sse = no
71 pext = no
72
73 ### 2.2 Architecture specific
74
75 ifeq ($(ARCH),general-32)
76         arch = any
77 endif
78
79 ifeq ($(ARCH),x86-32-old)
80         arch = i386
81 endif
82
83 ifeq ($(ARCH),x86-32)
84         arch = i386
85         prefetch = yes
86         sse = yes
87 endif
88
89 ifeq ($(ARCH),general-64)
90         arch = any
91         bits = 64
92 endif
93
94 ifeq ($(ARCH),x86-64)
95         arch = x86_64
96         bits = 64
97         prefetch = yes
98         bsfq = yes
99         sse = yes
100 endif
101
102 ifeq ($(ARCH),x86-64-modern)
103         arch = x86_64
104         bits = 64
105         prefetch = yes
106         bsfq = yes
107         popcnt = yes
108         sse = yes
109 endif
110
111 ifeq ($(ARCH),x86-64-bmi2)
112         arch = x86_64
113         bits = 64
114         prefetch = yes
115         bsfq = yes
116         popcnt = yes
117         sse = yes
118         pext = yes
119 endif
120
121 ifeq ($(ARCH),armv7)
122         arch = armv7
123         prefetch = yes
124         bsfq = yes
125 endif
126
127 ifeq ($(ARCH),ppc-32)
128         arch = ppc
129 endif
130
131 ifeq ($(ARCH),ppc-64)
132         arch = ppc64
133         bits = 64
134 endif
135
136
137 ### ==========================================================================
138 ### Section 3. Low-level configuration
139 ### ==========================================================================
140
141 ### 3.1 Selecting compiler (default = gcc)
142
143 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS)
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 -Wno-long-long -Wextra -Wshadow
154         LDFLAGS += -Wl,--no-as-needed
155 endif
156
157 ifeq ($(COMP),mingw)
158         comp=mingw
159         CXX=g++
160         CXXFLAGS += -Wextra -Wshadow
161         LDFLAGS += -static-libstdc++ -static-libgcc
162 endif
163
164 ifeq ($(COMP),icc)
165         comp=icc
166         CXX=icpc
167         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
168 endif
169
170 ifeq ($(COMP),clang)
171         comp=clang
172         CXX=clang++
173         CXXFLAGS += -pedantic -Wno-long-long -Wextra -Wshadow
174         ifeq ($(UNAME),Darwin)
175                 CXXFLAGS += -std=c++0x -stdlib=libc++
176         endif
177 endif
178
179 ifeq ($(comp),icc)
180         profile_prepare = icc-profile-prepare
181         profile_make = icc-profile-make
182         profile_use = icc-profile-use
183         profile_clean = icc-profile-clean
184 else
185         profile_prepare = gcc-profile-prepare
186         profile_make = gcc-profile-make
187         profile_use = gcc-profile-use
188         profile_clean = gcc-profile-clean
189 endif
190
191 ifeq ($(UNAME),Darwin)
192         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.10
193         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.10
194 endif
195
196 ### On mingw use Windows threads, otherwise POSIX
197 ifneq ($(comp),mingw)
198         # On Android Bionic's C library comes with its own pthread implementation bundled in
199         ifneq ($(arch),armv7)
200                 # Haiku has pthreads in its libroot, so only link it in on other platforms
201                 ifneq ($(UNAME),Haiku)
202                         LDFLAGS += -lpthread
203                 endif
204         endif
205 endif
206
207 ### 3.4 Debugging
208 ifeq ($(debug),no)
209         CXXFLAGS += -DNDEBUG
210 else
211         CXXFLAGS += -g
212 endif
213
214 ### 3.5 Optimization
215 ifeq ($(optimize),yes)
216
217         ifeq ($(comp),gcc)
218                 CXXFLAGS += -O3
219
220                 ifeq ($(UNAME),Darwin)
221                         ifeq ($(arch),i386)
222                                 CXXFLAGS += -mdynamic-no-pic
223                         endif
224                         ifeq ($(arch),x86_64)
225                                 CXXFLAGS += -mdynamic-no-pic
226                         endif
227                 endif
228
229                 ifeq ($(arch),armv7)
230                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
231                 endif
232         endif
233
234         ifeq ($(comp),mingw)
235                 CXXFLAGS += -O3
236         endif
237
238         ifeq ($(comp),icc)
239                 ifeq ($(UNAME),Darwin)
240                         CXXFLAGS += -fast -mdynamic-no-pic
241                 else
242                         CXXFLAGS += -fast
243                 endif
244         endif
245
246         ifeq ($(comp),clang)
247                 CXXFLAGS += -O3
248
249                 ifeq ($(UNAME),Darwin)
250                         ifeq ($(pext),no)
251                                 CXXFLAGS += -flto
252                                 LDFLAGS += $(CXXFLAGS)
253                         endif
254                         ifeq ($(arch),i386)
255                                 CXXFLAGS += -mdynamic-no-pic
256                         endif
257                         ifeq ($(arch),x86_64)
258                                 CXXFLAGS += -mdynamic-no-pic
259                         endif
260                 endif
261         endif
262 endif
263
264 ### 3.6. Bits
265 ifeq ($(bits),64)
266         CXXFLAGS += -DIS_64BIT
267 endif
268
269 ### 3.7 prefetch
270 ifeq ($(prefetch),yes)
271         ifeq ($(sse),yes)
272                 CXXFLAGS += -msse
273                 DEPENDFLAGS += -msse
274         endif
275 else
276         CXXFLAGS += -DNO_PREFETCH
277 endif
278
279 ### 3.8 bsfq
280 ifeq ($(bsfq),yes)
281         CXXFLAGS += -DUSE_BSFQ
282 endif
283
284 ### 3.9 popcnt
285 ifeq ($(popcnt),yes)
286         ifeq ($(comp),icc)
287                 CXXFLAGS += -msse3 -DUSE_POPCNT
288         else
289                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
290         endif
291 endif
292
293 ### 3.10 pext
294 ifeq ($(pext),yes)
295         CXXFLAGS += -DUSE_PEXT
296         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
297                 CXXFLAGS += -mbmi -mbmi2
298         endif
299 endif
300
301 ### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw.
302 ### This is a mix of compile and link time options because the lto link phase
303 ### needs access to the optimization flags.
304 ifeq ($(comp),gcc)
305         ifeq ($(optimize),yes)
306         ifeq ($(debug),no)
307                 GCC_MAJOR := `$(CXX) -dumpversion | cut -f1 -d.`
308                 GCC_MINOR := `$(CXX) -dumpversion | cut -f2 -d.`
309                 ifeq (1,$(shell expr \( $(GCC_MAJOR) \> 4 \) \| \( $(GCC_MAJOR) \= 4 \& $(GCC_MINOR) \>= 5 \)))
310                         CXXFLAGS += -flto
311                         LDFLAGS += $(CXXFLAGS)
312                 endif
313         endif
314         endif
315 endif
316
317 ### 3.12 Android 5 can only run position independent executables. Note that this
318 ### breaks Android 4.0 and earlier.
319 ifeq ($(arch),armv7)
320         CXXFLAGS += -fPIE
321         LDFLAGS += -fPIE -pie
322 endif
323
324
325 ### ==========================================================================
326 ### Section 4. Public targets
327 ### ==========================================================================
328
329 help:
330         @echo ""
331         @echo "To compile stockfish, type: "
332         @echo ""
333         @echo "make target ARCH=arch [COMP=comp]"
334         @echo ""
335         @echo "Supported targets:"
336         @echo ""
337         @echo "build                   > Standard build"
338         @echo "profile-build           > PGO build"
339         @echo "strip                   > Strip executable"
340         @echo "install                 > Install executable"
341         @echo "clean                   > Clean up"
342         @echo ""
343         @echo "Supported archs:"
344         @echo ""
345         @echo "x86-64                  > x86 64-bit"
346         @echo "x86-64-modern           > x86 64-bit with popcnt support"
347         @echo "x86-64-bmi2             > x86 64-bit with pext support"
348         @echo "x86-32                  > x86 32-bit with SSE support"
349         @echo "x86-32-old              > x86 32-bit fall back for old hardware"
350         @echo "ppc-64                  > PPC 64-bit"
351         @echo "ppc-32                  > PPC 32-bit"
352         @echo "armv7                   > ARMv7 32-bit"
353         @echo "general-64              > unspecified 64-bit"
354         @echo "general-32              > unspecified 32-bit"
355         @echo ""
356         @echo "Supported compilers:"
357         @echo ""
358         @echo "gcc                     > Gnu compiler (default)"
359         @echo "mingw                   > Gnu compiler with MinGW under Windows"
360         @echo "clang                   > LLVM Clang compiler"
361         @echo "icc                     > Intel compiler"
362         @echo ""
363         @echo "Examples. If you don't know what to do, you likely want to run: "
364         @echo ""
365         @echo "make build ARCH=x86-64    (This is for 64-bit systems)"
366         @echo "make build ARCH=x86-32    (This is for 32-bit systems)"
367         @echo ""
368
369 .PHONY: build profile-build
370 build:
371         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
372         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
373
374 profile-build:
375         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
376         @echo ""
377         @echo "Step 0/4. Preparing for profile build."
378         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
379         @echo ""
380         @echo "Step 1/4. Building executable for benchmark ..."
381         @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
382         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
383         @echo ""
384         @echo "Step 2/4. Running benchmark for pgo-build ..."
385         @$(PGOBENCH) > /dev/null
386         @echo ""
387         @echo "Step 3/4. Building final executable ..."
388         @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
389         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
390         @echo ""
391         @echo "Step 4/4. Deleting profile data ..."
392         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
393
394 strip:
395         strip $(EXE)
396
397 install:
398         -mkdir -p -m 755 $(BINDIR)
399         -cp $(EXE) $(BINDIR)
400         -strip $(BINDIR)/$(EXE)
401
402 clean:
403         $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
404
405 default:
406         help
407
408 ### ==========================================================================
409 ### Section 5. Private targets
410 ### ==========================================================================
411
412 all: $(EXE) .depend
413
414 config-sanity:
415         @echo ""
416         @echo "Config:"
417         @echo "debug: '$(debug)'"
418         @echo "optimize: '$(optimize)'"
419         @echo "arch: '$(arch)'"
420         @echo "bits: '$(bits)'"
421         @echo "prefetch: '$(prefetch)'"
422         @echo "bsfq: '$(bsfq)'"
423         @echo "popcnt: '$(popcnt)'"
424         @echo "sse: '$(sse)'"
425         @echo "pext: '$(pext)'"
426         @echo ""
427         @echo "Flags:"
428         @echo "CXX: $(CXX)"
429         @echo "CXXFLAGS: $(CXXFLAGS)"
430         @echo "LDFLAGS: $(LDFLAGS)"
431         @echo ""
432         @echo "Testing config sanity. If this fails, try 'make help' ..."
433         @echo ""
434         @test "$(debug)" = "yes" || test "$(debug)" = "no"
435         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
436         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
437          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
438         @test "$(bits)" = "32" || test "$(bits)" = "64"
439         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
440         @test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
441         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
442         @test "$(sse)" = "yes" || test "$(sse)" = "no"
443         @test "$(pext)" = "yes" || test "$(pext)" = "no"
444         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
445
446 $(EXE): $(OBJS)
447         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
448
449 gcc-profile-prepare:
450         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
451
452 gcc-profile-make:
453         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
454         EXTRACXXFLAGS='-fprofile-generate' \
455         EXTRALDFLAGS='-lgcov' \
456         all
457
458 gcc-profile-use:
459         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
460         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
461         EXTRALDFLAGS='-lgcov' \
462         all
463
464 gcc-profile-clean:
465         @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
466
467 icc-profile-prepare:
468         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
469         @mkdir profdir
470
471 icc-profile-make:
472         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
473         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
474         all
475
476 icc-profile-use:
477         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
478         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
479         all
480
481 icc-profile-clean:
482         @rm -rf profdir bench.txt
483
484 .depend:
485         -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
486
487 -include .depend
488