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