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