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