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