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