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