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 # We track all of the object files we might build so that we can find
59 # and include all of the .d files in one fell swoop.
62 MKFILE_DEPS := config.stamp $(call rwildcard,$(CFG_SRC_DIR)mk/,*)
63 NON_HOST_TRIPLES = $(filter-out $(CFG_HOST_TRIPLE),$(CFG_TARGET_TRIPLES))
65 ifneq ($(MAKE_RESTARTS),)
66 CFG_INFO := $(info cfg: make restarts: $(MAKE_RESTARTS))
69 CFG_INFO := $(info cfg: shell host triple $(CFG_HOST_TRIPLE))
71 ifneq ($(wildcard $(NON_HOST_TRIPLES)),)
72 CFG_INFO := $(info cfg: non host triples $(NON_HOST_TRIPLES))
75 CFG_RUSTC_FLAGS := $(RUSTFLAGS)
77 CFG_GCCISH_LINK_FLAGS :=
79 ifdef CFG_DISABLE_OPTIMIZE
80 $(info cfg: disabling rustc optimization (CFG_DISABLE_OPTIMIZE))
86 ifdef CFG_ENABLE_DEBUG
87 $(info cfg: enabling more debugging (CFG_ENABLE_DEBUG))
89 CFG_GCCISH_CFLAGS += -DRUST_DEBUG
91 CFG_GCCISH_CFLAGS += -DRUST_NDEBUG
95 CFG_RUSTC_FLAGS += --save-temps
98 CFG_RUSTC_FLAGS += -Z time-passes
100 ifdef TIME_LLVM_PASSES
101 CFG_RUSTC_FLAGS += -Z time-llvm-passes
104 # platform-specific auto-configuration
105 include $(CFG_SRC_DIR)mk/platform.mk
107 # Run the stage1/2 compilers under valgrind
108 ifdef VALGRIND_COMPILE
109 CFG_VALGRIND_COMPILE :=$(CFG_VALGRIND)
111 CFG_VALGRIND_COMPILE :=
114 CFG_RUNTIME :=$(call CFG_LIB_NAME,rustrt)
115 CFG_RUSTLLVM :=$(call CFG_LIB_NAME,rustllvm)
116 CFG_CORELIB :=$(call CFG_LIB_NAME,core)
117 CFG_STDLIB :=$(call CFG_LIB_NAME,std)
118 CFG_LIBRUSTC :=$(call CFG_LIB_NAME,rustc)
119 CFG_LIBSYNTAX :=$(call CFG_LIB_NAME,syntax)
121 STDLIB_GLOB :=$(call CFG_LIB_GLOB,std)
122 CORELIB_GLOB :=$(call CFG_LIB_GLOB,core)
123 LIBRUSTC_GLOB :=$(call CFG_LIB_GLOB,rustc)
124 LIBSYNTAX_GLOB :=$(call CFG_LIB_GLOB,syntax)
125 STDLIB_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,std)
126 CORELIB_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,core)
127 LIBRUSTC_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,rustc)
128 LIBSYNTAX_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,syntax)
130 # version-string calculation
131 CFG_GIT_DIR := $(CFG_SRC_DIR).git
133 CFG_VERSION = $(CFG_RELEASE)
135 ifneq ($(wildcard $(CFG_GIT)),)
136 ifneq ($(wildcard $(CFG_GIT_DIR)),)
137 CFG_VERSION += $(shell git --git-dir=$(CFG_GIT_DIR) log -1 \
138 --pretty=format:'(%h %ci)')
139 CFG_VER_HASH = $(shell git --git-dir=$(CFG_GIT_DIR) log -1 \
140 --pretty=format:'%H')
144 ifdef CFG_DISABLE_VALGRIND
145 $(info cfg: disabling valgrind (CFG_DISABLE_VALGRIND))
148 ifdef CFG_BAD_VALGRIND
149 $(info cfg: disabling valgrind due to its unreliability on this platform)
154 ######################################################################
155 # Target-and-rule "utility variables"
156 ######################################################################
167 X := $(CFG_EXE_SUFFIX)
169 # Look in doc and src dirs.
170 VPATH := $(S)doc $(S)src
172 # "Source" files we generate in builddir along the way.
175 # Delete the built-in rules.
183 ######################################################################
184 # Core library variables
185 ######################################################################
187 CORELIB_CRATE := $(S)src/libcore/core.rc
188 CORELIB_INPUTS := $(wildcard $(addprefix $(S)src/libcore/, \
189 core.rc *.rs */*.rs))
191 ######################################################################
192 # Standard library variables
193 ######################################################################
195 STDLIB_CRATE := $(S)src/libstd/std.rc
196 STDLIB_INPUTS := $(wildcard $(addprefix $(S)src/libstd/, \
199 ######################################################################
200 # rustc crate variables
201 ######################################################################
203 COMPILER_CRATE := $(S)src/rustc/rustc.rc
204 COMPILER_INPUTS := $(filter-out $(S)src/rustc/driver/rustc.rs, \
205 $(wildcard $(addprefix $(S)src/rustc/, \
206 rustc.rc *.rs */*.rs */*/*.rs */*/*/*.rs)))
208 LIBSYNTAX_CRATE := $(S)src/libsyntax/syntax.rc
209 LIBSYNTAX_INPUTS := $(wildcard $(addprefix $(S)src/libsyntax/, \
210 syntax.rc *.rs */*.rs */*/*.rs))
212 RUSTC_INPUTS := $(S)src/rustc/driver/rustc.rs
214 ######################################################################
216 ######################################################################
219 LLVM_COMPONENTS=x86 ipo bitreader bitwriter linker asmparser
222 # The configure script defines these variables with the target triples
223 # separated by Z. This defines new ones with the expected format.
224 CFG_LLVM_BUILD_DIR_$(1):=$$(CFG_LLVM_BUILD_DIR_$(subst -,_,$(1)))
225 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_INST_DIR_$(subst -,_,$(1)))
227 # Any rules that depend on LLVM should depend on LLVM_CONFIG
228 LLVM_CONFIG_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-config$$(X)
229 LLVM_MC_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-mc$$(X)
230 LLVM_VERSION_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --version)
231 LLVM_BINDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --bindir)
232 LLVM_INCDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --includedir)
233 LLVM_LIBDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libdir)
234 LLVM_LIBS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libs $$(LLVM_COMPONENTS))
235 LLVM_LDFLAGS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --ldflags)
236 # On FreeBSD, it may search wrong headers (that are for pre-installed LLVM),
237 # so we replace -I with -iquote to ensure that it searches bundled LLVM first.
238 LLVM_CXXFLAGS_$(1)=$$(subst -I, -iquote , $$(shell "$$(LLVM_CONFIG_$(1))" --cxxflags))
239 LLVM_HOST_TRIPLE_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --host-target)
241 LLVM_AS_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-as$$(X)
242 LLC_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llc$$(X)
246 $(foreach target,$(CFG_TARGET_TRIPLES), \
247 $(eval $(call DEF_LLVM_VARS,$(target))))
249 ######################################################################
250 # Exports for sub-utilities
251 ######################################################################
253 # Note that any variable that re-configure should pick up needs to be
259 export CFG_HOST_TRIPLE
261 export CFG_ENABLE_MINGW_CROSS
265 ######################################################################
267 ######################################################################
269 ######################################################################
270 # Per-stage targets and runner
271 ######################################################################
274 # $(1) is the stage number
275 # $(2) is the target triple
276 # $(3) is the host triple
278 # Destinations of artifacts for the host compiler
279 HROOT$(1)_H_$(3) = $(3)/stage$(1)
280 HBIN$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/bin
281 HLIB$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/$$(CFG_LIBDIR)
283 # Destinations of artifacts for target architectures
284 TROOT$(1)_T_$(2)_H_$(3) = $$(HLIB$(1)_H_$(3))/rustc/$(2)
285 TBIN$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/bin
286 TLIB$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/$$(CFG_LIBDIR)
288 # The name of the core and standard libraries used by rustc
289 ifdef CFG_DISABLE_SHAREDSTD
290 HCORELIB_DEFAULT$(1)_H_$(3) = \
291 $$(HLIB$(1)_H_$(3))/libcore.rlib
292 TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
293 $$(TLIB$(1)_T_$(2)_H_$(3))/libcore.rlib
295 HSTDLIB_DEFAULT$(1)_H_$(3) = \
296 $$(HLIB$(1)_H_$(3))/libstd.rlib
297 TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
298 $$(TLIB$(1)_T_$(2)_H_$(3))/libstd.rlib
300 HLIBRUSTC_DEFAULT$(1)_H_$(3) = \
301 $$(HLIB$(1)_H_$(3))/librustc.rlib
302 TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3) = \
303 $$(TLIB$(1)_T_$(2)_H_$(3))/librustc.rlib
305 HCORELIB_DEFAULT$(1)_H_$(3) = \
306 $$(HLIB$(1)_H_$(3))/$(CFG_CORELIB)
307 TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
308 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_CORELIB)
310 HSTDLIB_DEFAULT$(1)_H_$(3) = \
311 $$(HLIB$(1)_H_$(3))/$(CFG_STDLIB)
312 TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
313 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_STDLIB)
315 HLIBRUSTC_DEFAULT$(1)_H_$(3) = \
316 $$(HLIB$(1)_H_$(3))/$(CFG_LIBRUSTC)
317 TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3) = \
318 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_LIBRUSTC)
321 # Preqrequisites for using the stageN compiler
323 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
324 $$(HLIB$(1)_H_$(3))/$$(CFG_RUNTIME) \
325 $$(HLIB$(1)_H_$(3))/$$(CFG_RUSTLLVM) \
326 $$(HCORELIB_DEFAULT$(1)_H_$(3)) \
327 $$(HSTDLIB_DEFAULT$(1)_H_$(3)) \
328 $$(HLIBRUSTC_DEFAULT$(1)_H_$(3)) \
331 # Prerequisites for using the stageN compiler to build target artifacts
332 TSREQ$(1)_T_$(2)_H_$(3) = \
333 $$(HSREQ$(1)_H_$(3)) \
334 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_RUNTIME) \
335 $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a
337 # Prerequisites for complete stageN targets
338 SREQ$(1)_T_$(2)_H_$(3) = \
339 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
340 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_CORELIB) \
341 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_STDLIB) \
342 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC)
345 # Don't run the the stage0 compiler under valgrind - that ship has sailed
346 CFG_VALGRIND_COMPILE$(1) =
348 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
351 # Add RUSTFLAGS_STAGEN values to the build command
352 EXTRAFLAGS_STAGE$(1) = $$(RUSTFLAGS_STAGE$(1))
354 STAGE$(1)_T_$(2)_H_$(3) := \
355 $$(Q)$$(call CFG_RUN_TARG,$(1), \
356 $$(CFG_VALGRIND_COMPILE$(1)) \
357 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
359 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2))
361 PERF_STAGE$(1)_T_$(2)_H_$(3) := \
362 $$(Q)$$(call CFG_RUN_TARG,$(1), \
364 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
366 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2))
370 $(foreach build,$(CFG_TARGET_TRIPLES), \
371 $(eval $(foreach target,$(CFG_TARGET_TRIPLES), \
372 $(eval $(foreach stage,$(STAGES), \
373 $(eval $(call SREQ,$(stage),$(target),$(build))))))))
375 ######################################################################
378 # Builds a functional Rustc for the given host.
379 ######################################################################
381 define DEF_RUSTC_STAGE_TARGET
382 # $(1) == architecture
385 rustc-stage$(2)-H-$(1): \
386 $$(foreach target,$$(CFG_TARGET_TRIPLES), \
387 $$(SREQ$(2)_T_$$(target)_H_$(1)))
391 $(foreach host,$(CFG_TARGET_TRIPLES), \
392 $(eval $(foreach stage,1 2 3, \
393 $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
395 rustc-stage1: rustc-stage1-H-$(CFG_HOST_TRIPLE)
396 rustc-stage2: rustc-stage2-H-$(CFG_HOST_TRIPLE)
397 rustc-stage3: rustc-stage3-H-$(CFG_HOST_TRIPLE)
399 define DEF_RUSTC_TARGET
400 # $(1) == architecture
402 rustc-H-$(1): rustc-stage2-H-$(1)
405 $(foreach host,$(CFG_TARGET_TRIPLES), \
406 $(eval $(call DEF_RUSTC_TARGET,$(host))))
408 rustc-stage1: rustc-stage1-H-$(CFG_HOST_TRIPLE)
409 rustc-stage2: rustc-stage2-H-$(CFG_HOST_TRIPLE)
410 rustc-stage3: rustc-stage3-H-$(CFG_HOST_TRIPLE)
411 rustc: rustc-H-$(CFG_HOST_TRIPLE)
413 rustc-H-all: $(foreach host,$(CFG_TARGET_TRIPLES),rustc-H-$(host))
415 ######################################################################
417 ######################################################################
421 ifneq ($(CFG_IN_TRANSITION),)
423 CFG_INFO := $(info cfg:)
424 CFG_INFO := $(info cfg: *** compiler is in snapshot transition ***)
425 CFG_INFO := $(info cfg: *** stage2 and later will not be built ***)
426 CFG_INFO := $(info cfg:)
428 all: $(SREQ1$(CFG_HOST_TRIPLE)) $(GENERATED) docs
433 $(foreach target,$(CFG_TARGET_TRIPLES), \
434 $(SREQ3_T_$(target)_H_$(CFG_HOST_TRIPLE)))
435 FUZZ := $(HBIN2_H_$(CFG_HOST_TRIPLE))/fuzzer$(X)
436 CARGO := $(HBIN2_H_$(CFG_HOST_TRIPLE))/cargo$(X)
437 RUSTDOC := $(HBIN2_H_$(CFG_HOST_TRIPLE))/rustdoc$(X)
439 all: rustc $(GENERATED) docs $(FUZZ) $(CARGO) $(RUSTDOC)
444 ######################################################################
446 ######################################################################
448 ifndef CFG_DISABLE_MANAGE_SUBMODULES
449 # This is a pretty expensive operation but I don't see any way to avoid it
450 NEED_GIT_RECONFIG=$(shell cd "$(CFG_SRC_DIR)" && "$(CFG_GIT)" submodule status | grep -c '^\(+\|-\)')
455 ifeq ($(NEED_GIT_RECONFIG),0)
457 # If the submodules have changed then always execute config.mk
461 Makefile config.mk: config.stamp
463 config.stamp: $(S)configure $(S)Makefile.in $(S)src/snapshots.txt
464 @$(call E, cfg: reconfiguring)
465 $(Q)$(S)configure $(CFG_CONFIGURE_ARGS)
468 ######################################################################
469 # Primary-target makefiles
470 ######################################################################
472 include $(CFG_SRC_DIR)mk/target.mk
473 include $(CFG_SRC_DIR)mk/host.mk
474 include $(CFG_SRC_DIR)mk/stage0.mk
475 include $(CFG_SRC_DIR)mk/rt.mk
476 include $(CFG_SRC_DIR)mk/rustllvm.mk
477 include $(CFG_SRC_DIR)mk/tools.mk
478 include $(CFG_SRC_DIR)mk/docs.mk
479 include $(CFG_SRC_DIR)mk/llvm.mk
481 ######################################################################
482 # Secondary makefiles, conditionalized for speed
483 ######################################################################
485 ifneq ($(strip $(findstring dist,$(MAKECMDGOALS)) \
486 $(findstring check,$(MAKECMDGOALS)) \
487 $(findstring test,$(MAKECMDGOALS)) \
488 $(findstring tidy,$(MAKECMDGOALS)) \
489 $(findstring clean,$(MAKECMDGOALS))),)
490 CFG_INFO := $(info cfg: including dist rules)
491 include $(CFG_SRC_DIR)mk/dist.mk
494 ifneq ($(strip $(findstring snap,$(MAKECMDGOALS)) \
495 $(findstring clean,$(MAKECMDGOALS))),)
496 CFG_INFO := $(info cfg: including snap rules)
497 include $(CFG_SRC_DIR)mk/snap.mk
500 ifneq ($(findstring reformat,$(MAKECMDGOALS)),)
501 CFG_INFO := $(info cfg: including reformat rules)
502 include $(CFG_SRC_DIR)mk/pp.mk
505 ifneq ($(strip $(findstring check,$(MAKECMDGOALS)) \
506 $(findstring test,$(MAKECMDGOALS)) \
507 $(findstring perf,$(MAKECMDGOALS)) \
508 $(findstring tidy,$(MAKECMDGOALS))),)
509 CFG_INFO := $(info cfg: including test rules)
510 include $(CFG_SRC_DIR)mk/tests.mk
513 ifneq ($(findstring perf,$(MAKECMDGOALS)),)
514 CFG_INFO := $(info cfg: including perf rules)
515 include $(CFG_SRC_DIR)mk/perf.mk
518 ifneq ($(findstring clean,$(MAKECMDGOALS)),)
519 CFG_INFO := $(info cfg: including clean rules)
520 include $(CFG_SRC_DIR)mk/clean.mk
523 ifneq ($(findstring install,$(MAKECMDGOALS)),)
525 CFG_INFO := $(info cfg: setting CFG_PREFIX via DESTDIR, $(DESTDIR))
526 CFG_PREFIX:=$(DESTDIR)
530 CFG_INFO := $(info cfg: including install rules)
531 include $(CFG_SRC_DIR)mk/install.mk
534 ifneq ($(strip $(findstring TAGS.emacs,$(MAKECMDGOALS)) \
535 $(findstring TAGS.vi,$(MAKECMDGOALS))),)
536 CFG_INFO := $(info cfg: including ctags rules)
537 include $(CFG_SRC_DIR)mk/ctags.mk
540 # Find all of the .d files and include them to add information about
541 # header file dependencies.
542 ALL_DEP_FILES := $(ALL_OBJ_FILES:%.o=%.d)
543 -include $(ALL_DEP_FILES)