1 # An explanation of how the build is structured:
3 # There are multiple build stages (0-3) needed to verify that the
4 # compiler is properly self-hosting. Each stage is divided between
5 # 'host' artifacts and 'target' artifacts, where the stageN host
6 # compiler builds artifacts for 1 or more stageN target architectures.
7 # Once the stageN target compiler has been built for the host
8 # architecture it is promoted (copied) to a stageN+1 host artifact.
10 # The stage3 host compiler is a compiler that successfully builds
11 # itself and should (in theory) be bitwise identical to the stage2
12 # host compiler. The process is bootstrapped using a stage0 host
13 # compiler downloaded from a previous snapshot.
15 # At no time should stageN artifacts be interacting with artifacts
16 # from other stages. For consistency, we use the 'promotion' logic
17 # for all artifacts, even those that don't make sense on non-host
20 # The directory layout for a stage is intended to match the layout
21 # of the installed compiler, and looks like the following:
23 # stageN - this is the system root, corresponding to, e.g. /usr
24 # bin - binaries compiled for the host
25 # lib - libraries used by the host compiler
26 # rustc - rustc's own place to organize libraries
27 # $(target) - target-specific artifacts
28 # bin - binaries for target architectures
29 # lib - libraries for target architectures
31 # A note about host libraries:
33 # The only libraries that get promoted to stageN/lib are those needed
34 # by rustc. In general, rust programs, even those compiled for the
35 # host architecture will use libraries from the target
36 # directories. This gives rust some freedom to experiment with how
37 # libraries are managed and versioned without polluting the common
38 # areas of the filesystem.
40 # General rust binaries may stil live in the host bin directory; they
41 # will just link against the libraries in the target lib directory.
43 # Admittedly this is a little convoluted.
47 ######################################################################
48 # Residual auto-configuration
49 ######################################################################
51 # Recursive wildcard function
52 # http://blog.jgc.org/2011/07/gnu-make-recursive-wildcard-function.html
53 rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) \
54 $(filter $(subst *,%,$2),$d))
58 MKFILE_DEPS := config.stamp $(call rwildcard,$(CFG_SRC_DIR)mk/,*)
59 NON_HOST_TRIPLES = $(filter-out $(CFG_HOST_TRIPLE),$(CFG_TARGET_TRIPLES))
61 ifneq ($(MAKE_RESTARTS),)
62 CFG_INFO := $(info cfg: make restarts: $(MAKE_RESTARTS))
65 CFG_INFO := $(info cfg: shell host triple $(CFG_HOST_TRIPLE))
67 ifneq ($(wildcard $(NON_HOST_TRIPLES)),)
68 CFG_INFO := $(info cfg: non host triples $(NON_HOST_TRIPLES))
71 ifdef CFG_DISABLE_OPTIMIZE
72 $(info cfg: disabling rustc optimization (CFG_DISABLE_OPTIMIZE))
79 CFG_RUSTC_FLAGS += --save-temps
81 ifdef ENFORCE_MUT_VARS
82 CFG_RUSTC_FLAGS += --enforce-mut-vars
85 CFG_RUSTC_FLAGS += --time-passes
87 ifdef TIME_LLVM_PASSES
88 CFG_RUSTC_FLAGS += --time-llvm-passes
94 # platform-specific auto-configuration
95 include $(CFG_SRC_DIR)mk/platform.mk
97 # Run the stage1/2 compilers under valgrind
98 ifdef VALGRIND_COMPILE
99 CFG_VALGRIND_COMPILE :=$(CFG_VALGRIND)
101 CFG_VALGRIND_COMPILE :=
104 CFG_RUNTIME :=$(call CFG_LIB_NAME,rustrt)
105 CFG_RUSTLLVM :=$(call CFG_LIB_NAME,rustllvm)
106 CFG_CORELIB :=$(call CFG_LIB_NAME,core)
107 CFG_STDLIB :=$(call CFG_LIB_NAME,std)
108 CFG_LIBRUSTC :=$(call CFG_LIB_NAME,rustc)
109 CFG_LIBRUSTSYNTAX :=$(call CFG_LIB_NAME,rustsyntax)
111 STDLIB_GLOB :=$(call CFG_LIB_GLOB,std)
112 CORELIB_GLOB :=$(call CFG_LIB_GLOB,core)
113 LIBRUSTC_GLOB :=$(call CFG_LIB_GLOB,rustc)
114 LIBRUSTSYNTAX_GLOB :=$(call CFG_LIB_GLOB,rustsyntax)
115 STDLIB_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,std)
116 CORELIB_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,core)
117 LIBRUSTC_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,rustc)
118 LIBRUSTSYNTAX_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,rustsyntax)
120 # version-string calculation
121 CFG_GIT_DIR := $(CFG_SRC_DIR).git
123 CFG_VERSION = $(CFG_RELEASE)
125 ifneq ($(wildcard $(CFG_GIT)),)
126 ifneq ($(wildcard $(CFG_GIT_DIR)),)
127 CFG_VERSION += $(shell git --git-dir=$(CFG_GIT_DIR) log -1 \
128 --pretty=format:'(%h %ci)')
132 ifdef CFG_DISABLE_VALGRIND
133 $(info cfg: disabling valgrind (CFG_DISABLE_VALGRIND))
136 ifdef CFG_BAD_VALGRIND
137 $(info cfg: disabling valgrind due to its unreliability on this platform)
142 ######################################################################
143 # Target-and-rule "utility variables"
144 ######################################################################
155 X := $(CFG_EXE_SUFFIX)
157 # Look in doc and src dirs.
158 VPATH := $(S)doc $(S)src
160 # "Source" files we generate in builddir along the way.
163 # Delete the built-in rules.
171 ######################################################################
172 # Core library variables
173 ######################################################################
175 CORELIB_CRATE := $(S)src/libcore/core.rc
176 CORELIB_INPUTS := $(wildcard $(addprefix $(S)src/libcore/, \
177 core.rc *.rs */*.rs))
179 ######################################################################
180 # Standard library variables
181 ######################################################################
183 STDLIB_CRATE := $(S)src/libstd/std.rc
184 STDLIB_INPUTS := $(wildcard $(addprefix $(S)src/libstd/, \
187 ######################################################################
188 # rustc crate variables
189 ######################################################################
191 COMPILER_CRATE := $(S)src/rustc/rustc.rc
192 COMPILER_INPUTS := $(filter-out $(S)src/rustc/driver/rustc.rs, \
193 $(wildcard $(addprefix $(S)src/rustc/, \
194 rustc.rc *.rs */*.rs */*/*.rs)))
196 LIBRUSTSYNTAX_CRATE := $(S)src/librustsyntax/rustsyntax.rc
197 LIBRUSTSYNTAX_INPUTS := $(wildcard $(addprefix $(S)src/librustsyntax/, \
200 RUSTC_INPUTS := $(S)src/rustc/driver/rustc.rs
202 ######################################################################
204 ######################################################################
207 LLVM_COMPONENTS=x86 ipo bitreader bitwriter linker asmparser
210 # The configure script defines these variables with the target triples
211 # separated by Z. This defines new ones with the expected format.
212 CFG_LLVM_BUILD_DIR_$(1):=$$(CFG_LLVM_BUILD_DIR_$(subst -,_,$(1)))
213 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_INST_DIR_$(subst -,_,$(1)))
215 # Any rules that depend on LLVM should depend on LLVM_CONFIG
216 LLVM_CONFIG_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-config$$(X)
217 LLVM_MC_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-mc$$(X)
218 LLVM_VERSION_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --version)
219 LLVM_BINDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --bindir)
220 LLVM_INCDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --includedir)
221 LLVM_LIBDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libdir)
222 LLVM_LIBS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libs $$(LLVM_COMPONENTS))
223 LLVM_LDFLAGS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --ldflags)
224 # On FreeBSD, it may search wrong headers (that are for pre-installed LLVM),
225 # so we replace -I with -iquote to ensure that it searches bundled LLVM first.
226 LLVM_CXXFLAGS_$(1)=$$(subst -I, -iquote , $$(shell "$$(LLVM_CONFIG_$(1))" --cxxflags))
227 LLVM_HOST_TRIPLE_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --host-target)
229 LLVM_AS_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-as$$(X)
230 LLC_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llc$$(X)
234 $(foreach target,$(CFG_TARGET_TRIPLES), \
235 $(eval $(call DEF_LLVM_VARS,$(target))))
237 ######################################################################
238 # Exports for sub-utilities
239 ######################################################################
241 # Note that any variable that re-configure should pick up needs to be
247 export CFG_HOST_TRIPLE
249 export CFG_ENABLE_MINGW_CROSS
253 ######################################################################
255 ######################################################################
257 ######################################################################
258 # Per-stage targets and runner
259 ######################################################################
262 # $(1) is the stage number
263 # $(2) is the target triple
264 # $(3) is the host triple
266 # Destinations of artifacts for the host compiler
267 HROOT$(1)_H_$(3) = $(3)/stage$(1)
268 HBIN$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/bin
269 HLIB$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/$$(CFG_LIBDIR)
271 # Destinations of artifacts for target architectures
272 TROOT$(1)_T_$(2)_H_$(3) = $$(HLIB$(1)_H_$(3))/rustc/$(2)
273 TBIN$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/bin
274 TLIB$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/$$(CFG_LIBDIR)
276 # The name of the core and standard libraries used by rustc
277 ifdef CFG_DISABLE_SHAREDSTD
278 HCORELIB_DEFAULT$(1)_H_$(3) = \
279 $$(HLIB$(1)_H_$(3))/libcore.rlib
280 TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
281 $$(TLIB$(1)_T_$(2)_H_$(3))/libcore.rlib
283 HSTDLIB_DEFAULT$(1)_H_$(3) = \
284 $$(HLIB$(1)_H_$(3))/libstd.rlib
285 TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
286 $$(TLIB$(1)_T_$(2)_H_$(3))/libstd.rlib
288 HLIBRUSTC_DEFAULT$(1)_H_$(3) = \
289 $$(HLIB$(1)_H_$(3))/librustc.rlib
290 TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3) = \
291 $$(TLIB$(1)_T_$(2)_H_$(3))/librustc.rlib
293 HCORELIB_DEFAULT$(1)_H_$(3) = \
294 $$(HLIB$(1)_H_$(3))/$(CFG_CORELIB)
295 TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
296 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_CORELIB)
298 HSTDLIB_DEFAULT$(1)_H_$(3) = \
299 $$(HLIB$(1)_H_$(3))/$(CFG_STDLIB)
300 TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
301 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_STDLIB)
303 HLIBRUSTC_DEFAULT$(1)_H_$(3) = \
304 $$(HLIB$(1)_H_$(3))/$(CFG_LIBRUSTC)
305 TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3) = \
306 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_LIBRUSTC)
309 # Preqrequisites for using the stageN compiler
311 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
312 $$(HLIB$(1)_H_$(3))/$$(CFG_RUNTIME) \
313 $$(HLIB$(1)_H_$(3))/$$(CFG_RUSTLLVM) \
314 $$(HCORELIB_DEFAULT$(1)_H_$(3)) \
315 $$(HSTDLIB_DEFAULT$(1)_H_$(3)) \
316 $$(HLIBRUSTC_DEFAULT$(1)_H_$(3)) \
319 # Prerequisites for using the stageN compiler to build target artifacts
320 TSREQ$(1)_T_$(2)_H_$(3) = \
321 $$(HSREQ$(1)_H_$(3)) \
322 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_RUNTIME) \
323 $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a
325 # Prerequisites for complete stageN targets
326 SREQ$(1)_T_$(2)_H_$(3) = \
327 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
328 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_CORELIB) \
329 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_STDLIB) \
330 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC)
333 # Don't run the the stage0 compiler under valgrind - that ship has sailed
334 CFG_VALGRIND_COMPILE$(1) =
336 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
339 STAGE$(1)_T_$(2)_H_$(3) := \
340 $$(Q)$$(call CFG_RUN_TARG,$(1), \
341 $$(CFG_VALGRIND_COMPILE$(1)) \
342 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
343 $$(CFG_RUSTC_FLAGS) --target=$(2))
345 PERF_STAGE$(1)_T_$(2)_H_$(3) := \
346 $$(Q)$$(call CFG_RUN_TARG,$(1), \
348 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
349 $$(CFG_RUSTC_FLAGS) --target=$(2))
353 $(foreach build,$(CFG_TARGET_TRIPLES), \
354 $(eval $(foreach target,$(CFG_TARGET_TRIPLES), \
355 $(eval $(foreach stage,$(STAGES), \
356 $(eval $(call SREQ,$(stage),$(target),$(build))))))))
358 ######################################################################
361 # Builds a functional Rustc for the given host.
362 ######################################################################
364 define DEF_RUSTC_STAGE_TARGET
365 # $(1) == architecture
368 rustc-stage$(2)-H-$(1): \
369 $$(foreach target,$$(CFG_TARGET_TRIPLES), \
370 $$(SREQ$(2)_T_$$(target)_H_$(1)))
374 $(foreach host,$(CFG_TARGET_TRIPLES), \
375 $(eval $(foreach stage,1 2 3, \
376 $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
378 rustc-stage1: rustc-stage1-H-$(CFG_HOST_TRIPLE)
379 rustc-stage2: rustc-stage2-H-$(CFG_HOST_TRIPLE)
380 rustc-stage3: rustc-stage3-H-$(CFG_HOST_TRIPLE)
382 define DEF_RUSTC_TARGET
383 # $(1) == architecture
385 rustc-H-$(1): rustc-stage2-H-$(1)
388 $(foreach host,$(CFG_TARGET_TRIPLES), \
389 $(eval $(call DEF_RUSTC_TARGET,$(host))))
391 rustc-stage1: rustc-stage1-H-$(CFG_HOST_TRIPLE)
392 rustc-stage2: rustc-stage2-H-$(CFG_HOST_TRIPLE)
393 rustc-stage3: rustc-stage3-H-$(CFG_HOST_TRIPLE)
394 rustc: rustc-H-$(CFG_HOST_TRIPLE)
396 rustc-H-all: $(foreach host,$(CFG_TARGET_TRIPLES),rustc-H-$(host))
398 ######################################################################
400 ######################################################################
404 ifneq ($(CFG_IN_TRANSITION),)
406 CFG_INFO := $(info cfg:)
407 CFG_INFO := $(info cfg: *** compiler is in snapshot transition ***)
408 CFG_INFO := $(info cfg: *** stage2 and later will not be built ***)
409 CFG_INFO := $(info cfg:)
411 all: $(SREQ1$(CFG_HOST_TRIPLE)) $(GENERATED) docs
416 $(foreach target,$(CFG_TARGET_TRIPLES), \
417 $(SREQ3_T_$(target)_H_$(CFG_HOST_TRIPLE)))
418 FUZZ := $(HBIN3_H_$(CFG_HOST_TRIPLE))/fuzzer$(X)
419 CARGO := $(HBIN3_H_$(CFG_HOST_TRIPLE))/cargo$(X)
420 RUSTDOC := $(HBIN3_H_$(CFG_HOST_TRIPLE))/rustdoc$(X)
422 all: rustc $(GENERATED) docs $(FUZZ) $(CARGO) $(RUSTDOC)
427 ######################################################################
429 ######################################################################
431 ifndef CFG_DISABLE_MANAGE_SUBMODULES
432 # This is a pretty expensive operation but I don't see any way to avoid it
433 NEED_GIT_RECONFIG=$(shell cd "$(CFG_SRC_DIR)" && "$(CFG_GIT)" submodule status | grep -c '^\(+\|-\)')
438 ifeq ($(NEED_GIT_RECONFIG),0)
440 # If the submodules have changed then always execute config.mk
444 Makefile config.mk: config.stamp
446 config.stamp: $(S)configure $(S)Makefile.in $(S)src/snapshots.txt
447 @$(call E, cfg: reconfiguring)
448 $(Q)$(S)configure $(CFG_CONFIGURE_ARGS)
451 ######################################################################
452 # Primary-target makefiles
453 ######################################################################
455 include $(CFG_SRC_DIR)mk/target.mk
456 include $(CFG_SRC_DIR)mk/host.mk
457 include $(CFG_SRC_DIR)mk/stage0.mk
458 include $(CFG_SRC_DIR)mk/rt.mk
459 include $(CFG_SRC_DIR)mk/rustllvm.mk
460 include $(CFG_SRC_DIR)mk/autodep.mk
461 include $(CFG_SRC_DIR)mk/tools.mk
462 include $(CFG_SRC_DIR)mk/docs.mk
463 include $(CFG_SRC_DIR)mk/llvm.mk
465 ######################################################################
466 # Secondary makefiles, conditionalized for speed
467 ######################################################################
469 ifneq ($(strip $(findstring dist,$(MAKECMDGOALS)) \
470 $(findstring check,$(MAKECMDGOALS)) \
471 $(findstring test,$(MAKECMDGOALS)) \
472 $(findstring tidy,$(MAKECMDGOALS)) \
473 $(findstring clean,$(MAKECMDGOALS))),)
474 CFG_INFO := $(info cfg: including dist rules)
475 include $(CFG_SRC_DIR)mk/dist.mk
478 ifneq ($(strip $(findstring snap,$(MAKECMDGOALS)) \
479 $(findstring clean,$(MAKECMDGOALS))),)
480 CFG_INFO := $(info cfg: including snap rules)
481 include $(CFG_SRC_DIR)mk/snap.mk
484 ifneq ($(findstring reformat,$(MAKECMDGOALS)),)
485 CFG_INFO := $(info cfg: including reformat rules)
486 include $(CFG_SRC_DIR)mk/pp.mk
489 ifneq ($(strip $(findstring check,$(MAKECMDGOALS)) \
490 $(findstring test,$(MAKECMDGOALS)) \
491 $(findstring perf,$(MAKECMDGOALS)) \
492 $(findstring tidy,$(MAKECMDGOALS))),)
493 CFG_INFO := $(info cfg: including test rules)
494 include $(CFG_SRC_DIR)mk/tests.mk
497 ifneq ($(findstring perf,$(MAKECMDGOALS)),)
498 CFG_INFO := $(info cfg: including perf rules)
499 include $(CFG_SRC_DIR)mk/perf.mk
502 ifneq ($(findstring clean,$(MAKECMDGOALS)),)
503 CFG_INFO := $(info cfg: including clean rules)
504 include $(CFG_SRC_DIR)mk/clean.mk
507 ifneq ($(findstring install,$(MAKECMDGOALS)),)
509 CFG_INFO := $(info cfg: setting CFG_PREFIX via DESTDIR, $(DESTDIR))
510 CFG_PREFIX:=$(DESTDIR)
514 CFG_INFO := $(info cfg: including install rules)
515 include $(CFG_SRC_DIR)mk/install.mk
518 ifneq ($(strip $(findstring TAGS.emacs,$(MAKECMDGOALS)) \
519 $(findstring TAGS.vi,$(MAKECMDGOALS))),)
520 CFG_INFO := $(info cfg: including ctags rules)
521 include $(CFG_SRC_DIR)mk/ctags.mk