]> git.sesse.net Git - stockfish/blob - src/Makefile
Revert LTO for mingw on 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 # Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
5 #
6 # Stockfish is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Stockfish is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 ### ==========================================================================
21 ### Section 1. General Configuration
22 ### ==========================================================================
23
24 ### Executable name
25 ifeq ($(COMP),mingw)
26 EXE = stockfish.exe
27 else
28 EXE = stockfish
29 endif
30
31 ### Installation dir definitions
32 PREFIX = /usr/local
33 BINDIR = $(PREFIX)/bin
34
35 ### Built-in benchmark for pgo-builds
36 PGOBENCH = ./$(EXE) bench
37
38 ### Source and object files
39 SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
40         material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
41         search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp
42
43 OBJS = $(notdir $(SRCS:.cpp=.o))
44
45 VPATH = syzygy
46
47 ### Establish the operating system name
48 KERNEL = $(shell uname -s)
49 ifeq ($(KERNEL),Linux)
50         OS = $(shell uname -o)
51 endif
52
53 ### ==========================================================================
54 ### Section 2. High-level Configuration
55 ### ==========================================================================
56 #
57 # flag                --- Comp switch      --- Description
58 # ----------------------------------------------------------------------------
59 #
60 # debug = yes/no      --- -DNDEBUG         --- Enable/Disable debug mode
61 # sanitize = undefined/thread/no (-fsanitize )
62 #                     --- ( undefined )    --- enable undefined behavior checks
63 #                     --- ( thread    )    --- enable threading error  checks
64 # optimize = yes/no   --- (-O3/-fast etc.) --- Enable/Disable optimizations
65 # arch = (name)       --- (-arch)          --- Target architecture
66 # bits = 64/32        --- -DIS_64BIT       --- 64-/32-bit operating system
67 # prefetch = yes/no   --- -DUSE_PREFETCH   --- Use prefetch asm-instruction
68 # popcnt = yes/no     --- -DUSE_POPCNT     --- Use popcnt asm-instruction
69 # sse = yes/no        --- -msse            --- Use Intel Streaming SIMD Extensions
70 # pext = yes/no       --- -DUSE_PEXT       --- Use pext x86_64 asm-instruction
71 #
72 # Note that Makefile is space sensitive, so when adding new architectures
73 # or modifying existing flags, you have to make sure there are no extra spaces
74 # at the end of the line for flag values.
75
76 ### 2.1. General and architecture defaults
77 optimize = yes
78 debug = no
79 sanitize = no
80 bits = 64
81 prefetch = no
82 popcnt = no
83 sse = no
84 pext = no
85
86 ### 2.2 Architecture specific
87 ifeq ($(ARCH),general-32)
88         arch = any
89         bits = 32
90 endif
91
92 ifeq ($(ARCH),x86-32-old)
93         arch = i386
94         bits = 32
95 endif
96
97 ifeq ($(ARCH),x86-32)
98         arch = i386
99         bits = 32
100         prefetch = yes
101         sse = yes
102 endif
103
104 ifeq ($(ARCH),general-64)
105         arch = any
106 endif
107
108 ifeq ($(ARCH),x86-64)
109         arch = x86_64
110         prefetch = yes
111         sse = yes
112 endif
113
114 ifeq ($(ARCH),x86-64-modern)
115         arch = x86_64
116         prefetch = yes
117         popcnt = yes
118         sse = yes
119 endif
120
121 ifeq ($(ARCH),x86-64-bmi2)
122         arch = x86_64
123         prefetch = yes
124         popcnt = yes
125         sse = yes
126         pext = yes
127 endif
128
129 ifeq ($(ARCH),armv7)
130         arch = armv7
131         prefetch = yes
132         bits = 32
133 endif
134
135 ifeq ($(ARCH),armv8)
136         arch = armv8-a
137         prefetch = yes
138         popcnt = yes
139 endif
140
141 ifeq ($(ARCH),ppc-32)
142         arch = ppc
143         bits = 32
144 endif
145
146 ifeq ($(ARCH),ppc-64)
147         arch = ppc64
148         popcnt = yes
149         prefetch = yes
150 endif
151
152 ### ==========================================================================
153 ### Section 3. Low-level Configuration
154 ### ==========================================================================
155
156 ### 3.1 Selecting compiler (default = gcc)
157 CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
158 DEPENDFLAGS += -std=c++11
159 LDFLAGS += $(EXTRALDFLAGS)
160
161 ifeq ($(COMP),)
162         COMP=gcc
163 endif
164
165 ifeq ($(COMP),gcc)
166         comp=gcc
167         CXX=g++
168         CXXFLAGS += -pedantic -Wextra -Wshadow
169
170         ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
171                 ifeq ($(OS),Android)
172                         CXXFLAGS += -m$(bits)
173                         LDFLAGS += -m$(bits)
174                 endif
175         else
176                 CXXFLAGS += -m$(bits)
177                 LDFLAGS += -m$(bits)
178         endif
179
180         ifneq ($(KERNEL),Darwin)
181            LDFLAGS += -Wl,--no-as-needed
182         endif
183 endif
184
185 ifeq ($(COMP),mingw)
186         comp=mingw
187
188         ifeq ($(KERNEL),Linux)
189                 ifeq ($(bits),64)
190                         ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
191                                 CXX=x86_64-w64-mingw32-c++
192                         else
193                                 CXX=x86_64-w64-mingw32-c++-posix
194                         endif
195                 else
196                         ifeq ($(shell which i686-w64-mingw32-c++-posix),)
197                                 CXX=i686-w64-mingw32-c++
198                         else
199                                 CXX=i686-w64-mingw32-c++-posix
200                         endif
201                 endif
202         else
203                 CXX=g++
204         endif
205
206         CXXFLAGS += -Wextra -Wshadow
207         LDFLAGS += -static
208 endif
209
210 ifeq ($(COMP),icc)
211         comp=icc
212         CXX=icpc
213         CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
214 endif
215
216 ifeq ($(COMP),clang)
217         comp=clang
218         CXX=clang++
219         CXXFLAGS += -pedantic -Wextra -Wshadow
220
221         ifneq ($(KERNEL),Darwin)
222         ifneq ($(KERNEL),OpenBSD)
223                 LDFLAGS += -latomic
224         endif
225         endif
226
227         ifeq ($(ARCH),$(filter $(ARCH),armv7 armv8))
228                 ifeq ($(OS),Android)
229                         CXXFLAGS += -m$(bits)
230                         LDFLAGS += -m$(bits)
231                 endif
232         else
233                 CXXFLAGS += -m$(bits)
234                 LDFLAGS += -m$(bits)
235         endif
236 endif
237
238 ifeq ($(comp),icc)
239         profile_make = icc-profile-make
240         profile_use = icc-profile-use
241 else
242 ifeq ($(comp),clang)
243         profile_make = clang-profile-make
244         profile_use = clang-profile-use
245 else
246         profile_make = gcc-profile-make
247         profile_use = gcc-profile-use
248 endif
249 endif
250
251 ifeq ($(KERNEL),Darwin)
252         CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
253         LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
254 endif
255
256 ### Travis CI script uses COMPILER to overwrite CXX
257 ifdef COMPILER
258         COMPCXX=$(COMPILER)
259 endif
260
261 ### Allow overwriting CXX from command line
262 ifdef COMPCXX
263         CXX=$(COMPCXX)
264 endif
265
266 ### On mingw use Windows threads, otherwise POSIX
267 ifneq ($(comp),mingw)
268         # On Android Bionic's C library comes with its own pthread implementation bundled in
269         ifneq ($(OS),Android)
270                 # Haiku has pthreads in its libroot, so only link it in on other platforms
271                 ifneq ($(KERNEL),Haiku)
272                         LDFLAGS += -lpthread
273                 endif
274         endif
275 endif
276
277 ### 3.2.1 Debugging
278 ifeq ($(debug),no)
279         CXXFLAGS += -DNDEBUG
280 else
281         CXXFLAGS += -g
282 endif
283
284 ### 3.2.2 Debugging with undefined behavior sanitizers
285 ifneq ($(sanitize),no)
286         CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
287         LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
288 endif
289
290 ### 3.3 Optimization
291 ifeq ($(optimize),yes)
292
293         CXXFLAGS += -O3
294
295         ifeq ($(comp),gcc)
296                 ifeq ($(OS), Android)
297                         CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
298                 endif
299         endif
300
301         ifeq ($(comp),$(filter $(comp),gcc clang icc))
302                 ifeq ($(KERNEL),Darwin)
303                         CXXFLAGS += -mdynamic-no-pic
304                 endif
305         endif
306 endif
307
308 ### 3.4 Bits
309 ifeq ($(bits),64)
310         CXXFLAGS += -DIS_64BIT
311 endif
312
313 ### 3.5 prefetch
314 ifeq ($(prefetch),yes)
315         ifeq ($(sse),yes)
316                 CXXFLAGS += -msse
317                 DEPENDFLAGS += -msse
318         endif
319 else
320         CXXFLAGS += -DNO_PREFETCH
321 endif
322
323 ### 3.6 popcnt
324 ifeq ($(popcnt),yes)
325         ifeq ($(arch),$(filter $(arch),ppc64 armv8-a))
326                 CXXFLAGS += -DUSE_POPCNT
327         else ifeq ($(comp),icc)
328                 CXXFLAGS += -msse3 -DUSE_POPCNT
329         else
330                 CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
331         endif
332 endif
333
334 ### 3.7 pext
335 ifeq ($(pext),yes)
336         CXXFLAGS += -DUSE_PEXT
337         ifeq ($(comp),$(filter $(comp),gcc clang mingw))
338                 CXXFLAGS += -msse4 -mbmi2
339         endif
340 endif
341
342 ### 3.8 Link Time Optimization
343 ### This is a mix of compile and link time options because the lto link phase
344 ### needs access to the optimization flags.
345 ifeq ($(optimize),yes)
346 ifeq ($(debug), no)
347         ifeq ($(comp),$(filter $(comp),gcc clang))
348                 CXXFLAGS += -flto
349                 LDFLAGS += $(CXXFLAGS)
350         endif
351
352 # To use LTO and static linking on windows, the tool chain requires a recent gcc:
353 # gcc version 10.1 in msys2 or TDM-GCC version 9.2 are know to work, older might not.
354 # So, only enable it for a cross from Linux by default.
355         ifeq ($(comp),mingw)
356         ifeq ($(KERNEL),Linux)
357                 CXXFLAGS += -flto
358                 LDFLAGS += $(CXXFLAGS)
359         endif
360         endif
361 endif
362 endif
363
364 ### 3.9 Android 5 can only run position independent executables. Note that this
365 ### breaks Android 4.0 and earlier.
366 ifeq ($(OS), Android)
367         CXXFLAGS += -fPIE
368         LDFLAGS += -fPIE -pie
369 endif
370
371 ### ==========================================================================
372 ### Section 4. Public Targets
373 ### ==========================================================================
374
375 help:
376         @echo ""
377         @echo "To compile stockfish, type: "
378         @echo ""
379         @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
380         @echo ""
381         @echo "Supported targets:"
382         @echo ""
383         @echo "build                   > Standard build"
384         @echo "profile-build           > PGO build"
385         @echo "strip                   > Strip executable"
386         @echo "install                 > Install executable"
387         @echo "clean                   > Clean up"
388         @echo ""
389         @echo "Supported archs:"
390         @echo ""
391         @echo "x86-64-bmi2             > x86 64-bit with pext support (also enables SSE4)"
392         @echo "x86-64-modern           > x86 64-bit with popcnt support (also enables SSE3)"
393         @echo "x86-64                  > x86 64-bit generic"
394         @echo "x86-32                  > x86 32-bit (also enables SSE)"
395         @echo "x86-32-old              > x86 32-bit fall back for old hardware"
396         @echo "ppc-64                  > PPC 64-bit"
397         @echo "ppc-32                  > PPC 32-bit"
398         @echo "armv7                   > ARMv7 32-bit"
399         @echo "armv8                   > ARMv8 64-bit"
400         @echo "general-64              > unspecified 64-bit"
401         @echo "general-32              > unspecified 32-bit"
402         @echo ""
403         @echo "Supported compilers:"
404         @echo ""
405         @echo "gcc                     > Gnu compiler (default)"
406         @echo "mingw                   > Gnu compiler with MinGW under Windows"
407         @echo "clang                   > LLVM Clang compiler"
408         @echo "icc                     > Intel compiler"
409         @echo ""
410         @echo "Simple examples. If you don't know what to do, you likely want to run: "
411         @echo ""
412         @echo "make build ARCH=x86-64    (This is for 64-bit systems)"
413         @echo "make build ARCH=x86-32    (This is for 32-bit systems)"
414         @echo ""
415         @echo "Advanced examples, for experienced users: "
416         @echo ""
417         @echo "make build ARCH=x86-64 COMP=clang"
418         @echo "make profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-4.8"
419         @echo ""
420
421
422 .PHONY: help build profile-build strip install clean objclean profileclean \
423         config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
424         clang-profile-use clang-profile-make
425
426 build: config-sanity
427         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
428
429 profile-build: config-sanity objclean profileclean
430         @echo ""
431         @echo "Step 1/4. Building instrumented executable ..."
432         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
433         @echo ""
434         @echo "Step 2/4. Running benchmark for pgo-build ..."
435         $(PGOBENCH) > /dev/null
436         @echo ""
437         @echo "Step 3/4. Building optimized executable ..."
438         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
439         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
440         @echo ""
441         @echo "Step 4/4. Deleting profile data ..."
442         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
443
444 strip:
445         strip $(EXE)
446
447 install:
448         -mkdir -p -m 755 $(BINDIR)
449         -cp $(EXE) $(BINDIR)
450         -strip $(BINDIR)/$(EXE)
451
452 #clean all
453 clean: objclean profileclean
454         @rm -f .depend *~ core
455
456 # clean binaries and objects
457 objclean:
458         @rm -f $(EXE) *.o ./syzygy/*.o
459
460 # clean auxiliary profiling files
461 profileclean:
462         @rm -rf profdir
463         @rm -f bench.txt *.gcda *.gcno
464         @rm -f stockfish.profdata *.profraw
465
466 default:
467         help
468
469 ### ==========================================================================
470 ### Section 5. Private Targets
471 ### ==========================================================================
472
473 all: $(EXE) .depend
474
475 config-sanity:
476         @echo ""
477         @echo "Config:"
478         @echo "debug: '$(debug)'"
479         @echo "sanitize: '$(sanitize)'"
480         @echo "optimize: '$(optimize)'"
481         @echo "arch: '$(arch)'"
482         @echo "bits: '$(bits)'"
483         @echo "kernel: '$(KERNEL)'"
484         @echo "os: '$(OS)'"
485         @echo "prefetch: '$(prefetch)'"
486         @echo "popcnt: '$(popcnt)'"
487         @echo "sse: '$(sse)'"
488         @echo "pext: '$(pext)'"
489         @echo ""
490         @echo "Flags:"
491         @echo "CXX: $(CXX)"
492         @echo "CXXFLAGS: $(CXXFLAGS)"
493         @echo "LDFLAGS: $(LDFLAGS)"
494         @echo ""
495         @echo "Testing config sanity. If this fails, try 'make help' ..."
496         @echo ""
497         @test "$(debug)" = "yes" || test "$(debug)" = "no"
498         @test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
499         @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
500         @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
501          test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
502          test "$(arch)" = "armv7" || test "$(arch)" = "armv8-a"
503         @test "$(bits)" = "32" || test "$(bits)" = "64"
504         @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
505         @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
506         @test "$(sse)" = "yes" || test "$(sse)" = "no"
507         @test "$(pext)" = "yes" || test "$(pext)" = "no"
508         @test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
509
510 $(EXE): $(OBJS)
511         $(CXX) -o $@ $(OBJS) $(LDFLAGS)
512
513 clang-profile-make:
514         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
515         EXTRACXXFLAGS='-fprofile-instr-generate ' \
516         EXTRALDFLAGS=' -fprofile-instr-generate' \
517         all
518
519 clang-profile-use:
520         llvm-profdata merge -output=stockfish.profdata *.profraw
521         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
522         EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
523         EXTRALDFLAGS='-fprofile-use ' \
524         all
525
526 gcc-profile-make:
527         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
528         EXTRACXXFLAGS='-fprofile-generate' \
529         EXTRALDFLAGS='-lgcov' \
530         all
531
532 gcc-profile-use:
533         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
534         EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
535         EXTRALDFLAGS='-lgcov' \
536         all
537
538 icc-profile-make:
539         @mkdir -p profdir
540         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
541         EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
542         all
543
544 icc-profile-use:
545         $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
546         EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
547         all
548
549 .depend:
550         -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
551
552 -include .depend