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)
110 STDLIB_GLOB :=$(call CFG_LIB_GLOB,std)
111 CORELIB_GLOB :=$(call CFG_LIB_GLOB,core)
112 LIBRUSTC_GLOB :=$(call CFG_LIB_GLOB,rustc)
113 STDLIB_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,std)
114 CORELIB_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,core)
115 LIBRUSTC_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,rustc)
117 # version-string calculation
118 CFG_GIT_DIR := $(CFG_SRC_DIR).git
120 CFG_VERSION = $(CFG_RELEASE)
122 ifneq ($(wildcard $(CFG_GIT)),)
123 ifneq ($(wildcard $(CFG_GIT_DIR)),)
124 CFG_VERSION += $(shell git --git-dir=$(CFG_GIT_DIR) log -1 \
125 --pretty=format:'(%h %ci)')
129 ifdef CFG_DISABLE_VALGRIND
130 $(info cfg: disabling valgrind (CFG_DISABLE_VALGRIND))
133 ifdef CFG_BAD_VALGRIND
134 $(info cfg: disabling valgrind due to its unreliability on this platform)
139 ######################################################################
140 # Target-and-rule "utility variables"
141 ######################################################################
152 X := $(CFG_EXE_SUFFIX)
154 # Look in doc and src dirs.
155 VPATH := $(S)doc $(S)src
157 # "Source" files we generate in builddir along the way.
160 # Delete the built-in rules.
168 ######################################################################
169 # Core library variables
170 ######################################################################
172 CORELIB_CRATE := $(S)src/libcore/core.rc
173 CORELIB_INPUTS := $(wildcard $(addprefix $(S)src/libcore/, \
174 core.rc *.rs */*.rs))
176 ######################################################################
177 # Standard library variables
178 ######################################################################
180 STDLIB_CRATE := $(S)src/libstd/std.rc
181 STDLIB_INPUTS := $(wildcard $(addprefix $(S)src/libstd/, \
184 ######################################################################
185 # rustc crate variables
186 ######################################################################
188 COMPILER_CRATE := $(S)src/rustc/rustc.rc
189 COMPILER_INPUTS := $(filter-out $(S)src/rustc/driver/rustc.rs, \
190 $(wildcard $(addprefix $(S)src/rustc/, \
191 rustc.rc *.rs */*.rs */*/*.rs)))
193 RUSTC_INPUTS := $(S)src/rustc/driver/rustc.rs
195 ######################################################################
197 ######################################################################
200 LLVM_COMPONENTS=x86 ipo bitreader bitwriter linker asmparser
203 # The configure script defines these variables with the target triples
204 # separated by Z. This defines new ones with the expected format.
205 CFG_LLVM_BUILD_DIR_$(1):=$$(CFG_LLVM_BUILD_DIR_$(subst -,_,$(1)))
206 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_INST_DIR_$(subst -,_,$(1)))
208 # Any rules that depend on LLVM should depend on LLVM_CONFIG
209 LLVM_CONFIG_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-config$$(X)
210 LLVM_MC_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-mc$$(X)
211 LLVM_VERSION_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --version)
212 LLVM_BINDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --bindir)
213 LLVM_INCDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --includedir)
214 LLVM_LIBDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libdir)
215 LLVM_LIBS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libs $$(LLVM_COMPONENTS))
216 LLVM_LDFLAGS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --ldflags)
217 # On FreeBSD, it may search wrong headers (that are for pre-installed LLVM),
218 # so we replace -I with -iquote to ensure that it searches bundled LLVM first.
219 LLVM_CXXFLAGS_$(1)=$$(subst -I, -iquote , $$(shell "$$(LLVM_CONFIG_$(1))" --cxxflags))
220 LLVM_HOST_TRIPLE_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --host-target)
222 LLVM_AS_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-as$$(X)
223 LLC_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llc$$(X)
227 $(foreach target,$(CFG_TARGET_TRIPLES), \
228 $(eval $(call DEF_LLVM_VARS,$(target))))
230 ######################################################################
231 # Exports for sub-utilities
232 ######################################################################
234 # Note that any variable that re-configure should pick up needs to be
240 export CFG_HOST_TRIPLE
242 export CFG_ENABLE_MINGW_CROSS
246 ######################################################################
248 ######################################################################
250 ######################################################################
251 # Per-stage targets and runner
252 ######################################################################
255 # $(1) is the stage number
256 # $(2) is the target triple
257 # $(3) is the host triple
259 # Destinations of artifacts for the host compiler
260 HROOT$(1)_H_$(3) = $(3)/stage$(1)
261 HBIN$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/bin
262 HLIB$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/$$(CFG_LIBDIR)
264 # Destinations of artifacts for target architectures
265 TROOT$(1)_T_$(2)_H_$(3) = $$(HLIB$(1)_H_$(3))/rustc/$(2)
266 TBIN$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/bin
267 TLIB$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/$$(CFG_LIBDIR)
269 # The name of the core and standard libraries used by rustc
270 ifdef CFG_DISABLE_SHAREDSTD
271 HCORELIB_DEFAULT$(1)_H_$(3) = \
272 $$(HLIB$(1)_H_$(3))/libcore.rlib
273 TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
274 $$(TLIB$(1)_T_$(2)_H_$(3))/libcore.rlib
276 HSTDLIB_DEFAULT$(1)_H_$(3) = \
277 $$(HLIB$(1)_H_$(3))/libstd.rlib
278 TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
279 $$(TLIB$(1)_T_$(2)_H_$(3))/libstd.rlib
281 HLIBRUSTC_DEFAULT$(1)_H_$(3) = \
282 $$(HLIB$(1)_H_$(3))/librustc.rlib
283 TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3) = \
284 $$(TLIB$(1)_T_$(2)_H_$(3))/librustc.rlib
286 HCORELIB_DEFAULT$(1)_H_$(3) = \
287 $$(HLIB$(1)_H_$(3))/$(CFG_CORELIB)
288 TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
289 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_CORELIB)
291 HSTDLIB_DEFAULT$(1)_H_$(3) = \
292 $$(HLIB$(1)_H_$(3))/$(CFG_STDLIB)
293 TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
294 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_STDLIB)
296 HLIBRUSTC_DEFAULT$(1)_H_$(3) = \
297 $$(HLIB$(1)_H_$(3))/$(CFG_LIBRUSTC)
298 TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3) = \
299 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_LIBRUSTC)
302 # Preqrequisites for using the stageN compiler
304 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
305 $$(HLIB$(1)_H_$(3))/$$(CFG_RUNTIME) \
306 $$(HLIB$(1)_H_$(3))/$$(CFG_RUSTLLVM) \
307 $$(HCORELIB_DEFAULT$(1)_H_$(3)) \
308 $$(HSTDLIB_DEFAULT$(1)_H_$(3)) \
309 $$(HLIBRUSTC_DEFAULT$(1)_H_$(3)) \
312 # Prerequisites for using the stageN compiler to build target artifacts
313 TSREQ$(1)_T_$(2)_H_$(3) = \
314 $$(HSREQ$(1)_H_$(3)) \
315 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_RUNTIME) \
316 $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a
318 # Prerequisites for complete stageN targets
319 SREQ$(1)_T_$(2)_H_$(3) = \
320 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
321 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_CORELIB) \
322 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_STDLIB) \
323 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC)
326 # Don't run the the stage0 compiler under valgrind - that ship has sailed
327 CFG_VALGRIND_COMPILE$(1) =
329 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
332 STAGE$(1)_T_$(2)_H_$(3) := \
333 $$(Q)$$(call CFG_RUN_TARG,$(1), \
334 $$(CFG_VALGRIND_COMPILE$(1)) \
335 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
336 $$(CFG_RUSTC_FLAGS) --target=$(2))
338 PERF_STAGE$(1)_T_$(2)_H_$(3) := \
339 $$(Q)$$(call CFG_RUN_TARG,$(1), \
341 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
342 $$(CFG_RUSTC_FLAGS) --target=$(2))
346 $(foreach build,$(CFG_TARGET_TRIPLES), \
347 $(eval $(foreach target,$(CFG_TARGET_TRIPLES), \
348 $(eval $(foreach stage,$(STAGES), \
349 $(eval $(call SREQ,$(stage),$(target),$(build))))))))
351 ######################################################################
354 # Builds a functional Rustc for the given host.
355 ######################################################################
357 define DEF_RUSTC_STAGE_TARGET
358 # $(1) == architecture
361 rustc-stage$(2)-H-$(1): \
362 $$(foreach target,$$(CFG_TARGET_TRIPLES), \
363 $$(SREQ$(2)_T_$$(target)_H_$(1)))
367 $(foreach host,$(CFG_TARGET_TRIPLES), \
368 $(eval $(foreach stage,1 2 3, \
369 $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
371 rustc-stage1: rustc-stage1-H-$(CFG_HOST_TRIPLE)
372 rustc-stage2: rustc-stage2-H-$(CFG_HOST_TRIPLE)
373 rustc-stage3: rustc-stage3-H-$(CFG_HOST_TRIPLE)
375 define DEF_RUSTC_TARGET
376 # $(1) == architecture
378 rustc-H-$(1): rustc-stage2-H-$(1)
381 $(foreach host,$(CFG_TARGET_TRIPLES), \
382 $(eval $(call DEF_RUSTC_TARGET,$(host))))
384 rustc-stage1: rustc-stage1-H-$(CFG_HOST_TRIPLE)
385 rustc-stage2: rustc-stage2-H-$(CFG_HOST_TRIPLE)
386 rustc-stage3: rustc-stage3-H-$(CFG_HOST_TRIPLE)
387 rustc: rustc-H-$(CFG_HOST_TRIPLE)
389 rustc-H-all: $(foreach host,$(CFG_TARGET_TRIPLES),rustc-H-$(host))
391 ######################################################################
393 ######################################################################
397 ifneq ($(CFG_IN_TRANSITION),)
399 CFG_INFO := $(info cfg:)
400 CFG_INFO := $(info cfg: *** compiler is in snapshot transition ***)
401 CFG_INFO := $(info cfg: *** stage2 and later will not be built ***)
402 CFG_INFO := $(info cfg:)
404 all: $(SREQ1$(CFG_HOST_TRIPLE)) $(GENERATED) docs
409 $(foreach target,$(CFG_TARGET_TRIPLES), \
410 $(SREQ3_T_$(target)_H_$(CFG_HOST_TRIPLE)))
411 FUZZ := $(HBIN3_H_$(CFG_HOST_TRIPLE))/fuzzer$(X)
412 CARGO := $(HBIN3_H_$(CFG_HOST_TRIPLE))/cargo$(X)
413 RUSTDOC := $(HBIN3_H_$(CFG_HOST_TRIPLE))/rustdoc$(X)
415 all: rustc $(GENERATED) docs $(FUZZ) $(CARGO) $(RUSTDOC)
420 ######################################################################
422 ######################################################################
424 ifndef CFG_DISABLE_MANAGE_SUBMODULES
425 # This is a pretty expensive operation but I don't see any way to avoid it
426 NEED_GIT_RECONFIG=$(shell cd "$(CFG_SRC_DIR)" && "$(CFG_GIT)" submodule status | grep -c '^\(+\|-\)')
431 ifeq ($(NEED_GIT_RECONFIG),0)
433 # If the submodules have changed then always execute config.mk
437 Makefile config.mk: config.stamp
439 config.stamp: $(S)configure $(S)Makefile.in $(S)src/snapshots.txt
440 @$(call E, cfg: reconfiguring)
441 $(Q)$(S)configure $(CFG_CONFIGURE_ARGS)
445 ######################################################################
446 # Primary-target makefiles
447 ######################################################################
449 include $(CFG_SRC_DIR)mk/target.mk
450 include $(CFG_SRC_DIR)mk/host.mk
451 include $(CFG_SRC_DIR)mk/stage0.mk
452 include $(CFG_SRC_DIR)mk/rt.mk
453 include $(CFG_SRC_DIR)mk/rustllvm.mk
454 include $(CFG_SRC_DIR)mk/autodep.mk
455 include $(CFG_SRC_DIR)mk/tools.mk
456 include $(CFG_SRC_DIR)mk/docs.mk
457 include $(CFG_SRC_DIR)mk/llvm.mk
459 ######################################################################
460 # Secondary makefiles, conditionalized for speed
461 ######################################################################
463 ifneq ($(strip $(findstring dist,$(MAKECMDGOALS)) \
464 $(findstring check,$(MAKECMDGOALS)) \
465 $(findstring test,$(MAKECMDGOALS)) \
466 $(findstring tidy,$(MAKECMDGOALS)) \
467 $(findstring clean,$(MAKECMDGOALS))),)
468 CFG_INFO := $(info cfg: including dist rules)
469 include $(CFG_SRC_DIR)mk/dist.mk
472 ifneq ($(strip $(findstring snap,$(MAKECMDGOALS)) \
473 $(findstring clean,$(MAKECMDGOALS))),)
474 CFG_INFO := $(info cfg: including snap rules)
475 include $(CFG_SRC_DIR)mk/snap.mk
478 ifneq ($(findstring reformat,$(MAKECMDGOALS)),)
479 CFG_INFO := $(info cfg: including reformat rules)
480 include $(CFG_SRC_DIR)mk/pp.mk
483 ifneq ($(strip $(findstring check,$(MAKECMDGOALS)) \
484 $(findstring test,$(MAKECMDGOALS)) \
485 $(findstring perf,$(MAKECMDGOALS)) \
486 $(findstring tidy,$(MAKECMDGOALS))),)
487 CFG_INFO := $(info cfg: including test rules)
488 include $(CFG_SRC_DIR)mk/tests.mk
491 ifneq ($(findstring perf,$(MAKECMDGOALS)),)
492 CFG_INFO := $(info cfg: including perf rules)
493 include $(CFG_SRC_DIR)mk/perf.mk
496 ifneq ($(findstring clean,$(MAKECMDGOALS)),)
497 CFG_INFO := $(info cfg: including clean rules)
498 include $(CFG_SRC_DIR)mk/clean.mk
501 ifneq ($(findstring install,$(MAKECMDGOALS)),)
503 CFG_INFO := $(info cfg: setting CFG_PREFIX via DESTDIR, $(DESTDIR))
504 CFG_PREFIX:=$(DESTDIR)
508 CFG_INFO := $(info cfg: including install rules)
509 include $(CFG_SRC_DIR)mk/install.mk
512 ifneq ($(strip $(findstring TAGS.emacs,$(MAKECMDGOALS)) \
513 $(findstring TAGS.vi,$(MAKECMDGOALS))),)
514 CFG_INFO := $(info cfg: including ctags rules)
515 include $(CFG_SRC_DIR)mk/ctags.mk