]> git.lizzy.rs Git - rust.git/blob - mk/main.mk
auto merge of #13152 : huonw/rust/wtf-are-things-in-spans, r=alexcrichton
[rust.git] / mk / main.mk
1 # Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
4 #
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
10
11 ######################################################################
12 # Version numbers and strings
13 ######################################################################
14
15 # The version number
16 CFG_RELEASE_NUM=0.10
17 CFG_RELEASE_LABEL=-pre
18
19 ifndef CFG_NIGHTLY
20 # This is the normal version string
21 CFG_RELEASE=$(CFG_RELEASE_NUM)$(CFG_RELEASE_LABEL)
22 CFG_PACKAGE_VERS=$(CFG_RELEASE)
23 else
24 # Modify the version label for nightly builds
25 CFG_RELEASE=$(CFG_RELEASE_NUM)$(CFG_RELEASE_LABEL)-nightly
26 # When building nightly distributables just reuse the same "rust-nightly" name
27 # so when we upload we'll always override the previous nighly. This doesn't actually
28 # impact the version reported by rustc - it's just for file naming.
29 CFG_PACKAGE_VERS=nightly
30 endif
31 # The name of the package to use for creating tarballs, installers etc.
32 CFG_PACKAGE_NAME=rust-$(CFG_PACKAGE_VERS)
33
34 # The version string plus commit information - this is what rustc reports
35 CFG_VERSION = $(CFG_RELEASE)
36 CFG_GIT_DIR := $(CFG_SRC_DIR).git
37 # since $(CFG_GIT) may contain spaces (especially on Windows),
38 # we need to escape them. (" " to r"\ ")
39 # Note that $(subst ...) ignores space after `subst`,
40 # so we use a hack: define $(SPACE) which contains space character.
41 SPACE :=
42 SPACE +=
43 ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT))),)
44 ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT_DIR))),)
45     CFG_VERSION += $(shell git --git-dir='$(CFG_GIT_DIR)' log -1 \
46                      --pretty=format:'(%h %ci)')
47     CFG_VER_HASH = $(shell git --git-dir='$(CFG_GIT_DIR)' rev-parse HEAD)
48 endif
49 endif
50
51 # Windows exe's need numeric versions - don't use anything but
52 # numbers and dots here
53 CFG_VERSION_WIN = $(CFG_RELEASE_NUM)
54
55
56 ######################################################################
57 # More configuration
58 ######################################################################
59
60 # We track all of the object files we might build so that we can find
61 # and include all of the .d files in one fell swoop.
62 ALL_OBJ_FILES :=
63
64 MKFILE_DEPS := config.stamp $(call rwildcard,$(CFG_SRC_DIR)mk/,*)
65 MKFILES_FOR_TARBALL:=$(MKFILE_DEPS)
66 ifneq ($(NO_MKFILE_DEPS),)
67 MKFILE_DEPS :=
68 endif
69 NON_BUILD_HOST = $(filter-out $(CFG_BUILD),$(CFG_HOST))
70 NON_BUILD_TARGET = $(filter-out $(CFG_BUILD),$(CFG_TARGET))
71
72 ifneq ($(MAKE_RESTARTS),)
73 CFG_INFO := $(info cfg: make restarts: $(MAKE_RESTARTS))
74 endif
75
76 CFG_INFO := $(info cfg: build triple $(CFG_BUILD))
77 CFG_INFO := $(info cfg: host triples $(CFG_HOST))
78 CFG_INFO := $(info cfg: target triples $(CFG_TARGET))
79
80 ifneq ($(wildcard $(NON_BUILD_HOST)),)
81 CFG_INFO := $(info cfg: non-build host triples $(NON_BUILD_HOST))
82 endif
83 ifneq ($(wildcard $(NON_BUILD_TARGET)),)
84 CFG_INFO := $(info cfg: non-build target triples $(NON_BUILD_TARGET))
85 endif
86
87 CFG_RUSTC_FLAGS := $(RUSTFLAGS)
88 CFG_GCCISH_CFLAGS :=
89 CFG_GCCISH_LINK_FLAGS :=
90
91 ifdef CFG_DISABLE_OPTIMIZE
92   $(info cfg: disabling rustc optimization (CFG_DISABLE_OPTIMIZE))
93   CFG_RUSTC_FLAGS +=
94 else
95   # The rtopt cfg turns off runtime sanity checks
96   CFG_RUSTC_FLAGS += -O --cfg rtopt
97 endif
98
99 ifdef CFG_DISABLE_DEBUG
100   CFG_RUSTC_FLAGS += --cfg ndebug
101   CFG_GCCISH_CFLAGS += -DRUST_NDEBUG
102 else
103   $(info cfg: enabling more debugging (CFG_ENABLE_DEBUG))
104   CFG_RUSTC_FLAGS += --cfg debug
105   CFG_GCCISH_CFLAGS += -DRUST_DEBUG
106 endif
107
108 ifdef SAVE_TEMPS
109   CFG_RUSTC_FLAGS += --save-temps
110 endif
111 ifdef ASM_COMMENTS
112   CFG_RUSTC_FLAGS += -Z asm-comments
113 endif
114 ifdef TIME_PASSES
115   CFG_RUSTC_FLAGS += -Z time-passes
116 endif
117 ifdef TIME_LLVM_PASSES
118   CFG_RUSTC_FLAGS += -Z time-llvm-passes
119 endif
120 ifdef TRACE
121   CFG_RUSTC_FLAGS += -Z trace
122 endif
123 ifdef CFG_DISABLE_RPATH
124 CFG_RUSTC_FLAGS += -C no-rpath
125 endif
126
127 # The executables crated during this compilation process have no need to include
128 # static copies of libstd and libextra. We also generate dynamic versions of all
129 # libraries, so in the interest of space, prefer dynamic linking throughout the
130 # compilation process.
131 #
132 # Note though that these flags are omitted for stage2+. This means that the
133 # snapshot will be generated with a statically linked rustc so we only have to
134 # worry about the distribution of one file (with its native dynamic
135 # dependencies)
136 RUSTFLAGS_STAGE0 += -C prefer-dynamic
137 RUSTFLAGS_STAGE1 += -C prefer-dynamic
138
139 # platform-specific auto-configuration
140 include $(CFG_SRC_DIR)mk/platform.mk
141
142 # Run the stage1/2 compilers under valgrind
143 ifdef VALGRIND_COMPILE
144   CFG_VALGRIND_COMPILE :=$(CFG_VALGRIND)
145 else
146   CFG_VALGRIND_COMPILE :=
147 endif
148
149 ifdef CFG_ENABLE_VALGRIND
150   $(info cfg: enabling valgrind (CFG_ENABLE_VALGRIND))
151 else
152   CFG_VALGRIND :=
153 endif
154 ifdef CFG_BAD_VALGRIND
155   $(info cfg: disabling valgrind due to its unreliability on this platform)
156   CFG_VALGRIND :=
157 endif
158
159
160 ######################################################################
161 # Target-and-rule "utility variables"
162 ######################################################################
163
164 define DEF_X
165 X_$(1) := $(CFG_EXE_SUFFIX_$(1))
166 endef
167 $(foreach target,$(CFG_TARGET),\
168   $(eval $(call DEF_X,$(target))))
169
170 # "Source" files we generate in builddir along the way.
171 GENERATED :=
172
173 # Delete the built-in rules.
174 .SUFFIXES:
175 %:: %,v
176 %:: RCS/%,v
177 %:: RCS/%
178 %:: s.%
179 %:: SCCS/s.%
180
181
182 ######################################################################
183 # Cleaning out old crates
184 ######################################################################
185
186 # $(1) is the path for directory to match against
187 # $(2) is the glob to use in the match
188 #
189 # Note that a common bug is to accidentally construct the glob denoted
190 # by $(2) with a space character prefix, which invalidates the
191 # construction $(1)$(2).
192 define CHECK_FOR_OLD_GLOB_MATCHES
193   $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then echo "warning: there are previous" \'$(notdir $(2))\' "libraries:" $$MATCHES; fi
194 endef
195
196 # Same interface as above, but deletes rather than just listing the files.
197 ifdef VERBOSE
198 define REMOVE_ALL_OLD_GLOB_MATCHES
199   $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then echo "warning: removing previous" \'$(notdir $(1))\' "libraries:" $$MATCHES; rm $$MATCHES ; fi
200 endef
201 else
202 define REMOVE_ALL_OLD_GLOB_MATCHES
203   $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then rm $$MATCHES ; fi
204 endef
205 endif
206
207 # We use a different strategy for LIST_ALL_OLD_GLOB_MATCHES_EXCEPT
208 # than in the macros above because it needs the result of running the
209 # `ls` command after other rules in the command list have run; the
210 # macro-expander for $(wildcard ...) would deliver its results too
211 # soon. (This is in contrast to the macros above, which are meant to
212 # be run at the outset of a command list in a rule.)
213 ifdef VERBOSE
214 define LIST_ALL_OLD_GLOB_MATCHES
215   @echo "info: now are following matches for" '$(notdir $(1))' "libraries:"
216   @( ls $(1) 2>/dev/null || true )
217 endef
218 else
219 define LIST_ALL_OLD_GLOB_MATCHES
220 endef
221 endif
222
223 ######################################################################
224 # LLVM macros
225 ######################################################################
226
227 # FIXME: x86-ism
228 LLVM_COMPONENTS=x86 arm mips ipo bitreader bitwriter linker asmparser jit mcjit \
229                 interpreter instrumentation
230
231 # Only build these LLVM tools
232 LLVM_TOOLS=bugpoint llc llvm-ar llvm-as llvm-dis llvm-mc opt llvm-extract
233
234 define DEF_LLVM_VARS
235 # The configure script defines these variables with the target triples
236 # separated by Z. This defines new ones with the expected format.
237 ifeq ($$(CFG_LLVM_ROOT),)
238 CFG_LLVM_BUILD_DIR_$(1):=$$(CFG_LLVM_BUILD_DIR_$(subst -,_,$(1)))
239 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_INST_DIR_$(subst -,_,$(1)))
240 else
241 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_ROOT)
242 endif
243
244 # Any rules that depend on LLVM should depend on LLVM_CONFIG
245 LLVM_CONFIG_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-config$$(X_$(1))
246 LLVM_MC_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-mc$$(X_$(1))
247 LLVM_VERSION_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --version)
248 LLVM_BINDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --bindir)
249 LLVM_INCDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --includedir)
250 LLVM_LIBDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libdir)
251 LLVM_LIBS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libs $$(LLVM_COMPONENTS))
252 LLVM_LDFLAGS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --ldflags)
253 # On FreeBSD, it may search wrong headers (that are for pre-installed LLVM),
254 # so we replace -I with -iquote to ensure that it searches bundled LLVM first.
255 LLVM_CXXFLAGS_$(1)=$$(subst -I, -iquote , $$(shell "$$(LLVM_CONFIG_$(1))" --cxxflags))
256 LLVM_HOST_TRIPLE_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --host-target)
257
258 LLVM_AS_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-as$$(X_$(1))
259 LLC_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llc$$(X_$(1))
260
261 endef
262
263 $(foreach host,$(CFG_HOST), \
264  $(eval $(call DEF_LLVM_VARS,$(host))))
265
266 ######################################################################
267 # Exports for sub-utilities
268 ######################################################################
269
270 # Note that any variable that re-configure should pick up needs to be
271 # exported
272
273 export CFG_SRC_DIR
274 export CFG_BUILD_DIR
275 export CFG_VERSION
276 export CFG_VERSION_WIN
277 export CFG_RELEASE
278 export CFG_PACKAGE_NAME
279 export CFG_BUILD
280 export CFG_LLVM_ROOT
281 export CFG_ENABLE_MINGW_CROSS
282 export CFG_PREFIX
283 export CFG_LIBDIR
284 export CFG_RUSTLIBDIR
285 export CFG_LIBDIR_RELATIVE
286 export CFG_DISABLE_INJECT_STD_VERSION
287
288 ######################################################################
289 # Per-stage targets and runner
290 ######################################################################
291
292 STAGES = 0 1 2 3
293
294 define SREQ
295 # $(1) is the stage number
296 # $(2) is the target triple
297 # $(3) is the host triple
298
299 # Destinations of artifacts for the host compiler
300 HROOT$(1)_H_$(3) = $(3)/stage$(1)
301 HBIN$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/bin
302 HLIB$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/$$(CFG_LIBDIR_RELATIVE)
303
304 # Destinations of artifacts for target architectures
305 TROOT$(1)_T_$(2)_H_$(3) = $$(HLIB$(1)_H_$(3))/$$(CFG_RUSTLIBDIR)/$(2)
306 TBIN$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/bin
307 TLIB$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/lib
308
309 # Preqrequisites for using the stageN compiler
310 ifeq ($(1),0)
311 HSREQ$(1)_H_$(3) = $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))
312 else
313 HSREQ$(1)_H_$(3) = \
314         $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
315         $$(HLIB$(1)_H_$(3))/stamp.rustc \
316         $$(foreach dep,$$(RUST_DEPS_rustc),$$(HLIB$(1)_H_$(3))/stamp.$$(dep)) \
317         $$(MKFILE_DEPS)
318 endif
319
320 # Prerequisites for using the stageN compiler to build target artifacts
321 TSREQ$(1)_T_$(2)_H_$(3) = \
322         $$(HSREQ$(1)_H_$(3)) \
323         $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a \
324         $$(TLIB$(1)_T_$(2)_H_$(3))/libcompiler-rt.a
325
326 # Prerequisites for a working stageN compiler and libraries, for a specific
327 # target
328 SREQ$(1)_T_$(2)_H_$(3) = \
329         $$(TSREQ$(1)_T_$(2)_H_$(3)) \
330         $$(foreach dep,$$(TARGET_CRATES),\
331             $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep))
332
333 # Prerequisites for a working stageN compiler and complete set of target
334 # libraries
335 CSREQ$(1)_T_$(2)_H_$(3) = \
336         $$(TSREQ$(1)_T_$(2)_H_$(3)) \
337         $$(HBIN$(1)_H_$(3))/rustdoc$$(X_$(3)) \
338         $$(foreach dep,$$(CRATES),$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
339         $$(foreach dep,$$(HOST_CRATES),$$(HLIB$(1)_H_$(3))/stamp.$$(dep))
340
341 ifeq ($(1),0)
342 # Don't run the stage0 compiler under valgrind - that ship has sailed
343 CFG_VALGRIND_COMPILE$(1) =
344 else
345 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
346 endif
347
348 # Add RUSTFLAGS_STAGEN values to the build command
349 EXTRAFLAGS_STAGE$(1) = $$(RUSTFLAGS_STAGE$(1))
350
351 CFGFLAG$(1)_T_$(2)_H_$(3) = stage$(1)
352
353 # Pass --cfg stage0 only for the build->host part of stage0;
354 # if you're building a cross config, the host->* parts are
355 # effectively stage1, since it uses the just-built stage0.
356 ifeq ($(1),0)
357 ifneq ($(strip $(CFG_BUILD)),$(strip $(3)))
358 CFGFLAG$(1)_T_$(2)_H_$(3) = stage1
359 endif
360 endif
361
362 ifdef CFG_DISABLE_RPATH
363 ifeq ($$(OSTYPE_$(3)),apple-darwin)
364   RPATH_VAR$(1)_T_$(2)_H_$(3) := \
365       DYLD_LIBRARY_PATH="$$$$DYLD_LIBRARY_PATH:$$(CURDIR)/$$(HLIB$(1)_H_$(3))"
366 else
367   RPATH_VAR$(1)_T_$(2)_H_$(3) := \
368       LD_LIBRARY_PATH="$$$$LD_LIBRARY_PATH:$$(CURDIR)/$$(HLIB$(1)_H_$(3))"
369 endif
370 else
371     RPATH_VAR$(1)_T_$(2)_H_$(3) :=
372 endif
373
374 STAGE$(1)_T_$(2)_H_$(3) :=                                              \
375         $$(Q)$$(RPATH_VAR$(1)_T_$(2)_H_$(3))                            \
376                 $$(call CFG_RUN_TARG_$(3),$(1),                         \
377                 $$(CFG_VALGRIND_COMPILE$(1))                            \
378                 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))                     \
379                 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3))                     \
380                 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
381                 $$(RUSTC_FLAGS_$(2))
382
383 PERF_STAGE$(1)_T_$(2)_H_$(3) :=                                         \
384         $$(Q)$$(call CFG_RUN_TARG_$(3),$(1),                            \
385                 $$(CFG_PERF_TOOL)                                       \
386                 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))                     \
387                 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3))                     \
388                 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
389                 $$(RUSTC_FLAGS_$(2))
390
391 endef
392
393 $(foreach build,$(CFG_HOST), \
394  $(eval $(foreach target,$(CFG_TARGET), \
395   $(eval $(foreach stage,$(STAGES), \
396    $(eval $(call SREQ,$(stage),$(target),$(build))))))))
397
398 ######################################################################
399 # rustc-H-targets
400 #
401 # Builds a functional Rustc for the given host.
402 ######################################################################
403
404 define DEF_RUSTC_STAGE_TARGET
405 # $(1) == architecture
406 # $(2) == stage
407
408 rustc-stage$(2)-H-$(1):                                                 \
409         $$(foreach target,$$(CFG_TARGET),$$(SREQ$(2)_T_$$(target)_H_$(1)))
410
411 endef
412
413 $(foreach host,$(CFG_HOST),                                             \
414  $(eval $(foreach stage,1 2 3,                                          \
415   $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
416
417 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
418 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
419 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
420
421 define DEF_RUSTC_TARGET
422 # $(1) == architecture
423
424 rustc-H-$(1): rustc-stage2-H-$(1)
425 endef
426
427 $(foreach host,$(CFG_TARGET),                   \
428  $(eval $(call DEF_RUSTC_TARGET,$(host))))
429
430 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
431 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
432 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
433 rustc: rustc-H-$(CFG_BUILD)
434
435 rustc-H-all: $(foreach host,$(CFG_HOST),rustc-H-$(host))
436
437 ######################################################################
438 # Entrypoint rule
439 ######################################################################
440
441 .DEFAULT_GOAL := all
442
443 define ALL_TARGET_N
444 ifneq ($$(findstring $(1),$$(CFG_HOST)),)
445 # This is a host
446 all-target-$(1)-host-$(2): $$(CSREQ2_T_$(1)_H_$(2))
447 else
448 # This is a target only
449 all-target-$(1)-host-$(2): $$(SREQ2_T_$(1)_H_$(2))
450 endif
451 endef
452
453 $(foreach target,$(CFG_TARGET), \
454  $(foreach host,$(CFG_HOST), \
455  $(eval $(call ALL_TARGET_N,$(target),$(host)))))
456
457 ALL_TARGET_RULES = $(foreach target,$(CFG_TARGET), \
458         $(foreach host,$(CFG_HOST), \
459  all-target-$(target)-host-$(host)))
460
461 all: $(ALL_TARGET_RULES) $(GENERATED) docs
462
463 ######################################################################
464 # Build system documentation
465 ######################################################################
466
467 # $(1) is the name of the doc <section> in Makefile.in
468 # pick everything between tags | remove first line | remove last line
469 # | remove extra (?) line | strip leading `#` from lines
470 SHOW_DOCS = $(Q)awk '/<$(1)>/,/<\/$(1)>/' $(S)/Makefile.in | sed '1d' | sed '$$d' | sed 's/^\# \?//'
471
472 help:
473         $(call SHOW_DOCS,help)
474
475 tips:
476         $(call SHOW_DOCS,tips)
477
478 nitty-gritty:
479         $(call SHOW_DOCS,nitty-gritty)