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