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 # We track all of the object files we might build so that we can find
12 # and include all of the .d files in one fell swoop.
15 ifneq ($(NO_MAKEFILE_DEPS),)
18 MKFILE_DEPS := config.stamp $(call rwildcard,$(CFG_SRC_DIR)mk/,*)
20 NON_BUILD_HOST = $(filter-out $(CFG_BUILD),$(CFG_HOST))
21 NON_BUILD_TARGET = $(filter-out $(CFG_BUILD),$(CFG_TARGET))
23 ifneq ($(MAKE_RESTARTS),)
24 CFG_INFO := $(info cfg: make restarts: $(MAKE_RESTARTS))
27 CFG_INFO := $(info cfg: build triple $(CFG_BUILD))
28 CFG_INFO := $(info cfg: host triples $(CFG_HOST))
29 CFG_INFO := $(info cfg: target triples $(CFG_TARGET))
31 ifneq ($(wildcard $(NON_BUILD_HOST)),)
32 CFG_INFO := $(info cfg: non-build host triples $(NON_BUILD_HOST))
34 ifneq ($(wildcard $(NON_BUILD_TARGET)),)
35 CFG_INFO := $(info cfg: non-build target triples $(NON_BUILD_TARGET))
38 CFG_RUSTC_FLAGS := $(RUSTFLAGS)
40 CFG_GCCISH_LINK_FLAGS :=
42 ifdef CFG_DISABLE_OPTIMIZE
43 $(info cfg: disabling rustc optimization (CFG_DISABLE_OPTIMIZE))
46 # The rtopt cfg turns off runtime sanity checks
47 CFG_RUSTC_FLAGS += -O --cfg rtopt
50 ifdef CFG_DISABLE_DEBUG
51 CFG_RUSTC_FLAGS += --cfg ndebug
52 CFG_GCCISH_CFLAGS += -DRUST_NDEBUG
54 $(info cfg: enabling more debugging (CFG_ENABLE_DEBUG))
55 CFG_RUSTC_FLAGS += --cfg debug
56 CFG_GCCISH_CFLAGS += -DRUST_DEBUG
60 CFG_RUSTC_FLAGS += --save-temps
63 CFG_RUSTC_FLAGS += -Z asm-comments
66 CFG_RUSTC_FLAGS += -Z time-passes
68 ifdef TIME_LLVM_PASSES
69 CFG_RUSTC_FLAGS += -Z time-llvm-passes
72 CFG_RUSTC_FLAGS += -Z trace
74 ifdef CFG_DISABLE_RPATH
75 CFG_RUSTC_FLAGS += -C no-rpath
78 # The executables crated during this compilation process have no need to include
79 # static copies of libstd and libextra. We also generate dynamic versions of all
80 # libraries, so in the interest of space, prefer dynamic linking throughout the
81 # compilation process.
83 # Note though that these flags are omitted for stage2+. This means that the
84 # snapshot will be generated with a statically linked rustc so we only have to
85 # worry about the distribution of one file (with its native dynamic
87 RUSTFLAGS_STAGE0 += -C prefer-dynamic
88 RUSTFLAGS_STAGE1 += -C prefer-dynamic
90 # platform-specific auto-configuration
91 include $(CFG_SRC_DIR)mk/platform.mk
93 # Run the stage1/2 compilers under valgrind
94 ifdef VALGRIND_COMPILE
95 CFG_VALGRIND_COMPILE :=$(CFG_VALGRIND)
97 CFG_VALGRIND_COMPILE :=
100 # version-string calculation
101 CFG_GIT_DIR := $(CFG_SRC_DIR).git
102 CFG_RELEASE = 0.10-pre
103 CFG_VERSION = $(CFG_RELEASE)
104 # windows exe's need numeric versions - don't use anything but
105 # numbers and dots here
106 CFG_VERSION_WIN = 0.10
108 # since $(CFG_GIT) may contain spaces (especially on Windows),
109 # we need to escape them. (" " to r"\ ")
110 # Note that $(subst ...) ignores space after `subst`,
111 # so we use a hack: define $(SPACE) which contains space character.
114 ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT))),)
115 ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT_DIR))),)
116 CFG_VERSION += $(shell git --git-dir='$(CFG_GIT_DIR)' log -1 \
117 --pretty=format:'(%h %ci)')
118 CFG_VER_HASH = $(shell git --git-dir='$(CFG_GIT_DIR)' rev-parse HEAD)
122 ifdef CFG_ENABLE_VALGRIND
123 $(info cfg: enabling valgrind (CFG_ENABLE_VALGRIND))
127 ifdef CFG_BAD_VALGRIND
128 $(info cfg: disabling valgrind due to its unreliability on this platform)
133 ######################################################################
134 # Target-and-rule "utility variables"
135 ######################################################################
138 X_$(1) := $(CFG_EXE_SUFFIX_$(1))
140 $(foreach target,$(CFG_TARGET),\
141 $(eval $(call DEF_X,$(target))))
143 # "Source" files we generate in builddir along the way.
146 # Delete the built-in rules.
155 ######################################################################
156 # Cleaning out old crates
157 ######################################################################
159 # $(1) is the path for directory to match against
160 # $(2) is the glob to use in the match
162 # Note that a common bug is to accidentally construct the glob denoted
163 # by $(2) with a space character prefix, which invalidates the
164 # construction $(1)$(2).
165 define CHECK_FOR_OLD_GLOB_MATCHES
166 $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then echo "warning: there are previous" \'$(notdir $(2))\' "libraries:" $$MATCHES; fi
169 # Same interface as above, but deletes rather than just listing the files.
171 define REMOVE_ALL_OLD_GLOB_MATCHES
172 $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then echo "warning: removing previous" \'$(notdir $(1))\' "libraries:" $$MATCHES; rm $$MATCHES ; fi
175 define REMOVE_ALL_OLD_GLOB_MATCHES
176 $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then rm $$MATCHES ; fi
180 # We use a different strategy for LIST_ALL_OLD_GLOB_MATCHES_EXCEPT
181 # than in the macros above because it needs the result of running the
182 # `ls` command after other rules in the command list have run; the
183 # macro-expander for $(wildcard ...) would deliver its results too
184 # soon. (This is in contrast to the macros above, which are meant to
185 # be run at the outset of a command list in a rule.)
187 define LIST_ALL_OLD_GLOB_MATCHES
188 @echo "info: now are following matches for" '$(notdir $(1))' "libraries:"
189 @( ls $(1) 2>/dev/null || true )
192 define LIST_ALL_OLD_GLOB_MATCHES
196 ######################################################################
198 ######################################################################
201 LLVM_COMPONENTS=x86 arm mips ipo bitreader bitwriter linker asmparser jit mcjit \
202 interpreter instrumentation
204 # Only build these LLVM tools
205 LLVM_TOOLS=bugpoint llc llvm-ar llvm-as llvm-dis llvm-mc opt llvm-extract
208 # The configure script defines these variables with the target triples
209 # separated by Z. This defines new ones with the expected format.
210 CFG_LLVM_BUILD_DIR_$(1):=$$(CFG_LLVM_BUILD_DIR_$(subst -,_,$(1)))
211 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_INST_DIR_$(subst -,_,$(1)))
213 # Any rules that depend on LLVM should depend on LLVM_CONFIG
214 LLVM_CONFIG_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-config$$(X_$(1))
215 LLVM_MC_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-mc$$(X_$(1))
216 LLVM_VERSION_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --version)
217 LLVM_BINDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --bindir)
218 LLVM_INCDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --includedir)
219 LLVM_LIBDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libdir)
220 LLVM_LIBS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libs $$(LLVM_COMPONENTS))
221 LLVM_LDFLAGS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --ldflags)
222 # On FreeBSD, it may search wrong headers (that are for pre-installed LLVM),
223 # so we replace -I with -iquote to ensure that it searches bundled LLVM first.
224 LLVM_CXXFLAGS_$(1)=$$(subst -I, -iquote , $$(shell "$$(LLVM_CONFIG_$(1))" --cxxflags))
225 LLVM_HOST_TRIPLE_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --host-target)
227 LLVM_AS_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-as$$(X_$(1))
228 LLC_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llc$$(X_$(1))
232 $(foreach host,$(CFG_HOST), \
233 $(eval $(call DEF_LLVM_VARS,$(host))))
235 ######################################################################
236 # Exports for sub-utilities
237 ######################################################################
239 # Note that any variable that re-configure should pick up needs to be
245 export CFG_VERSION_WIN
249 export CFG_ENABLE_MINGW_CROSS
252 export CFG_RUSTLIBDIR
253 export CFG_LIBDIR_RELATIVE
254 export CFG_DISABLE_INJECT_STD_VERSION
256 ######################################################################
257 # Per-stage targets and runner
258 ######################################################################
263 # $(1) is the stage number
264 # $(2) is the target triple
265 # $(3) is the host triple
267 # Destinations of artifacts for the host compiler
268 HROOT$(1)_H_$(3) = $(3)/stage$(1)
269 HBIN$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/bin
270 HLIB$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/$$(CFG_LIBDIR_RELATIVE)
272 # Destinations of artifacts for target architectures
273 TROOT$(1)_T_$(2)_H_$(3) = $$(HLIB$(1)_H_$(3))/$$(CFG_RUSTLIBDIR)/$(2)
274 TBIN$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/bin
275 TLIB$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/lib
277 # Preqrequisites for using the stageN compiler
279 HSREQ$(1)_H_$(3) = $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))
282 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
283 $$(HLIB$(1)_H_$(3))/stamp.rustc \
284 $$(foreach dep,$$(RUST_DEPS_rustc),$$(HLIB$(1)_H_$(3))/stamp.$$(dep)) \
288 # Prerequisites for using the stageN compiler to build target artifacts
289 TSREQ$(1)_T_$(2)_H_$(3) = \
290 $$(HSREQ$(1)_H_$(3)) \
291 $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a \
292 $$(TLIB$(1)_T_$(2)_H_$(3))/libcompiler-rt.a
294 # Prerequisites for a working stageN compiler and libraries, for a specific
296 SREQ$(1)_T_$(2)_H_$(3) = \
297 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
298 $$(foreach dep,$$(TARGET_CRATES),\
299 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep))
301 # Prerequisites for a working stageN compiler and complete set of target
303 CSREQ$(1)_T_$(2)_H_$(3) = \
304 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
305 $$(HBIN$(1)_H_$(3))/rustdoc$$(X_$(3)) \
306 $$(foreach dep,$$(CRATES),$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
307 $$(foreach dep,$$(HOST_CRATES),$$(HLIB$(1)_H_$(3))/stamp.$$(dep))
310 # Don't run the the stage0 compiler under valgrind - that ship has sailed
311 CFG_VALGRIND_COMPILE$(1) =
313 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
316 # Add RUSTFLAGS_STAGEN values to the build command
317 EXTRAFLAGS_STAGE$(1) = $$(RUSTFLAGS_STAGE$(1))
319 CFGFLAG$(1)_T_$(2)_H_$(3) = stage$(1)
321 # Pass --cfg stage0 only for the build->host part of stage0;
322 # if you're building a cross config, the host->* parts are
323 # effectively stage1, since it uses the just-built stage0.
325 ifneq ($(strip $(CFG_BUILD)),$(strip $(3)))
326 CFGFLAG$(1)_T_$(2)_H_$(3) = stage1
330 ifdef CFG_DISABLE_RPATH
331 ifeq ($$(OSTYPE_$(3)),apple-darwin)
332 RPATH_VAR$(1)_T_$(2)_H_$(3) := \
333 DYLD_LIBRARY_PATH="$$$$DYLD_LIBRARY_PATH:$$(HLIB$(1)_H_$(3))"
335 RPATH_VAR$(1)_T_$(2)_H_$(3) := \
336 LD_LIBRARY_PATH="$$$$LD_LIBRARY_PATH:$$(HLIB$(1)_H_$(3))"
339 RPATH_VAR$(1)_T_$(2)_H_$(3) :=
342 STAGE$(1)_T_$(2)_H_$(3) := \
343 $$(Q)$$(RPATH_VAR$(1)_T_$(2)_H_$(3)) \
344 $$(call CFG_RUN_TARG_$(3),$(1), \
345 $$(CFG_VALGRIND_COMPILE$(1)) \
346 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
347 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3)) \
348 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
351 PERF_STAGE$(1)_T_$(2)_H_$(3) := \
352 $$(Q)$$(call CFG_RUN_TARG_$(3),$(1), \
354 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
355 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3)) \
356 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
361 $(foreach build,$(CFG_HOST), \
362 $(eval $(foreach target,$(CFG_TARGET), \
363 $(eval $(foreach stage,$(STAGES), \
364 $(eval $(call SREQ,$(stage),$(target),$(build))))))))
366 ######################################################################
369 # Builds a functional Rustc for the given host.
370 ######################################################################
372 define DEF_RUSTC_STAGE_TARGET
373 # $(1) == architecture
376 rustc-stage$(2)-H-$(1): \
377 $$(foreach target,$$(CFG_TARGET),$$(SREQ$(2)_T_$$(target)_H_$(1)))
381 $(foreach host,$(CFG_HOST), \
382 $(eval $(foreach stage,1 2 3, \
383 $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
385 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
386 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
387 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
389 define DEF_RUSTC_TARGET
390 # $(1) == architecture
392 rustc-H-$(1): rustc-stage2-H-$(1)
395 $(foreach host,$(CFG_TARGET), \
396 $(eval $(call DEF_RUSTC_TARGET,$(host))))
398 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
399 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
400 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
401 rustc: rustc-H-$(CFG_BUILD)
403 rustc-H-all: $(foreach host,$(CFG_HOST),rustc-H-$(host))
405 ######################################################################
407 ######################################################################
412 ifneq ($$(findstring $(1),$$(CFG_HOST)),)
414 all-target-$(1)-host-$(2): $$(CSREQ2_T_$(1)_H_$(2))
416 # This is a target only
417 all-target-$(1)-host-$(2): $$(SREQ2_T_$(1)_H_$(2))
421 $(foreach target,$(CFG_TARGET), \
422 $(foreach host,$(CFG_HOST), \
423 $(eval $(call ALL_TARGET_N,$(target),$(host)))))
425 ALL_TARGET_RULES = $(foreach target,$(CFG_TARGET), \
426 $(foreach host,$(CFG_HOST), \
427 all-target-$(target)-host-$(host)))
429 all: $(ALL_TARGET_RULES) $(GENERATED) docs
432 # Show the comments from Makefile.in as "help"
433 # pick everything between tags | remove first line | remove last line
434 # | remove extra (?) line | strip leading `#` from lines
435 $(Q)awk '/<help>/,/<\/help>/' $(S)/Makefile.in | sed '1d' | sed '$$d' | sed 's/^# \?//'