]> git.lizzy.rs Git - rust.git/blob - mk/main.mk
auto merge of #13791 : lifthrasiir/rust/mod-inner-span, r=huonw
[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.11
17 CFG_RELEASE_LABEL=-pre
18
19 ifndef CFG_ENABLE_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_LIBDIR_RELATIVE
285 export CFG_DISABLE_INJECT_STD_VERSION
286
287 ######################################################################
288 # Per-stage targets and runner
289 ######################################################################
290
291 STAGES = 0 1 2 3
292
293 define SREQ
294 # $(1) is the stage number
295 # $(2) is the target triple
296 # $(3) is the host triple
297
298 # Destinations of artifacts for the host compiler
299 HROOT$(1)_H_$(3) = $(3)/stage$(1)
300 HBIN$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/bin
301 HLIB$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/$$(CFG_LIBDIR_RELATIVE)
302
303 # Destinations of artifacts for target architectures
304 TROOT$(1)_T_$(2)_H_$(3) = $$(HLIB$(1)_H_$(3))/rustlib/$(2)
305 TBIN$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/bin
306 TLIB$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/lib
307
308 # Preqrequisites for using the stageN compiler
309 ifeq ($(1),0)
310 HSREQ$(1)_H_$(3) = $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))
311 else
312 HSREQ$(1)_H_$(3) = \
313         $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
314         $$(MKFILE_DEPS)
315 endif
316
317 # Prerequisites for using the stageN compiler to build target artifacts
318 TSREQ$(1)_T_$(2)_H_$(3) = \
319         $$(HSREQ$(1)_H_$(3)) \
320         $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a \
321         $$(TLIB$(1)_T_$(2)_H_$(3))/libcompiler-rt.a
322
323 # Prerequisites for a working stageN compiler and libraries, for a specific
324 # target
325 SREQ$(1)_T_$(2)_H_$(3) = \
326         $$(TSREQ$(1)_T_$(2)_H_$(3)) \
327         $$(foreach dep,$$(TARGET_CRATES),\
328             $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep))
329
330 # Prerequisites for a working stageN compiler and complete set of target
331 # libraries
332 CSREQ$(1)_T_$(2)_H_$(3) = \
333         $$(TSREQ$(1)_T_$(2)_H_$(3)) \
334         $$(HBIN$(1)_H_$(3))/rustdoc$$(X_$(3)) \
335         $$(foreach dep,$$(CRATES),$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep))
336
337 ifeq ($(1),0)
338 # Don't run the stage0 compiler under valgrind - that ship has sailed
339 CFG_VALGRIND_COMPILE$(1) =
340 else
341 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
342 endif
343
344 # Add RUSTFLAGS_STAGEN values to the build command
345 EXTRAFLAGS_STAGE$(1) = $$(RUSTFLAGS_STAGE$(1))
346
347 CFGFLAG$(1)_T_$(2)_H_$(3) = stage$(1)
348
349 endef
350
351 # Same macro/variables as above, but defined in a separate loop so it can use
352 # all the varibles above for all archs. The RPATH_VAR setup sometimes needs to
353 # reach across triples to get things in order.
354 define SREQ_CMDS
355
356 ifeq ($$(OSTYPE_$(3)),apple-darwin)
357   RPATH_VAR$(1)_T_$(2)_H_$(3) := \
358       DYLD_LIBRARY_PATH="$$$$DYLD_LIBRARY_PATH:$$(CURDIR)/$$(HLIB$(1)_H_$(3))"
359 else
360   RPATH_VAR$(1)_T_$(2)_H_$(3) := \
361       LD_LIBRARY_PATH="$$$$LD_LIBRARY_PATH:$$(CURDIR)/$$(HLIB$(1)_H_$(3))"
362 endif
363
364 # Pass --cfg stage0 only for the build->host part of stage0;
365 # if you're building a cross config, the host->* parts are
366 # effectively stage1, since it uses the just-built stage0.
367 #
368 # This logic is similar to how the LD_LIBRARY_PATH variable must
369 # change be slightly different when doing cross compilations.
370 # The build doesn't copy over all target libraries into
371 # a new directory, so we need to point the library path at
372 # the build directory where all the target libraries came
373 # from (the stage0 build host). Otherwise the relative rpaths
374 # inside of the rustc binary won't get resolved correctly.
375 ifeq ($(1),0)
376 ifneq ($(strip $(CFG_BUILD)),$(strip $(3)))
377 CFGFLAG$(1)_T_$(2)_H_$(3) = stage1
378
379 ifeq ($$(OSTYPE_$(3)),apple-darwin)
380   RPATH_VAR$(1)_T_$(2)_H_$(3) := \
381       DYLD_LIBRARY_PATH="$$$$DYLD_LIBRARY_PATH:$$(CURDIR)/$$(TLIB1_T_$(2)_H_$(CFG_BUILD))"
382 else
383   RPATH_VAR$(1)_T_$(2)_H_$(3) := \
384       LD_LIBRARY_PATH="$$$$LD_LIBRARY_PATH:$$(CURDIR)/$$(TLIB1_T_$(2)_H_$(CFG_BUILD))"
385 endif
386 endif
387 endif
388
389 STAGE$(1)_T_$(2)_H_$(3) :=                                              \
390         $$(Q)$$(RPATH_VAR$(1)_T_$(2)_H_$(3))                            \
391                 $$(call CFG_RUN_TARG_$(3),$(1),                         \
392                 $$(CFG_VALGRIND_COMPILE$(1))                            \
393                 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))                     \
394                 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3))                     \
395                 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
396                 $$(RUSTC_FLAGS_$(2))
397
398 PERF_STAGE$(1)_T_$(2)_H_$(3) :=                                         \
399         $$(Q)$$(call CFG_RUN_TARG_$(3),$(1),                            \
400                 $$(CFG_PERF_TOOL)                                       \
401                 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))                     \
402                 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3))                     \
403                 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
404                 $$(RUSTC_FLAGS_$(2))
405
406 endef
407
408 $(foreach build,$(CFG_HOST), \
409  $(eval $(foreach target,$(CFG_TARGET), \
410   $(eval $(foreach stage,$(STAGES), \
411    $(eval $(call SREQ,$(stage),$(target),$(build))))))))
412
413 $(foreach build,$(CFG_HOST), \
414  $(eval $(foreach target,$(CFG_TARGET), \
415   $(eval $(foreach stage,$(STAGES), \
416    $(eval $(call SREQ_CMDS,$(stage),$(target),$(build))))))))
417
418 ######################################################################
419 # rustc-H-targets
420 #
421 # Builds a functional Rustc for the given host.
422 ######################################################################
423
424 define DEF_RUSTC_STAGE_TARGET
425 # $(1) == architecture
426 # $(2) == stage
427
428 rustc-stage$(2)-H-$(1):                                                 \
429         $$(foreach target,$$(CFG_TARGET),$$(SREQ$(2)_T_$$(target)_H_$(1)))
430
431 endef
432
433 $(foreach host,$(CFG_HOST),                                             \
434  $(eval $(foreach stage,1 2 3,                                          \
435   $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
436
437 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
438 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
439 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
440
441 define DEF_RUSTC_TARGET
442 # $(1) == architecture
443
444 rustc-H-$(1): rustc-stage2-H-$(1)
445 endef
446
447 $(foreach host,$(CFG_TARGET),                   \
448  $(eval $(call DEF_RUSTC_TARGET,$(host))))
449
450 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
451 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
452 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
453 rustc: rustc-H-$(CFG_BUILD)
454
455 rustc-H-all: $(foreach host,$(CFG_HOST),rustc-H-$(host))
456
457 ######################################################################
458 # Entrypoint rule
459 ######################################################################
460
461 .DEFAULT_GOAL := all
462
463 define ALL_TARGET_N
464 ifneq ($$(findstring $(1),$$(CFG_HOST)),)
465 # This is a host
466 all-target-$(1)-host-$(2): $$(CSREQ2_T_$(1)_H_$(2))
467 else
468 # This is a target only
469 all-target-$(1)-host-$(2): $$(SREQ2_T_$(1)_H_$(2))
470 endif
471 endef
472
473 $(foreach target,$(CFG_TARGET), \
474  $(foreach host,$(CFG_HOST), \
475  $(eval $(call ALL_TARGET_N,$(target),$(host)))))
476
477 ALL_TARGET_RULES = $(foreach target,$(CFG_TARGET), \
478         $(foreach host,$(CFG_HOST), \
479  all-target-$(target)-host-$(host)))
480
481 all: $(ALL_TARGET_RULES) $(GENERATED) docs
482
483 ######################################################################
484 # Build system documentation
485 ######################################################################
486
487 # $(1) is the name of the doc <section> in Makefile.in
488 # pick everything between tags | remove first line | remove last line
489 # | remove extra (?) line | strip leading `#` from lines
490 SHOW_DOCS = $(Q)awk '/<$(1)>/,/<\/$(1)>/' $(S)/Makefile.in | sed '1d' | sed '$$d' | sed 's/^\# \?//'
491
492 help:
493         $(call SHOW_DOCS,help)
494
495 tips:
496         $(call SHOW_DOCS,tips)
497
498 nitty-gritty:
499         $(call SHOW_DOCS,nitty-gritty)