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