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