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