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 MKFILE_DEPS := config.stamp $(call rwildcard,$(CFG_SRC_DIR)mk/,*)
16 NON_BUILD_HOST = $(filter-out $(CFG_BUILD),$(CFG_HOST))
17 NON_BUILD_TARGET = $(filter-out $(CFG_BUILD),$(CFG_TARGET))
19 ifneq ($(MAKE_RESTARTS),)
20 CFG_INFO := $(info cfg: make restarts: $(MAKE_RESTARTS))
23 CFG_INFO := $(info cfg: build triple $(CFG_BUILD))
24 CFG_INFO := $(info cfg: host triples $(CFG_HOST))
25 CFG_INFO := $(info cfg: target triples $(CFG_TARGET))
27 ifneq ($(wildcard $(NON_BUILD_HOST)),)
28 CFG_INFO := $(info cfg: non-build host triples $(NON_BUILD_HOST))
30 ifneq ($(wildcard $(NON_BUILD_TARGET)),)
31 CFG_INFO := $(info cfg: non-build target triples $(NON_BUILD_TARGET))
34 CFG_RUSTC_FLAGS := $(RUSTFLAGS)
36 CFG_GCCISH_LINK_FLAGS :=
38 ifdef CFG_DISABLE_OPTIMIZE
39 $(info cfg: disabling rustc optimization (CFG_DISABLE_OPTIMIZE))
42 # The rtopt cfg turns off runtime sanity checks
43 CFG_RUSTC_FLAGS += -O --cfg rtopt
46 ifdef CFG_DISABLE_DEBUG
47 CFG_RUSTC_FLAGS += --cfg ndebug
48 CFG_GCCISH_CFLAGS += -DRUST_NDEBUG
50 $(info cfg: enabling more debugging (CFG_ENABLE_DEBUG))
51 CFG_RUSTC_FLAGS += --cfg debug
52 CFG_GCCISH_CFLAGS += -DRUST_DEBUG
56 CFG_RUSTC_FLAGS += --save-temps
59 CFG_RUSTC_FLAGS += -Z asm-comments
62 CFG_RUSTC_FLAGS += -Z time-passes
64 ifdef TIME_LLVM_PASSES
65 CFG_RUSTC_FLAGS += -Z time-llvm-passes
68 CFG_RUSTC_FLAGS += -Z trace
70 ifdef CFG_DISABLE_RPATH
71 CFG_RUSTC_FLAGS += -C no-rpath
74 # The executables crated during this compilation process have no need to include
75 # static copies of libstd and libextra. We also generate dynamic versions of all
76 # libraries, so in the interest of space, prefer dynamic linking throughout the
77 # compilation process.
79 # Note though that these flags are omitted for stage2+. This means that the
80 # snapshot will be generated with a statically linked rustc so we only have to
81 # worry about the distribution of one file (with its native dynamic
83 RUSTFLAGS_STAGE0 += -C prefer-dynamic
84 RUSTFLAGS_STAGE1 += -C prefer-dynamic
86 # platform-specific auto-configuration
87 include $(CFG_SRC_DIR)mk/platform.mk
89 # Run the stage1/2 compilers under valgrind
90 ifdef VALGRIND_COMPILE
91 CFG_VALGRIND_COMPILE :=$(CFG_VALGRIND)
93 CFG_VALGRIND_COMPILE :=
96 # version-string calculation
97 CFG_GIT_DIR := $(CFG_SRC_DIR).git
98 CFG_RELEASE = 0.10-pre
99 CFG_VERSION = $(CFG_RELEASE)
100 # windows exe's need numeric versions - don't use anything but
101 # numbers and dots here
102 CFG_VERSION_WIN = 0.10
104 # since $(CFG_GIT) may contain spaces (especially on Windows),
105 # we need to escape them. (" " to r"\ ")
106 # Note that $(subst ...) ignores space after `subst`,
107 # so we use a hack: define $(SPACE) which contains space character.
110 ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT))),)
111 ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT_DIR))),)
112 CFG_VERSION += $(shell git --git-dir='$(CFG_GIT_DIR)' log -1 \
113 --pretty=format:'(%h %ci)')
114 CFG_VER_HASH = $(shell git --git-dir='$(CFG_GIT_DIR)' rev-parse HEAD)
118 ifdef CFG_ENABLE_VALGRIND
119 $(info cfg: enabling valgrind (CFG_ENABLE_VALGRIND))
123 ifdef CFG_BAD_VALGRIND
124 $(info cfg: disabling valgrind due to its unreliability on this platform)
129 ######################################################################
130 # Target-and-rule "utility variables"
131 ######################################################################
134 X_$(1) := $(CFG_EXE_SUFFIX_$(1))
136 $(foreach target,$(CFG_TARGET),\
137 $(eval $(call DEF_X,$(target))))
139 # "Source" files we generate in builddir along the way.
142 # Delete the built-in rules.
151 ######################################################################
152 # Cleaning out old crates
153 ######################################################################
155 # $(1) is the path for directory to match against
156 # $(2) is the glob to use in the match
158 # Note that a common bug is to accidentally construct the glob denoted
159 # by $(2) with a space character prefix, which invalidates the
160 # construction $(1)$(2).
161 define CHECK_FOR_OLD_GLOB_MATCHES
162 $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then echo "warning: there are previous" \'$(notdir $(2))\' "libraries:" $$MATCHES; fi
165 # Same interface as above, but deletes rather than just listing the files.
167 define REMOVE_ALL_OLD_GLOB_MATCHES
168 $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then echo "warning: removing previous" \'$(notdir $(1))\' "libraries:" $$MATCHES; rm $$MATCHES ; fi
171 define REMOVE_ALL_OLD_GLOB_MATCHES
172 $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then rm $$MATCHES ; fi
176 # We use a different strategy for LIST_ALL_OLD_GLOB_MATCHES_EXCEPT
177 # than in the macros above because it needs the result of running the
178 # `ls` command after other rules in the command list have run; the
179 # macro-expander for $(wildcard ...) would deliver its results too
180 # soon. (This is in contrast to the macros above, which are meant to
181 # be run at the outset of a command list in a rule.)
183 define LIST_ALL_OLD_GLOB_MATCHES
184 @echo "info: now are following matches for" '$(notdir $(1))' "libraries:"
185 @( ls $(1) 2>/dev/null || true )
188 define LIST_ALL_OLD_GLOB_MATCHES
192 ######################################################################
194 ######################################################################
197 LLVM_COMPONENTS=x86 arm mips ipo bitreader bitwriter linker asmparser jit mcjit \
198 interpreter instrumentation
200 # Only build these LLVM tools
201 LLVM_TOOLS=bugpoint llc llvm-ar llvm-as llvm-dis llvm-mc opt llvm-extract
204 # The configure script defines these variables with the target triples
205 # separated by Z. This defines new ones with the expected format.
206 CFG_LLVM_BUILD_DIR_$(1):=$$(CFG_LLVM_BUILD_DIR_$(subst -,_,$(1)))
207 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_INST_DIR_$(subst -,_,$(1)))
209 # Any rules that depend on LLVM should depend on LLVM_CONFIG
210 LLVM_CONFIG_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-config$$(X_$(1))
211 LLVM_MC_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-mc$$(X_$(1))
212 LLVM_VERSION_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --version)
213 LLVM_BINDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --bindir)
214 LLVM_INCDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --includedir)
215 LLVM_LIBDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libdir)
216 LLVM_LIBS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libs $$(LLVM_COMPONENTS))
217 LLVM_LDFLAGS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --ldflags)
218 # On FreeBSD, it may search wrong headers (that are for pre-installed LLVM),
219 # so we replace -I with -iquote to ensure that it searches bundled LLVM first.
220 LLVM_CXXFLAGS_$(1)=$$(subst -I, -iquote , $$(shell "$$(LLVM_CONFIG_$(1))" --cxxflags))
221 LLVM_HOST_TRIPLE_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --host-target)
223 LLVM_AS_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-as$$(X_$(1))
224 LLC_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llc$$(X_$(1))
228 $(foreach host,$(CFG_HOST), \
229 $(eval $(call DEF_LLVM_VARS,$(host))))
231 ######################################################################
232 # Exports for sub-utilities
233 ######################################################################
235 # Note that any variable that re-configure should pick up needs to be
241 export CFG_VERSION_WIN
245 export CFG_ENABLE_MINGW_CROSS
248 export CFG_RUSTLIBDIR
249 export CFG_LIBDIR_RELATIVE
250 export CFG_DISABLE_INJECT_STD_VERSION
252 ######################################################################
253 # Per-stage targets and runner
254 ######################################################################
259 # $(1) is the stage number
260 # $(2) is the target triple
261 # $(3) is the host triple
263 # Destinations of artifacts for the host compiler
264 HROOT$(1)_H_$(3) = $(3)/stage$(1)
265 HBIN$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/bin
266 HLIB$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/$$(CFG_LIBDIR_RELATIVE)
268 # Destinations of artifacts for target architectures
269 TROOT$(1)_T_$(2)_H_$(3) = $$(HLIB$(1)_H_$(3))/$$(CFG_RUSTLIBDIR)/$(2)
270 TBIN$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/bin
271 TLIB$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/lib
273 # Preqrequisites for using the stageN compiler
275 HSREQ$(1)_H_$(3) = $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))
278 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
279 $$(HLIB$(1)_H_$(3))/stamp.rustc \
280 $$(foreach dep,$$(RUST_DEPS_rustc),$$(HLIB$(1)_H_$(3))/stamp.$$(dep)) \
284 # Prerequisites for using the stageN compiler to build target artifacts
285 TSREQ$(1)_T_$(2)_H_$(3) = \
286 $$(HSREQ$(1)_H_$(3)) \
287 $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a \
288 $$(TLIB$(1)_T_$(2)_H_$(3))/libcompiler-rt.a
290 # Prerequisites for a working stageN compiler and libraries, for a specific
292 SREQ$(1)_T_$(2)_H_$(3) = \
293 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
294 $$(foreach dep,$$(TARGET_CRATES),\
295 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep))
297 # Prerequisites for a working stageN compiler and complete set of target
299 CSREQ$(1)_T_$(2)_H_$(3) = \
300 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
301 $$(HBIN$(1)_H_$(3))/rustdoc$$(X_$(3)) \
302 $$(foreach dep,$$(CRATES),$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
303 $$(foreach dep,$$(HOST_CRATES),$$(HLIB$(1)_H_$(3))/stamp.$$(dep))
306 # Don't run the the stage0 compiler under valgrind - that ship has sailed
307 CFG_VALGRIND_COMPILE$(1) =
309 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
312 # Add RUSTFLAGS_STAGEN values to the build command
313 EXTRAFLAGS_STAGE$(1) = $$(RUSTFLAGS_STAGE$(1))
315 CFGFLAG$(1)_T_$(2)_H_$(3) = stage$(1)
317 # Pass --cfg stage0 only for the build->host part of stage0;
318 # if you're building a cross config, the host->* parts are
319 # effectively stage1, since it uses the just-built stage0.
321 ifneq ($(strip $(CFG_BUILD)),$(strip $(3)))
322 CFGFLAG$(1)_T_$(2)_H_$(3) = stage1
326 ifdef CFG_DISABLE_RPATH
327 ifeq ($$(OSTYPE_$(3)),apple-darwin)
328 RPATH_VAR$(1)_T_$(2)_H_$(3) := \
329 DYLD_LIBRARY_PATH="$$$$DYLD_LIBRARY_PATH:$$(HLIB$(1)_H_$(3))"
331 RPATH_VAR$(1)_T_$(2)_H_$(3) := \
332 LD_LIBRARY_PATH="$$$$LD_LIBRARY_PATH:$$(HLIB$(1)_H_$(3))"
335 RPATH_VAR$(1)_T_$(2)_H_$(3) :=
338 STAGE$(1)_T_$(2)_H_$(3) := \
339 $$(Q)$$(RPATH_VAR$(1)_T_$(2)_H_$(3)) \
340 $$(call CFG_RUN_TARG_$(3),$(1), \
341 $$(CFG_VALGRIND_COMPILE$(1)) \
342 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
343 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3)) \
344 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
347 PERF_STAGE$(1)_T_$(2)_H_$(3) := \
348 $$(Q)$$(call CFG_RUN_TARG_$(3),$(1), \
350 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
351 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3)) \
352 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
357 $(foreach build,$(CFG_HOST), \
358 $(eval $(foreach target,$(CFG_TARGET), \
359 $(eval $(foreach stage,$(STAGES), \
360 $(eval $(call SREQ,$(stage),$(target),$(build))))))))
362 ######################################################################
365 # Builds a functional Rustc for the given host.
366 ######################################################################
368 define DEF_RUSTC_STAGE_TARGET
369 # $(1) == architecture
372 rustc-stage$(2)-H-$(1): \
373 $$(foreach target,$$(CFG_TARGET),$$(SREQ$(2)_T_$$(target)_H_$(1)))
377 $(foreach host,$(CFG_HOST), \
378 $(eval $(foreach stage,1 2 3, \
379 $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
381 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
382 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
383 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
385 define DEF_RUSTC_TARGET
386 # $(1) == architecture
388 rustc-H-$(1): rustc-stage2-H-$(1)
391 $(foreach host,$(CFG_TARGET), \
392 $(eval $(call DEF_RUSTC_TARGET,$(host))))
394 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
395 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
396 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
397 rustc: rustc-H-$(CFG_BUILD)
399 rustc-H-all: $(foreach host,$(CFG_HOST),rustc-H-$(host))
401 ######################################################################
403 ######################################################################
408 ifneq ($$(findstring $(1),$$(CFG_HOST)),)
410 all-target-$(1)-host-$(2): $$(CSREQ2_T_$(1)_H_$(2))
412 # This is a target only
413 all-target-$(1)-host-$(2): $$(SREQ2_T_$(1)_H_$(2))
417 $(foreach target,$(CFG_TARGET), \
418 $(foreach host,$(CFG_HOST), \
419 $(eval $(call ALL_TARGET_N,$(target),$(host)))))
421 ALL_TARGET_RULES = $(foreach target,$(CFG_TARGET), \
422 $(foreach host,$(CFG_HOST), \
423 all-target-$(target)-host-$(host)))
425 all: $(ALL_TARGET_RULES) $(GENERATED) docs
428 # Show the comments from Makefile.in as "help"
429 # pick everything between tags | remove first line | remove last line
430 # | remove extra (?) line | strip leading `#` from lines
431 $(Q)awk '/<help>/,/<\/help>/' $(S)/Makefile.in | sed '1d' | sed '$$d' | sed 's/^# \?//'