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 CFG_RUSTC_FLAGS += -Z trace
107 # platform-specific auto-configuration
108 include $(CFG_SRC_DIR)mk/platform.mk
110 # Run the stage1/2 compilers under valgrind
111 ifdef VALGRIND_COMPILE
112 CFG_VALGRIND_COMPILE :=$(CFG_VALGRIND)
114 CFG_VALGRIND_COMPILE :=
117 CFG_RUNTIME :=$(call CFG_LIB_NAME,rustrt)
118 CFG_RUSTLLVM :=$(call CFG_LIB_NAME,rustllvm)
119 CFG_CORELIB :=$(call CFG_LIB_NAME,core)
120 CFG_STDLIB :=$(call CFG_LIB_NAME,std)
121 CFG_LIBRUSTC :=$(call CFG_LIB_NAME,rustc)
122 CFG_LIBSYNTAX :=$(call CFG_LIB_NAME,syntax)
124 STDLIB_GLOB :=$(call CFG_LIB_GLOB,std)
125 CORELIB_GLOB :=$(call CFG_LIB_GLOB,core)
126 LIBRUSTC_GLOB :=$(call CFG_LIB_GLOB,rustc)
127 LIBSYNTAX_GLOB :=$(call CFG_LIB_GLOB,syntax)
128 STDLIB_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,std)
129 CORELIB_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,core)
130 LIBRUSTC_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,rustc)
131 LIBSYNTAX_DSYM_GLOB :=$(call CFG_LIB_DSYM_GLOB,syntax)
133 # version-string calculation
134 CFG_GIT_DIR := $(CFG_SRC_DIR).git
136 CFG_VERSION = $(CFG_RELEASE)
138 ifneq ($(wildcard $(CFG_GIT)),)
139 ifneq ($(wildcard $(CFG_GIT_DIR)),)
140 CFG_VERSION += $(shell git --git-dir=$(CFG_GIT_DIR) log -1 \
141 --pretty=format:'(%h %ci)')
142 CFG_VER_HASH = $(shell git --git-dir=$(CFG_GIT_DIR) log -1 \
143 --pretty=format:'%H')
147 ifdef CFG_DISABLE_VALGRIND
148 $(info cfg: disabling valgrind (CFG_DISABLE_VALGRIND))
151 ifdef CFG_BAD_VALGRIND
152 $(info cfg: disabling valgrind due to its unreliability on this platform)
157 ######################################################################
158 # Target-and-rule "utility variables"
159 ######################################################################
170 X := $(CFG_EXE_SUFFIX)
172 # Look in doc and src dirs.
173 VPATH := $(S)doc $(S)src
175 # "Source" files we generate in builddir along the way.
178 # Delete the built-in rules.
186 ######################################################################
187 # Core library variables
188 ######################################################################
190 CORELIB_CRATE := $(S)src/libcore/core.rc
191 CORELIB_INPUTS := $(wildcard $(addprefix $(S)src/libcore/, \
192 core.rc *.rs */*.rs))
194 ######################################################################
195 # Standard library variables
196 ######################################################################
198 STDLIB_CRATE := $(S)src/libstd/std.rc
199 STDLIB_INPUTS := $(wildcard $(addprefix $(S)src/libstd/, \
202 ######################################################################
203 # rustc crate variables
204 ######################################################################
206 COMPILER_CRATE := $(S)src/rustc/rustc.rc
207 COMPILER_INPUTS := $(filter-out $(S)src/rustc/driver/rustc.rs, \
208 $(wildcard $(addprefix $(S)src/rustc/, \
209 rustc.rc *.rs */*.rs */*/*.rs */*/*/*.rs)))
211 LIBSYNTAX_CRATE := $(S)src/libsyntax/syntax.rc
212 LIBSYNTAX_INPUTS := $(wildcard $(addprefix $(S)src/libsyntax/, \
213 syntax.rc *.rs */*.rs */*/*.rs))
215 RUSTC_INPUTS := $(S)src/rustc/driver/rustc.rs
217 ######################################################################
219 ######################################################################
222 LLVM_COMPONENTS=x86 ipo bitreader bitwriter linker asmparser jit mcjit \
226 # The configure script defines these variables with the target triples
227 # separated by Z. This defines new ones with the expected format.
228 CFG_LLVM_BUILD_DIR_$(1):=$$(CFG_LLVM_BUILD_DIR_$(subst -,_,$(1)))
229 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_INST_DIR_$(subst -,_,$(1)))
231 # Any rules that depend on LLVM should depend on LLVM_CONFIG
232 LLVM_CONFIG_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-config$$(X)
233 LLVM_MC_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-mc$$(X)
234 LLVM_VERSION_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --version)
235 LLVM_BINDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --bindir)
236 LLVM_INCDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --includedir)
237 LLVM_LIBDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libdir)
238 LLVM_LIBS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libs $$(LLVM_COMPONENTS))
239 LLVM_LDFLAGS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --ldflags)
240 # On FreeBSD, it may search wrong headers (that are for pre-installed LLVM),
241 # so we replace -I with -iquote to ensure that it searches bundled LLVM first.
242 LLVM_CXXFLAGS_$(1)=$$(subst -I, -iquote , $$(shell "$$(LLVM_CONFIG_$(1))" --cxxflags))
243 LLVM_HOST_TRIPLE_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --host-target)
245 LLVM_AS_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-as$$(X)
246 LLC_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llc$$(X)
250 $(foreach target,$(CFG_TARGET_TRIPLES), \
251 $(eval $(call DEF_LLVM_VARS,$(target))))
253 ######################################################################
254 # Exports for sub-utilities
255 ######################################################################
257 # Note that any variable that re-configure should pick up needs to be
263 export CFG_HOST_TRIPLE
265 export CFG_ENABLE_MINGW_CROSS
269 ######################################################################
271 ######################################################################
273 ######################################################################
274 # Per-stage targets and runner
275 ######################################################################
278 # $(1) is the stage number
279 # $(2) is the target triple
280 # $(3) is the host triple
282 # Destinations of artifacts for the host compiler
283 HROOT$(1)_H_$(3) = $(3)/stage$(1)
284 HBIN$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/bin
285 HLIB$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/$$(CFG_LIBDIR)
287 # Destinations of artifacts for target architectures
288 TROOT$(1)_T_$(2)_H_$(3) = $$(HLIB$(1)_H_$(3))/rustc/$(2)
289 TBIN$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/bin
290 TLIB$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/$$(CFG_LIBDIR)
292 # The name of the core and standard libraries used by rustc
293 ifdef CFG_DISABLE_SHAREDSTD
294 HCORELIB_DEFAULT$(1)_H_$(3) = \
295 $$(HLIB$(1)_H_$(3))/libcore.rlib
296 TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
297 $$(TLIB$(1)_T_$(2)_H_$(3))/libcore.rlib
299 HSTDLIB_DEFAULT$(1)_H_$(3) = \
300 $$(HLIB$(1)_H_$(3))/libstd.rlib
301 TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
302 $$(TLIB$(1)_T_$(2)_H_$(3))/libstd.rlib
304 HLIBRUSTC_DEFAULT$(1)_H_$(3) = \
305 $$(HLIB$(1)_H_$(3))/librustc.rlib
306 TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3) = \
307 $$(TLIB$(1)_T_$(2)_H_$(3))/librustc.rlib
309 HCORELIB_DEFAULT$(1)_H_$(3) = \
310 $$(HLIB$(1)_H_$(3))/$(CFG_CORELIB)
311 TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
312 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_CORELIB)
314 HSTDLIB_DEFAULT$(1)_H_$(3) = \
315 $$(HLIB$(1)_H_$(3))/$(CFG_STDLIB)
316 TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3) = \
317 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_STDLIB)
319 HLIBRUSTC_DEFAULT$(1)_H_$(3) = \
320 $$(HLIB$(1)_H_$(3))/$(CFG_LIBRUSTC)
321 TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3) = \
322 $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_LIBRUSTC)
325 # Preqrequisites for using the stageN compiler
327 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
328 $$(HLIB$(1)_H_$(3))/$$(CFG_RUNTIME) \
329 $$(HLIB$(1)_H_$(3))/$$(CFG_RUSTLLVM) \
330 $$(HCORELIB_DEFAULT$(1)_H_$(3)) \
331 $$(HSTDLIB_DEFAULT$(1)_H_$(3)) \
332 $$(HLIBRUSTC_DEFAULT$(1)_H_$(3)) \
335 # Prerequisites for using the stageN compiler to build target artifacts
336 TSREQ$(1)_T_$(2)_H_$(3) = \
337 $$(HSREQ$(1)_H_$(3)) \
338 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_RUNTIME) \
339 $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a
341 # Prerequisites for complete stageN targets
342 SREQ$(1)_T_$(2)_H_$(3) = \
343 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
344 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_CORELIB) \
345 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_STDLIB) \
346 $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC)
349 # Don't run the the stage0 compiler under valgrind - that ship has sailed
350 CFG_VALGRIND_COMPILE$(1) =
352 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
355 # Add RUSTFLAGS_STAGEN values to the build command
356 EXTRAFLAGS_STAGE$(1) = $$(RUSTFLAGS_STAGE$(1))
358 CFGFLAG$(1)_T_$(2)_H_$(3) = stage$(1)
360 # Pass --cfg stage0 only for the build->host part of stage0;
361 # if you're building a cross config, the host->* parts are
362 # effectively stage1, since it uses the just-built stage0.
364 ifneq ($(strip $(CFG_HOST_TRIPLE)),$(strip $(3)))
365 CFGFLAG$(1)_T_$(2)_H_$(3) = stage1
369 STAGE$(1)_T_$(2)_H_$(3) := \
370 $$(Q)$$(call CFG_RUN_TARG,$(1), \
371 $$(CFG_VALGRIND_COMPILE$(1)) \
372 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
373 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3)) \
374 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2))
376 PERF_STAGE$(1)_T_$(2)_H_$(3) := \
377 $$(Q)$$(call CFG_RUN_TARG,$(1), \
379 $$(HBIN$(1)_H_$(3))/rustc$$(X) \
380 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3)) \
381 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2))
385 $(foreach build,$(CFG_TARGET_TRIPLES), \
386 $(eval $(foreach target,$(CFG_TARGET_TRIPLES), \
387 $(eval $(foreach stage,$(STAGES), \
388 $(eval $(call SREQ,$(stage),$(target),$(build))))))))
390 ######################################################################
393 # Builds a functional Rustc for the given host.
394 ######################################################################
396 define DEF_RUSTC_STAGE_TARGET
397 # $(1) == architecture
400 rustc-stage$(2)-H-$(1): \
401 $$(foreach target,$$(CFG_TARGET_TRIPLES), \
402 $$(SREQ$(2)_T_$$(target)_H_$(1)))
406 $(foreach host,$(CFG_TARGET_TRIPLES), \
407 $(eval $(foreach stage,1 2 3, \
408 $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
410 rustc-stage1: rustc-stage1-H-$(CFG_HOST_TRIPLE)
411 rustc-stage2: rustc-stage2-H-$(CFG_HOST_TRIPLE)
412 rustc-stage3: rustc-stage3-H-$(CFG_HOST_TRIPLE)
414 define DEF_RUSTC_TARGET
415 # $(1) == architecture
417 rustc-H-$(1): rustc-stage2-H-$(1)
420 $(foreach host,$(CFG_TARGET_TRIPLES), \
421 $(eval $(call DEF_RUSTC_TARGET,$(host))))
423 rustc-stage1: rustc-stage1-H-$(CFG_HOST_TRIPLE)
424 rustc-stage2: rustc-stage2-H-$(CFG_HOST_TRIPLE)
425 rustc-stage3: rustc-stage3-H-$(CFG_HOST_TRIPLE)
426 rustc: rustc-H-$(CFG_HOST_TRIPLE)
428 rustc-H-all: $(foreach host,$(CFG_TARGET_TRIPLES),rustc-H-$(host))
430 ######################################################################
432 ######################################################################
436 ifneq ($(CFG_IN_TRANSITION),)
438 CFG_INFO := $(info cfg:)
439 CFG_INFO := $(info cfg: *** compiler is in snapshot transition ***)
440 CFG_INFO := $(info cfg: *** stage2 and later will not be built ***)
441 CFG_INFO := $(info cfg:)
443 all: $(SREQ1$(CFG_HOST_TRIPLE)) $(GENERATED) docs
448 $(foreach target,$(CFG_TARGET_TRIPLES), \
449 $(SREQ3_T_$(target)_H_$(CFG_HOST_TRIPLE)))
450 FUZZ := $(HBIN2_H_$(CFG_HOST_TRIPLE))/fuzzer$(X)
451 CARGO := $(HBIN2_H_$(CFG_HOST_TRIPLE))/cargo$(X)
452 RUSTDOC := $(HBIN2_H_$(CFG_HOST_TRIPLE))/rustdoc$(X)
454 all: rustc $(GENERATED) docs $(FUZZ) $(CARGO) $(RUSTDOC)
459 ######################################################################
461 ######################################################################
463 ifndef CFG_DISABLE_MANAGE_SUBMODULES
464 # This is a pretty expensive operation but I don't see any way to avoid it
465 NEED_GIT_RECONFIG=$(shell cd "$(CFG_SRC_DIR)" && "$(CFG_GIT)" submodule status | grep -c '^\(+\|-\)')
470 ifeq ($(NEED_GIT_RECONFIG),0)
472 # If the submodules have changed then always execute config.mk
476 Makefile config.mk: config.stamp
478 config.stamp: $(S)configure $(S)Makefile.in $(S)src/snapshots.txt
479 @$(call E, cfg: reconfiguring)
480 $(Q)$(S)configure $(CFG_CONFIGURE_ARGS)
483 ######################################################################
484 # Primary-target makefiles
485 ######################################################################
487 include $(CFG_SRC_DIR)mk/target.mk
488 include $(CFG_SRC_DIR)mk/host.mk
489 include $(CFG_SRC_DIR)mk/stage0.mk
490 include $(CFG_SRC_DIR)mk/rt.mk
491 include $(CFG_SRC_DIR)mk/rustllvm.mk
492 include $(CFG_SRC_DIR)mk/tools.mk
493 include $(CFG_SRC_DIR)mk/docs.mk
494 include $(CFG_SRC_DIR)mk/llvm.mk
496 ######################################################################
497 # Secondary makefiles, conditionalized for speed
498 ######################################################################
500 ifneq ($(strip $(findstring dist,$(MAKECMDGOALS)) \
501 $(findstring check,$(MAKECMDGOALS)) \
502 $(findstring test,$(MAKECMDGOALS)) \
503 $(findstring tidy,$(MAKECMDGOALS)) \
504 $(findstring clean,$(MAKECMDGOALS))),)
505 CFG_INFO := $(info cfg: including dist rules)
506 include $(CFG_SRC_DIR)mk/dist.mk
509 ifneq ($(strip $(findstring snap,$(MAKECMDGOALS)) \
510 $(findstring clean,$(MAKECMDGOALS))),)
511 CFG_INFO := $(info cfg: including snap rules)
512 include $(CFG_SRC_DIR)mk/snap.mk
515 ifneq ($(findstring reformat,$(MAKECMDGOALS)),)
516 CFG_INFO := $(info cfg: including reformat rules)
517 include $(CFG_SRC_DIR)mk/pp.mk
520 ifneq ($(strip $(findstring check,$(MAKECMDGOALS)) \
521 $(findstring test,$(MAKECMDGOALS)) \
522 $(findstring perf,$(MAKECMDGOALS)) \
523 $(findstring tidy,$(MAKECMDGOALS))),)
524 CFG_INFO := $(info cfg: including test rules)
525 include $(CFG_SRC_DIR)mk/tests.mk
528 ifneq ($(findstring perf,$(MAKECMDGOALS)),)
529 CFG_INFO := $(info cfg: including perf rules)
530 include $(CFG_SRC_DIR)mk/perf.mk
533 ifneq ($(findstring clean,$(MAKECMDGOALS)),)
534 CFG_INFO := $(info cfg: including clean rules)
535 include $(CFG_SRC_DIR)mk/clean.mk
538 ifneq ($(findstring install,$(MAKECMDGOALS)),)
540 CFG_INFO := $(info cfg: setting CFG_PREFIX via DESTDIR, $(DESTDIR)/$(CFG_PREFIX))
541 CFG_PREFIX:=$(DESTDIR)/$(CFG_PREFIX)
545 CFG_INFO := $(info cfg: including install rules)
546 include $(CFG_SRC_DIR)mk/install.mk
549 ifneq ($(strip $(findstring TAGS.emacs,$(MAKECMDGOALS)) \
550 $(findstring TAGS.vi,$(MAKECMDGOALS))),)
551 CFG_INFO := $(info cfg: including ctags rules)
552 include $(CFG_SRC_DIR)mk/ctags.mk
555 # Find all of the .d files and include them to add information about
556 # header file dependencies.
557 ALL_DEP_FILES := $(ALL_OBJ_FILES:%.o=%.d)
558 -include $(ALL_DEP_FILES)