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