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