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