Fix compiling of 32 bit binary on 64-bit Windows
[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 psqt.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 DEPENDFLAGS += -std=c++11
145 LDFLAGS += $(EXTRALDFLAGS)
146
147 ifeq ($(COMP),)
148         COMP=gcc
149 endif
150
151 ifeq ($(COMP),gcc)
152         comp=gcc
153         CXX=g++
154         CXXFLAGS += -pedantic -Wextra -Wshadow
155         ifneq ($(UNAME),Darwin)
156            LDFLAGS += -Wl,--no-as-needed
157         endif
158 endif
159
160 ifeq ($(COMP),mingw)
161         comp=mingw
162
163         ifeq ($(bits),64)
164                 ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
165                         CXX=x86_64-w64-mingw32-c++
166                 else
167                         CXX=x86_64-w64-mingw32-c++-posix
168                 endif
169         else
170                 ifeq ($(shell which i686-w64-mingw32-c++-posix),)
171                         CXX=i686-w64-mingw32-c++
172                 else
173                         CXX=i686-w64-mingw32-c++-posix
174                 endif
175         endif
176
177         CXXFLAGS += -Wextra -Wshadow
178         LDFLAGS += -static
179 endif
180
181 ifeq ($(COMP),icc)
182         comp=icc
183         CXX=icpc
184         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
185 endif
186
187 ifeq ($(COMP),clang)
188         comp=clang
189         CXX=clang++
190         CXXFLAGS += -pedantic -Wextra -Wshadow
191         ifeq ($(UNAME),Darwin)
192                 CXXFLAGS += -std=c++0x -stdlib=libc++
193                 DEPENDFLAGS += -std=c++0x -stdlib=libc++
194         endif
195 endif
196
197 ifeq ($(comp),icc)
198         profile_prepare = icc-profile-prepare
199         profile_make = icc-profile-make
200         profile_use = icc-profile-use
201         profile_clean = icc-profile-clean
202 else
203         profile_prepare = gcc-profile-prepare
204         profile_make = gcc-profile-make
205         profile_use = gcc-profile-use
206         profile_clean = gcc-profile-clean
207 endif
208
209 ifeq ($(UNAME),Darwin)
210         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
211         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
212 endif
213
214 ### Travis CI script uses COMPILER to overwrite CXX
215 ifdef COMPILER
216         CXX=$(COMPILER)
217 endif
218
219 ### On mingw use Windows threads, otherwise POSIX
220 ifneq ($(comp),mingw)
221         # On Android Bionic's C library comes with its own pthread implementation bundled in
222         ifneq ($(arch),armv7)
223                 # Haiku has pthreads in its libroot, so only link it in on other platforms
224                 ifneq ($(UNAME),Haiku)
225                         LDFLAGS += -lpthread
226                 endif
227         endif
228 endif
229
230 ### 3.4 Debugging
231 ifeq ($(debug),no)
232         CXXFLAGS += -DNDEBUG
233 else
234         CXXFLAGS += -g
235 endif
236
237 ### 3.5 Optimization
238 ifeq ($(optimize),yes)
239
240         ifeq ($(comp),gcc)
241                 CXXFLAGS += -O3
242
243                 ifeq ($(UNAME),Darwin)
244                         ifeq ($(arch),i386)
245                                 CXXFLAGS += -mdynamic-no-pic
246                         endif
247                         ifeq ($(arch),x86_64)
248                                 CXXFLAGS += -mdynamic-no-pic
249                         endif
250                 endif
251
252                 ifeq ($(arch),armv7)
253                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
254                 endif
255         endif
256
257         ifeq ($(comp),mingw)
258                 CXXFLAGS += -O3
259         endif
260
261         ifeq ($(comp),icc)
262                 ifeq ($(UNAME),Darwin)
263                         CXXFLAGS += -fast -mdynamic-no-pic
264                 else
265                         CXXFLAGS += -fast
266                 endif
267         endif
268
269         ifeq ($(comp),clang)
270                 CXXFLAGS += -O3
271
272                 ifeq ($(UNAME),Darwin)
273                         ifeq ($(pext),no)
274                                 CXXFLAGS += -flto
275                                 LDFLAGS += $(CXXFLAGS)
276                         endif
277                         ifeq ($(arch),i386)
278                                 CXXFLAGS += -mdynamic-no-pic
279                         endif
280                         ifeq ($(arch),x86_64)
281                                 CXXFLAGS += -mdynamic-no-pic
282                         endif
283                 endif
284         endif
285 endif
286
287 ### 3.6. Bits
288 ifeq ($(bits),64)
289         CXXFLAGS += -DIS_64BIT
290 endif
291
292 ### 3.7 prefetch
293 ifeq ($(prefetch),yes)
294         ifeq ($(sse),yes)
295                 CXXFLAGS += -msse
296                 DEPENDFLAGS += -msse
297         endif
298 else
299         CXXFLAGS += -DNO_PREFETCH
300 endif
301
302 ### 3.8 bsfq
303 ifeq ($(bsfq),yes)
304         CXXFLAGS += -DUSE_BSFQ
305 endif
306
307 ### 3.9 popcnt
308 ifeq ($(popcnt),yes)
309         ifeq ($(comp),icc)
310                 CXXFLAGS += -msse3 -DUSE_POPCNT
311         else
312                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
313         endif
314 endif
315
316 ### 3.10 pext
317 ifeq ($(pext),yes)
318         CXXFLAGS += -DUSE_PEXT
319         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
320                 CXXFLAGS += -mbmi -mbmi2
321         endif
322 endif
323
324 ### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
325 ### This is a mix of compile and link time options because the lto link phase
326 ### needs access to the optimization flags.
327 ifeq ($(comp),gcc)
328         ifeq ($(optimize),yes)
329         ifeq ($(debug),no)
330                 CXXFLAGS += -flto
331                 LDFLAGS += $(CXXFLAGS)
332         endif
333         endif
334 endif
335
336 ifeq ($(comp),mingw)
337         ifeq ($(UNAME),Linux)
338         ifeq ($(optimize),yes)
339         ifeq ($(debug),no)
340                 CXXFLAGS += -flto
341                 LDFLAGS += $(CXXFLAGS)
342         endif
343         endif
344         endif
345 endif
346
347 ### 3.12 Android 5 can only run position independent executables. Note that this
348 ### breaks Android 4.0 and earlier.
349 ifeq ($(arch),armv7)
350         CXXFLAGS += -fPIE
351         LDFLAGS += -fPIE -pie
352 endif
353
354
355 ### ==========================================================================
356 ### Section 4. Public targets
357 ### ==========================================================================
358
359 help:
360         @echo ""
361         @echo "To compile stockfish, type: "
362         @echo ""
363         @echo "make target ARCH=arch [COMP=comp]"
364         @echo ""
365         @echo "Supported targets:"
366         @echo ""
367         @echo "build                   > Standard build"
368         @echo "profile-build           > PGO build"
369         @echo "strip                   > Strip executable"
370         @echo "install                 > Install executable"
371         @echo "clean                   > Clean up"
372         @echo ""
373         @echo "Supported archs:"
374         @echo ""
375         @echo "x86-64                  > x86 64-bit"
376         @echo "x86-64-modern           > x86 64-bit with popcnt support"
377         @echo "x86-64-bmi2             > x86 64-bit with pext support"
378         @echo "x86-32                  > x86 32-bit with SSE support"
379         @echo "x86-32-old              > x86 32-bit fall back for old hardware"
380         @echo "ppc-64                  > PPC 64-bit"
381         @echo "ppc-32                  > PPC 32-bit"
382         @echo "armv7                   > ARMv7 32-bit"
383         @echo "general-64              > unspecified 64-bit"
384         @echo "general-32              > unspecified 32-bit"
385         @echo ""
386         @echo "Supported compilers:"
387         @echo ""
388         @echo "gcc                     > Gnu compiler (default)"
389         @echo "mingw                   > Gnu compiler with MinGW under Windows"
390         @echo "clang                   > LLVM Clang compiler"
391         @echo "icc                     > Intel compiler"
392         @echo ""
393         @echo "Examples. If you don't know what to do, you likely want to run: "
394         @echo ""
395         @echo "make build ARCH=x86-64    (This is for 64-bit systems)"
396         @echo "make build ARCH=x86-32    (This is for 32-bit systems)"
397         @echo ""
398
399 .PHONY: build profile-build
400 build:
401         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
402         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
403
404 profile-build:
405         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
406         @echo ""
407         @echo "Step 0/4. Preparing for profile build."
408         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
409         @echo ""
410         @echo "Step 1/4. Building executable for benchmark ..."
411         @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
412         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
413         @echo ""
414         @echo "Step 2/4. Running benchmark for pgo-build ..."
415         $(PGOBENCH) > /dev/null
416         @echo ""
417         @echo "Step 3/4. Building final executable ..."
418         @touch *.cpp *.h syzygy/*.cpp syzygy/*.h
419         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
420         @echo ""
421         @echo "Step 4/4. Deleting profile data ..."
422         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
423
424 strip:
425         strip $(EXE)
426
427 install:
428         -mkdir -p -m 755 $(BINDIR)
429         -cp $(EXE) $(BINDIR)
430         -strip $(BINDIR)/$(EXE)
431
432 clean:
433         $(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
434
435 default:
436         help
437
438 ### ==========================================================================
439 ### Section 5. Private targets
440 ### ==========================================================================
441
442 all: $(EXE) .depend
443
444 config-sanity:
445         @echo ""
446         @echo "Config:"
447         @echo "debug: '$(debug)'"
448         @echo "optimize: '$(optimize)'"
449         @echo "arch: '$(arch)'"
450         @echo "bits: '$(bits)'"
451         @echo "prefetch: '$(prefetch)'"
452         @echo "bsfq: '$(bsfq)'"
453         @echo "popcnt: '$(popcnt)'"
454         @echo "sse: '$(sse)'"
455         @echo "pext: '$(pext)'"
456         @echo ""
457         @echo "Flags:"
458         @echo "CXX: $(CXX)"
459         @echo "CXXFLAGS: $(CXXFLAGS)"
460         @echo "LDFLAGS: $(LDFLAGS)"
461         @echo ""
462         @echo "Testing config sanity. If this fails, try 'make help' ..."
463         @echo ""
464         @test "$(debug)" = "yes" || test "$(debug)" = "no"
465         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
466         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
467          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
468         @test "$(bits)" = "32" || test "$(bits)" = "64"
469         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
470         @test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
471         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
472         @test "$(sse)" = "yes" || test "$(sse)" = "no"
473         @test "$(pext)" = "yes" || test "$(pext)" = "no"
474         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
475
476 $(EXE): $(OBJS)
477         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
478
479 gcc-profile-prepare:
480         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
481
482 gcc-profile-make:
483         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
484         EXTRACXXFLAGS='-fprofile-generate' \
485         EXTRALDFLAGS='-lgcov' \
486         all
487
488 gcc-profile-use:
489         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
490         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
491         EXTRALDFLAGS='-lgcov' \
492         all
493
494 gcc-profile-clean:
495         @rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
496
497 icc-profile-prepare:
498         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
499         @mkdir profdir
500
501 icc-profile-make:
502         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
503         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
504         all
505
506 icc-profile-use:
507         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
508         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
509         all
510
511 icc-profile-clean:
512         @rm -rf profdir bench.txt
513
514 .depend:
515         -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
516
517 -include .depend
518