]> git.lizzy.rs Git - rust.git/blob - mk/main.mk
mk: Add NO_MKFILE_DEPS for turning off rebuild from makefile changes
[rust.git] / mk / main.mk
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.
4 #
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.
10
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.
13 ALL_OBJ_FILES :=
14
15 ifneq ($(NO_MAKEFILE_DEPS),)
16 MKFILE_DEPS :=
17 else
18 MKFILE_DEPS := config.stamp $(call rwildcard,$(CFG_SRC_DIR)mk/,*)
19 endif
20 NON_BUILD_HOST = $(filter-out $(CFG_BUILD),$(CFG_HOST))
21 NON_BUILD_TARGET = $(filter-out $(CFG_BUILD),$(CFG_TARGET))
22
23 ifneq ($(MAKE_RESTARTS),)
24 CFG_INFO := $(info cfg: make restarts: $(MAKE_RESTARTS))
25 endif
26
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))
30
31 ifneq ($(wildcard $(NON_BUILD_HOST)),)
32 CFG_INFO := $(info cfg: non-build host triples $(NON_BUILD_HOST))
33 endif
34 ifneq ($(wildcard $(NON_BUILD_TARGET)),)
35 CFG_INFO := $(info cfg: non-build target triples $(NON_BUILD_TARGET))
36 endif
37
38 CFG_RUSTC_FLAGS := $(RUSTFLAGS)
39 CFG_GCCISH_CFLAGS :=
40 CFG_GCCISH_LINK_FLAGS :=
41
42 ifdef CFG_DISABLE_OPTIMIZE
43   $(info cfg: disabling rustc optimization (CFG_DISABLE_OPTIMIZE))
44   CFG_RUSTC_FLAGS +=
45 else
46   # The rtopt cfg turns off runtime sanity checks
47   CFG_RUSTC_FLAGS += -O --cfg rtopt
48 endif
49
50 ifdef CFG_DISABLE_DEBUG
51   CFG_RUSTC_FLAGS += --cfg ndebug
52   CFG_GCCISH_CFLAGS += -DRUST_NDEBUG
53 else
54   $(info cfg: enabling more debugging (CFG_ENABLE_DEBUG))
55   CFG_RUSTC_FLAGS += --cfg debug
56   CFG_GCCISH_CFLAGS += -DRUST_DEBUG
57 endif
58
59 ifdef SAVE_TEMPS
60   CFG_RUSTC_FLAGS += --save-temps
61 endif
62 ifdef ASM_COMMENTS
63   CFG_RUSTC_FLAGS += -Z asm-comments
64 endif
65 ifdef TIME_PASSES
66   CFG_RUSTC_FLAGS += -Z time-passes
67 endif
68 ifdef TIME_LLVM_PASSES
69   CFG_RUSTC_FLAGS += -Z time-llvm-passes
70 endif
71 ifdef TRACE
72   CFG_RUSTC_FLAGS += -Z trace
73 endif
74 ifdef CFG_DISABLE_RPATH
75 CFG_RUSTC_FLAGS += -C no-rpath
76 endif
77
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.
82 #
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
86 # dependencies)
87 RUSTFLAGS_STAGE0 += -C prefer-dynamic
88 RUSTFLAGS_STAGE1 += -C prefer-dynamic
89
90 # platform-specific auto-configuration
91 include $(CFG_SRC_DIR)mk/platform.mk
92
93 # Run the stage1/2 compilers under valgrind
94 ifdef VALGRIND_COMPILE
95   CFG_VALGRIND_COMPILE :=$(CFG_VALGRIND)
96 else
97   CFG_VALGRIND_COMPILE :=
98 endif
99
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
107
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.
112 SPACE :=
113 SPACE +=
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)
119 endif
120 endif
121
122 ifdef CFG_ENABLE_VALGRIND
123   $(info cfg: enabling valgrind (CFG_ENABLE_VALGRIND))
124 else
125   CFG_VALGRIND :=
126 endif
127 ifdef CFG_BAD_VALGRIND
128   $(info cfg: disabling valgrind due to its unreliability on this platform)
129   CFG_VALGRIND :=
130 endif
131
132
133 ######################################################################
134 # Target-and-rule "utility variables"
135 ######################################################################
136
137 define DEF_X
138 X_$(1) := $(CFG_EXE_SUFFIX_$(1))
139 endef
140 $(foreach target,$(CFG_TARGET),\
141   $(eval $(call DEF_X,$(target))))
142
143 # "Source" files we generate in builddir along the way.
144 GENERATED :=
145
146 # Delete the built-in rules.
147 .SUFFIXES:
148 %:: %,v
149 %:: RCS/%,v
150 %:: RCS/%
151 %:: s.%
152 %:: SCCS/s.%
153
154
155 ######################################################################
156 # Cleaning out old crates
157 ######################################################################
158
159 # $(1) is the path for directory to match against
160 # $(2) is the glob to use in the match
161 #
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
167 endef
168
169 # Same interface as above, but deletes rather than just listing the files.
170 ifdef VERBOSE
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
173 endef
174 else
175 define REMOVE_ALL_OLD_GLOB_MATCHES
176   $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then rm $$MATCHES ; fi
177 endef
178 endif
179
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.)
186 ifdef VERBOSE
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 )
190 endef
191 else
192 define LIST_ALL_OLD_GLOB_MATCHES
193 endef
194 endif
195
196 ######################################################################
197 # LLVM macros
198 ######################################################################
199
200 # FIXME: x86-ism
201 LLVM_COMPONENTS=x86 arm mips ipo bitreader bitwriter linker asmparser jit mcjit \
202                 interpreter instrumentation
203
204 # Only build these LLVM tools
205 LLVM_TOOLS=bugpoint llc llvm-ar llvm-as llvm-dis llvm-mc opt llvm-extract
206
207 define DEF_LLVM_VARS
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)))
212
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)
226
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))
229
230 endef
231
232 $(foreach host,$(CFG_HOST), \
233  $(eval $(call DEF_LLVM_VARS,$(host))))
234
235 ######################################################################
236 # Exports for sub-utilities
237 ######################################################################
238
239 # Note that any variable that re-configure should pick up needs to be
240 # exported
241
242 export CFG_SRC_DIR
243 export CFG_BUILD_DIR
244 export CFG_VERSION
245 export CFG_VERSION_WIN
246 export CFG_RELEASE
247 export CFG_BUILD
248 export CFG_LLVM_ROOT
249 export CFG_ENABLE_MINGW_CROSS
250 export CFG_PREFIX
251 export CFG_LIBDIR
252 export CFG_RUSTLIBDIR
253 export CFG_LIBDIR_RELATIVE
254 export CFG_DISABLE_INJECT_STD_VERSION
255
256 ######################################################################
257 # Per-stage targets and runner
258 ######################################################################
259
260 STAGES = 0 1 2 3
261
262 define SREQ
263 # $(1) is the stage number
264 # $(2) is the target triple
265 # $(3) is the host triple
266
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)
271
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
276
277 # Preqrequisites for using the stageN compiler
278 ifeq ($(1),0)
279 HSREQ$(1)_H_$(3) = $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))
280 else
281 HSREQ$(1)_H_$(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)) \
285         $$(MKFILE_DEPS)
286 endif
287
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
293
294 # Prerequisites for a working stageN compiler and libraries, for a specific
295 # target
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))
300
301 # Prerequisites for a working stageN compiler and complete set of target
302 # libraries
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))
308
309 ifeq ($(1),0)
310 # Don't run the the stage0 compiler under valgrind - that ship has sailed
311 CFG_VALGRIND_COMPILE$(1) =
312 else
313 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
314 endif
315
316 # Add RUSTFLAGS_STAGEN values to the build command
317 EXTRAFLAGS_STAGE$(1) = $$(RUSTFLAGS_STAGE$(1))
318
319 CFGFLAG$(1)_T_$(2)_H_$(3) = stage$(1)
320
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.
324 ifeq ($(1),0)
325 ifneq ($(strip $(CFG_BUILD)),$(strip $(3)))
326 CFGFLAG$(1)_T_$(2)_H_$(3) = stage1
327 endif
328 endif
329
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))"
334 else
335   RPATH_VAR$(1)_T_$(2)_H_$(3) := \
336       LD_LIBRARY_PATH="$$$$LD_LIBRARY_PATH:$$(HLIB$(1)_H_$(3))"
337 endif
338 else
339     RPATH_VAR$(1)_T_$(2)_H_$(3) :=
340 endif
341
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)) \
349                 $$(RUSTC_FLAGS_$(2))
350
351 PERF_STAGE$(1)_T_$(2)_H_$(3) :=                                         \
352         $$(Q)$$(call CFG_RUN_TARG_$(3),$(1),                            \
353                 $$(CFG_PERF_TOOL)                                       \
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)) \
357                 $$(RUSTC_FLAGS_$(2))
358
359 endef
360
361 $(foreach build,$(CFG_HOST), \
362  $(eval $(foreach target,$(CFG_TARGET), \
363   $(eval $(foreach stage,$(STAGES), \
364    $(eval $(call SREQ,$(stage),$(target),$(build))))))))
365
366 ######################################################################
367 # rustc-H-targets
368 #
369 # Builds a functional Rustc for the given host.
370 ######################################################################
371
372 define DEF_RUSTC_STAGE_TARGET
373 # $(1) == architecture
374 # $(2) == stage
375
376 rustc-stage$(2)-H-$(1):                                                 \
377         $$(foreach target,$$(CFG_TARGET),$$(SREQ$(2)_T_$$(target)_H_$(1)))
378
379 endef
380
381 $(foreach host,$(CFG_HOST),                                             \
382  $(eval $(foreach stage,1 2 3,                                          \
383   $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
384
385 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
386 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
387 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
388
389 define DEF_RUSTC_TARGET
390 # $(1) == architecture
391
392 rustc-H-$(1): rustc-stage2-H-$(1)
393 endef
394
395 $(foreach host,$(CFG_TARGET),                   \
396  $(eval $(call DEF_RUSTC_TARGET,$(host))))
397
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)
402
403 rustc-H-all: $(foreach host,$(CFG_HOST),rustc-H-$(host))
404
405 ######################################################################
406 # Entrypoint rule
407 ######################################################################
408
409 .DEFAULT_GOAL := all
410
411 define ALL_TARGET_N
412 ifneq ($$(findstring $(1),$$(CFG_HOST)),)
413 # This is a host
414 all-target-$(1)-host-$(2): $$(CSREQ2_T_$(1)_H_$(2))
415 else
416 # This is a target only
417 all-target-$(1)-host-$(2): $$(SREQ2_T_$(1)_H_$(2))
418 endif
419 endef
420
421 $(foreach target,$(CFG_TARGET), \
422  $(foreach host,$(CFG_HOST), \
423  $(eval $(call ALL_TARGET_N,$(target),$(host)))))
424
425 ALL_TARGET_RULES = $(foreach target,$(CFG_TARGET), \
426         $(foreach host,$(CFG_HOST), \
427  all-target-$(target)-host-$(host)))
428
429 all: $(ALL_TARGET_RULES) $(GENERATED) docs
430
431 help:
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/^# \?//'