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.
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.
11 ######################################################################
12 # Version numbers and strings
13 ######################################################################
17 CFG_RELEASE_LABEL=-pre
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)
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
31 # The name of the package to use for creating tarballs, installers etc.
32 CFG_PACKAGE_NAME=rust-$(CFG_PACKAGE_VERS)
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.
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)
51 # Windows exe's need numeric versions - don't use anything but
52 # numbers and dots here
53 CFG_VERSION_WIN = $(CFG_RELEASE_NUM)
56 ######################################################################
58 ######################################################################
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.
64 MKFILE_DEPS := config.stamp $(call rwildcard,$(CFG_SRC_DIR)mk/,*)
65 MKFILES_FOR_TARBALL:=$(MKFILE_DEPS)
66 ifneq ($(NO_MKFILE_DEPS),)
69 NON_BUILD_HOST = $(filter-out $(CFG_BUILD),$(CFG_HOST))
70 NON_BUILD_TARGET = $(filter-out $(CFG_BUILD),$(CFG_TARGET))
72 ifneq ($(MAKE_RESTARTS),)
73 CFG_INFO := $(info cfg: make restarts: $(MAKE_RESTARTS))
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))
80 ifneq ($(wildcard $(NON_BUILD_HOST)),)
81 CFG_INFO := $(info cfg: non-build host triples $(NON_BUILD_HOST))
83 ifneq ($(wildcard $(NON_BUILD_TARGET)),)
84 CFG_INFO := $(info cfg: non-build target triples $(NON_BUILD_TARGET))
87 CFG_RUSTC_FLAGS := $(RUSTFLAGS)
89 CFG_GCCISH_LINK_FLAGS :=
91 ifdef CFG_DISABLE_OPTIMIZE
92 $(info cfg: disabling rustc optimization (CFG_DISABLE_OPTIMIZE))
95 # The rtopt cfg turns off runtime sanity checks
96 CFG_RUSTC_FLAGS += -O --cfg rtopt
99 ifdef CFG_DISABLE_DEBUG
100 CFG_RUSTC_FLAGS += --cfg ndebug
101 CFG_GCCISH_CFLAGS += -DRUST_NDEBUG
103 $(info cfg: enabling more debugging (CFG_ENABLE_DEBUG))
104 CFG_RUSTC_FLAGS += --cfg debug
105 CFG_GCCISH_CFLAGS += -DRUST_DEBUG
109 CFG_RUSTC_FLAGS += --save-temps
112 CFG_RUSTC_FLAGS += -Z asm-comments
115 CFG_RUSTC_FLAGS += -Z time-passes
117 ifdef TIME_LLVM_PASSES
118 CFG_RUSTC_FLAGS += -Z time-llvm-passes
121 CFG_RUSTC_FLAGS += -Z trace
123 ifdef CFG_DISABLE_RPATH
124 CFG_RUSTC_FLAGS += -C no-rpath
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.
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
136 RUSTFLAGS_STAGE0 += -C prefer-dynamic
137 RUSTFLAGS_STAGE1 += -C prefer-dynamic
139 # platform-specific auto-configuration
140 include $(CFG_SRC_DIR)mk/platform.mk
142 # Run the stage1/2 compilers under valgrind
143 ifdef VALGRIND_COMPILE
144 CFG_VALGRIND_COMPILE :=$(CFG_VALGRIND)
146 CFG_VALGRIND_COMPILE :=
149 ifdef CFG_ENABLE_VALGRIND
150 $(info cfg: enabling valgrind (CFG_ENABLE_VALGRIND))
154 ifdef CFG_BAD_VALGRIND
155 $(info cfg: disabling valgrind due to its unreliability on this platform)
160 ######################################################################
161 # Target-and-rule "utility variables"
162 ######################################################################
165 X_$(1) := $(CFG_EXE_SUFFIX_$(1))
167 $(foreach target,$(CFG_TARGET),\
168 $(eval $(call DEF_X,$(target))))
170 # "Source" files we generate in builddir along the way.
173 # Delete the built-in rules.
182 ######################################################################
183 # Cleaning out old crates
184 ######################################################################
186 # $(1) is the path for directory to match against
187 # $(2) is the glob to use in the match
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
196 # Same interface as above, but deletes rather than just listing the files.
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
202 define REMOVE_ALL_OLD_GLOB_MATCHES
203 $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then rm $$MATCHES ; fi
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.)
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 )
219 define LIST_ALL_OLD_GLOB_MATCHES
223 ######################################################################
225 ######################################################################
228 LLVM_COMPONENTS=x86 arm mips ipo bitreader bitwriter linker asmparser jit mcjit \
229 interpreter instrumentation
231 # Only build these LLVM tools
232 LLVM_TOOLS=bugpoint llc llvm-ar llvm-as llvm-dis llvm-mc opt llvm-extract
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)))
241 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_ROOT)
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)
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))
263 $(foreach host,$(CFG_HOST), \
264 $(eval $(call DEF_LLVM_VARS,$(host))))
266 ######################################################################
267 # Exports for sub-utilities
268 ######################################################################
270 # Note that any variable that re-configure should pick up needs to be
276 export CFG_VERSION_WIN
278 export CFG_PACKAGE_NAME
281 export CFG_ENABLE_MINGW_CROSS
284 export CFG_LIBDIR_RELATIVE
285 export CFG_DISABLE_INJECT_STD_VERSION
287 ######################################################################
288 # Per-stage targets and runner
289 ######################################################################
294 # $(1) is the stage number
295 # $(2) is the target triple
296 # $(3) is the host triple
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)
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
308 # Preqrequisites for using the stageN compiler
310 HSREQ$(1)_H_$(3) = $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))
313 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
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
323 # Prerequisites for a working stageN compiler and libraries, for a specific
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))
330 # Prerequisites for a working stageN compiler and complete set of target
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))
338 # Don't run the stage0 compiler under valgrind - that ship has sailed
339 CFG_VALGRIND_COMPILE$(1) =
341 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
344 # Add RUSTFLAGS_STAGEN values to the build command
345 EXTRAFLAGS_STAGE$(1) = $$(RUSTFLAGS_STAGE$(1))
347 CFGFLAG$(1)_T_$(2)_H_$(3) = stage$(1)
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.
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))"
360 RPATH_VAR$(1)_T_$(2)_H_$(3) := \
361 LD_LIBRARY_PATH="$$$$LD_LIBRARY_PATH:$$(CURDIR)/$$(HLIB$(1)_H_$(3))"
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.
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.
376 ifneq ($(strip $(CFG_BUILD)),$(strip $(3)))
377 CFGFLAG$(1)_T_$(2)_H_$(3) = stage1
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))"
383 RPATH_VAR$(1)_T_$(2)_H_$(3) := \
384 LD_LIBRARY_PATH="$$$$LD_LIBRARY_PATH:$$(CURDIR)/$$(TLIB1_T_$(2)_H_$(CFG_BUILD))"
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)) \
398 PERF_STAGE$(1)_T_$(2)_H_$(3) := \
399 $$(Q)$$(call CFG_RUN_TARG_$(3),$(1), \
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)) \
408 $(foreach build,$(CFG_HOST), \
409 $(eval $(foreach target,$(CFG_TARGET), \
410 $(eval $(foreach stage,$(STAGES), \
411 $(eval $(call SREQ,$(stage),$(target),$(build))))))))
413 $(foreach build,$(CFG_HOST), \
414 $(eval $(foreach target,$(CFG_TARGET), \
415 $(eval $(foreach stage,$(STAGES), \
416 $(eval $(call SREQ_CMDS,$(stage),$(target),$(build))))))))
418 ######################################################################
421 # Builds a functional Rustc for the given host.
422 ######################################################################
424 define DEF_RUSTC_STAGE_TARGET
425 # $(1) == architecture
428 rustc-stage$(2)-H-$(1): \
429 $$(foreach target,$$(CFG_TARGET),$$(SREQ$(2)_T_$$(target)_H_$(1)))
433 $(foreach host,$(CFG_HOST), \
434 $(eval $(foreach stage,1 2 3, \
435 $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
437 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
438 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
439 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
441 define DEF_RUSTC_TARGET
442 # $(1) == architecture
444 rustc-H-$(1): rustc-stage2-H-$(1)
447 $(foreach host,$(CFG_TARGET), \
448 $(eval $(call DEF_RUSTC_TARGET,$(host))))
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)
455 rustc-H-all: $(foreach host,$(CFG_HOST),rustc-H-$(host))
457 ######################################################################
459 ######################################################################
464 ifneq ($$(findstring $(1),$$(CFG_HOST)),)
466 all-target-$(1)-host-$(2): $$(CSREQ2_T_$(1)_H_$(2))
468 # This is a target only
469 all-target-$(1)-host-$(2): $$(SREQ2_T_$(1)_H_$(2))
473 $(foreach target,$(CFG_TARGET), \
474 $(foreach host,$(CFG_HOST), \
475 $(eval $(call ALL_TARGET_N,$(target),$(host)))))
477 ALL_TARGET_RULES = $(foreach target,$(CFG_TARGET), \
478 $(foreach host,$(CFG_HOST), \
479 all-target-$(target)-host-$(host)))
481 all: $(ALL_TARGET_RULES) $(GENERATED) docs
483 ######################################################################
484 # Build system documentation
485 ######################################################################
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/^\# \?//'
493 $(call SHOW_DOCS,help)
496 $(call SHOW_DOCS,tips)
499 $(call SHOW_DOCS,nitty-gritty)