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